get_api_wx_card_qrcode_logic.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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.GetUnlimited(
  36. "share",
  37. req.Path,
  38. false,
  39. req.EnvVersion,
  40. req.Width,
  41. false,
  42. &pphash,
  43. false,
  44. )
  45. if err != nil {
  46. fmt.Printf("get miniprogram qrcode failed:%v\n", err)
  47. return
  48. }
  49. defer resp.Body.Close()
  50. imageContent, _ := io.ReadAll(resp.Body)
  51. w.Header().Set("Content-Type", "image/png")
  52. w.Header().Set("Content-Length", strconv.Itoa(len(imageContent)))
  53. nbytes, err := w.Write(imageContent)
  54. fmt.Printf("nbytes=%d err=%v\n", nbytes, err)
  55. }