wechat.go 4.4 KB

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