qrcode.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package qrcode
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strings"
  6. "github.com/silenceper/wechat/v2/miniprogram/context"
  7. "github.com/silenceper/wechat/v2/util"
  8. )
  9. const (
  10. createWXAQRCodeURL = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=%s"
  11. getWXACodeURL = "https://api.weixin.qq.com/wxa/getwxacode?access_token=%s"
  12. getWXACodeUnlimitURL = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=%s"
  13. )
  14. // QRCode struct
  15. type QRCode struct {
  16. *context.Context
  17. }
  18. // NewQRCode 实例
  19. func NewQRCode(context *context.Context) *QRCode {
  20. qrCode := new(QRCode)
  21. qrCode.Context = context
  22. return qrCode
  23. }
  24. // Color QRCode color
  25. type Color struct {
  26. R string `json:"r"`
  27. G string `json:"g"`
  28. B string `json:"b"`
  29. }
  30. // QRCoder 小程序码参数
  31. type QRCoder struct {
  32. // page 必须是已经发布的小程序存在的页面,根路径前不要填加 /,不能携带参数(参数请放在scene字段里),如果不填写这个字段,默认跳主页面
  33. Page string `json:"page,omitempty"`
  34. // path 扫码进入的小程序页面路径
  35. Path string `json:"path,omitempty"`
  36. // checkPath 检查page 是否存在,为 true 时 page 必须是已经发布的小程序存在的页面(否则报错);为 false 时允许小程序未发布或者 page 不存在, 但page 有数量上限(60000个)请勿滥用
  37. CheckPath bool `json:"check_path,omitempty"`
  38. // width 图片宽度
  39. Width int `json:"width,omitempty"`
  40. // scene 最大32个可见字符,只支持数字,大小写英文以及部分特殊字符:!#$&'()*+,/:;=?@-._~,其它字符请自行编码为合法字符(因不支持%,中文无法使用 urlencode 处理,请使用其他编码方式)
  41. Scene string `json:"scene,omitempty"`
  42. // autoColor 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调
  43. AutoColor bool `json:"auto_color,omitempty"`
  44. // lineColor AutoColor 为 false 时生效,使用 rgb 设置颜色 例如 {"r":"xxx","g":"xxx","b":"xxx"},十进制表示
  45. LineColor Color `json:"line_color,omitempty"`
  46. // isHyaline 是否需要透明底色
  47. IsHyaline bool `json:"is_hyaline,omitempty"`
  48. // envVersion 要打开的小程序版本。正式版为 "release",体验版为 "trial",开发版为 "develop"
  49. EnvVersion string `json:"env_version,omitempty"`
  50. }
  51. // fetchCode 请求并返回二维码二进制数据
  52. func (qrCode *QRCode) fetchCode(urlStr string, body interface{}) (response []byte, err error) {
  53. var accessToken string
  54. accessToken, err = qrCode.GetAccessToken()
  55. if err != nil {
  56. return
  57. }
  58. urlStr = fmt.Sprintf(urlStr, accessToken)
  59. var contentType string
  60. response, contentType, err = util.PostJSONWithRespContentType(urlStr, body)
  61. if err != nil {
  62. return
  63. }
  64. if strings.HasPrefix(contentType, "application/json") {
  65. // 返回错误信息
  66. var result util.CommonError
  67. err = json.Unmarshal(response, &result)
  68. if err == nil && result.ErrCode != 0 {
  69. err = fmt.Errorf("fetchCode error : errcode=%v , errmsg=%v", result.ErrCode, result.ErrMsg)
  70. return nil, err
  71. }
  72. }
  73. if contentType == "image/jpeg" {
  74. // 返回文件
  75. return response, nil
  76. }
  77. err = fmt.Errorf("fetchCode error : unknown response content type - %v", contentType)
  78. return nil, err
  79. }
  80. // CreateWXAQRCode 获取小程序二维码,适用于需要的码数量较少的业务场景
  81. // 文档地址: https://developers.weixin.qq.com/miniprogram/dev/api/createWXAQRCode.html
  82. func (qrCode *QRCode) CreateWXAQRCode(coderParams QRCoder) (response []byte, err error) {
  83. return qrCode.fetchCode(createWXAQRCodeURL, coderParams)
  84. }
  85. // GetWXACode 获取小程序码,适用于需要的码数量较少的业务场景
  86. // 文档地址: https://developers.weixin.qq.com/miniprogram/dev/api/getWXACode.html
  87. func (qrCode *QRCode) GetWXACode(coderParams QRCoder) (response []byte, err error) {
  88. return qrCode.fetchCode(getWXACodeURL, coderParams)
  89. }
  90. // GetWXACodeUnlimit 获取小程序码,适用于需要的码数量极多的业务场景
  91. // 文档地址: https://developers.weixin.qq.com/miniprogram/dev/api/getWXACodeUnlimit.html
  92. func (qrCode *QRCode) GetWXACodeUnlimit(coderParams QRCoder) (response []byte, err error) {
  93. return qrCode.fetchCode(getWXACodeUnlimitURL, coderParams)
  94. }