123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- package services
- import (
- "bytes"
- "encoding/json"
- "errors"
- "eta/eta_pub/models"
- "eta/eta_pub/services/alarm_msg"
- "eta/eta_pub/utils"
- "fmt"
- "io/ioutil"
- "net/http"
- "strconv"
- "strings"
- "time"
- )
- type SendTemplateResponse struct {
- Errcode int `json:"errcode"`
- Errmsg string `json:"errmsg"`
- MsgID int `json:"msgid"`
- }
- type ClearQuotaResponse struct {
- Errcode int `json:"errcode"`
- Errmsg string `json:"errmsg"`
- }
- func SendWxTemplateMsg(sendInfo *models.SendWxTemplate) (err error) {
- var msg string
- defer func() {
- if err != nil {
- go alarm_msg.SendAlarmMsg("发送模版消息失败,Err:"+err.Error()+";msg:"+msg, 3)
- utils.FileLog.Info(fmt.Sprintf("发送模版消息失败,Err:%s,%s", err.Error(), msg))
- }
- if msg != "" {
- utils.FileLog.Info(fmt.Sprintf("发送模版消息失败,msg:%s", msg))
- }
- }()
- utils.FileLog.Info("services SendMsg")
- fmt.Println("send start")
- utils.FileLog.Info("send start")
- sendMap := make(map[string]interface{})
- sendData := make(map[string]interface{})
- var uniqueCodeStr string
- if sendInfo.WxAppId != "" {
- uniqueCodeStr += sendInfo.WxAppId
- }
- if sendInfo.First != "" {
- sendData["first"] = map[string]interface{}{"value": sendInfo.First, "color": "#173177"}
- uniqueCodeStr += sendInfo.First
- }
- if sendInfo.Keyword1 != "" {
- sendData["keyword1"] = map[string]interface{}{"value": sendInfo.Keyword1, "color": "#173177"}
- uniqueCodeStr += sendInfo.Keyword1
- }
- if sendInfo.Keyword2 != "" {
- sendData["keyword2"] = map[string]interface{}{"value": sendInfo.Keyword2, "color": "#173177"}
- uniqueCodeStr += sendInfo.Keyword2
- }
- if sendInfo.Keyword3 != "" {
- sendData["keyword3"] = map[string]interface{}{"value": sendInfo.Keyword3, "color": "#173177"}
- uniqueCodeStr += sendInfo.Keyword3
- }
- if sendInfo.Keyword4 != "" {
- sendData["keyword4"] = map[string]interface{}{"value": sendInfo.Keyword4, "color": "#173177"}
- uniqueCodeStr += sendInfo.Keyword4
- }
- if sendInfo.Keyword5 != "" {
- sendData["keyword5"] = map[string]interface{}{"value": sendInfo.Keyword5, "color": "#173177"}
- uniqueCodeStr += sendInfo.Keyword5
- }
- if sendInfo.Productname != "" {
- sendData["productname"] = map[string]interface{}{"value": sendInfo.Productname, "color": "#173177"}
- uniqueCodeStr += sendInfo.Productname
- }
- if sendInfo.Date != "" {
- sendData["date"] = map[string]interface{}{"value": sendInfo.Date, "color": "#173177"}
- uniqueCodeStr += sendInfo.Date
- }
- if sendInfo.Remark != "" {
- sendData["remark"] = map[string]interface{}{"value": sendInfo.Remark, "color": "#173177"}
- uniqueCodeStr += sendInfo.Remark
- }
- if sendInfo.TemplateId != "" {
- sendMap["template_id"] = sendInfo.TemplateId
- uniqueCodeStr += sendInfo.TemplateId
- }
- if sendInfo.RedirectUrl != "" {
- if strings.Contains(sendInfo.RedirectUrl, "http") || strings.Contains(sendInfo.RedirectUrl, "https") || sendInfo.RedirectTarget == 0 {
- sendMap["url"] = sendInfo.RedirectUrl
- } else {
- var xcxAppId string
- switch sendInfo.RedirectTarget {
- case 1:
- xcxAppId = utils.WxYbAppId
- case 2:
- xcxAppId = utils.WxCrmAppId
- case 3:
- xcxAppId = utils.WxCygxAppId
- case 4:
- xcxAppId = utils.WxCopyYbAppId
- case 5:
- xcxAppId = utils.WxHtAppId
- default:
- err = errors.New("无效的微信小程序跳转方式:RedirectTarget" + strconv.Itoa(sendInfo.RedirectTarget))
- return err
- }
- sendMap["miniprogram"] = map[string]interface{}{"appid": xcxAppId, "pagepath": sendInfo.RedirectUrl}
- }
- uniqueCodeStr += sendInfo.RedirectUrl
- }
- sendMap["data"] = sendData
- uniqueCode := utils.MD5(uniqueCodeStr)
- err = sendTemplateMsg(sendInfo.WxAppId, sendMap, sendInfo.OpenIdArr, sendInfo.Resource, uniqueCode, sendInfo.SendType)
- if err != nil {
- utils.FileLog.Info("send err:" + err.Error())
- }
- fmt.Println("send end")
- utils.FileLog.Info("send end")
- return
- }
- // sendTemplateMsg 整理openid以及以往历史推送记录,移除已经推送的记录,并开始依次推送
- func sendTemplateMsg(wxAppId string, sendMap map[string]interface{}, openIdArr []string, resource, uniqueCode string, sendType int) (err error) {
- existList, err := models.GetTemplateRecordByUniqueCode(uniqueCode)
- if err != nil && err.Error() != utils.ErrNoRow() {
- utils.FileLog.Info(fmt.Sprintf("GetTemplateRecordByUniqueCode Err:%s", err.Error()))
- return err
- }
- if len(existList) == 0 {
- for _, openId := range openIdArr {
- sendMap["touser"] = openId
- data, err := json.Marshal(sendMap)
- if err != nil {
- fmt.Println("SendTemplateMsgOne Marshal Err:", err.Error())
- utils.FileLog.Info(fmt.Sprintf("SendTemplateMsgOne Marshal Err:%s", err.Error()))
- return err
- }
- err = toSendTemplateMsg(wxAppId, data, resource, sendType, openId, uniqueCode)
- if err != nil {
- fmt.Println("send err:", err.Error())
- utils.FileLog.Info(fmt.Sprintf("ToSendTemplateMsg Err:%s", err.Error()))
- }
- }
- } else {
- existOpenIdMap := make(map[string]string)
- for _, v := range existList {
- existOpenIdMap[v.OpenId] = v.OpenId
- }
- for _, openId := range openIdArr {
- if _, ok := existOpenIdMap[openId]; !ok {
- sendMap["touser"] = openId
- data, err := json.Marshal(sendMap)
- if err != nil {
- fmt.Println("SendTemplateMsgOne Marshal Err:", err.Error())
- utils.FileLog.Info(fmt.Sprintf("SendTemplateMsgOne Marshal Err:%s", err.Error()))
- return err
- }
- err = toSendTemplateMsg(wxAppId, data, resource, sendType, openId, uniqueCode)
- if err != nil {
- fmt.Println("send err:", err.Error())
- utils.FileLog.Info(fmt.Sprintf("ToSendTemplateMsg Err:%s", err.Error()))
- }
- }
- }
- }
- return
- }
- // toSendTemplateMsg 实际推送微信
- func toSendTemplateMsg(wxAppId string, data []byte, resource string, sendType int, openId, uniqueCode string) (err error) {
- utils.FileLog.Info("Send:" + string(data))
- //获取accessToken
- var accessToken string
- var errMsg string
- accessToken, err, errMsg = getWxAccessToken(wxAppId, "")
- if err != nil {
- utils.FileLog.Info(fmt.Sprintf("获取Token失败,err:%s,errMsg:%s", err.Error(), errMsg))
- return
- }
- sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
- client := http.Client{}
- resp, err := client.Post(sendUrl, "application/json", bytes.NewBuffer(data))
- if err != nil {
- return
- }
- defer resp.Body.Close()
- body, _ := ioutil.ReadAll(resp.Body)
- utils.FileLog.Info("SendResult:" + string(body))
- var templateResponse SendTemplateResponse
- err = json.Unmarshal(body, &templateResponse)
- if err != nil {
- utils.FileLog.Info(fmt.Sprintf("SendResult Unmarshal Err:%s", err.Error()))
- return err
- }
- //新增模板消息推送记录
- {
- tr := new(models.UserTemplateRecord)
- tr.OpenId = openId
- tr.Resource = resource
- tr.SendData = string(data)
- tr.Result = string(body)
- tr.CreateDate = time.Now().Format(utils.FormatDate)
- tr.CreateTime = time.Now().Format(utils.FormatDateTime)
- if templateResponse.Errcode == 0 {
- tr.SendStatus = 1
- } else {
- tr.SendStatus = 0
- }
- tr.UniqueCode = uniqueCode
- tr.SendType = sendType
- tr.WxAppId = wxAppId
- go func() {
- err = models.AddUserTemplateRecord(tr)
- if err != nil {
- utils.FileLog.Info(fmt.Sprintf("AddUserTemplateRecord Err:%s", err.Error()))
- }
- }()
- }
- //accessToken过期
- if templateResponse.Errcode == 40001 {
- //强刷token并重新推送
- accessToken, err, errMsg = refreshWxAccessToken(wxAppId, "")
- if err != nil {
- utils.FileLog.Info(fmt.Sprintf("refreshWxAccessToken Err:%s", err.Error()))
- return err
- }
- return toSendTemplateMsg(wxAppId, data, resource, sendType, openId, uniqueCode)
- }
- //模板消息发送超过当日10万次限制错误处理
- if templateResponse.Errcode == 45009 {
- key := "CACHE_SendTemplateMsg_ERR"
- isExist := utils.Rc.IsExist(key)
- if isExist == true {
- return
- } else {
- result, _ := json.Marshal(templateResponse)
- if err != nil {
- utils.FileLog.Info(fmt.Sprintf("templateResponse Marshal Err:%s", err.Error()))
- return err
- }
- //发送邮件提醒异常
- go alarm_msg.SendAlarmMsg("模板消息发送超过当日10万次限制,templateResponse = "+string(result), 3)
- //go utils.SendEmail("异常提醒:", "模板消息发送超过当日10万次限制,templateResponse = "+string(result), utils.EmailSendToUsers)
- //设置3分钟缓存,不允许重复添加
- utils.Rc.SetNX(key, 1, 6*time.Minute)
- }
- //清空发送次数
- sendUrl := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/clear_quota?access_token=%s", accessToken)
- clearData := make(map[string]interface{})
- clearData["appid"] = utils.WxAppId
- clearJson, _ := json.Marshal(clearData)
- utils.FileLog.Info("clear_quota data:" + string(clearJson))
- resp, err := client.Post(sendUrl, "application/json", bytes.NewBuffer(clearJson))
- if err != nil {
- return err
- }
- defer resp.Body.Close()
- clearBody, err := ioutil.ReadAll(resp.Body)
- utils.FileLog.Info("clear_quota result:" + string(clearBody))
- var clearQuotaResponse ClearQuotaResponse
- err = json.Unmarshal(clearBody, &clearQuotaResponse)
- if err != nil {
- utils.FileLog.Info(fmt.Sprintf("clearQuotaResponse Unmarshal Err:%s", err.Error()))
- return err
- }
- if clearQuotaResponse.Errcode == 0 {
- //发送邮件解决异常
- go alarm_msg.SendAlarmMsg("异常已解决,自动清理限制接口,调用成功", 3)
- //go utils.SendEmail("异常已解决:", "自动清理限制接口,调用成功", utils.EmailSendToUsers)
- //重新推送一次
- toSendTemplateMsg(wxAppId, data, resource, sendType, openId, uniqueCode)
- }
- }
- return
- }
- // GetWxAccessToken 获取微信token
- func GetWxAccessToken(wxAppId, wxAppSecret string) (accessToken string, err error, errMsg string) {
- accessToken, err, errMsg = getWxAccessToken(wxAppId, wxAppSecret)
- if err != nil {
- utils.FileLog.Info(fmt.Sprintf("获取Token失败,err:%s,errMsg:%s", err.Error(), errMsg))
- return
- }
- return
- }
- // getWxAccessToken 获取微信token
- func getWxAccessToken(wxAppId, wxAppSecret string) (accessToken string, err error, errMsg string) {
- redisKey := getRedisKeyByAppid(wxAppId)
- if redisKey == `` {
- errMsg = "未配置缓存key"
- err = errors.New(errMsg)
- return
- }
- accessToken, err = utils.Rc.RedisString(redisKey)
- //fmt.Println(err)
- //fmt.Println(accessToken)
- //if err != nil {
- // errMsg = "GetWxAccessToken Err:" + err.Error()
- // utils.FileLog.Info("获取Token失败,msg:" + errMsg)
- // return
- //}
- //取到数据后就直接返回了,没有后续了
- if accessToken != "" {
- return
- }
- //缓存中没有取到数据,那么需要去强制刷新新的accessToken
- return refreshWxAccessToken(wxAppId, wxAppSecret)
- }
- // refreshWxAccessToken 强制刷新微信token
- func refreshWxAccessToken(wxAppId, wxAppSecret string) (accessToken string, err error, errMsg string) {
- fmt.Println("强制刷新" + wxAppId + "微信token")
- defer func() {
- if errMsg != `` {
- utils.FileLog.Info(fmt.Sprintf("强制刷新%s微信token异常:%s", wxAppId, errMsg))
- }
- }()
- redisKey := getRedisKeyByAppid(wxAppId)
- if redisKey == `` {
- errMsg = "未配置缓存key"
- err = errors.New(errMsg)
- return
- }
- if wxAppSecret == "" {
- wxAppSecret, _ = utils.WxAppList[wxAppId]
- }
- if wxAppSecret == "" {
- err = errors.New("缺少密钥信息")
- utils.FileLog.Info(fmt.Sprintf("获取Token失败, errMsg:%s", err.Error()))
- return
- }
- //调用微信官方接口获取新的accessToken
- wxAccessToken, tmpErr := models.GetWxToken(wxAppId, wxAppSecret)
- if tmpErr != nil {
- err = tmpErr
- errMsg = "通过微信接口获取accessToken失败 Err:" + err.Error()
- return
- }
- //如果没有token数据
- if wxAccessToken.AccessToken == "" {
- errMsg = "微信返回的accessToken异常: Err:" + wxAccessToken.Errmsg
- err = errors.New(errMsg)
- return
- }
- accessToken = wxAccessToken.AccessToken
- //如果是弘则研究的appid,那么需要更新mysql的accessToken
- if wxAppId == utils.WxAppId {
- expiresIn := time.Now().Add(time.Duration(wxAccessToken.ExpiresIn) * time.Second).Unix()
- err = models.ModifyAccessToken(wxAccessToken.AccessToken, expiresIn)
- if err != nil {
- errMsg = "更新mysql中的accessToken失败 Err:" + err.Error()
- return
- }
- }
- //更新redis的accessToken(过期时间提前十分钟)
- redisTimeExpire := time.Duration(wxAccessToken.ExpiresIn-600) * time.Second
- err = utils.Rc.Put(redisKey, accessToken, redisTimeExpire)
- if err != nil {
- errMsg = "更新redis中的accessToken失败 Err:" + err.Error()
- return
- }
- return
- }
- // 根据微信appid获取对应的缓存key
- func getRedisKeyByAppid(wxAppId string) (redisKey string) {
- switch wxAppId {
- case utils.WxAppId:
- redisKey = utils.CACHE_WX_ACCESS_TOKEN_HZ
- case utils.AdminWxAppId:
- redisKey = utils.HZ_ADMIN_WX_ACCESS_TOEKN + wxAppId
- default:
- redisKey = utils.HZ_ADMIN_WX_ACCESS_TOEKN + wxAppId
- }
- return redisKey
- }
|