wechat.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package services
  2. import (
  3. "eta/eta_mini_crm_ht/models"
  4. "eta/eta_mini_crm_ht/utils"
  5. "fmt"
  6. "github.com/silenceper/wechat/v2"
  7. "github.com/silenceper/wechat/v2/cache"
  8. "github.com/silenceper/wechat/v2/credential"
  9. "github.com/silenceper/wechat/v2/officialaccount"
  10. "github.com/silenceper/wechat/v2/officialaccount/config"
  11. "github.com/silenceper/wechat/v2/officialaccount/js"
  12. "github.com/silenceper/wechat/v2/officialaccount/user"
  13. "time"
  14. )
  15. type WechatAccessToken struct {
  16. }
  17. func GetWxChat() (officialAccount *officialaccount.OfficialAccount) {
  18. wc := wechat.NewWechat()
  19. memory := cache.NewMemory()
  20. conf := &config.Config{
  21. AppID: utils.HT_WX_APPID,
  22. AppSecret: utils.HT_WX_APP_SECRET,
  23. Token: "",
  24. EncodingAESKey: "",
  25. Cache: memory,
  26. }
  27. officialAccount = wc.GetOfficialAccount(conf)
  28. wechatAccessToken := &WechatAccessToken{}
  29. officialAccount.SetAccessTokenHandle(wechatAccessToken)
  30. return
  31. }
  32. // GetAccessToken 获取accessToken
  33. func (wechat WechatAccessToken) GetAccessToken() (accessToken string, err error) {
  34. //如果300s就要过期了,那么就去刷新accessToken
  35. wxToken, err := models.GetWxTokenById()
  36. if err != nil {
  37. utils.FileLog.Info("获取微信token失败,Err:", err.Error())
  38. return
  39. }
  40. //如果300s就要过期了,那么就去刷新accessToken
  41. if wxToken.ExpiresIn < time.Now().Unix()+300 {
  42. tmpAccessToken, expires, tmpErr := getTokenFromServer(utils.HT_WX_APPID, utils.HT_WX_APP_SECRET)
  43. if tmpErr != nil {
  44. err = tmpErr
  45. return
  46. }
  47. var updateCols = []string{"access_token", "expires_in"}
  48. wxToken.AccessToken = tmpAccessToken
  49. wxToken.ExpiresIn = time.Now().Unix() + expires - 600 //快过期前10分钟就刷新掉
  50. _ = wxToken.Update(updateCols)
  51. }
  52. accessToken = wxToken.AccessToken
  53. return
  54. }
  55. // accessToken, err = utils.Rc.RedisString(utils.CACHE_WX_ACCESS_TOKEN_DW)
  56. // if accessToken != "" {
  57. // return
  58. // }
  59. // // 缓存中没有取到数据,那么就需要强制刷新的accessToken
  60. // tmpAccessToken, _, tmpErr := getTokenFromServer(utils.DW_WX_APPID, utils.DW_WX_APP_SECRET)
  61. // if tmpAccessToken == "" {
  62. // err = errors.New("获取微信token失败,Err:" + tmpErr.Error())
  63. // return
  64. // }
  65. // redisTimeExpire := time.Duration(expires-600) * time.Second
  66. // err = utils.Rc.Put(utils.CACHE_WX_ACCESS_TOKEN_HZ, tmpAccessToken, redisTimeExpire)
  67. // if err != nil {
  68. // err = errors.New("更新微信token失败")
  69. // return
  70. // }
  71. // accessToken = tmpAccessToken
  72. // return
  73. // //如果300s就要过期了,那么就去刷新accessToken
  74. // if wxToken.ExpiresIn < time.Now().Unix()+300 {
  75. // tmpAccessToken, expires, tmpErr := getTokenFromServer(WxAppId, WxAppSecret)
  76. // if tmpErr != nil {
  77. // err = tmpErr
  78. // return
  79. // }
  80. // var updateCols = []string{"access_token", "expires_in"}
  81. // wxToken.AccessToken = tmpAccessToken
  82. // wxToken.ExpiresIn = expires - 600 //快过期前10分钟就刷新掉
  83. // wxToken.Update(updateCols)
  84. // }
  85. // accessToken = wxToken.AccessToken
  86. // return refreshWxAccessToken(wxAppId, wxAppSecret)
  87. // getTokenFromServer 服务端获取accessToken
  88. func getTokenFromServer(appid, wxSecret string) (accessToken string, expires int64, err error) {
  89. apiUrl := "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s"
  90. resAccessToken, err := credential.GetTokenFromServer(fmt.Sprintf(apiUrl, appid, wxSecret))
  91. if err != nil {
  92. return
  93. }
  94. expires = resAccessToken.ExpiresIn
  95. accessToken = resAccessToken.AccessToken
  96. return
  97. }
  98. // GetUserInfo 获取微信用户详情
  99. func GetUserInfo(openid string) (userInfo *user.Info, err error) {
  100. wechatClient := GetWxChat()
  101. userClient := wechatClient.GetUser()
  102. userInfo, err = userClient.GetUserInfo(openid)
  103. return
  104. }
  105. // GetJsConfig 获取公众号jsConfig
  106. func GetJsConfig(signUrl string) (jsConf *js.Config, err error) {
  107. wechatClient := GetWxChat()
  108. j := wechatClient.GetJs()
  109. jsConf, err = j.GetConfig(signUrl)
  110. return
  111. }