104 lines
2.9 KiB
Go
Raw Normal View History

2025-04-26 13:50:26 +08:00
package smdl
import (
"admin/apps/game/domain/entity"
2025-05-09 18:28:15 +08:00
"admin/apps/game/domain/projects/smdl/internal"
2025-04-26 13:50:26 +08:00
"admin/apps/game/model"
"admin/apps/game/model/dto"
"admin/internal/errcode"
"admin/lib/httpclient"
2025-04-28 15:56:04 +08:00
"admin/lib/utils"
2025-05-09 18:28:15 +08:00
"net/url"
2025-05-15 17:30:33 +08:00
"sort"
2025-05-09 18:28:15 +08:00
"strconv"
2025-04-26 13:50:26 +08:00
)
2025-04-28 15:56:04 +08:00
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"`
}
2025-04-26 13:50:26 +08:00
type RoleHook struct {
}
2025-05-09 18:28:15 +08:00
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) {
2025-04-26 13:50:26 +08:00
alisrvAddr := projectInfo.GetApiAddr()
if alisrvAddr == "" {
2025-05-09 18:28:15 +08:00
return 0, nil, nil, errcode.New(errcode.ServerError, "项目%v没有配置api服务器地址", projectInfo.Po.Name)
}
2025-05-13 18:13:22 +08:00
serverList, err := getAllRunningServers(projectInfo)
2025-05-09 18:28:15 +08:00
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
2025-04-26 13:50:26 +08:00
}
type RspData struct {
2025-05-09 18:28:15 +08:00
State struct {
Code int `json:"code"`
Msg string `json:"msg"`
} `json:"state"`
2025-05-10 10:18:06 +08:00
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))
}
2025-04-26 13:50:26 +08:00
}
2025-05-09 18:28:15 +08:00
for _, reqServer := range reqPageServerList {
2025-05-10 10:18:06 +08:00
body.Set("serverid", reqServer.RawInfo.ServerId)
body.Set("offset", strconv.Itoa(reqServer.Offset))
body.Set("count", strconv.Itoa(reqServer.Count))
2025-05-09 18:28:15 +08:00
rsp := &RspData{}
err := httpclient.Request(alisrvAddr+"/rolelist", "get", body, rsp)
if err != nil {
return 0, nil, nil, err
}
2025-04-26 13:50:26 +08:00
2025-05-10 10:18:06 +08:00
for _, role := range rsp.Data {
2025-05-09 18:28:15 +08:00
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),
2025-05-10 10:18:06 +08:00
CreatedAt: utils.ParseUnixTimestamp2LocalTime(role.CreateTime),
2025-05-09 18:28:15 +08:00
}
et := (&entity.CommonResource{}).FromPo(rolePo)
rows = append(rows, et.ToCommonDto())
2025-04-28 15:56:04 +08:00
}
2025-04-26 13:50:26 +08:00
}
2025-05-15 17:30:33 +08:00
sort.SliceStable(rows, func(i, j int) bool {
return rows[i]["Status"].(string) < rows[j]["Status"].(string)
})
2025-05-10 10:18:06 +08:00
return totalCount, fields, rows, nil
2025-04-26 13:50:26 +08:00
}