wechatHelper.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package wechat
  2. import (
  3. "context"
  4. "dsbqj-admin/pkg/cache"
  5. "dsbqj-admin/pkg/logger"
  6. "dsbqj-admin/pkg/util"
  7. "github.com/goccy/go-json"
  8. "github.com/silenceper/wechat"
  9. "os"
  10. "sync/atomic"
  11. wechatCache "dsbqj-admin/pkg/cache/wechat"
  12. )
  13. var GOpenid OpenidMgr
  14. type OpenidMgr struct {
  15. V atomic.Value //值
  16. Over int64 //过期时间
  17. }
  18. type WechatHelper struct {
  19. ctx context.Context
  20. Wechat *wechat.Wechat
  21. AppId string
  22. AppSecret string
  23. }
  24. const (
  25. subscribeUrl = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="
  26. )
  27. func NewWechatHelper() *WechatHelper {
  28. appID := os.Getenv("WECHAT_APPID")
  29. appSecret := os.Getenv("WECHAT_SECRET")
  30. wx := wechat.NewWechat(&wechat.Config{
  31. AppID: appID,
  32. AppSecret: appSecret,
  33. Cache: wechatCache.NewAccessToken(cache.RedisReport),
  34. })
  35. return &WechatHelper{
  36. ctx: context.Background(),
  37. AppId: appID,
  38. AppSecret: appSecret,
  39. Wechat: wx,
  40. }
  41. }
  42. type errmsg struct {
  43. Errcode int `json:"errcode"` //错误码
  44. Errmsg string `json:"errmsg"` //错误信息
  45. Result struct {
  46. Suggest string `json:"suggest"` //建议,有risky、pass、review三种值
  47. Label int `json:"label"` //命中标签枚举值,100 正常;10001 广告;20001 时政;20002 色情;20003 辱骂;20006 违法犯罪;20008 欺诈;20012 低俗;20013 版权;21000 其他
  48. } `json:"result"`
  49. }
  50. type Subscribe struct {
  51. ToUser string `json:"touser"`
  52. TemplateId string `json:"template_id"`
  53. MiniProgramState string `json:"miniprogram_state"`
  54. Lang string `json:"lang"`
  55. Data interface{} `json:"data"`
  56. }
  57. func (this *WechatHelper) SendWechatSubscribe(openid string, template string, msg interface{}) int {
  58. token, _ := this.Wechat.GetAccessToken()
  59. url := subscribeUrl + token
  60. reqData := Subscribe{ToUser: openid, TemplateId: template, MiniProgramState: "developer", Lang: "zh_CN"}
  61. reqData.Data = msg
  62. data, _ := json.Marshal(reqData)
  63. buf, err := util.HTTPPost(url, string(data))
  64. if err != nil {
  65. logger.Info("[ERROR] CheckWechatMsg post err!err:", err.Error())
  66. return 101
  67. }
  68. var re = new(errmsg)
  69. err = json.Unmarshal(buf, re)
  70. if err != nil {
  71. logger.Info("[ERROR] CheckWechatMsg Unmarshal err!err:", err)
  72. return 103
  73. }
  74. if re.Errcode != 0 {
  75. if re.Errcode == 40003 || re.Errcode == 43101 { //openid无效,appid与openid不匹配, 用户未订阅消息(用户未在近两小时访问小程序)
  76. logger.Info("CheckWechatMsg msg: %s ret: %d errmsg: %s", msg, re.Errcode, re.Errmsg)
  77. return 0
  78. }
  79. } else {
  80. if re.Result.Label != 100 && re.Result.Label != 0 {
  81. logger.Info("[ERROR]CheckWechatMsg msg:", msg, " ret:", re.Result.Label, " ,suggest:", re.Result.Suggest)
  82. return re.Result.Label
  83. }
  84. }
  85. return re.Errcode
  86. }