wechat.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. TemplateIdByProduct string //产品运行报告通知-模板ID
  24. TemplateRedirectUrl string //模板消息跳转地址
  25. DayReportTemplateRedirectUrl string //模板消息跳转地址(晨报、周报)
  26. TemplateIdByCompanyApply string //客户申请单审批通知-模板ID
  27. TemplateCompanyApplyRedirectUrl string //审批单模板消息跳转地址
  28. TemplateIdByCompanyReceive string //销售跨部门领取客户通知-模板ID
  29. WxMsgTemplateIdActivityChangeApply string //查研观向活动变更通知-模板ID
  30. WxMsgTemplateIdActivityChangeApplyXzs string //查研观向活动变更通知-模板ID(小助手)
  31. TemplateIdByProductXzs string //产品运行报告通知-模板ID(小助手)
  32. WxCygxAppId string //查研观向小程序APPID
  33. WxMsgTemplateIdAskByUser string //查研观向用户提问新消息推送ID
  34. WxCrmAppId string //随手办公小程序APPID
  35. WxPublicIdXzs string //查研观向小助手公众号
  36. WxPublicSecretXzs string //查研观向小助手公众号
  37. PcWxAppId string //pc版AppId
  38. PcWxAppSecret string //pc版AppSecret
  39. )
  40. func initConf() {
  41. if global.CONFIG.Serve.RunMode == "debug" {
  42. WxAppId = "wx9b5d7291e581233a"
  43. WxAppSecret = "f4d52e34021eee262dce9682b31f8861"
  44. WxId = "gh_5dc508325c6f"
  45. TemplateIdByProduct = "-YjuPOB7Fqd-S3ilabYa6wvjDY9aXmeEfPN6DCiy-EY"
  46. TemplateRedirectUrl = "http://rddpweb.brilliantstart.cn/reportdtl?id="
  47. DayReportTemplateRedirectUrl = "http://report.brilliantstart.cn/#/allindex/" //晨报模板跳转url
  48. TemplateIdByCompanyApply = "eTalI1LbiT_B0mTaBeDTlfSMITenK8dQIgEB5yqjjvA"
  49. TemplateCompanyApplyRedirectUrl = "http://advisoryadmin.brilliantstart.cn/approval/approval/list"
  50. //销售跨部门领取客户通知
  51. TemplateIdByCompanyReceive = "KAxFvX79f-DHmtC_Jw98elvqxiDPK0xP_b48LUT-Y14"
  52. WxMsgTemplateIdActivityChangeApply = "CB7bOl7f3viMG4s1uhRo7WM0Jbx3WvodKuIZ8A_z8fM"
  53. WxMsgTemplateIdAskByUser = `qfNuops-sKrfIkbA7U97A7gSrX03mUpoEpJksRUdloo`
  54. PcWxAppId = "wxcba9a7ec590ee2d5"
  55. PcWxAppSecret = "aa58d257e2521d768cbf1bf89989769d"
  56. } else {
  57. WxAppId = "wx4a844c734d8c8e56"
  58. WxAppSecret = "26c586e7ccb3c575433f0f37797b3eeb"
  59. WxId = "gh_b67e0049fb8c"
  60. TemplateIdByProduct = "Cp2wF8gvBtxyWV4DeYuI172oqwyYXVRSm3AyJO42d84"
  61. TemplateRedirectUrl = "https://ficc.hzinsights.com/reportdtl?id="
  62. DayReportTemplateRedirectUrl = "https://report.hzinsights.com/#/allindex/" //晨报模板跳转url
  63. TemplateIdByCompanyApply = "ZKcOfNIWBpwHJxpptufHIK1mp2nIwkT3cxub-35cFqI"
  64. TemplateCompanyApplyRedirectUrl = "https://ficc.hzinsights.com/approval/approval/list"
  65. WxMsgTemplateIdActivityChangeApply = "dYg6iHooRq74PyCXmw_Ns7qdJZmbtLoKS2p2FKeaXl0"
  66. //销售跨部门领取客户通知
  67. TemplateIdByCompanyReceive = "A5fV-XWBcu-LIj_W-tBiOJ-D39a9WDd9GOB0WGbpoBg"
  68. TemplateCompanyApplyRedirectUrl = "https://ficc.hzinsights.com/approval/approval/list"
  69. PcWxAppId = "wx4da95782cfc8c5eb"
  70. PcWxAppSecret = "8f82ebf2ba3aa06ce44541726385df64"
  71. }
  72. }
  73. func GetWxChat() (officialAccount *officialaccount.OfficialAccount) {
  74. initConf() //初始化参数
  75. wc := wechat.NewWechat()
  76. memory := cache.NewMemory()
  77. conf := &config.Config{
  78. AppID: WxAppId,
  79. AppSecret: WxAppSecret,
  80. Token: "",
  81. EncodingAESKey: "",
  82. Cache: memory,
  83. }
  84. officialAccount = wc.GetOfficialAccount(conf)
  85. wechatAccessToken := &WechatAccessToken{}
  86. officialAccount.SetAccessTokenHandle(wechatAccessToken)
  87. return
  88. }
  89. // GetUserInfo 获取微信用户详情
  90. func GetUserInfo(openid string) (userInfo *user.Info, err error) {
  91. wechatClient := GetWxChat()
  92. userClient := wechatClient.GetUser()
  93. userInfo, err = userClient.GetUserInfo(openid)
  94. return
  95. }
  96. // GetJsConfig 获取公众号jsConfig
  97. func GetJsConfig(signUrl string) (jsConf *js.Config, err error) {
  98. wechatClient := GetWxChat()
  99. j := wechatClient.GetJs()
  100. jsConf, err = j.GetConfig(signUrl)
  101. return
  102. }
  103. type WechatAccessToken struct {
  104. }
  105. // GetAccessToken 获取accessToken
  106. func (wechat WechatAccessToken) GetAccessToken() (accessToken string, err error) {
  107. wxToken, err := wx_token.GetById()
  108. if err != nil {
  109. return
  110. }
  111. //如果300s就要过期了,那么就去刷新accessToken
  112. if wxToken.ExpiresIn < time.Now().Unix()+300 {
  113. tmpAccessToken, expires, tmpErr := getTokenFromServer(WxAppId, WxAppSecret)
  114. if tmpErr != nil {
  115. err = tmpErr
  116. return
  117. }
  118. var updateCols = []string{"AccessToken", "ExpiresIn"}
  119. wxToken.AccessToken = tmpAccessToken
  120. wxToken.ExpiresIn = expires - 600 //快过期前10分钟就刷新掉
  121. wxToken.Update(updateCols)
  122. }
  123. accessToken = wxToken.AccessToken
  124. return
  125. }
  126. // getTokenFromServer 服务端获取accessToken
  127. func getTokenFromServer(appid, wxSecret string) (accessToken string, expires int64, err error) {
  128. apiUrl := "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s"
  129. resAccessToken, err := credential.GetTokenFromServer(fmt.Sprintf(apiUrl, appid, wxSecret))
  130. if err != nil {
  131. return
  132. }
  133. expires = resAccessToken.ExpiresIn
  134. accessToken = resAccessToken.AccessToken
  135. return
  136. }
  137. func PcWxGetUserOpenIdByCode(code string) (item *pc.WxAccessToken, err error) {
  138. if code == "" {
  139. err = errors.New("code is empty")
  140. return nil, err
  141. }
  142. initConf() //初始化参数
  143. requestUrl := `https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code`
  144. requestUrl = fmt.Sprintf(requestUrl, PcWxAppId, PcWxAppSecret, code)
  145. result, err := http.Get(requestUrl)
  146. if err != nil {
  147. return nil, err
  148. }
  149. err = json.Unmarshal(result, &item)
  150. return
  151. }
  152. func PcWxGetUserInfo(openId, accessToken string) (item *pc.WxUserInfo, err error) {
  153. requestUrl := `https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s`
  154. requestUrl = fmt.Sprintf(requestUrl, accessToken, openId)
  155. result, err := http.Get(requestUrl)
  156. if err != nil {
  157. return
  158. }
  159. err = json.Unmarshal(result, &item)
  160. return
  161. }