wechat.go 3.8 KB

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