package admin import ( "encoding/base64" "net/http" "net/url" "bindbox-game/configs" "bindbox-game/internal/code" "bindbox-game/internal/pkg/core" "bindbox-game/internal/pkg/validation" "bindbox-game/internal/pkg/wechat" ) type miniappQRCodeRequest struct { InviteCode string `json:"invite_code"` DouyinID string `json:"douyin_id"` Width *int `json:"width"` } type miniappQRCodeResponse struct { ImageBase64 string `json:"image_base64"` } func (h *handler) GenerateMiniAppQRCode() core.HandlerFunc { return func(ctx core.Context) { req := new(miniappQRCodeRequest) if err := ctx.ShouldBindJSON(req); err != nil { ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err))) return } if req.InviteCode == "" || req.DouyinID == "" { ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "invite_code 与 douyin_id 不能为空")) return } path := "/pages/login/index?invite_code=" + url.QueryEscape(req.InviteCode) + "&douyin_id=" + url.QueryEscape(req.DouyinID) cfg := configs.Get() wxcfg := &wechat.WechatConfig{AppID: cfg.Wechat.AppID, AppSecret: cfg.Wechat.AppSecret} q := &wechat.QRCodeRequest{Path: path} if req.Width != nil { q.Width = *req.Width } rsp, err := wechat.GetQRCodeWithConfig(ctx, wxcfg, q) if err != nil { ctx.AbortWithError(core.Error(http.StatusBadRequest, 12001, err.Error())) return } out := &miniappQRCodeResponse{ImageBase64: base64.StdEncoding.EncodeToString(rsp.Buffer)} ctx.Payload(out) } }