151 lines
5.4 KiB
Go
151 lines
5.4 KiB
Go
package gamemanagement
|
||
|
||
import (
|
||
"strconv"
|
||
"strings"
|
||
|
||
"hyapp-admin-server/internal/integration/userclient"
|
||
"hyapp-admin-server/internal/middleware"
|
||
"hyapp-admin-server/internal/response"
|
||
gamev1 "hyapp.local/api/proto/game/v1"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
type gameWhitelistEnabledRequest struct {
|
||
Enabled bool `json:"enabled"`
|
||
}
|
||
|
||
type gameWhitelistUserRequest struct {
|
||
// 字符串承载 user_id,避免浏览器 Number 对大整数发生精度损失。
|
||
UserID string `json:"userId"`
|
||
}
|
||
|
||
type gameWhitelistUserDTO struct {
|
||
UserID string `json:"userId"`
|
||
DisplayUserID string `json:"displayUserId"`
|
||
Username string `json:"username"`
|
||
Avatar string `json:"avatar"`
|
||
Status string `json:"status"`
|
||
CreatedByAdminID int64 `json:"createdByAdminId"`
|
||
CreatedAtMS int64 `json:"createdAtMs"`
|
||
}
|
||
|
||
// SetGameWhitelistEnabled 只切换访问策略,不隐式增删成员;空白名单开启后应明确表现为无人可见。
|
||
func (h *Handler) SetGameWhitelistEnabled(c *gin.Context) {
|
||
gameID := strings.TrimSpace(c.Param("game_id"))
|
||
var req gameWhitelistEnabledRequest
|
||
if gameID == "" || c.ShouldBindJSON(&req) != nil {
|
||
response.BadRequest(c, "游戏白名单参数不正确")
|
||
return
|
||
}
|
||
resp, err := h.game.SetGameWhitelistEnabled(c.Request.Context(), &gamev1.SetGameWhitelistEnabledRequest{
|
||
Meta: requestMeta(c), GameId: gameID, Enabled: req.Enabled,
|
||
})
|
||
if err != nil {
|
||
response.BadRequest(c, err.Error())
|
||
return
|
||
}
|
||
h.auditLog(c, "set-game-whitelist-enabled", "game_catalog", gameID, strconv.FormatBool(req.Enabled))
|
||
response.OK(c, catalogFromProto(resp.GetGame()))
|
||
}
|
||
|
||
// ListGameWhitelistUsers 从 game-service 读取成员事实,再批量补 user-service 展示资料,避免复制用户主数据到游戏库。
|
||
func (h *Handler) ListGameWhitelistUsers(c *gin.Context) {
|
||
gameID := strings.TrimSpace(c.Param("game_id"))
|
||
if gameID == "" {
|
||
response.BadRequest(c, "游戏 ID 参数不正确")
|
||
return
|
||
}
|
||
resp, err := h.game.ListGameWhitelistUsers(c.Request.Context(), &gamev1.ListGameWhitelistUsersRequest{
|
||
Meta: requestMeta(c), GameId: gameID, PageSize: int32(parsePositiveInt(c.Query("pageSize"), 200)),
|
||
})
|
||
if err != nil {
|
||
response.ServerError(c, "获取游戏白名单失败")
|
||
return
|
||
}
|
||
userIDs := make([]int64, 0, len(resp.GetUsers()))
|
||
for _, item := range resp.GetUsers() {
|
||
userIDs = append(userIDs, item.GetUserId())
|
||
}
|
||
profiles := map[int64]*userclient.User{}
|
||
if len(userIDs) > 0 {
|
||
profiles, err = h.user.BatchGetUsers(c.Request.Context(), userclient.BatchGetUsersRequest{
|
||
RequestID: middleware.CurrentRequestID(c), Caller: "admin-server", UserIDs: userIDs,
|
||
})
|
||
if err != nil {
|
||
response.ServerError(c, "获取白名单用户资料失败")
|
||
return
|
||
}
|
||
}
|
||
items := make([]gameWhitelistUserDTO, 0, len(resp.GetUsers()))
|
||
for _, item := range resp.GetUsers() {
|
||
items = append(items, gameWhitelistUserFromProto(item, profiles[item.GetUserId()]))
|
||
}
|
||
response.OK(c, gin.H{"items": items, "serverTimeMs": resp.GetServerTimeMs()})
|
||
}
|
||
|
||
// AddGameWhitelistUser 先按内部 user_id 读取 owner 主数据,只有真实存在的用户才写入游戏访问成员表。
|
||
func (h *Handler) AddGameWhitelistUser(c *gin.Context) {
|
||
gameID := strings.TrimSpace(c.Param("game_id"))
|
||
var req gameWhitelistUserRequest
|
||
if gameID == "" || c.ShouldBindJSON(&req) != nil {
|
||
response.BadRequest(c, "游戏白名单用户参数不正确")
|
||
return
|
||
}
|
||
userID, err := strconv.ParseInt(strings.TrimSpace(req.UserID), 10, 64)
|
||
if err != nil || userID <= 0 {
|
||
response.BadRequest(c, "用户 ID 参数不正确")
|
||
return
|
||
}
|
||
profile, err := h.user.GetUser(c.Request.Context(), userclient.GetUserRequest{
|
||
RequestID: middleware.CurrentRequestID(c), Caller: "admin-server", UserID: userID,
|
||
})
|
||
if err != nil || profile == nil {
|
||
response.BadRequest(c, "用户不存在")
|
||
return
|
||
}
|
||
resp, err := h.game.AddGameWhitelistUser(c.Request.Context(), &gamev1.AddGameWhitelistUserRequest{
|
||
Meta: requestMeta(c), GameId: gameID, UserId: userID,
|
||
})
|
||
if err != nil {
|
||
response.BadRequest(c, err.Error())
|
||
return
|
||
}
|
||
h.auditLog(c, "add-game-whitelist-user", "game_user_whitelist", gameID+":"+req.UserID, "added")
|
||
response.OK(c, gameWhitelistUserFromProto(resp.GetUser(), profile))
|
||
}
|
||
|
||
func (h *Handler) DeleteGameWhitelistUser(c *gin.Context) {
|
||
gameID := strings.TrimSpace(c.Param("game_id"))
|
||
userID, err := strconv.ParseInt(strings.TrimSpace(c.Param("user_id")), 10, 64)
|
||
if gameID == "" || err != nil || userID <= 0 {
|
||
response.BadRequest(c, "游戏白名单用户参数不正确")
|
||
return
|
||
}
|
||
if _, err := h.game.DeleteGameWhitelistUser(c.Request.Context(), &gamev1.DeleteGameWhitelistUserRequest{
|
||
Meta: requestMeta(c), GameId: gameID, UserId: userID,
|
||
}); err != nil {
|
||
response.BadRequest(c, err.Error())
|
||
return
|
||
}
|
||
h.auditLog(c, "delete-game-whitelist-user", "game_user_whitelist", gameID+":"+strconv.FormatInt(userID, 10), "deleted")
|
||
response.OK(c, gin.H{"userId": strconv.FormatInt(userID, 10)})
|
||
}
|
||
|
||
func gameWhitelistUserFromProto(item *gamev1.GameWhitelistUser, profile *userclient.User) gameWhitelistUserDTO {
|
||
if item == nil {
|
||
return gameWhitelistUserDTO{}
|
||
}
|
||
dto := gameWhitelistUserDTO{
|
||
UserID: strconv.FormatInt(item.GetUserId(), 10), CreatedByAdminID: item.GetCreatedByAdminId(), CreatedAtMS: item.GetCreatedAtMs(),
|
||
}
|
||
if profile != nil {
|
||
dto.DisplayUserID = profile.DisplayUserID
|
||
dto.Username = profile.Username
|
||
dto.Avatar = profile.Avatar
|
||
dto.Status = profile.Status
|
||
}
|
||
return dto
|
||
}
|