get_api_wx_card_qrcode_logic.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package wxcard
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "strconv"
  8. "wechat-api/hook/wechat"
  9. "wechat-api/internal/svc"
  10. "wechat-api/internal/types"
  11. "github.com/ArtisanCloud/PowerWeChat/v2/src/kernel/power"
  12. "github.com/zeromicro/go-zero/core/logx"
  13. )
  14. type GetApiWxCardQrcodeLogic struct {
  15. logx.Logger
  16. ctx context.Context
  17. svcCtx *svc.ServiceContext
  18. }
  19. func NewGetApiWxCardQrcodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetApiWxCardQrcodeLogic {
  20. return &GetApiWxCardQrcodeLogic{
  21. Logger: logx.WithContext(ctx),
  22. ctx: ctx,
  23. svcCtx: svcCtx}
  24. }
  25. func (l *GetApiWxCardQrcodeLogic) GetApiWxCardQrcode(req *types.QrcodeReq, w http.ResponseWriter) {
  26. AppId := l.svcCtx.Config.Miniprogram.Appid
  27. Secret := l.svcCtx.Config.Miniprogram.Secret
  28. // 1. 初始化小程序应用实例
  29. app, err := wechat.NewClient(AppId, Secret)
  30. if err != nil {
  31. fmt.Printf("init wechat miniprogram failed:%v\n", err)
  32. return
  33. }
  34. pphash := power.HashMap{}
  35. resp, err := app.WXACode.Get(req.Path, req.Width, false, &pphash, false)
  36. if err != nil {
  37. fmt.Printf("get miniprogram qrcode failed:%v\n", err)
  38. return
  39. }
  40. defer resp.Body.Close()
  41. imageContent, _ := io.ReadAll(resp.Body)
  42. w.Header().Set("Content-Type", "image/png")
  43. w.Header().Set("Content-Length", strconv.Itoa(len(imageContent)))
  44. _, _ = fmt.Fprintln(w, imageContent)
  45. //fmt.Printf("imageContent=%v, contentLength=%d\n", imageContent, resp.ContentLength)
  46. }