Files
ai-agent/internal/controllers/third/wechat_controller.go
T
mlogclub 79614b2d07 Refactor import paths to use internal/pkg/httpx/params
- Updated multiple repository and service files to replace imports from "github.com/mlogclub/simple/web/params" with "cs-agent/internal/pkg/httpx/params".
- Adjusted context handling in auth_service and ws_service to use gin.Context instead of iris.Context.
- Ensured consistent usage of HTTP status responses across websocket handlers.
2026-05-23 22:10:20 +08:00

69 lines
1.6 KiB
Go

package third
import (
"cs-agent/internal/pkg/httpx/params"
"cs-agent/internal/wxwork"
"io"
"net/http"
"github.com/gin-gonic/gin"
"github.com/silenceper/wechat/v2/work/kf"
)
type WechatController struct {
Ctx *gin.Context
}
// GetCallback GET请求用于校验回调是否配置正确
func (c *WechatController) GetCallback() {
cli, err := wxwork.GetWorkCli().GetKF()
if err != nil {
c.Ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
options := kf.SignatureOptions{}
if err := params.ReadForm(c.Ctx, &options); err != nil {
c.Ctx.AbortWithError(http.StatusUnauthorized, err)
return
}
// 调用VerifyURL方法校验当前请求,如果合法则把解密后的内容作为响应返回给微信服务器
echo, err := cli.VerifyURL(options)
if err == nil {
c.Ctx.String(http.StatusOK, echo)
} else {
c.Ctx.AbortWithError(http.StatusUnauthorized, err)
}
}
// PostCallback POST请求用于接收回调
func (c *WechatController) PostCallback() {
cli, err := wxwork.GetWorkCli().GetKF()
if err != nil {
c.Ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
var (
message kf.CallbackMessage
body []byte
)
// 读取原始消息内容
body, err = io.ReadAll(c.Ctx.Request.Body)
if err != nil {
c.Ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
// 解析原始数据
message, err = cli.GetCallbackMessage(body)
if err != nil {
c.Ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
if err := wxwork.ConsumeCallback(message); err != nil {
c.Ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
c.Ctx.String(http.StatusOK, "ok")
}