Refactor controllers to use httpx for JSON responses and eliminate context dependency

- Updated TagController methods to use httpx.WriteJSON for consistent JSON response handling.
- Refactored TicketController to replace context-dependent methods with standalone functions using httpx.
- Modified UserController to utilize httpx for error handling and response formatting.
- Adjusted WechatController to remove context dependency and standardize response handling.
This commit is contained in:
mlogclub
2026-05-23 22:22:18 +08:00
parent 7fdf96dd9d
commit e9d2e41f9e
34 changed files with 2395 additions and 2313 deletions
+13 -17
View File
@@ -10,36 +10,32 @@ import (
"github.com/silenceper/wechat/v2/work/kf"
)
type WechatController struct {
Ctx *gin.Context
}
// GetCallback GET请求用于校验回调是否配置正确
func (c *WechatController) GetCallback() {
func WechatGetCallback(ctx *gin.Context) {
cli, err := wxwork.GetWorkCli().GetKF()
if err != nil {
c.Ctx.AbortWithError(http.StatusInternalServerError, err)
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
options := kf.SignatureOptions{}
if err := params.ReadForm(c.Ctx, &options); err != nil {
c.Ctx.AbortWithError(http.StatusUnauthorized, err)
if err := params.ReadForm(ctx, &options); err != nil {
ctx.AbortWithError(http.StatusUnauthorized, err)
return
}
// 调用VerifyURL方法校验当前请求,如果合法则把解密后的内容作为响应返回给微信服务器
echo, err := cli.VerifyURL(options)
if err == nil {
c.Ctx.String(http.StatusOK, echo)
ctx.String(http.StatusOK, echo)
} else {
c.Ctx.AbortWithError(http.StatusUnauthorized, err)
ctx.AbortWithError(http.StatusUnauthorized, err)
}
}
// PostCallback POST请求用于接收回调
func (c *WechatController) PostCallback() {
func WechatPostCallback(ctx *gin.Context) {
cli, err := wxwork.GetWorkCli().GetKF()
if err != nil {
c.Ctx.AbortWithError(http.StatusInternalServerError, err)
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
var (
@@ -47,22 +43,22 @@ func (c *WechatController) PostCallback() {
body []byte
)
// 读取原始消息内容
body, err = io.ReadAll(c.Ctx.Request.Body)
body, err = io.ReadAll(ctx.Request.Body)
if err != nil {
c.Ctx.AbortWithError(http.StatusInternalServerError, err)
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
// 解析原始数据
message, err = cli.GetCallbackMessage(body)
if err != nil {
c.Ctx.AbortWithError(http.StatusInternalServerError, err)
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
if err := wxwork.ConsumeCallback(message); err != nil {
c.Ctx.AbortWithError(http.StatusInternalServerError, err)
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
c.Ctx.String(http.StatusOK, "ok")
ctx.String(http.StatusOK, "ok")
}