qrcode.go 3.5 KB

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