qrcode.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package wechat
  2. import (
  3. "context"
  4. "encoding/json"
  5. "net/http"
  6. "net/url"
  7. "strings"
  8. "go-common/app/interface/main/web-goblin/model/wechat"
  9. "go-common/library/cache/redis"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. )
  13. const (
  14. _accessTokenKey = "a_t_k"
  15. _accessTokenURI = "/cgi-bin/token"
  16. _qrcodeURI = "/wxa/getwxacodeunlimit"
  17. )
  18. // RawAccessToken get wechat access token.
  19. func (d *Dao) RawAccessToken(c context.Context) (data *wechat.AccessToken, err error) {
  20. params := url.Values{}
  21. params.Set("grant_type", "client_credential")
  22. params.Set("appid", d.c.Wechat.AppID)
  23. params.Set("secret", d.c.Wechat.Secret)
  24. var req *http.Request
  25. if req, err = http.NewRequest(http.MethodGet, d.wxAccessTokenURL+"?"+params.Encode(), nil); err != nil {
  26. log.Error("AccessToken http.NewRequest error(%v)", err)
  27. return
  28. }
  29. var res struct {
  30. Errcode int `json:"errcode"`
  31. Errmsg string `json:"errmsg"`
  32. AccessToken string `json:"access_token"`
  33. ExpiresIN int64 `json:"expires_in"`
  34. }
  35. if err = d.httpClient.Do(c, req, &res); err != nil {
  36. log.Error("AccessToken d.httpClient.Do error(%v)", err)
  37. return
  38. }
  39. if res.Errcode != ecode.OK.Code() {
  40. log.Error("AccessToken errcode error(%d) msg(%s)", res.Errcode, res.Errmsg)
  41. err = ecode.RequestErr
  42. return
  43. }
  44. data = &wechat.AccessToken{AccessToken: res.AccessToken, ExpiresIn: res.ExpiresIN}
  45. return
  46. }
  47. // Qrcode get qrcode.
  48. func (d *Dao) Qrcode(c context.Context, accessToken, arg string) (qrcode []byte, err error) {
  49. var (
  50. req *http.Request
  51. bs []byte
  52. jsonErr error
  53. )
  54. params := url.Values{}
  55. params.Set("access_token", accessToken)
  56. if req, err = http.NewRequest(http.MethodPost, d.wxQrcodeURL+"?"+params.Encode(), strings.NewReader(arg)); err != nil {
  57. log.Error("Qrcode http.NewRequest error(%v)", err)
  58. return
  59. }
  60. if bs, err = d.httpClient.Raw(c, req); err != nil {
  61. log.Error("Qrcode d.httpClient.Do error(%v)", err)
  62. return
  63. }
  64. var res struct {
  65. Errcode int `json:"errcode"`
  66. Errmsg string `json:"errmsg"`
  67. }
  68. if jsonErr = json.Unmarshal(bs, &res); jsonErr == nil && res.Errcode != ecode.OK.Code() {
  69. log.Error("Qrcode errcode error(%d) msg(%s)", res.Errcode, res.Errmsg)
  70. err = ecode.RequestErr
  71. return
  72. }
  73. qrcode = bs
  74. return
  75. }
  76. // CacheAccessToken cache access token
  77. func (d *Dao) CacheAccessToken(c context.Context) (data *wechat.AccessToken, err error) {
  78. var (
  79. value []byte
  80. key = _accessTokenKey
  81. conn = d.redis.Get(c)
  82. )
  83. defer conn.Close()
  84. if value, err = redis.Bytes(conn.Do("GET", key)); err != nil {
  85. if err == redis.ErrNil {
  86. err = nil
  87. } else {
  88. log.Error("CacheAccessToken conn.Do(GET, %s) error(%v)", key, err)
  89. }
  90. return
  91. }
  92. data = new(wechat.AccessToken)
  93. if err = json.Unmarshal(value, &data); err != nil {
  94. log.Error("CacheAccessToken json.Unmarshal(%v) error(%v)", value, err)
  95. }
  96. return
  97. }
  98. // AddCacheAccessToken add access token cache
  99. func (d *Dao) AddCacheAccessToken(c context.Context, data *wechat.AccessToken) (err error) {
  100. var (
  101. bs []byte
  102. key = _accessTokenKey
  103. conn = d.redis.Get(c)
  104. )
  105. defer conn.Close()
  106. if bs, err = json.Marshal(data); err != nil {
  107. log.Error("AddCacheAccessToken json.Marshal(%v) error (%v)", data, err)
  108. return
  109. }
  110. if err = conn.Send("SET", key, bs); err != nil {
  111. log.Error("conn.Send(SET, %s, %s) error(%v)", key, string(bs), err)
  112. return
  113. }
  114. expire := data.ExpiresIn - 60
  115. if err = conn.Send("EXPIRE", key, expire); err != nil {
  116. log.Error("conn.Send(Expire, %s, %d) error(%v)", key, expire, err)
  117. return
  118. }
  119. if err = conn.Flush(); err != nil {
  120. log.Error("conn.Flush error(%v)", err)
  121. return
  122. }
  123. for i := 0; i < 2; i++ {
  124. if _, err = conn.Receive(); err != nil {
  125. log.Error("conn.Receive() error(%v)", err)
  126. return
  127. }
  128. }
  129. return
  130. }