12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- package wxcard
- import (
- "context"
- "fmt"
- "io"
- "net/http"
- "strconv"
- "wechat-api/hook/wechat"
- "wechat-api/internal/svc"
- "wechat-api/internal/types"
- "github.com/ArtisanCloud/PowerWeChat/v2/src/kernel/power"
- "github.com/zeromicro/go-zero/core/logx"
- )
- type GetApiWxCardQrcodeLogic struct {
- logx.Logger
- ctx context.Context
- svcCtx *svc.ServiceContext
- }
- func NewGetApiWxCardQrcodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetApiWxCardQrcodeLogic {
- return &GetApiWxCardQrcodeLogic{
- Logger: logx.WithContext(ctx),
- ctx: ctx,
- svcCtx: svcCtx}
- }
- func (l *GetApiWxCardQrcodeLogic) GetApiWxCardQrcode(req *types.QrcodeReq, w http.ResponseWriter) {
- AppId := l.svcCtx.Config.Miniprogram.Appid
- Secret := l.svcCtx.Config.Miniprogram.Secret
- // 1. 初始化小程序应用实例
- app, err := wechat.NewClient(AppId, Secret)
- if err != nil {
- fmt.Printf("init wechat miniprogram failed:%v\n", err)
- return
- }
- pphash := power.HashMap{}
- resp, err := app.WXACode.GetUnlimited(
- "share",
- req.Path,
- false,
- req.EnvVersion,
- req.Width,
- false,
- &pphash,
- false,
- )
- if err != nil {
- fmt.Printf("get miniprogram qrcode failed:%v\n", err)
- return
- }
- defer resp.Body.Close()
- imageContent, _ := io.ReadAll(resp.Body)
- w.Header().Set("Content-Type", "image/png")
- w.Header().Set("Content-Length", strconv.Itoa(len(imageContent)))
- nbytes, err := w.Write(imageContent)
- fmt.Printf("nbytes=%d err=%v\n", nbytes, err)
- }
|