169 lines
4.8 KiB
Go
Executable File
169 lines
4.8 KiB
Go
Executable File
package app
|
||
|
||
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 {
|
||
AppID string `form:"app_id"` // 小程序ID
|
||
AdminID int32 `form:"admin_id"` // 客服编号
|
||
Name string `form:"name"` // 小程序名称
|
||
Page int `form:"page"` // 当前页码,默认为第一页
|
||
PageSize int `form:"page_size"` // 每页返回的数据量
|
||
}
|
||
|
||
type listData struct {
|
||
ID int32 `json:"id"` // 小程序编号
|
||
AppID string `json:"app_id"` // 小程序ID
|
||
AppSecret string `json:"app_secret"` // 小程序密钥
|
||
Name string `json:"name"` // 小程序名称
|
||
Description string `json:"description"` // 小程序描述
|
||
Avatar string `json:"avatar"` // 小程序头像
|
||
TemplateID string `json:"template_id"` // 模版ID
|
||
CreatedAt string `json:"created_at"` // 创建时间
|
||
UpdatedAt string `json:"updated_at"` // 更新时间
|
||
MessageTotal int64 `json:"message_total"` // 消息总数
|
||
CheckStatusText string `json:"check_status_text"` // 状态文字描述
|
||
}
|
||
|
||
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 name query string false "小程序名称"
|
||
// @Param app_id query string false "小程序ID"
|
||
// @Param admin_id query int 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 /api/admin/apps [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.ListAppError,
|
||
fmt.Sprintf("%s: 一次最多只能查询 100 条", code.Text(code.ListAppError)),
|
||
))
|
||
return
|
||
}
|
||
|
||
query := h.readDB.MiniProgram.WithContext(ctx.RequestContext())
|
||
|
||
if ctx.SessionUserInfo().IsSuper != 1 {
|
||
query = query.Where(h.readDB.MiniProgram.AdminID.Eq(ctx.SessionUserInfo().Id))
|
||
} else {
|
||
if req.AdminID != 0 {
|
||
query = query.Where(h.readDB.MiniProgram.AdminID.Eq(req.AdminID))
|
||
}
|
||
}
|
||
|
||
if req.AppID != "" {
|
||
query = query.Where(h.readDB.MiniProgram.AppID.Eq(req.AppID))
|
||
}
|
||
|
||
if req.Name != "" {
|
||
query = query.Where(h.readDB.MiniProgram.Name.Like(fmt.Sprintf("%%%s%%", req.Name)))
|
||
}
|
||
|
||
listQueryDB := query.Session(&gorm.Session{})
|
||
countQueryDB := query.Session(&gorm.Session{})
|
||
|
||
resultData, err := listQueryDB.
|
||
Order(h.readDB.MiniProgram.ID.Desc()).
|
||
Limit(req.PageSize).
|
||
Offset((req.Page - 1) * req.PageSize).Find()
|
||
if err != nil {
|
||
ctx.AbortWithError(core.Error(
|
||
http.StatusBadRequest,
|
||
code.ListAppError,
|
||
fmt.Sprintf("%s:%s", code.Text(code.ListAppError), err.Error())),
|
||
)
|
||
return
|
||
}
|
||
|
||
count, err := countQueryDB.Count()
|
||
if err != nil {
|
||
ctx.AbortWithError(core.Error(
|
||
http.StatusBadRequest,
|
||
code.ListAppError,
|
||
fmt.Sprintf("%s:%s", code.Text(code.ListAppError), 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 {
|
||
userTotalCount, _ := h.readDB.AppUser.WithContext(ctx.RequestContext()).
|
||
Where(h.readDB.AppUser.AppID.Eq(v.AppID)).
|
||
Count()
|
||
|
||
checkStatusText := "未知"
|
||
if v.Status == 1 {
|
||
checkStatusText = "正常"
|
||
} else if v.Status == -1 {
|
||
checkStatusText = "封禁"
|
||
}
|
||
|
||
res.List[k] = listData{
|
||
ID: v.ID,
|
||
AppID: v.AppID,
|
||
AppSecret: v.AppSecret,
|
||
Name: v.Name,
|
||
Description: v.Description,
|
||
Avatar: v.Avatar,
|
||
TemplateID: v.TemplateID,
|
||
CreatedAt: timeutil.FriendlyTime(v.CreatedAt),
|
||
UpdatedAt: timeutil.FriendlyTime(v.UpdatedAt),
|
||
MessageTotal: userTotalCount,
|
||
CheckStatusText: checkStatusText,
|
||
}
|
||
}
|
||
|
||
ctx.Payload(res)
|
||
}
|
||
}
|