ai_scheduler/internal/server/router/wx.go

49 lines
1.4 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 router
import (
"ai_scheduler/internal/pkg/wx"
"context"
"encoding/json"
"fmt"
"github.com/gofiber/fiber/v2"
)
// WxRegist 注册一个通用路由:把 /wx/<path> 的请求转换为对 wx.Request 的调用,使用泛型 map[string]any 作为 Req/Res。
// 这样项目后台可以直接调用所有在 internal/pkg/wx 中定义的 API(路径以 wx.Path 常量为准)。
func WxRegist(
r fiber.Router,
) {
// POST /api/v1/project/wx/login/getLoginQrCode
r.All("wx/*", func(c *fiber.Ctx) error {
sub := c.Params("*")
if sub == "" {
return fiber.ErrBadRequest
}
var reqBody map[string]any
if len(c.Body()) > 0 {
if err := json.Unmarshal(c.Body(), &reqBody); err != nil {
return fiber.NewError(fiber.StatusBadRequest, "invalid json body: "+err.Error())
}
} else {
reqBody = map[string]any{}
}
token := c.Get("X-finder-TOKEN")
var res map[string]any
// 使用 wx.Request 的泛型版本,Req/Res 类型为 map[string]any
if err := wx.Request[map[string]any, map[string]any](context.Background(), token, wx.Api[map[string]any, map[string]any]{Path: wx.Path(sub)}, reqBody, &res); err != nil {
// 返回详细错误
return fiber.NewError(fiber.StatusBadGateway, fmt.Sprintf("upstream wx error: %v", err))
}
// 直接返回上游 data(已经被 wx.Request 解包)
c.Locals("skip_response_wrap", true)
return c.JSON(res)
})
}