qrcode.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. // width 图片宽度
  37. Width int `json:"width,omitempty"`
  38. // scene 最大32个可见字符,只支持数字,大小写英文以及部分特殊字符:!#$&'()*+,/:;=?@-._~,其它字符请自行编码为合法字符(因不支持%,中文无法使用 urlencode 处理,请使用其他编码方式)
  39. Scene string `json:"scene,omitempty"`
  40. // autoColor 自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调
  41. AutoColor bool `json:"auto_color,omitempty"`
  42. // lineColor AutoColor 为 false 时生效,使用 rgb 设置颜色 例如 {"r":"xxx","g":"xxx","b":"xxx"},十进制表示
  43. LineColor Color `json:"line_color,omitempty"`
  44. // isHyaline 是否需要透明底色
  45. IsHyaline bool `json:"is_hyaline,omitempty"`
  46. }
  47. // fetchCode 请求并返回二维码二进制数据
  48. func (qrCode *QRCode) fetchCode(urlStr string, body interface{}) (response []byte, err error) {
  49. var accessToken string
  50. accessToken, err = qrCode.GetAccessToken()
  51. if err != nil {
  52. return
  53. }
  54. urlStr = fmt.Sprintf(urlStr, accessToken)
  55. var contentType string
  56. response, contentType, err = util.PostJSONWithRespContentType(urlStr, body)
  57. if err != nil {
  58. return
  59. }
  60. if strings.HasPrefix(contentType, "application/json") {
  61. // 返回错误信息
  62. var result util.CommonError
  63. err = json.Unmarshal(response, &result)
  64. if err == nil && result.ErrCode != 0 {
  65. err = fmt.Errorf("fetchCode error : errcode=%v , errmsg=%v", result.ErrCode, result.ErrMsg)
  66. return nil, err
  67. }
  68. }
  69. if contentType == "image/jpeg" {
  70. // 返回文件
  71. return response, nil
  72. }
  73. err = fmt.Errorf("fetchCode error : unknown response content type - %v", contentType)
  74. return nil, err
  75. }
  76. // CreateWXAQRCode 获取小程序二维码,适用于需要的码数量较少的业务场景
  77. // 文档地址: https://developers.weixin.qq.com/miniprogram/dev/api/createWXAQRCode.html
  78. func (qrCode *QRCode) CreateWXAQRCode(coderParams QRCoder) (response []byte, err error) {
  79. return qrCode.fetchCode(createWXAQRCodeURL, coderParams)
  80. }
  81. // GetWXACode 获取小程序码,适用于需要的码数量较少的业务场景
  82. // 文档地址: https://developers.weixin.qq.com/miniprogram/dev/api/getWXACode.html
  83. func (qrCode *QRCode) GetWXACode(coderParams QRCoder) (response []byte, err error) {
  84. return qrCode.fetchCode(getWXACodeURL, coderParams)
  85. }
  86. // GetWXACodeUnlimit 获取小程序码,适用于需要的码数量极多的业务场景
  87. // 文档地址: https://developers.weixin.qq.com/miniprogram/dev/api/getWXACodeUnlimit.html
  88. func (qrCode *QRCode) GetWXACodeUnlimit(coderParams QRCoder) (response []byte, err error) {
  89. return qrCode.fetchCode(getWXACodeUnlimitURL, coderParams)
  90. }