浏览代码

update 完善优化代码

Alvin 8 月之前
父节点
当前提交
b3c1eb42c6

+ 1 - 4
game/game_cluster/nodes/webadmin/common/packResponse.go

@@ -33,10 +33,7 @@ func PackOkResult(c *gin.Context, statusCode int32, data ...interface{}) {
 	result := &code.Result{
 		Code:    statusCode,
 		Message: code.GetMessage(statusCode),
-	}
-
-	if len(data) > 0 {
-		result.Data = data
+		Data:    data,
 	}
 
 	c.JSON(http.StatusOK, result)

+ 18 - 1
game/game_cluster/nodes/webadmin/controller/admin.go

@@ -86,6 +86,23 @@ func (a *Admin) Del(ctx *gin.Context) {
 	common.PackOkResult(ctx, code.OK, nil)
 }
 
+// Find 获取管理员信息
+func (a *Admin) Find(ctx *gin.Context) {
+	var req entity.AdminFindReq
+	if err := ctx.ShouldBindJSON(&req); err != nil {
+		common.PackOkResult(ctx, code.ParamError)
+		return
+	}
+
+	resp, err := a.sev.Find(ctx, req)
+	if err != nil {
+		common.PackOkResult(ctx, err.Code)
+		return
+	}
+
+	common.PackOkResult(ctx, code.OK, resp)
+}
+
 // FindAll 获取所有管理员
 func (a *Admin) FindAll(ctx *gin.Context) {
 	var req entity.AdminFindAllReq
@@ -94,7 +111,7 @@ func (a *Admin) FindAll(ctx *gin.Context) {
 		return
 	}
 
-	resp, err := a.sev.FindAll(ctx, req.Page, req.Size, req.Username)
+	resp, err := a.sev.FindAll(ctx, req)
 	if err != nil {
 		common.PackOkResult(ctx, err.Code)
 		return

+ 5 - 5
game/game_cluster/nodes/webadmin/controller/synthesis.go

@@ -21,7 +21,7 @@ func NewSynthesis() *Synthesis {
 // FindUserLogDaily 查询用户日活跃
 func (s *Synthesis) FindUserLogDaily(ctx *gin.Context) {
 	var req entity.UserLogDailyReq
-	if err := ctx.ShouldBindJSON(req); err != nil {
+	if err := ctx.ShouldBindJSON(&req); err != nil {
 		common.PackOkResult(ctx, code.ParamError)
 		return
 	}
@@ -38,7 +38,7 @@ func (s *Synthesis) FindUserLogDaily(ctx *gin.Context) {
 // FindUserRetention 查询用户留存
 func (s *Synthesis) FindUserRetention(ctx *gin.Context) {
 	var req entity.UserRetentionReq
-	if err := ctx.ShouldBindJSON(req); err != nil {
+	if err := ctx.ShouldBindJSON(&req); err != nil {
 		common.PackOkResult(ctx, code.ParamError)
 		return
 	}
@@ -66,7 +66,7 @@ func (s *Synthesis) FindUserCountryCount(ctx *gin.Context) {
 // FindWithdrawal 查询提现记录
 func (s *Synthesis) FindWithdrawal(ctx *gin.Context) {
 	var req entity.UserWithdrawalReq
-	if err := ctx.ShouldBindJSON(req); err != nil {
+	if err := ctx.ShouldBindJSON(&req); err != nil {
 		common.PackOkResult(ctx, code.ParamError)
 		return
 	}
@@ -83,7 +83,7 @@ func (s *Synthesis) FindWithdrawal(ctx *gin.Context) {
 // WithdrawalStatus 修改提现状态
 func (s *Synthesis) WithdrawalStatus(ctx *gin.Context) {
 	var req entity.UserWithdrawalStatus
-	if err := ctx.ShouldBindJSON(req); err != nil {
+	if err := ctx.ShouldBindJSON(&req); err != nil {
 		common.PackOkResult(ctx, code.ParamError)
 		return
 	}
@@ -100,7 +100,7 @@ func (s *Synthesis) WithdrawalStatus(ctx *gin.Context) {
 // WithdrawalStatusBatch 修改提现状态批量
 func (s *Synthesis) WithdrawalStatusBatch(ctx *gin.Context) {
 	var req entity.UserWithdrawalStatusBatch
-	if err := ctx.ShouldBindJSON(req); err != nil {
+	if err := ctx.ShouldBindJSON(&req); err != nil {
 		common.PackOkResult(ctx, code.ParamError)
 		return
 	}

+ 3 - 3
game/game_cluster/nodes/webadmin/controller/whitelist.go

@@ -29,7 +29,7 @@ func NewWhitelist() *Whitelist {
 // @Router /v1/admin/whitelist/list [post]
 func (w *Whitelist) GetWhitelist(ctx *gin.Context) {
 	var req entity.WhitelistListReq
-	if err := ctx.ShouldBindJSON(req); err != nil {
+	if err := ctx.ShouldBindJSON(&req); err != nil {
 		common.PackOkResult(ctx, code.ParamError)
 		return
 	}
@@ -54,7 +54,7 @@ func (w *Whitelist) GetWhitelist(ctx *gin.Context) {
 // @Router /v1/admin/whitelist/add [post]
 func (w *Whitelist) AddWhitelist(ctx *gin.Context) {
 	var req entity.WhitelistAddReq
-	if err := ctx.ShouldBindJSON(req); err != nil {
+	if err := ctx.ShouldBindJSON(&req); err != nil {
 		common.PackOkResult(ctx, code.ParamError)
 		return
 	}
@@ -79,7 +79,7 @@ func (w *Whitelist) AddWhitelist(ctx *gin.Context) {
 // @Router /v1/admin/whitelist/remove [post]
 func (w *Whitelist) RemoveWhitelist(ctx *gin.Context) {
 	var req entity.WhitelistRemoveReq
-	if err := ctx.ShouldBindJSON(req); err != nil {
+	if err := ctx.ShouldBindJSON(&req); err != nil {
 		common.PackOkResult(ctx, code.ParamError)
 		return
 	}

+ 7 - 5
game/game_cluster/nodes/webadmin/entity/admin.go

@@ -41,9 +41,7 @@ type AdminListDetail struct {
 }
 
 type AdminChangePasswordReq struct {
-	Password string `json:"
-
-"`
+	Password string `json:"password"`
 	Username string `json:"username"`
 }
 
@@ -51,11 +49,15 @@ type AdminDelReq struct {
 	Username string `json:"username"`
 }
 
-type AdminFindAllReq struct {
+type AdminFindReq struct {
 	Page     int    `json:"page"`
 	Size     int    `json:"size"`
 	Username string `json:"username"`
-	Total    int64  `json:"total"`
+}
+
+type AdminFindAllReq struct {
+	Page int `json:"page"`
+	Size int `json:"size"`
 }
 
 type AdminUpdateStatusReq struct {

+ 0 - 1
game/game_cluster/nodes/webadmin/entity/user_country_count.go

@@ -2,7 +2,6 @@ package entity
 
 type UserCountryResp struct {
 	Details []*UserCountryDetail `json:"details"`
-	Total   int64                `json:"total"`
 }
 
 type UserCountryDetail struct {

+ 0 - 1
game/game_cluster/nodes/webadmin/entity/user_log_daily.go

@@ -29,5 +29,4 @@ type UserLogDailyReq struct {
 	Channel   string `json:"channel"`
 	Page      int    `json:"page"`
 	Size      int    `json:"size"`
-	Total     int64  `json:"total"`
 }

+ 3 - 2
game/game_cluster/nodes/webadmin/router/router.go

@@ -48,9 +48,10 @@ func (c *Controller) InitApiRouter(u *gin.RouterGroup) {
 	u.POST("/user/changePassword", controller.NewAdmin().ChangePassword)
 	u.POST("/user/add", controller.NewAdmin().Add)
 	u.POST("/user/del", controller.NewAdmin().Del)
-	u.POST("/user/find", controller.NewAdmin().FindAll)
+	u.POST("/user/find", controller.NewAdmin().Find)
+	u.POST("/user/findAll", controller.NewAdmin().FindAll)
 	u.POST("/user/update", controller.NewAdmin().UpdateStatus)
-	u.POST("/user/server_status", controller.NewAdmin().GetServerStatus)
+	u.POST("/user/serverStatus", controller.NewAdmin().GetServerStatus)
 	u.POST("/role/add", controller.NewRole().Add)
 	u.POST("/role/update", controller.NewRole().Update)
 	u.POST("/role/del", controller.NewRole().Del)

+ 98 - 8
game/game_cluster/nodes/webadmin/service/admin.go

@@ -277,28 +277,30 @@ func (a *Admin) UpdateStatus(ctx context.Context, username string, status int) *
 	return nil
 }
 
-// FindAll 查找所有管理员信息
-func (a *Admin) FindAll(ctx context.Context, page int, pageSize int, username string) (*entity.AdminListResp, *code.Result) {
+// Find 获取管理员信息
+func (a *Admin) Find(ctx context.Context, req entity.AdminFindReq) (*entity.AdminListResp, *code.Result) {
 	// 日志记录
-	mhayaLogger.Warnf("Finding admins with page %d and page size %d, username: %s", page, pageSize, maskUsername(username))
+	mhayaLogger.Warnf("Find req: %#v", req)
 
 	// 验证参数
-	if page <= 0 {
+	page := req.Page
+	if req.Page <= 0 {
 		page = 1
 	}
-	if pageSize <= 0 {
+	pageSize := req.Size
+	if req.Size <= 0 {
 		pageSize = 10
 	}
 
 	// 构建查询条件
 	filter := bson.M{}
-	if username != "" {
-		filter["username"] = bson.M{"$regex": escapeRegex(username), "$options": "i"}
+	if req.Username != "" {
+		filter["username"] = bson.M{"$regex": escapeRegex(req.Username), "$options": "i"}
 	}
 	// 查询总数
 	count, err := mdb.MDB.Collection("admin").CountDocuments(ctx, filter)
 	if err != nil {
-		mhayaLogger.Warnf("FindAll CountDocuments error:", err)
+		mhayaLogger.Warnf("Find CountDocuments error:", err)
 		return nil, common.NewResult(code.InternalError)
 	}
 
@@ -309,6 +311,94 @@ func (a *Admin) FindAll(ctx context.Context, page int, pageSize int, username st
 
 	// 执行查询
 	cursor, err := mdb.MDB.Collection("admin").Find(ctx, filter, findOptions)
+	if err != nil {
+		mhayaLogger.Warnf("Find Find error:", err)
+		return nil, common.NewResult(code.InternalError)
+	}
+	defer func() {
+		if closeErr := cursor.Close(ctx); closeErr != nil {
+			mhayaLogger.Warnf("Error closing cursor: %v", closeErr)
+		}
+	}()
+
+	// 解析结果
+	admins := make([]*model.Admin, 0)
+	for cursor.Next(ctx) {
+		var admin model.Admin
+		err := cursor.Decode(&admin)
+		if err != nil {
+			mhayaLogger.Warnf("Find Decode error:", err)
+			return nil, common.NewResult(code.InternalError)
+		}
+		admins = append(admins, &admin)
+	}
+
+	if err := cursor.Err(); err != nil {
+		mhayaLogger.Warnf("Find cursor error:", err)
+		return nil, common.NewResult(code.InternalError)
+	}
+
+	var details []*entity.AdminListDetail
+	for _, admin := range admins {
+		roleName := ""
+		roleName, _ = a.GetRoleName(admin.RoleId)
+
+		details = append(details, &entity.AdminListDetail{
+			Id:            admin.GetID(),
+			Username:      admin.Username,
+			RealName:      admin.RealName,
+			RoleId:        admin.RoleId,
+			RoleName:      roleName,
+			Status:        admin.Status,
+			CreatedAt:     admin.CreatedAt,
+			UpdatedAt:     admin.UpdatedAt,
+			LastLoginIp:   admin.LastLoginIp,
+			LastLoginTime: admin.LastLoginTime,
+		})
+	}
+
+	return &entity.AdminListResp{
+		Details: details,
+		Total:   count,
+	}, nil
+}
+
+// FindAll 查找所有管理员信息
+func (a *Admin) FindAll(ctx context.Context, req entity.AdminFindAllReq) (*entity.AdminListResp, *code.Result) {
+	// 日志记录
+	mhayaLogger.Warnf("FindAll req: %#v", req)
+
+	// 验证参数
+	page := req.Page
+	if req.Page <= 0 {
+		page = 1
+	}
+	pageSize := req.Size
+	if req.Size <= 0 {
+		pageSize = 10
+	}
+
+	// 构建查询条件
+	// filter := bson.M{}
+	// if req.Username != "" {
+	// 	filter["username"] = bson.M{"$regex": escapeRegex(req.Username), "$options": "i"}
+	// }
+	// 查询总数
+	// count, err := mdb.MDB.Collection("admin").CountDocuments(ctx, filter)
+	count, err := mdb.MDB.Collection("admin").CountDocuments(ctx, map[string]interface{}{})
+	if err != nil {
+		mhayaLogger.Warnf("FindAll CountDocuments error:", err)
+		return nil, common.NewResult(code.InternalError)
+	}
+
+	// 设置分页选项
+	skip := (page - 1) * pageSize
+	limit := pageSize
+	findOptions := options.Find().SetSkip(int64(skip)).SetLimit(int64(limit))
+
+	// 执行查询
+	// cursor, err := mdb.MDB.Collection("admin").Find(ctx, filter, findOptions)
+	cursor, err := mdb.MDB.Collection("admin").Find(ctx, map[string]interface{}{}, findOptions)
 	if err != nil {
 		mhayaLogger.Warnf("FindAll Find error:", err)
 		return nil, common.NewResult(code.InternalError)

+ 0 - 1
game/game_cluster/nodes/webadmin/service/synthesis.go

@@ -57,7 +57,6 @@ func (s *Synthesis) FindMDBUserLogDaily(req entity.UserLogDailyReq) (*entity.Use
 
 	// 分页参数
 	skip := (req.Page - 1) * req.Size
-	req.Total = 0
 	// 计算总数
 	count, err := collection.CountDocuments(ctx, filter)
 	if err != nil {