wechat.go 5.1 KB

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