邹方成 f1a364bae2 feat(小程序): 添加小程序密钥支持并修复更新时间显示问题
添加 AppSecret 字段支持小程序认证
修复列表页中更新时间显示错误的问题
新增微信小程序二维码生成接口
2025-10-18 11:18:25 +08:00

152 lines
4.1 KiB
Go
Executable File
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 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"` // 小程序头像
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 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 /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 {
res.List[k] = listData{
ID: v.ID,
AppID: v.AppID,
AppSecret: v.AppSecret,
Name: v.Name,
Description: v.Description,
Avatar: v.Avatar,
CreatedAt: timeutil.FriendlyTime(v.CreatedAt),
UpdatedAt: timeutil.FriendlyTime(v.UpdatedAt),
}
}
ctx.Payload(res)
}
}