bindbox-game/internal/api/admin/system_user.go
邹方成 42e7cb5f12
Some checks failed
Build docker and publish / linux (1.24.5) (push) Failing after 31s
feat(interceptor): 添加APP端token验证接口并实现用户私有数据鉴权
refactor(api/user): 重构用户相关接口使用token验证替代user_id路径参数

docs: 更新API文档规范,明确私有接口需携带token及返回字段要求

fix(service/user): 避免写入未使用字段的零值导致MySQL校验错误

style: 统一格式化部分代码缩进和导入顺序

chore: 更新DS_Store等IDE配置文件
2025-11-15 00:49:53 +08:00

148 lines
4.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package admin
import (
"net/http"
"time"
"bindbox-game/internal/pkg/core"
"bindbox-game/internal/pkg/validation"
)
type userListRequest struct {
UserName string `form:"userName"`
UserGender string `form:"userGender"`
UserPhone string `form:"userPhone"`
UserEmail string `form:"userEmail"`
Status string `form:"status"`
Current int `form:"current"`
Size int `form:"size"`
}
type userListItem struct {
ID int32 `json:"id"`
Avatar string `json:"avatar"`
Status string `json:"status"`
UserName string `json:"userName"`
UserGender string `json:"userGender"`
NickName string `json:"nickName"`
UserPhone string `json:"userPhone"`
UserEmail string `json:"userEmail"`
UserRoles []string `json:"userRoles"`
CreateBy string `json:"createBy"`
CreateTime string `json:"createTime"`
UpdateBy string `json:"updateBy"`
UpdateTime string `json:"updateTime"`
}
type userListResponse struct {
Records []userListItem `json:"records"`
Current int `json:"current"`
Size int `json:"size"`
Total int64 `json:"total"`
}
// ListUsers 系统用户列表(映射到 Admin 表)
// @Summary 系统用户列表
// @Description 返回系统用户分页数据
// @Tags 管理端.系统
// @Accept json
// @Produce json
// @Param current query int true "页码" default(1)
// @Param size query int true "每页数量" default(20)
// @Param userName query string false "用户名"
// @Param userEmail query string false "邮箱"
// @Param userPhone query string false "手机号"
// @Param status query string false "状态"
// @Success 200 {object} userListResponse
// @Router /api/user/list [get]
// @Security LoginVerifyToken
func (h *handler) ListUsers() core.HandlerFunc {
return func(ctx core.Context) {
req := new(userListRequest)
res := new(userListResponse)
if err := ctx.ShouldBindForm(req); err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, 10001, validation.Error(err)))
return
}
if req.Current <= 0 {
req.Current = 1
}
if req.Size <= 0 {
req.Size = 20
}
q := h.readDB.Admin.WithContext(ctx.RequestContext()).ReadDB()
if req.UserName != "" {
q = q.Where(h.readDB.Admin.Username.Like("%" + req.UserName + "%"))
}
if req.UserEmail != "" {
// 注意Admin模型没有邮箱字段这里用昵称字段来支持搜索
q = q.Where(h.readDB.Admin.Nickname.Like("%" + req.UserEmail + "%"))
}
if req.UserPhone != "" {
q = q.Where(h.readDB.Admin.Mobile.Like("%" + req.UserPhone + "%"))
}
if req.Status != "" {
// 状态筛选1-正常 0-禁用
if req.Status == "1" || req.Status == "正常" {
q = q.Where(h.readDB.Admin.LoginStatus.Eq(1))
} else if req.Status == "0" || req.Status == "禁用" {
q = q.Where(h.readDB.Admin.LoginStatus.Eq(0))
}
}
total, err := q.Count()
if err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, 10002, err.Error()))
return
}
admins, err := q.Offset((req.Current - 1) * req.Size).Limit(req.Size).Find()
if err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, 10003, err.Error()))
return
}
res.Records = make([]userListItem, len(admins))
for i, a := range admins {
res.Records[i] = userListItem{
ID: a.ID,
Avatar: a.Avatar,
Status: mapLoginStatus(a.LoginStatus),
UserName: a.Username,
UserGender: "未知",
NickName: a.Nickname,
UserPhone: a.Mobile,
UserEmail: "",
UserRoles: rolesFromAdmin(a.IsSuper),
CreateBy: a.CreatedUser,
CreateTime: a.CreatedAt.Format(time.RFC3339),
UpdateBy: a.UpdatedUser,
UpdateTime: a.UpdatedAt.Format(time.RFC3339),
}
}
res.Current = req.Current
res.Size = req.Size
res.Total = total
ctx.Payload(res)
}
}
func mapLoginStatus(s int32) string {
switch s {
case 1:
return "1"
case 0:
return "4"
default:
return "2"
}
}
func rolesFromAdmin(isSuper int32) []string {
if isSuper == 1 {
return []string{"R_SUPER"}
}
return []string{"R_ADMIN"}
}