wechat.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package wechat
  2. import (
  3. "fmt"
  4. "github.com/silenceper/wechat/v2"
  5. "github.com/silenceper/wechat/v2/cache"
  6. "github.com/silenceper/wechat/v2/credential"
  7. "github.com/silenceper/wechat/v2/officialaccount"
  8. "github.com/silenceper/wechat/v2/officialaccount/config"
  9. "github.com/silenceper/wechat/v2/officialaccount/js"
  10. "github.com/silenceper/wechat/v2/officialaccount/user"
  11. "hongze/hongze_yb/global"
  12. "hongze/hongze_yb/models/tables/wx_token"
  13. "time"
  14. )
  15. var (
  16. WxId string //微信原始ID
  17. WxAppId string
  18. WxAppSecret string
  19. TemplateIdByProduct string //产品运行报告通知-模板ID
  20. TemplateRedirectUrl string //模板消息跳转地址
  21. DayReportTemplateRedirectUrl string //模板消息跳转地址(晨报、周报)
  22. TemplateIdByCompanyApply string //客户申请单审批通知-模板ID
  23. TemplateCompanyApplyRedirectUrl string //审批单模板消息跳转地址
  24. TemplateIdByCompanyReceive string //销售跨部门领取客户通知-模板ID
  25. WxMsgTemplateIdActivityChangeApply string //查研观向活动变更通知-模板ID
  26. WxMsgTemplateIdActivityChangeApplyXzs string //查研观向活动变更通知-模板ID(小助手)
  27. TemplateIdByProductXzs string //产品运行报告通知-模板ID(小助手)
  28. WxCygxAppId string //查研观向小程序APPID
  29. WxMsgTemplateIdAskByUser string //查研观向用户提问新消息推送ID
  30. WxCrmAppId string //随手办公小程序APPID
  31. WxPublicIdXzs string //查研观向小助手公众号
  32. WxPublicSecretXzs string //查研观向小助手公众号
  33. )
  34. func initConf() {
  35. if global.CONFIG.Serve.RunMode == "debug" {
  36. WxAppId = "wx9b5d7291e581233a"
  37. WxAppSecret = "f4d52e34021eee262dce9682b31f8861"
  38. WxId = "gh_5dc508325c6f"
  39. TemplateIdByProduct = "-YjuPOB7Fqd-S3ilabYa6wvjDY9aXmeEfPN6DCiy-EY"
  40. TemplateRedirectUrl = "http://rddpweb.brilliantstart.cn/reportdtl?id="
  41. DayReportTemplateRedirectUrl = "http://report.brilliantstart.cn/#/allindex/" //晨报模板跳转url
  42. TemplateIdByCompanyApply = "eTalI1LbiT_B0mTaBeDTlfSMITenK8dQIgEB5yqjjvA"
  43. TemplateCompanyApplyRedirectUrl = "http://advisoryadmin.brilliantstart.cn/approval/approval/list"
  44. //销售跨部门领取客户通知
  45. TemplateIdByCompanyReceive = "KAxFvX79f-DHmtC_Jw98elvqxiDPK0xP_b48LUT-Y14"
  46. WxMsgTemplateIdActivityChangeApply = "CB7bOl7f3viMG4s1uhRo7WM0Jbx3WvodKuIZ8A_z8fM"
  47. WxMsgTemplateIdAskByUser = `qfNuops-sKrfIkbA7U97A7gSrX03mUpoEpJksRUdloo`
  48. } else {
  49. WxAppId = "wx4a844c734d8c8e56"
  50. WxAppSecret = "26c586e7ccb3c575433f0f37797b3eeb"
  51. WxId = "gh_b67e0049fb8c"
  52. TemplateIdByProduct = "Cp2wF8gvBtxyWV4DeYuI172oqwyYXVRSm3AyJO42d84"
  53. TemplateRedirectUrl = "https://ficc.hzinsights.com/reportdtl?id="
  54. DayReportTemplateRedirectUrl = "https://report.hzinsights.com/#/allindex/" //晨报模板跳转url
  55. TemplateIdByCompanyApply = "ZKcOfNIWBpwHJxpptufHIK1mp2nIwkT3cxub-35cFqI"
  56. TemplateCompanyApplyRedirectUrl = "https://ficc.hzinsights.com/approval/approval/list"
  57. WxMsgTemplateIdActivityChangeApply = "dYg6iHooRq74PyCXmw_Ns7qdJZmbtLoKS2p2FKeaXl0"
  58. //销售跨部门领取客户通知
  59. TemplateIdByCompanyReceive = "A5fV-XWBcu-LIj_W-tBiOJ-D39a9WDd9GOB0WGbpoBg"
  60. TemplateCompanyApplyRedirectUrl = "https://ficc.hzinsights.com/approval/approval/list"
  61. }
  62. }
  63. func GetWxChat() (officialAccount *officialaccount.OfficialAccount) {
  64. initConf() //初始化参数
  65. wc := wechat.NewWechat()
  66. memory := cache.NewMemory()
  67. conf := &config.Config{
  68. AppID: WxAppId,
  69. AppSecret: WxAppSecret,
  70. Token: "",
  71. EncodingAESKey: "",
  72. Cache: memory,
  73. }
  74. officialAccount = wc.GetOfficialAccount(conf)
  75. wechatAccessToken := &WechatAccessToken{}
  76. officialAccount.SetAccessTokenHandle(wechatAccessToken)
  77. return
  78. }
  79. // GetUserInfo 获取微信用户详情
  80. func GetUserInfo(openid string) (userInfo *user.Info, err error) {
  81. wechatClient := GetWxChat()
  82. userClient := wechatClient.GetUser()
  83. userInfo, err = userClient.GetUserInfo(openid)
  84. return
  85. }
  86. // GetJsConfig 获取公众号jsConfig
  87. func GetJsConfig(signUrl string) (jsConf *js.Config, err error) {
  88. wechatClient := GetWxChat()
  89. j := wechatClient.GetJs()
  90. jsConf, err = j.GetConfig(signUrl)
  91. return
  92. }
  93. type WechatAccessToken struct {
  94. }
  95. // GetAccessToken 获取accessToken
  96. func (wechat WechatAccessToken) GetAccessToken() (accessToken string, err error) {
  97. wxToken, err := wx_token.GetById()
  98. if err != nil {
  99. return
  100. }
  101. //如果300s就要过期了,那么就去刷新accessToken
  102. if wxToken.ExpiresIn < time.Now().Unix()+300 {
  103. tmpAccessToken, expires, tmpErr := getTokenFromServer(WxAppId, WxAppSecret)
  104. if tmpErr != nil {
  105. err = tmpErr
  106. return
  107. }
  108. var updateCols = []string{"AccessToken", "ExpiresIn"}
  109. wxToken.AccessToken = tmpAccessToken
  110. wxToken.ExpiresIn = expires - 600 //快过期前10分钟就刷新掉
  111. wxToken.Update(updateCols)
  112. }
  113. accessToken = wxToken.AccessToken
  114. return
  115. }
  116. // getTokenFromServer 服务端获取accessToken
  117. func getTokenFromServer(appid, wxSecret string) (accessToken string, expires int64, err error) {
  118. apiUrl := "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s"
  119. resAccessToken, err := credential.GetTokenFromServer(fmt.Sprintf(apiUrl, appid, wxSecret))
  120. if err != nil {
  121. return
  122. }
  123. expires = resAccessToken.ExpiresIn
  124. accessToken = resAccessToken.AccessToken
  125. return
  126. }