package services import ( "bytes" "encoding/json" "errors" "fmt" "hongze/hongze_public_api/models" "hongze/hongze_public_api/services/alarm_msg" "hongze/hongze_public_api/utils" "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.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 if sendInfo.RedirectTarget == 1 { xcxAppId = utils.WxYbAppId } else if sendInfo.RedirectTarget == 2 { xcxAppId = utils.WxCrmAppId } else if sendInfo.RedirectTarget == 3 { xcxAppId = utils.WxCygxAppId } else { err = errors.New("无效的微信小程序跳转方式:RedirectTarget" + strconv.Itoa(sendInfo.RedirectTarget)) } 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 if wxAppId == utils.WxAppId { accessToken, err, errMsg = getWxAccessToken() if err != nil { utils.FileLog.Info(fmt.Sprintf("获取Token失败,err:%s,errMsg:%s",err.Error(), errMsg)) return } }else{ var wxAppSecret string var ok bool if wxAppSecret, ok = utils.WxAppList[wxAppId]; !ok { err = errors.New("缺少密钥信息") utils.FileLog.Info(fmt.Sprintf("获取Token失败, errMsg:%s",err.Error())) return } accessToken, err = WxGetRedisAccessToken(wxAppId, wxAppSecret) if err != nil { utils.FileLog.Info(fmt.Sprintf("获取Token失败, errMsg:%s",err.Error())) 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() 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()(accessToken string,err error,errMsg string){ accessToken, err = utils.Rc.RedisString(utils.CACHE_WX_ACCESS_TOKEN_HZ) //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() } // refreshWxAccessToken 强制刷新微信token func refreshWxAccessToken()(accessToken string,err error,errMsg string){ fmt.Println("强制刷新微信token") //调用微信官方接口获取新的accessToken wxAccessToken, tmpErr := models.GetWxToken(utils.WxAppId, utils.WxAppSecret) if tmpErr != nil { errMsg = "通过微信接口获取accessToken失败 Err:" + err.Error() err = tmpErr return } //如果没有token数据 if wxAccessToken.AccessToken == "" { errMsg = "微信返回的accessToken异常: Err:" + wxAccessToken.Errmsg err = errors.New(errMsg) return } accessToken = wxAccessToken.AccessToken //更新mysql的accessToken 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(utils.CACHE_WX_ACCESS_TOKEN_HZ,accessToken,redisTimeExpire) if err!=nil{ errMsg = "更新redis中的accessToken失败 Err:" + err.Error() return } return } // WxGetRedisAccessToken 从redis中获取token func WxGetRedisAccessToken(wxAppId, wxAppSecret string) (accessToken string, err error) { //从redis中获取token校验验证码 accessToken, err = utils.Rc.RedisString(utils.HZ_ADMIN_WX_ACCESS_TOEKN+wxAppId) if err != nil { err = nil token, tErr := models.GetWxToken(wxAppId, wxAppSecret) if tErr != nil { return "", tErr } if token.Errmsg != "" { err = errors.New("获取access_token 失败 errcode:" + token.Errmsg + " ;errmsg:" + token.Errmsg) return "", err } //更新redis的accessToken(过期时间提前十分钟) redisTimeExpire := time.Duration(token.ExpiresIn - 600) * time.Second err = utils.Rc.Put(utils.HZ_ADMIN_WX_ACCESS_TOEKN+wxAppId, token.AccessToken, redisTimeExpire) if err != nil { err = errors.New("保存access_token失败 " + err.Error()) return accessToken, err } accessToken = token.AccessToken return } return }