bindbox-game/internal/api/app/app_check_status.go
邹方成 084b802b05 feat(小程序): 添加检查小程序状态接口
refactor(测试): 移除测试中的硬编码凭证
fix(模板消息): 将小程序状态改为正式版
docs(swagger): 更新API文档并移除密码必填限制
2025-11-06 20:37:01 +08:00

115 lines
3.5 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 app
import (
"fmt"
"net/http"
"mini-chat/internal/code"
"mini-chat/internal/pkg/core"
"mini-chat/internal/pkg/httpclient"
"mini-chat/internal/pkg/validation"
"go.uber.org/zap"
)
// checkAppStatusRequest 请求参数
// 功能描述用于检查指定小程序app_id的运行状态
// 参数说明:
// - AppID(string): 小程序ID必填通过query参数传入form:"app_id"
// 返回值该函数返回一个core.HandlerFunc处理器在HTTP请求完成后通过ctx.Payload返回JSON数据
type checkAppStatusRequest struct {
AppID string `form:"app_id" binding:"required"` // 小程序ID
}
// checkAppStatusResponse 响应数据结构
// 功能描述:返回外部验证服务的状态码与状态文本,并提供统一的文字描述
// 字段说明:
// - AppID(string): 小程序ID
// - Code(int): 外部接口返回的状态码1表示正常0表示封禁其它表示未知
// - Status(string): 外部接口返回的原始状态描述
// - CheckStatusText(string): 根据Code映射得到的中文状态文字正常/封禁/未知)
type checkAppStatusResponse struct {
AppID string `json:"app_id"` // 小程序ID
Code int `json:"code"` // 外部接口返回的状态码
CheckStatusText string `json:"check_status_text"` // 状态文字描述
}
// CheckAppStatus 检查小程序状态
// @Summary 检查小程序状态
// @Description 管理端根据 app_id 调用外部验证服务获取状态(正常/封禁/未知)
// @Tags 管理端.小程序
// @Accept json
// @Produce json
// @Param app_id query string true "小程序ID"
// @Success 200 {object} checkAppStatusResponse
// @Failure 400 {object} code.Failure
// @Router /api/admin/app/check_status [get]
// @Security LoginVerifyToken
func (h *handler) CheckAppStatus() core.HandlerFunc {
return func(ctx core.Context) {
// 绑定请求参数
req := new(checkAppStatusRequest)
if err := ctx.ShouldBindForm(req); err != nil {
ctx.AbortWithError(core.Error(
http.StatusBadRequest,
code.ParamBindError,
validation.Error(err)),
)
return
}
// 调用外部验证服务
// 外部接口: https://api.wxapi.work/xcx/checkxcx.php?appid=<AppID>
// 期望返回: {"code":1, "appid":"xxx", "status":"ok"}
type externalCheckResp struct {
Code int `json:"code"`
Appid string `json:"appid"`
Status string `json:"status"`
}
checkRes := new(externalCheckResp)
response, err := httpclient.GetHttpClientWithContext(ctx.RequestContext()).R().
SetQueryParams(map[string]string{
"appid": req.AppID,
}).
SetResult(checkRes).
Get("https://api.wxapi.work/xcx/checkxcx.php")
if err != nil {
// 记录请求错误
h.logger.Error("请求APP验证服务失败", zap.Error(err))
ctx.AbortWithError(core.Error(
http.StatusBadRequest,
code.ListAppError,
fmt.Sprintf("%s%s", code.Text(code.ListAppError), err.Error()),
))
return
}
// 检查响应状态码
if response.IsError() {
h.logger.Error(fmt.Sprintf("请求APP验证服务异常(%d)", response.StatusCode()))
}
// 将外部返回的code映射为中文状态
statusText := "未知"
switch checkRes.Code {
case 1:
statusText = "正常"
case 0:
statusText = "封禁"
default:
statusText = "未知"
}
// 组织响应数据并返回
res := &checkAppStatusResponse{
AppID: req.AppID,
Code: checkRes.Code,
CheckStatusText: statusText,
}
ctx.Payload(res)
}
}