package services import ( "bytes" "encoding/json" "errors" "fmt" "hongze/hongze_clpt/models" "hongze/hongze_clpt/utils" "io/ioutil" "net/http" "strconv" "time" ) // 发送报告提问消息提醒 func SendWxMsgWithAsk(name, askTime, askMsg, title string, openIdList []*models.OpenIdList, articleId int) (err error) { defer func() { if err != nil { go utils.SendAlarmMsg(fmt.Sprint("发送报告提问消息提醒模版消息失败,用户名", name, "文章ID:", articleId), 2) } }() var first string var keyword1 string var keyword2 string var keyword3 string var keyword4 string var remark string first = "研选报告有新的提问,请及时处理" keyword1 = name keyword3 = askTime keyword4 = askMsg remark = title openIdArr := make([]string, 0) for _, v := range openIdList { openIdArr = append(openIdArr, v.OpenId) } redirectUrl := utils.WX_MSG_PATH_ARTICLE_DETAIL + strconv.Itoa(articleId) sendInfo := new(SendWxTemplate) sendInfo.First = first sendInfo.Keyword1 = keyword1 sendInfo.Keyword2 = keyword2 sendInfo.Keyword3 = keyword3 sendInfo.Keyword4 = keyword4 sendInfo.Remark = remark sendInfo.TemplateId = utils.WxMsgTemplateIdAskMsgXzs sendInfo.RedirectUrl = redirectUrl sendInfo.RedirectTarget = 3 sendInfo.Resource = strconv.Itoa(articleId) sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD sendInfo.OpenIdArr = openIdArr err = PublicSendTemplateMsg(sendInfo) return } func WxSendTemplateMsg(sendUrl string, sendMap map[string]interface{}, items []*models.OpenIdList, resource string, sendType int) (err error) { for _, v := range items { sendMap["touser"] = v.OpenId data, err := json.Marshal(sendMap) if err != nil { fmt.Println("SendTemplateMsgOne Marshal Err:", err.Error()) return err } utils.FileLog.Info("One SendData:%s", string(data)) fmt.Println(string(data)) err = SendTemplateMsg(sendUrl, data, resource, sendType, v) if err != nil { fmt.Println("send err:", err.Error()) return err } } return } func SendTemplateMsg(sendUrl string, data []byte, resource string, sendType int, dataItem *models.OpenIdList) (err error) { 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:%s", string(body)) var templateResponse SendTemplateResponse err = json.Unmarshal(body, &templateResponse) fmt.Println(templateResponse.Errcode) fmt.Println(templateResponse.Errmsg) if err != nil { return err } //新增模板消息推送记录 { tr := new(models.UserTemplateRecord) tr.UserId = dataItem.UserId tr.OpenId = dataItem.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.SendType = sendType go func() { err = models.AddUserTemplateRecord(tr) if err != nil { utils.FileLog.Info(fmt.Sprintf("AddUserTemplateRecord Err:%s", err.Error())) } }() } return } // 活动带问提醒 func SendActivityAskApplyTemplateMsgV2(keyword1, keyword2, keyword3, keyword4, remark string, itemOpenid *models.OpenIdList, itemAct *models.ActivityDetail, msgId int) (err error) { var msg string defer func() { if err != nil { go utils.SendEmail("发送模版消息失败"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers) go utils.SendAlarmMsg("活动带问提醒发送模版消息失败"+remark, 2) } if msg != "" { utils.FileLog.Info("发送模版消息失败,msg:%s", msg) } }() activityId := itemAct.ActivityId var redirectUrl string redirectUrl = utils.WX_MSG_PATH_TEMPLATE_MSG + "SourceId=" + strconv.Itoa(msgId) + "&SourceType=2" openIdArr := make([]string, 0) openIdArr = append(openIdArr, itemOpenid.OpenId) sendInfo := new(SendWxTemplate) sendInfo.Keyword1 = keyword1 sendInfo.Keyword2 = keyword2 sendInfo.Keyword3 = keyword3 sendInfo.Keyword4 = keyword4 sendInfo.Remark = remark sendInfo.TemplateId = utils.WxMsgTemplateIdApplyXzs sendInfo.RedirectUrl = redirectUrl sendInfo.RedirectTarget = 3 sendInfo.Resource = strconv.Itoa(activityId) //mapSource留言跳转类型,1:文章、2:活动、3:微路演视频、4:活动视频、5:活动音频 if utils.RunMode != "release" { sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ACTIVITY_ADD } else { redirectUrl = utils.WX_MSG_PATH_TEMPLATE_MSG + "SourceId=" + strconv.Itoa(msgId) + "&SourceType=2" } sendInfo.OpenIdArr = openIdArr err = PublicSendTemplateMsg(sendInfo) return } // 微路演新增留言提问消息提醒 func SendWxMsgWithMicroRoadshowAsk(req models.AddVideoCommnetReq, user *models.WxUserItem, resourceId, msgId int, title string, isResearch bool) (err error) { var msg string defer func() { if err != nil || msg != "" { go utils.SendAlarmMsg(fmt.Sprint("发送微路演新增留言提问消息提醒失败,用户名", req.Title, "ID:", req.SourceId), 2) } }() companyItem, e := models.GetSellerDetailAllByCompanyId(user.CompanyId) if e != nil { return } if companyItem == nil { msg = "获取客户所属销售信息失败,UserID:" + strconv.Itoa(user.UserId) } var first string var keyword1 string var keyword2 string var keyword3 string var keyword4 string var remark string var mobile string first = req.Title + "有新的留言,请及时处理" keyword1 = user.CompanyName + "——" + user.RealName + "(" + companyItem.SellerName + ")" keyword3 = time.Now().Format(utils.FormatDateTime) keyword2 = title keyword4 = req.Content if isResearch { //研选报告添加汪洋 mobile = companyItem.Mobile + "," + utils.WxMsgTemplateIdAskMsgMobilePublic + "," + utils.ActSendMsgMobile } else { mobile = companyItem.Mobile + "," + utils.WxMsgTemplateIdAskMsgMobilePublic } openIdList, e := models.GetWxOpenIdByMobileList(mobile) if e != nil { err = errors.New("GetWxOpenIdByMobileList, Err: " + e.Error()) return } openIdArr := make([]string, 0) for _, v := range openIdList { openIdArr = append(openIdArr, v.OpenId) } mapSource := make(map[int]string) mapSource[1] = "5" mapSource[2] = "4" mapSource[3] = "3" if mapSource[resourceId] == "" { err = errors.New("留言消息类型错误, resourceId: " + strconv.Itoa(resourceId)) return } //SourceType int `description:"视频来源: 1-音频; 2-活动视频; 3-微路演视频 (不传默认为1)"` //mapSource留言跳转类型,1:文章、2:活动、3:微路演视频、4:活动视频、5:活动音频 redirectUrl := "" if utils.RunMode != "release" { if req.SourceType == 3 { redirectUrl = utils.WX_MSG_PATH_ACTIVITY_INDUSTRYR_EPORT + strconv.Itoa(resourceId) remark = "点击进入产业资源包详情页" } else { redirectUrl = utils.WX_MSG_PATH_ACTIVITY_DETAIL + strconv.Itoa(resourceId) remark = "点击查看活动详情" } } else { redirectUrl = utils.WX_MSG_PATH_TEMPLATE_MSG + "SourceId=" + strconv.Itoa(msgId) + "&SourceType=" + mapSource[resourceId] } sendInfo := new(SendWxTemplate) sendInfo.First = first sendInfo.Keyword1 = keyword1 sendInfo.Keyword2 = keyword2 sendInfo.Keyword3 = keyword3 sendInfo.Keyword4 = keyword4 sendInfo.Remark = remark sendInfo.TemplateId = utils.WxMsgTemplateIdApplyXzs sendInfo.RedirectUrl = redirectUrl sendInfo.RedirectTarget = 3 sendInfo.Resource = strconv.Itoa(req.SourceId) sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_COMMENT sendInfo.OpenIdArr = openIdArr err = PublicSendTemplateMsg(sendInfo) if err != nil { return } return } // 发送用户留言提醒 func SendCommentWxTemplateMsg(req models.AddCygxArticleCommentReq, user *models.WxUserItem, articleInfo *models.ArticleDetail, msgId int) (err error) { var msg string defer func() { if err != nil { go utils.SendEmail("发送模版消息失败"+"【"+utils.APPNAME+"】"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers) go utils.SendAlarmMsg(fmt.Sprint("发送用户用户留言提醒失败,文章ID:", req.ArticleId), 2) utils.FileLog.Info("发送模版消息失败,Err:%s", err.Error()) } }() companyItem, e := models.GetSellerDetailAllByCompanyId(user.CompanyId) if e != nil { return } if companyItem == nil { msg = "获取客户所属销售信息失败,UserID:" + strconv.Itoa(user.UserId) } var first string var keyword1 string var keyword2 string var keyword3 string var keyword4 string var remark string var redirectUrl string var mobile string first = "【" + articleInfo.Title + "】有新的留言,请及时处理" keyword1 = user.CompanyName + "——" + user.RealName + "(" + companyItem.SellerName + ")" keyword2 = "留言:" + articleInfo.Title keyword3 = time.Now().Format(utils.FormatDateTime) keyword4 = req.Content remark = "点击查看报告详情" if articleInfo.ArticleTypeId > 0 { //研选报告添加汪洋 mobile = companyItem.Mobile + "," + utils.WxMsgTemplateIdAskMsgMobilePublic + "," + utils.ActSendMsgMobile } else { mobile = companyItem.Mobile + "," + utils.WxMsgTemplateIdAskMsgMobilePublic } openIdList, e := models.GetWxOpenIdByMobileList(mobile) if e != nil { err = errors.New("GetWxOpenIdByMobileList, Err: " + e.Error()) return } openIdArr := make([]string, 0) for _, v := range openIdList { openIdArr = append(openIdArr, v.OpenId) } //mapSource留言跳转类型,1:文章、2:活动、3:微路演视频、4:活动视频、5:活动音频 redirectUrl = utils.WX_MSG_PATH_TEMPLATE_MSG + "SourceId=" + strconv.Itoa(msgId) + "&SourceType=1" sendInfo := new(SendWxTemplate) sendInfo.First = first sendInfo.Keyword1 = keyword1 sendInfo.Keyword2 = keyword2 sendInfo.Keyword3 = keyword3 sendInfo.Keyword4 = keyword4 sendInfo.Remark = remark sendInfo.TemplateId = utils.WxMsgTemplateIdAskMsgXzs sendInfo.RedirectUrl = redirectUrl sendInfo.RedirectTarget = 3 sendInfo.Resource = strconv.Itoa(req.ArticleId) sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_COMMENT sendInfo.OpenIdArr = openIdArr err = PublicSendTemplateMsg(sendInfo) if err != nil { return } return } // 发送用户操作活动消息提醒 func SendWxMsgWithActivityUserRemind(keyword1, keyword2 string, openIdList []*models.OpenIdList, activityId int) (err error) { defer func() { if err != nil { go utils.SendAlarmMsg(fmt.Sprint("发送用户操作活动消息提醒,文章ID:", activityId), 2) } }() openIdArr := make([]string, 0) for _, v := range openIdList { openIdArr = append(openIdArr, v.OpenId) } redirectUrl := utils.WX_MSG_PATH_ACTIVITY_SPECIAL_DETAIL + strconv.Itoa(activityId) sendInfo := new(SendWxTemplate) sendInfo.Keyword1 = keyword1 sendInfo.Keyword2 = keyword2 sendInfo.TemplateId = utils.WxMsgTemplateIdArticleUserRemind sendInfo.RedirectUrl = redirectUrl sendInfo.RedirectTarget = 3 sendInfo.Resource = strconv.Itoa(activityId) sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD sendInfo.OpenIdArr = openIdArr err = PublicSendTemplateMsg(sendInfo) return } // 发送用户阅读报告消息提醒 func SendWxMsgWithArticleUserRemind(keyword1, keyword2 string, openIdList []*models.OpenIdList, articleId int) (err error) { if len(openIdList) == 0 { return } defer func() { if err != nil { go utils.SendAlarmMsg(fmt.Sprint("发送报告提问消息提醒模版消息失败,文章ID:", articleId), 2) } }() openIdArr := make([]string, 0) for _, v := range openIdList { openIdArr = append(openIdArr, v.OpenId) } redirectUrl := utils.WX_MSG_PATH_ARTICLE_DETAIL + strconv.Itoa(articleId) sendInfo := new(SendWxTemplate) sendInfo.Keyword1 = keyword1 sendInfo.Keyword2 = keyword2 sendInfo.TemplateId = utils.WxMsgTemplateIdArticleUserRemind sendInfo.RedirectUrl = redirectUrl sendInfo.RedirectTarget = 3 sendInfo.Resource = strconv.Itoa(articleId) sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD sendInfo.OpenIdArr = openIdArr err = PublicSendTemplateMsg(sendInfo) return } // SendActivitieCancelSignTemplateMsg 取消活动给销售和内容组四人发模版消息 func SendActivitieCancelSignTemplateMsg(user *models.WxUserItem, activityDetail *models.ActivityDetail) (err error) { defer func() { if err != nil { go utils.SendAlarmMsg(fmt.Sprint("取消活动给销售和内容组四人发模版消息Err:", err.Error(), "活动ID:", activityDetail.ActivityId, "手机号:", user.Mobile), 2) } }() //获取销售手机号 sellerItemQy, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2) if err != nil { return } configCode := utils.TPL_MSG_NEI_RONG_ZU cnf, e := models.GetConfigByCode(configCode) if e != nil { err = errors.New("GetConfigByCode, Err: " + e.Error() + configCode) return } openIdList, e := models.GetWxOpenIdByMobileList(cnf.ConfigValue + "," + sellerItemQy.Mobile) if e != nil && e.Error() != utils.ErrNoRow() { err = errors.New("GetUserRecordListByMobile, Err: " + e.Error() + cnf.ConfigValue) return } keyword1 := user.RealName + "--" + user.CompanyName + "(" + sellerItemQy.RealName + ")" keyword2 := user.Mobile keyword3 := time.Now().Format(utils.FormatDateTime) keyword4 := "取消报名" + "【" + activityDetail.ActivityName + "】" openIdArr := make([]string, 0) for _, v := range openIdList { openIdArr = append(openIdArr, v.OpenId) } sendInfo := new(SendWxTemplate) sendInfo.Keyword1 = keyword1 sendInfo.Keyword2 = keyword2 sendInfo.Keyword3 = keyword3 sendInfo.Keyword4 = keyword4 sendInfo.TemplateId = utils.WxMsgTemplateIdApplyXzs sendInfo.RedirectTarget = 3 sendInfo.Resource = user.Mobile sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_APPLY sendInfo.OpenIdArr = openIdArr err = PublicSendTemplateMsg(sendInfo) return } // SendNeiRongZuActivitieSignTemplateMsg 报名活动给销售和内容组四人发模版消息 func SendNeiRongZuActivitieSignTemplateMsg(user *models.WxUserItem, activityDetail *models.ActivityDetail) (err error) { defer func() { if err != nil { go utils.SendAlarmMsg(fmt.Sprint("取消活动给销售和内容组四人发模版消息Err:", err.Error(), "活动ID:", activityDetail.ActivityId, "手机号:", user.Mobile), 2) } }() //获取销售手机号 sellerItemQy, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2) if err != nil && err.Error() != utils.ErrNoRow() { return } configCode := utils.TPL_MSG_NEI_RONG_ZU cnf, e := models.GetConfigByCode(configCode) if e != nil { err = errors.New("GetConfigByCode, Err: " + e.Error() + configCode) return } openIdList, e := models.GetWxOpenIdByMobileList(cnf.ConfigValue + "," + sellerItemQy.Mobile) if e != nil && e.Error() != utils.ErrNoRow() { err = errors.New("GetUserRecordListByMobile, Err: " + e.Error() + cnf.ConfigValue) return err } keyword1 := user.RealName + "--" + user.CompanyName + "(" + sellerItemQy.RealName + ")" keyword2 := user.Mobile keyword3 := time.Now().Format(utils.FormatDateTime) keyword4 := "报名" + "【" + activityDetail.ActivityName + "】" openIdArr := make([]string, 0) for _, v := range openIdList { openIdArr = append(openIdArr, v.OpenId) } sendInfo := new(SendWxTemplate) sendInfo.Keyword1 = keyword1 sendInfo.Keyword2 = keyword2 sendInfo.Keyword3 = keyword3 sendInfo.Keyword4 = keyword4 sendInfo.TemplateId = utils.WxMsgTemplateIdApplyXzs sendInfo.RedirectTarget = 3 sendInfo.Resource = user.Mobile sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_APPLY sendInfo.OpenIdArr = openIdArr err = PublicSendTemplateMsg(sendInfo) return }