package admin import ( "fmt" "net/http" "mini-chat/internal/code" "mini-chat/internal/pkg/core" "mini-chat/internal/pkg/timeutil" "mini-chat/internal/pkg/validation" "gorm.io/gorm" ) type listRequest struct { Username string `form:"username"` // 用户名 Nickname string `form:"nickname"` // 昵称 Page int `form:"page"` // 当前页码,默认为第一页 PageSize int `form:"page_size"` // 每页返回的数据量 } type listData struct { ID int32 `json:"id"` // 编号 UserName string `json:"username"` // 用户名 NickName string `json:"nickname"` // 昵称 Mobile string `json:"mobile"` // 手机号 Avatar string `json:"avatar"` // 头像 CreatedAt string `json:"created_at"` // 创建时间 UpdatedAt string `json:"updated_at"` // 更新时间 } type listResponse struct { Page int `json:"page"` // 当前页码 PageSize int `json:"page_size"` // 每页返回的数据量 Total int64 `json:"total"` // 符合查询条件的总记录数 List []listData `json:"list"` } // PageList 客服列表 // @Summary 客服列表 // @Description 客服列表 // @Tags 管理端.客服管理 // @Accept json // @Produce json // @Param username query string false "用户名" // @Param nickname query string false "昵称" // @Param page query int true "当前页码" default(1) // @Param page_size query int true "每页返回的数据量,最多 100 条" default(20) // @Success 200 {object} listResponse // @Failure 400 {object} code.Failure // @Router /admin/list [get] // @Security LoginVerifyToken func (h *handler) PageList() core.HandlerFunc { return func(ctx core.Context) { req := new(listRequest) res := new(listResponse) if err := ctx.ShouldBindForm(req); err != nil { ctx.AbortWithError(core.Error( http.StatusBadRequest, code.ParamBindError, validation.Error(err)), ) return } if req.Page == 0 { req.Page = 1 } if req.PageSize == 0 { req.PageSize = 20 } if req.PageSize > 100 { ctx.AbortWithError(core.Error( http.StatusBadRequest, code.ListAdminError, fmt.Sprintf("%s: 一次最多只能查询 100 条", code.Text(code.ListAdminError)), )) return } query := h.readDB.Admin.WithContext(ctx.RequestContext()) if ctx.SessionUserInfo().IsSuper != 1 { ctx.AbortWithError(core.Error( http.StatusBadRequest, code.ListAdminError, fmt.Sprintf("%s: %s", code.Text(code.ListAdminError), "禁止操作")), ) return } if req.Username != "" { query = query.Where(h.readDB.Admin.Username.Like(fmt.Sprintf("%%%s%%", req.Username))) } if req.Nickname != "" { query = query.Where(h.readDB.Admin.Nickname.Like(fmt.Sprintf("%%%s%%", req.Nickname))) } listQueryDB := query.Session(&gorm.Session{}) countQueryDB := query.Session(&gorm.Session{}) resultData, err := listQueryDB. Order(h.readDB.Admin.ID.Desc()). Limit(req.PageSize). Offset((req.Page - 1) * req.PageSize).Find() if err != nil { ctx.AbortWithError(core.Error( http.StatusBadRequest, code.ListAdminError, fmt.Sprintf("%s:%s", code.Text(code.ListAdminError), err.Error())), ) return } count, err := countQueryDB.Count() if err != nil { ctx.AbortWithError(core.Error( http.StatusBadRequest, code.ListAdminError, fmt.Sprintf("%s:%s", code.Text(code.ListAdminError), err.Error())), ) return } res.Page = req.Page res.PageSize = req.PageSize res.Total = count res.List = make([]listData, len(resultData)) for k, v := range resultData { res.List[k] = listData{ ID: v.ID, UserName: v.Username, NickName: v.Nickname, Mobile: v.Mobile, Avatar: v.Avatar, CreatedAt: timeutil.FriendlyTime(v.CreatedAt), UpdatedAt: timeutil.FriendlyTime(v.CreatedAt), } } ctx.Payload(res) } }