140 lines
4.3 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"
"math"
2025-05-09 18:28:15 +08:00
"net/url"
2025-04-26 13:50:26 +08:00
"strconv"
)
type AccountHook struct {
}
2025-05-09 18:28:15 +08:00
func (hook *AccountHook) 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-04-26 13:50:26 +08:00
}
2025-05-09 18:28:15 +08:00
serverList, err := internal.GetRemoteServerInfoList(alisrvAddr)
if err != nil {
return 0, nil, nil, err
}
totalCount, reqPageServerList, err := getPaginationServerReqList(projectInfo, serverList, params, func(info *internal.ServerInfo) int { return info.TotalAccountCount })
if err != nil {
return 0, nil, nil, err
}
// {"data":[{"account":"20250425cs01","roleList":[{"account":"20250425cs01","createAt":1745567667525,"latestLoginAt":1745567714234,"roleId":"20001000001","serverId":"20001"}],"serverId":"20001"}],
// "state":{"code":2000000,"msg":"OK"}}
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"`
Data []struct {
Account string `json:"account"`
ServerId string `json:"serverId"`
RoleList []*Role `json:"roleList"`
2025-04-26 13:50:26 +08:00
} `json:"data"`
}
2025-05-09 18:28:15 +08:00
for _, reqServer := range reqPageServerList {
body := &url.Values{}
body.Add("serverid", reqServer.RawInfo.ServerId)
body.Add("offset", strconv.Itoa(reqServer.Offset))
body.Add("count", strconv.Itoa(reqServer.Count))
body.Add("userlist", "true")
rsp := &RspData{}
err := httpclient.Request(alisrvAddr+"/rolelist", "get", body, rsp)
if err != nil {
return 0, nil, nil, err
}
for _, user := range rsp.Data {
userPo := &model.Account{
Account: user.Account,
ServerConfId: user.ServerId,
}
latestLoginTime := int64(0)
earliestCreateTime := int64(math.MaxInt64)
for _, v := range user.RoleList {
userPo.RoleIds = append(userPo.RoleIds, v.RoleId)
if latestLoginTime < v.LatestLoginTime {
latestLoginTime = v.LatestLoginTime
}
if earliestCreateTime > v.CreateTime {
earliestCreateTime = v.CreateTime
}
}
userPo.LatestLoginTime = utils.ParseUnixTimestamp2LocalTime(latestLoginTime)
userPo.CreateTime = utils.ParseUnixTimestamp2LocalTime(earliestCreateTime)
et := (&entity.CommonResource{}).FromPo(userPo)
rows = append(rows, et.ToCommonDto())
}
2025-04-26 13:50:26 +08:00
}
2025-05-09 18:28:15 +08:00
return totalCount, fields, rows, nil
}
func getPaginationServerReqList(projectInfo *entity.Project, serverInfoList internal.ServerInfoList,
params *dto.CommonListReq, queryCountField func(*internal.ServerInfo) int) (
totalCount int, reqServerInfoList []*internal.PageServerCountInfo, err error) {
pageNo := params.PageNo
pageLen := params.PageLen
whereConditions := params.ParsedWhereConditions
reqServerList := make([]*internal.PageServerCountInfo, 0)
2025-04-26 13:50:26 +08:00
2025-05-09 18:28:15 +08:00
if len(whereConditions.Conditions) > 1 {
// 第一个是ProjectId条件
if whereConditions.Conditions[1].Key != "ServerConfId" { // 账号、角色的条件检索只支持区服
return 0, nil, errcode.New(errcode.ParamsInvalid, "where condition invalid:%+v", whereConditions.Conditions[0])
2025-04-26 13:50:26 +08:00
}
2025-05-09 18:28:15 +08:00
// 前端指定了查询某个区服的数据
specServerId := whereConditions.Conditions[1].Value1.(string)
for _, v := range serverInfoList {
if v.ServerId == specServerId {
offset := (pageNo - 1) * pageLen
limit := pageLen
totalCount = queryCountField(v)
reqServerList = append(reqServerList, &internal.PageServerCountInfo{
RawInfo: v,
Offset: offset,
Count: limit,
})
break
2025-04-28 15:56:04 +08:00
}
2025-05-09 18:28:15 +08:00
}
} else {
// 根据分页参数计算需要几个区服数据参与查询
totalCountTmp, tidyPageInfoList := serverInfoList.TidyToPageList(pageLen, queryCountField)
if len(tidyPageInfoList) == 0 {
// 没有找到分页信息,返回空表
return 0, nil, nil
}
totalCount = totalCountTmp
for i, tidyPageInfo := range tidyPageInfoList {
if i+1 == pageNo {
reqServerList = append(reqServerList, tidyPageInfo.ServerList...)
break
2025-04-28 15:56:04 +08:00
}
2025-04-26 13:50:26 +08:00
}
}
2025-05-09 18:28:15 +08:00
return totalCount, reqServerList, nil
2025-04-26 13:50:26 +08:00
}