wechat.go 5.2 KB

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