wechat.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package wechat
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "github.com/rdlucklib/rdluck_tools/http"
  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. "hongze/hongze_yb/global"
  15. "hongze/hongze_yb/models/response/pc"
  16. "hongze/hongze_yb/models/tables/wx_token"
  17. "time"
  18. )
  19. var (
  20. WxId string //微信原始ID
  21. WxAppId string
  22. WxAppSecret string
  23. TemplateIdWithCommunityQuestion string // 问答社区回复模板消息ID
  24. WxYbAppId string // 研报小程序AppID
  25. PcWxAppId string //pc版AppId
  26. PcWxAppSecret string //pc版AppSecret
  27. )
  28. func init() {
  29. WxYbAppId = "wxb059c872d79b9967"
  30. if global.CONFIG.Serve.RunMode == "debug" {
  31. WxAppId = "wx9b5d7291e581233a"
  32. WxAppSecret = "f4d52e34021eee262dce9682b31f8861"
  33. WxId = "gh_5dc508325c6f"
  34. TemplateIdWithCommunityQuestion = "CB7bOl7f3viMG4s1uhRo7WM0Jbx3WvodKuIZ8A_z8fM"
  35. PcWxAppId = "wxcba9a7ec590ee2d5"
  36. PcWxAppSecret = "aa58d257e2521d768cbf1bf89989769d"
  37. } else {
  38. WxAppId = "wx4a844c734d8c8e56"
  39. WxAppSecret = "26c586e7ccb3c575433f0f37797b3eeb"
  40. WxId = "gh_b67e0049fb8c"
  41. TemplateIdWithCommunityQuestion = "dYg6iHooRq74PyCXmw_Ns7qdJZmbtLoKS2p2FKeaXl0"
  42. PcWxAppId = "wx4da95782cfc8c5eb"
  43. PcWxAppSecret = "8f82ebf2ba3aa06ce44541726385df64"
  44. }
  45. }
  46. func GetWxChat() (officialAccount *officialaccount.OfficialAccount) {
  47. wc := wechat.NewWechat()
  48. memory := cache.NewMemory()
  49. conf := &config.Config{
  50. AppID: WxAppId,
  51. AppSecret: WxAppSecret,
  52. Token: "",
  53. EncodingAESKey: "",
  54. Cache: memory,
  55. }
  56. officialAccount = wc.GetOfficialAccount(conf)
  57. wechatAccessToken := &WechatAccessToken{}
  58. officialAccount.SetAccessTokenHandle(wechatAccessToken)
  59. return
  60. }
  61. // GetUserInfo 获取微信用户详情
  62. func GetUserInfo(openid string) (userInfo *user.Info, err error) {
  63. wechatClient := GetWxChat()
  64. userClient := wechatClient.GetUser()
  65. userInfo, err = userClient.GetUserInfo(openid)
  66. return
  67. }
  68. // GetJsConfig 获取公众号jsConfig
  69. func GetJsConfig(signUrl string) (jsConf *js.Config, err error) {
  70. wechatClient := GetWxChat()
  71. j := wechatClient.GetJs()
  72. jsConf, err = j.GetConfig(signUrl)
  73. return
  74. }
  75. type WechatAccessToken struct {
  76. }
  77. // GetAccessToken 获取accessToken
  78. func (wechat WechatAccessToken) GetAccessToken() (accessToken string, err error) {
  79. wxToken, err := wx_token.GetById()
  80. if err != nil {
  81. return
  82. }
  83. //如果300s就要过期了,那么就去刷新accessToken
  84. if wxToken.ExpiresIn < time.Now().Unix()+300 {
  85. tmpAccessToken, expires, tmpErr := getTokenFromServer(WxAppId, WxAppSecret)
  86. if tmpErr != nil {
  87. err = tmpErr
  88. return
  89. }
  90. var updateCols = []string{"AccessToken", "ExpiresIn"}
  91. wxToken.AccessToken = tmpAccessToken
  92. wxToken.ExpiresIn = expires - 600 //快过期前10分钟就刷新掉
  93. wxToken.Update(updateCols)
  94. }
  95. accessToken = wxToken.AccessToken
  96. return
  97. }
  98. // getTokenFromServer 服务端获取accessToken
  99. func getTokenFromServer(appid, wxSecret string) (accessToken string, expires int64, err error) {
  100. apiUrl := "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s"
  101. resAccessToken, err := credential.GetTokenFromServer(fmt.Sprintf(apiUrl, appid, wxSecret))
  102. if err != nil {
  103. return
  104. }
  105. expires = resAccessToken.ExpiresIn
  106. accessToken = resAccessToken.AccessToken
  107. return
  108. }
  109. func PcWxGetUserOpenIdByCode(code string) (item *pc.WxAccessToken, err error) {
  110. if code == "" {
  111. err = errors.New("code is empty")
  112. return nil, err
  113. }
  114. requestUrl := `https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code`
  115. requestUrl = fmt.Sprintf(requestUrl, PcWxAppId, PcWxAppSecret, code)
  116. result, err := http.Get(requestUrl)
  117. if err != nil {
  118. return nil, err
  119. }
  120. err = json.Unmarshal(result, &item)
  121. return
  122. }
  123. func PcWxGetUserInfo(openId, accessToken string) (item *pc.WxUserInfo, err error) {
  124. requestUrl := `https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s`
  125. requestUrl = fmt.Sprintf(requestUrl, accessToken, openId)
  126. result, err := http.Get(requestUrl)
  127. if err != nil {
  128. return
  129. }
  130. err = json.Unmarshal(result, &item)
  131. return
  132. }