bindbox-game/internal/api/admin/miniapp_qrcode.go
邹方成 45815bfb7d chore: 清理无用文件与优化代码结构
refactor(utils): 修复密码哈希比较逻辑错误
feat(user): 新增按状态筛选优惠券接口
docs: 添加虚拟发货与任务中心相关文档
fix(wechat): 修正Code2Session上下文传递问题
test: 补充订单折扣与积分转换测试用例
build: 更新配置文件与构建脚本
style: 清理多余的空行与注释
2025-12-18 17:35:55 +08:00

65 lines
1.7 KiB
Go

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"`
ChannelCode string `json:"channel_code"`
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
}
q := url.Values{}
if req.InviteCode != "" {
q.Set("invite_code", req.InviteCode)
}
if req.DouyinID != "" {
q.Set("douyin_id", req.DouyinID)
}
if req.ChannelCode != "" {
q.Set("channel_code", req.ChannelCode)
}
if len(q) == 0 {
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "参数不能为空"))
return
}
path := "/pages/login/index?" + q.Encode()
cfg := configs.Get()
wxcfg := &wechat.WechatConfig{AppID: cfg.Wechat.AppID, AppSecret: cfg.Wechat.AppSecret}
qReq := &wechat.QRCodeRequest{Path: path}
if req.Width != nil {
qReq.Width = *req.Width
}
rsp, err := wechat.GetQRCodeWithConfig(ctx, wxcfg, qReq)
if err != nil {
ctx.AbortWithError(core.Error(http.StatusBadRequest, 12001, err.Error()))
return
}
out := &miniappQRCodeResponse{ImageBase64: base64.StdEncoding.EncodeToString(rsp.Buffer)}
ctx.Payload(out)
}
}