115 lines
3.5 KiB
Go
115 lines
3.5 KiB
Go
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)
|
||
}
|
||
}
|