package smdl import ( "admin/apps/game/domain/entity" "admin/apps/game/domain/projects/smdl/internal" "admin/apps/game/model" "admin/apps/game/model/dto" "admin/internal/errcode" "admin/lib/httpclient" "admin/lib/utils" "net/url" "sort" "strconv" ) type Role struct { RoleId string `json:"roleId"` Account string `json:"account"` ServerConfId string `json:"serverId"` Name string `json:"roleName"` Status string `json:"status"` Level int `json:"roleLevel"` Profession string `json:"profession"` LatestLoginTime int64 `json:"latestLoginAt"` CreateTime int64 `json:"createAt"` } type RoleHook struct { } func (hook *RoleHook) List(projectInfo *entity.Project, resource string, params *dto.CommonListReq, fields []*dto.CommonDtoFieldDesc, totalCount int, rows []dto.CommonDtoValues) ( int, []*dto.CommonDtoFieldDesc, []dto.CommonDtoValues, error) { alisrvAddr := projectInfo.GetApiAddr() if alisrvAddr == "" { return 0, nil, nil, errcode.New(errcode.ServerError, "项目%v没有配置api服务器地址", projectInfo.Po.Name) } serverList, err := getAllRunningServers(projectInfo) if err != nil { return 0, nil, nil, err } totalCount, reqPageServerList, err := getPaginationServerReqList(projectInfo, serverList, params, func(info *internal.ServerInfo) int { return info.TotalRoleCount }) if err != nil { return 0, nil, nil, err } type RspData struct { State struct { Code int `json:"code"` Msg string `json:"msg"` } `json:"state"` Data []*Role `json:"data"` } body := &url.Values{} for _, cond := range params.ParsedWhereConditions.Conditions { if cond.Key == "Account" { // 按账号查询 body.Set("userId", cond.Value1.(string)) } else if cond.Key == "RoleId" { body.Set("roleId", cond.Value1.(string)) } else if cond.Key == "Name" { body.Set("name", cond.Value1.(string)) } } for _, reqServer := range reqPageServerList { body.Set("serverid", reqServer.RawInfo.ServerId) body.Set("offset", strconv.Itoa(reqServer.Offset)) body.Set("count", strconv.Itoa(reqServer.Count)) rsp := &RspData{} err := httpclient.Request(alisrvAddr+"/rolelist", "get", body, rsp) if err != nil { return 0, nil, nil, err } for _, role := range rsp.Data { rolePo := &model.Role{ RoleId: role.RoleId, Account: role.Account, ServerConfId: role.ServerConfId, Name: role.Name, Status: role.Status, Level: role.Level, Profession: role.Profession, LatestLoginTime: utils.ParseUnixTimestamp2LocalTime(role.LatestLoginTime), CreatedAt: utils.ParseUnixTimestamp2LocalTime(role.CreateTime), } et := (&entity.CommonResource{}).FromPo(rolePo) rows = append(rows, et.ToCommonDto()) } } sort.SliceStable(rows, func(i, j int) bool { return rows[i]["Status"].(string) < rows[j]["Status"].(string) }) return totalCount, fields, rows, nil }