wechat_send_msg_cygx.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. package services
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "eta/eta_pub/models"
  7. "eta/eta_pub/services/alarm_msg"
  8. "eta/eta_pub/utils"
  9. "fmt"
  10. "io/ioutil"
  11. "net/http"
  12. "strings"
  13. "time"
  14. )
  15. func SendWxTemplateMsgCygx(sendInfo *models.SendWxTemplate) (err error) {
  16. var msg string
  17. defer func() {
  18. if err != nil {
  19. go alarm_msg.SendAlarmMsg("查研观向小助手发送模版消息失败,Err:"+err.Error()+";msg:"+msg, 3)
  20. utils.FileLog.Info(fmt.Sprintf("发送模版消息失败,Err:%s,%s", err.Error(), msg))
  21. }
  22. if msg != "" {
  23. utils.FileLog.Info(fmt.Sprintf("发送模版消息失败,msg:%s", msg))
  24. }
  25. }()
  26. utils.FileLog.Info("services SendMsg")
  27. fmt.Println("send start")
  28. utils.FileLog.Info("send start")
  29. sendMap := make(map[string]interface{})
  30. sendData := make(map[string]interface{})
  31. var uniqueCodeStr string
  32. if sendInfo.First != "" {
  33. sendData["first"] = map[string]interface{}{"value": sendInfo.First, "color": "#173177"}
  34. uniqueCodeStr += sendInfo.First
  35. }
  36. if sendInfo.Keyword1 != "" {
  37. sendData["keyword1"] = map[string]interface{}{"value": sendInfo.Keyword1, "color": "#173177"}
  38. uniqueCodeStr += sendInfo.Keyword1
  39. }
  40. if sendInfo.Keyword2 != "" {
  41. sendData["keyword2"] = map[string]interface{}{"value": sendInfo.Keyword2, "color": "#173177"}
  42. uniqueCodeStr += sendInfo.Keyword2
  43. }
  44. if sendInfo.Keyword3 != "" {
  45. sendData["keyword3"] = map[string]interface{}{"value": sendInfo.Keyword3, "color": "#173177"}
  46. uniqueCodeStr += sendInfo.Keyword3
  47. }
  48. if sendInfo.Keyword4 != "" {
  49. sendData["keyword4"] = map[string]interface{}{"value": sendInfo.Keyword4, "color": "#173177"}
  50. uniqueCodeStr += sendInfo.Keyword4
  51. }
  52. if sendInfo.Remark != "" {
  53. sendData["remark"] = map[string]interface{}{"value": sendInfo.Remark, "color": "#173177"}
  54. uniqueCodeStr += sendInfo.Remark
  55. }
  56. if sendInfo.TemplateId != "" {
  57. sendMap["template_id"] = sendInfo.TemplateId
  58. uniqueCodeStr += sendInfo.TemplateId
  59. }
  60. if sendInfo.RedirectUrl != "" {
  61. if strings.Contains(sendInfo.RedirectUrl, "http") || strings.Contains(sendInfo.RedirectUrl, "https") || sendInfo.RedirectTarget == 0 {
  62. sendMap["url"] = sendInfo.RedirectUrl
  63. } else {
  64. sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxCygxAppId, "pagepath": sendInfo.RedirectUrl}
  65. }
  66. uniqueCodeStr += sendInfo.RedirectUrl
  67. }
  68. sendMap["data"] = sendData
  69. uniqueCode := utils.MD5(uniqueCodeStr)
  70. err = sendTemplateMsgCygx(sendMap, sendInfo.OpenIdArr, sendInfo.Resource, uniqueCode, sendInfo.SendType)
  71. if err != nil {
  72. utils.FileLog.Info("send err:" + err.Error())
  73. }
  74. fmt.Println("send end")
  75. utils.FileLog.Info("send end")
  76. return
  77. }
  78. // sendTemplateMsg 整理openid以及以往历史推送记录,移除已经推送的记录,并开始依次推送
  79. func sendTemplateMsgCygx(sendMap map[string]interface{}, openIdArr []string, resource, uniqueCode string, sendType int) (err error) {
  80. for _, openId := range openIdArr {
  81. sendMap["touser"] = openId
  82. data, err := json.Marshal(sendMap)
  83. if err != nil {
  84. fmt.Println("SendTemplateMsgOne Marshal Err:", err.Error())
  85. utils.FileLog.Info(fmt.Sprintf("SendTemplateMsgOne Marshal Err:%s", err.Error()))
  86. err = errors.New("SendTemplateMsgOne Marshal Err:" + err.Error())
  87. return err
  88. }
  89. err = toSendTemplateMsgCygx(data, resource, sendType, openId, uniqueCode)
  90. if err != nil {
  91. err = errors.New("toSendTemplateMsgCygx Err:" + err.Error())
  92. fmt.Println("send err:", err.Error())
  93. utils.FileLog.Info(fmt.Sprintf("ToSendTemplateMsg Err:%s", err.Error()))
  94. }
  95. }
  96. return
  97. }
  98. // toSendTemplateMsg 实际推送微信
  99. func toSendTemplateMsgCygx(data []byte, resource string, sendType int, openId, uniqueCode string) (err error) {
  100. utils.FileLog.Info("Send:" + string(data))
  101. //获取accessToken
  102. accessToken, err, errMsg := getWxAccessTokenCygx()
  103. if err != nil {
  104. err = errors.New("getWxAccessTokenCygx Err:" + err.Error())
  105. utils.FileLog.Info(fmt.Sprintf("获取Token失败,err:%s,errMsg:%s", err.Error(), errMsg))
  106. return
  107. }
  108. sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  109. client := http.Client{}
  110. resp, err := client.Post(sendUrl, "application/json", bytes.NewBuffer(data))
  111. if err != nil {
  112. err = errors.New("Post Err:" + err.Error())
  113. return
  114. }
  115. defer resp.Body.Close()
  116. body, _ := ioutil.ReadAll(resp.Body)
  117. utils.FileLog.Info("Cygx_SendResult:" + string(body))
  118. var templateResponse SendTemplateResponse
  119. err = json.Unmarshal(body, &templateResponse)
  120. if err != nil {
  121. err = errors.New("SendResult Unmarshal Err:" + err.Error())
  122. utils.FileLog.Info(fmt.Sprintf("SendResult Unmarshal Err:%s", err.Error()))
  123. return err
  124. }
  125. //新增模板消息推送记录
  126. {
  127. tr := new(models.UserTemplateRecord)
  128. tr.OpenId = openId
  129. tr.Resource = resource
  130. tr.SendData = string(data)
  131. tr.Result = string(body)
  132. tr.CreateDate = time.Now().Format(utils.FormatDate)
  133. tr.CreateTime = time.Now().Format(utils.FormatDateTime)
  134. if templateResponse.Errcode == 0 {
  135. tr.SendStatus = 1
  136. } else {
  137. tr.SendStatus = 0
  138. }
  139. tr.UniqueCode = uniqueCode
  140. tr.SendType = sendType
  141. go func() {
  142. err = models.AddUserTemplateRecord(tr)
  143. if err != nil {
  144. err = errors.New("AddUserTemplateRecord Err:" + err.Error())
  145. utils.FileLog.Info(fmt.Sprintf("AddUserTemplateRecord Err:%s", err.Error()))
  146. }
  147. }()
  148. }
  149. //accessToken过期
  150. if templateResponse.Errcode == 40001 {
  151. //强刷token并重新推送
  152. accessToken, err, errMsg = refreshWxAccessTokenCygx()
  153. if err != nil {
  154. err = errors.New("refreshWxAccessToken Err:" + err.Error())
  155. utils.FileLog.Info(fmt.Sprintf("refreshWxAccessToken Err:%s", err.Error()))
  156. return err
  157. }
  158. return toSendTemplateMsgCygx(data, resource, sendType, openId, uniqueCode)
  159. }
  160. //模板消息发送超过当日10万次限制错误处理
  161. if templateResponse.Errcode == 45009 {
  162. key := "CACHE_SendTemplateMsg_ERR"
  163. isExist := utils.Rc.IsExist(key)
  164. if isExist == true {
  165. return
  166. } else {
  167. result, _ := json.Marshal(templateResponse)
  168. if err != nil {
  169. utils.FileLog.Info(fmt.Sprintf("templateResponse Marshal Err:%s", err.Error()))
  170. return err
  171. }
  172. //发送邮件提醒异常
  173. go alarm_msg.SendAlarmMsg("模板消息发送超过当日10万次限制,templateResponse = "+string(result), 3)
  174. //go utils.SendEmail("异常提醒:", "模板消息发送超过当日10万次限制,templateResponse = "+string(result), utils.EmailSendToUsers)
  175. //设置3分钟缓存,不允许重复添加
  176. utils.Rc.SetNX(key, 1, 6*time.Minute)
  177. }
  178. //清空发送次数
  179. sendUrl := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/clear_quota?access_token=%s", accessToken)
  180. clearData := make(map[string]interface{})
  181. clearData["appid"] = utils.WxAppIdCygx
  182. clearJson, _ := json.Marshal(clearData)
  183. utils.FileLog.Info("clear_quota data:" + string(clearJson))
  184. resp, err := client.Post(sendUrl, "application/json", bytes.NewBuffer(clearJson))
  185. if err != nil {
  186. return err
  187. }
  188. defer resp.Body.Close()
  189. clearBody, err := ioutil.ReadAll(resp.Body)
  190. utils.FileLog.Info("clear_quota result:" + string(clearBody))
  191. var clearQuotaResponse ClearQuotaResponse
  192. err = json.Unmarshal(clearBody, &clearQuotaResponse)
  193. if err != nil {
  194. utils.FileLog.Info(fmt.Sprintf("clearQuotaResponse Unmarshal Err:%s", err.Error()))
  195. return err
  196. }
  197. //if clearQuotaResponse.Errcode == 0 {
  198. // //发送邮件解决异常
  199. // go alarm_msg.SendAlarmMsg("异常已解决,自动清理限制接口,调用成功", 3)
  200. // //go utils.SendEmail("异常已解决:", "自动清理限制接口,调用成功", utils.EmailSendToUsers)
  201. // //重新推送一次
  202. // toSendTemplateMsgCygx(data, resource, sendType, openId, uniqueCode)
  203. //}
  204. }
  205. return
  206. }
  207. // getWxAccessTokenCygx 获取微信token
  208. func getWxAccessTokenCygx() (accessToken string, err error, errMsg string) {
  209. var tokenKey string
  210. if utils.RunMode == "debug" {
  211. tokenKey = utils.CACHE_WX_ACCESS_TOKEN_HZ
  212. } else {
  213. tokenKey = utils.CACHE_WX_ACCESS_TOKEN_CYGX
  214. }
  215. accessToken, err = utils.Rc.RedisString(tokenKey)
  216. if accessToken != "" {
  217. return
  218. }
  219. //缓存中没有取到数据,那么需要去强制刷新新的accessToken
  220. return refreshWxAccessTokenCygx()
  221. }
  222. // refreshWxAccessTokenCygx 强制刷新微信token
  223. func refreshWxAccessTokenCygx() (accessToken string, err error, errMsg string) {
  224. fmt.Println("强制刷新" + utils.WxAppIdCygx + "微信token")
  225. if errMsg != `` {
  226. utils.FileLog.Info(fmt.Sprintf("强制刷新%s微信token异常:%s", utils.WxAppIdCygx, errMsg))
  227. }
  228. //调用微信官方接口获取新的accessToken
  229. wxAccessToken, tmpErr := models.GetWxTokenCygx()
  230. if tmpErr != nil {
  231. err = tmpErr
  232. errMsg = "通过微信接口获取accessToken失败 Err:" + err.Error()
  233. return
  234. }
  235. //如果没有token数据
  236. if wxAccessToken.AccessToken == "" {
  237. errMsg = "微信返回的accessToken异常: Err:" + wxAccessToken.Errmsg
  238. err = errors.New(errMsg)
  239. return
  240. }
  241. accessToken = wxAccessToken.AccessToken
  242. //更新mysql的accessToken(如果是debug环境的话)
  243. var tokenKey string
  244. if utils.RunMode == "debug" {
  245. expiresIn := time.Now().Add(time.Duration(wxAccessToken.ExpiresIn) * time.Second).Unix()
  246. err = models.ModifyAccessToken(wxAccessToken.AccessToken, expiresIn)
  247. if err != nil {
  248. errMsg = "更新mysql中的accessToken失败 Err:" + err.Error()
  249. return
  250. }
  251. tokenKey = utils.CACHE_WX_ACCESS_TOKEN_HZ
  252. } else {
  253. tokenKey = utils.CACHE_WX_ACCESS_TOKEN_CYGX
  254. }
  255. //更新redis的accessToken(过期时间提前十分钟)
  256. redisTimeExpire := time.Duration(wxAccessToken.ExpiresIn-600) * time.Second
  257. err = utils.Rc.Put(tokenKey, accessToken, redisTimeExpire)
  258. if err != nil {
  259. errMsg = "更新redis中的accessToken失败 Err:" + err.Error()
  260. return
  261. }
  262. return
  263. }