token.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package wechat
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta_gn/eta_report/models"
  6. "eta_gn/eta_report/services/alarm_msg"
  7. "eta_gn/eta_report/utils"
  8. "fmt"
  9. "io/ioutil"
  10. "net/http"
  11. "strings"
  12. "time"
  13. )
  14. type WxTokenResp struct {
  15. Ret int
  16. Msg string
  17. ErrMsg string
  18. ErrCode string
  19. Data WxTokenData
  20. Success bool `description:"true 执行成功,false 执行失败"`
  21. IsSendEmail bool `json:"-"`
  22. }
  23. type WxTokenReq struct {
  24. WxAppId string `description:"公众号appId"`
  25. WxAppSecret string `description:"公众号appSecret"`
  26. }
  27. type WxTokenData struct {
  28. AccessToken string `description:"微信token"`
  29. }
  30. func GetAccessToken(tokenReq WxTokenReq) (accessToken string, err error, errMsg string) {
  31. if utils.EtaPubUrl == `` {
  32. return
  33. }
  34. url := fmt.Sprintf("%s%s", utils.EtaPubUrl, "v1/wechat/access_token")
  35. defer func() {
  36. if err != nil {
  37. go alarm_msg.SendAlarmMsg(errMsg, 1)
  38. }
  39. }()
  40. postData, err := json.Marshal(tokenReq)
  41. if err != nil {
  42. errMsg = "SendTemplateMsg json.Marshal Err:" + err.Error()
  43. return
  44. }
  45. body := ioutil.NopCloser(strings.NewReader(string(postData)))
  46. client := &http.Client{}
  47. req, err := http.NewRequest("POST", url, body)
  48. if err != nil {
  49. errMsg = "SendTemplateMsg http.NewRequest Err:" + err.Error()
  50. return
  51. }
  52. contentType := "application/json;charset=utf-8"
  53. req.Header.Set("Content-Type", contentType)
  54. req.Header.Set("Authorization", utils.EtaPubAuthorization)
  55. resp, err := client.Do(req)
  56. if err != nil {
  57. errMsg = "http client.Do Err:" + err.Error()
  58. return
  59. }
  60. defer resp.Body.Close()
  61. b, err := ioutil.ReadAll(resp.Body)
  62. if err != nil {
  63. errMsg = "ioutil.ReadAll Err:" + err.Error()
  64. return
  65. }
  66. result := new(WxTokenResp)
  67. err = json.Unmarshal(b, &result)
  68. if err != nil {
  69. errMsg = "json.Unmarshal Err:" + err.Error()
  70. return
  71. }
  72. if result.Ret != 200 {
  73. err = errors.New(string(b))
  74. errMsg = "请求失败:" + err.Error()
  75. return
  76. }
  77. accessToken = result.Data.AccessToken
  78. if accessToken == "" {
  79. err = errors.New("请求accessToken失败")
  80. errMsg = "请求accessToken失败"
  81. return
  82. }
  83. return
  84. }
  85. func GetWxAccessToken(wxAppId, wxAppSecret string) (accessToken string, err error, errMsg string) {
  86. accessToken, err, errMsg = getWxAccessToken(wxAppId, wxAppSecret)
  87. if err != nil {
  88. utils.FileLog.Info(fmt.Sprintf("获取Token失败,err:%s,errMsg:%s", err.Error(), errMsg))
  89. return
  90. }
  91. return
  92. }
  93. func getWxAccessToken(wxAppId, wxAppSecret string) (accessToken string, err error, errMsg string) {
  94. redisKey := getRedisKeyByAppid(wxAppId)
  95. if redisKey == `` {
  96. errMsg = "未配置缓存key"
  97. err = errors.New(errMsg)
  98. return
  99. }
  100. accessToken, err = utils.Rc.RedisString(redisKey)
  101. if accessToken != "" {
  102. return
  103. }
  104. return refreshWxAccessToken(wxAppId, wxAppSecret)
  105. }
  106. func refreshWxAccessToken(wxAppId, wxAppSecret string) (accessToken string, err error, errMsg string) {
  107. fmt.Println("强制刷新" + wxAppId + "微信token")
  108. defer func() {
  109. if errMsg != `` {
  110. utils.FileLog.Info(fmt.Sprintf("强制刷新%s微信token异常:%s", wxAppId, errMsg))
  111. }
  112. }()
  113. if wxAppId == "" || wxAppSecret == "" {
  114. errMsg = fmt.Sprintf("必要信息为空, wxAppId: %s, wxAppSecret: %s", wxAppId, wxAppSecret)
  115. err = errors.New(errMsg)
  116. return
  117. }
  118. redisKey := getRedisKeyByAppid(wxAppId)
  119. if redisKey == `` {
  120. errMsg = "未配置缓存key"
  121. err = errors.New(errMsg)
  122. return
  123. }
  124. wxAccessToken, tmpErr := models.GetWxToken(wxAppId, wxAppSecret)
  125. if tmpErr != nil {
  126. err = tmpErr
  127. errMsg = "通过微信接口获取accessToken失败 Err:" + err.Error()
  128. return
  129. }
  130. if wxAccessToken.AccessToken == "" {
  131. errMsg = "微信返回的accessToken异常: Err:" + wxAccessToken.Errmsg
  132. err = errors.New(errMsg)
  133. return
  134. }
  135. accessToken = wxAccessToken.AccessToken
  136. redisTimeExpire := time.Duration(wxAccessToken.ExpiresIn-600) * time.Second
  137. err = utils.Rc.Put(redisKey, accessToken, redisTimeExpire)
  138. if err != nil {
  139. errMsg = "更新redis中的accessToken失败 Err:" + err.Error()
  140. return
  141. }
  142. return
  143. }
  144. func getRedisKeyByAppid(wxAppId string) (redisKey string) {
  145. redisKey = utils.ETA_WX_ACCESS_TOEKN + wxAppId
  146. return redisKey
  147. }