123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- 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.Get(req.Path, 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)))
- _, _ = fmt.Fprintln(w, imageContent)
- //fmt.Printf("imageContent=%v, contentLength=%d\n", imageContent, resp.ContentLength)
- }
|