package services import ( "encoding/json" "errors" "fmt" "hongze/hongze_mfyx/models" "hongze/hongze_mfyx/models/order" "hongze/hongze_mfyx/utils" "io/ioutil" "net/http" "strconv" "strings" "time" "unicode" ) type SendWxCategoryTemplate struct { WxAppId string `description:"公众号appId"` Keywords []string `description:"模板消息keyword字段"` TemplateId string `description:"模板id"` RedirectUrl string `description:"跳转地址"` RedirectTarget int `description:"小程序跳转目标:1:弘则研报小程序,2:随手办公小程序,3:查研观向,4:买方研选"` Resource string `description:"资源唯一标识"` SendType int `description:"发送的消息类型:1:报告,2:指标更新提醒,3:审批通知,4:销售领取客户通知,5:活动取消通知,6活动更改时间通知,7:关注的作者发布报告通知,8:发送日报(周报、双周报、月报)模板消息,9:活动预约/报名时间通知"` OpenIdArr []string `description:"消息接收者openid"` } // 推送类目模板消息 func SendCategoryTemplateMsg(sendInfo *SendWxCategoryTemplate) (err error) { postData, err := json.Marshal(sendInfo) if err != nil { go utils.SendAlarmMsg("SendCategoryTemplateMsg json.Marshal Err:"+err.Error(), 1) return err } body := ioutil.NopCloser(strings.NewReader(string(postData))) client := &http.Client{} req, err := http.NewRequest("POST", utils.SendWxCategoryTemplateMsgUrl, body) if err != nil { go utils.SendAlarmMsg("SendCategoryTemplateMsg http.NewRequest Err:"+err.Error(), 1) return err } contentType := "application/json;charset=utf-8" req.Header.Set("Content-Type", contentType) req.Header.Set("Authorization", utils.SendTemplateMsgAuthorization) resp, err := client.Do(req) if err != nil { fmt.Println("http client.Do Err:" + err.Error()) return err } defer resp.Body.Close() b, err := ioutil.ReadAll(resp.Body) if err != nil { return err } result := new(models.BaseResponse) err = json.Unmarshal(b, &result) if err != nil { return err } if result.Ret != 200 { err = errors.New(string(b)) return err } return } // 发送用户操作活动消息提醒 func SendWxCategoryMsgWithActivityUserRemind(keyWord1, keyWord2, keyWord3, keyWord4, redirectUrl string, openIdList []*models.OpenIdList, activityId int) (err error) { if len(openIdList) == 0 { return } defer func() { if err != nil { go utils.SendAlarmMsg(fmt.Sprint("发送用户操作活动消息提醒,文章ID:", activityId), 2) } }() keyWord1 = utils.TruncateActivityNameString(keyWord1) openIdArr := make([]string, 0) for _, v := range openIdList { openIdArr = append(openIdArr, v.OpenId) } var keywords []string keywords = append(keywords, keyWord1, keyWord2, keyWord3, keyWord4) sendInfo := new(SendWxCategoryTemplate) sendInfo.Keywords = keywords sendInfo.TemplateId = utils.WxMsgCategoryTemplateIdActivityPublishApply sendInfo.RedirectTarget = 4 sendInfo.RedirectUrl = redirectUrl sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_APPLY sendInfo.OpenIdArr = openIdArr err = SendCategoryTemplateMsg(sendInfo) //sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxAppId, "pagepath": "activityPages/activityDetail/activityDetail?id=" + strconv.Itoa(activityId)} return } // 试用申请模板消息 func SendPermissionApplyCategoryTemplateMsg(realName, companyName, mobile, applyMethod string, itemOpenid *models.OpenIdList, redirectUrl string) (err error) { defer func() { if err != nil { go utils.SendAlarmMsg(fmt.Sprint("权限申请模板消息推送消息失败,手机号", mobile, "公司:", companyName, "手机号:", mobile, ";Err:"+err.Error()), 2) } }() var keyword1 string var keyword2 string var keyword3 string var keyword4 string var keyword5 string detail, e := models.GetCygxApplyRecordByMobile(mobile) if e != nil { err = errors.New("GetCygxApplyRecordByMobile" + e.Error()) return } //如果是潜在客户就推送可查看详情的模版消息 if utils.RunMode == "release" { if detail.ApplyMethod == 2 { redirectUrl = utils.WX_MSG_PATH_APPLY_DETAIL + strconv.Itoa(detail.ApplyRecordId) } } openIdArr := make([]string, 0) openIdArr = append(openIdArr, itemOpenid.OpenId) sendInfo := new(SendWxCategoryTemplate) keyword1 = realName keyword2 = companyName keyword3 = "权限申请" keyword4 = time.Now().Format(utils.FormatDateTimeMinute2) keyword5 = applyMethod sendInfo.Keywords = []string{keyword1, keyword2, keyword3, keyword4, keyword5} sendInfo.TemplateId = utils.WxCategoryMsgTemplateIdApplyXzs sendInfo.RedirectUrl = redirectUrl sendInfo.RedirectTarget = 4 sendInfo.Resource = mobile sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_APPLY sendInfo.OpenIdArr = openIdArr err = SendCategoryTemplateMsg(sendInfo) return } // 用户报名模板消息推送(研选公众号类目模版消息) func SendNeiRongZuActivitieSignCategoryTemplateMsg(user *models.WxUserItem, activityDetail *models.ActivityDetail) (err error) { defer func() { if err != nil { go utils.SendAlarmMsg(fmt.Sprint("发送用户操作活动消息提醒,文章ID:", activityDetail.ActivityId), 2) fmt.Println(err) } }() sellerItemQy, e := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2) if e != nil && e.Error() != utils.ErrNoRow() { return } var mobile string var sellerRealName string var mobiles []string mobiles = append(mobiles, utils.RAI_MOBILE_GAO_YI_WEN, utils.RAI_MOBILE_DONG_HENG, utils.RAI_MOBILE_WU_ANG_DI, utils.MobileShenTao, utils.MobileZhangChuanXin) //高亦文、董衡、吴昂迪手机号 if sellerItemQy != nil { mobiles = append(mobiles, sellerItemQy.Mobile) sellerRealName = sellerItemQy.RealName } else { sellerRealName = "__" } mobile = strings.Join(mobiles, ",") openIdList, e := models.GetMfyxWxOpenIdByMobileList(mobile) if e != nil { err = errors.New("GetMfyxWxOpenIdByMobileList, Err: " + e.Error()) return err } if len(openIdList) == 0 { err = nil return } openIdArr := make([]string, 0) for _, v := range openIdList { openIdArr = append(openIdArr, v.OpenId) } var keywords []string keywords = append(keywords, user.RealName, user.CompanyName, sellerRealName, time.Now().Format(utils.FormatDateTimeMinute2), utils.TruncateActivityNameString("报名:"+activityDetail.ActivityName)) redirectUrl := utils.WX_MSG_PATH_YX_ACTIVITY_DETAIL + strconv.Itoa(activityDetail.ActivityId) //活动详情地址 sendInfo := new(SendWxCategoryTemplate) sendInfo.Keywords = keywords sendInfo.TemplateId = utils.WxCategoryMsgTemplateIdApplyXzs sendInfo.RedirectTarget = 4 sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_APPLY sendInfo.OpenIdArr = openIdArr sendInfo.RedirectUrl = redirectUrl err = SendCategoryTemplateMsg(sendInfo) return } // 用户取消报名模板消息推送(研选公众号类目模版消息) func SendActivitieCancelSignCategoryTemplateMsg(user *models.WxUserItem, activityDetail *models.ActivityDetail) (err error) { defer func() { if err != nil { fmt.Println(err) go utils.SendAlarmMsg(fmt.Sprint("发送用户操作活动消息提醒,活动ID:", activityDetail.ActivityId), 2) } }() sellerItemQy, e := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2) if e != nil && e.Error() != utils.ErrNoRow() { return } var mobile string var sellerRealName string var mobiles []string mobiles = append(mobiles, utils.RAI_MOBILE_GAO_YI_WEN, utils.RAI_MOBILE_DONG_HENG, utils.RAI_MOBILE_WU_ANG_DI, utils.MobileShenTao, utils.MobileZhangChuanXin) //高亦文、董衡、吴昂迪手机号 if sellerItemQy != nil { mobiles = append(mobiles, sellerItemQy.Mobile) sellerRealName = sellerItemQy.RealName } else { sellerRealName = "__" } mobile = strings.Join(mobiles, ",") openIdList, e := models.GetMfyxWxOpenIdByMobileList(mobile) if e != nil { err = errors.New("GetMfyxWxOpenIdByMobileList, Err: " + e.Error()) return err } if len(openIdList) == 0 { err = nil return } openIdArr := make([]string, 0) for _, v := range openIdList { openIdArr = append(openIdArr, v.OpenId) } var keywords []string keywords = append(keywords, user.RealName, user.CompanyName, sellerRealName, time.Now().Format(utils.FormatDateTimeMinute2), utils.TruncateActivityNameString("取消报名:"+activityDetail.ActivityName)) redirectUrl := utils.WX_MSG_PATH_YX_ACTIVITY_DETAIL + strconv.Itoa(activityDetail.ActivityId) //活动详情地址 sendInfo := new(SendWxCategoryTemplate) sendInfo.Keywords = keywords sendInfo.TemplateId = utils.WxCategoryMsgTemplateIdApplyXzs sendInfo.RedirectTarget = 4 sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_APPLY sendInfo.OpenIdArr = openIdArr sendInfo.RedirectUrl = redirectUrl err = SendCategoryTemplateMsg(sendInfo) return } // 发送用户留言提醒 func SendCommentWxCategoryTemplateMsg(req models.AddCygxArticleCommentReq, user *models.WxUserItem, articleInfo *models.ArticleDetail, msgId int) (err error) { defer func() { if err != nil { go utils.SendAlarmMsg(fmt.Sprint("发送用户用户留言提醒失败,文章ID:", req.ArticleId), 2) } }() companyItem, e := models.GetSellerDetailAllByCompanyId(user.CompanyId) if e != nil { err = errors.New("GetSellerDetailAllByCompanyId, Err: " + e.Error()) return } var keyword1 string var keyword2 string var keyword3 string var keyword4 string var keyword5 string var redirectUrl string var mobile string var keywords []string keyword1 = user.RealName keyword2 = companyItem.CompanyName keyword3 = companyItem.SellerName keyword4 = time.Now().Format(utils.FormatDateTimeMinute2) keyword5 = "报告留言:" + req.Content keyword5 = utils.TruncateActivityNameString(keyword5) keywords = append(keywords, keyword1, keyword2, keyword3, keyword4, keyword5) if articleInfo.ArticleTypeId > 0 { //研选报告添加汪洋 mobile = companyItem.Mobile + "," + utils.WxMsgTemplateIdAskMsgMobilePublic + "," + utils.ActSendMsgMobile } else { mobile = companyItem.Mobile + "," + utils.WxMsgTemplateIdAskMsgMobilePublic } openIdList, e := models.GetMfyxWxOpenIdByMobileList(mobile) if e != nil { err = errors.New("GetMfyxWxOpenIdByMobileList, Err: " + e.Error()) return } openIdArr := make([]string, 0) for _, v := range openIdList { openIdArr = append(openIdArr, v.OpenId) } redirectUrl = utils.WX_MSG_PATH_YX_COMMENT_DETAIL + strconv.Itoa(msgId) + "&type=文章" sendInfo := new(SendWxCategoryTemplate) sendInfo.Keywords = keywords sendInfo.TemplateId = utils.WxCategoryMsgTemplateIdApplyXzs sendInfo.RedirectTarget = 4 sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_APPLY sendInfo.OpenIdArr = openIdArr sendInfo.RedirectUrl = redirectUrl err = SendCategoryTemplateMsg(sendInfo) return } // 发送活动提问消息提醒(研选公众号类目模版消息) func SendWxCategoryMsgWithAsk(user *models.WxUserItem, activityInfo *models.ActivityDetail, askMsg string, askId int) (err error) { if activityInfo.ChartPermissionId != utils.CHART_PERMISSION_ID_YANXUAN { return } defer func() { if err != nil { fmt.Println(err) go utils.SendAlarmMsg(fmt.Sprint("发送活动提问消息提醒类目模版消息失败,用户名", user.RealName, "提问ID:", askId), 2) } }() companyId := user.CompanyId var sellerName string //潜在客户、未开权益,已开FICC的客户推给王芳,开通权益的客户推给对应销售 mobile := utils.WxMsgTemplateIdAskMsgMobilePublic if companyId > 1 { raiCount, e := models.GetCompanyProductCount(companyId, utils.COMPANY_PRODUCT_RAI_ID) if e != nil { err = errors.New("GetCompanyProductCount, Err: " + e.Error()) return } if raiCount > 0 { sealldetail, e := models.GetRaiSellerByCompanyId(companyId) if e != nil { err = errors.New("GetRaiSellerByCompanyId, Err: " + e.Error()) return } sellerName = sealldetail.RealName mobile += "," + sealldetail.Mobile } } var mobiles []string mobiles = append(mobiles, utils.RAI_MOBILE_GAO_YI_WEN, utils.RAI_MOBILE_DONG_HENG, utils.RAI_MOBILE_WU_ANG_DI, utils.RAI_MOBILE_GE_LIN) //葛琳、高亦文、董衡、吴昂迪手机号 mobile += "," + strings.Join(mobiles, ",") openIdList, e := models.GetMfyxWxOpenIdByMobileList(mobile) if e != nil { err = errors.New("GetMfyxWxOpenIdByMobileList, Err: " + e.Error()) return err } if len(openIdList) == 0 { return } var keyword1 string var keyword2 string var keyword3 string var keyword4 string var keyword5 string keyword1 = user.RealName keyword2 = utils.TruncateActivityNameString(user.CompanyName) keyword3 = sellerName keyword4 = time.Now().Format(utils.FormatDateTimeMinute2) keyword5 = utils.TruncateActivityNameString("活动提问:" + askMsg) keywords := []string{keyword1, keyword2, keyword3, keyword4, keyword5} openIdArr := make([]string, 0) for _, v := range openIdList { openIdArr = append(openIdArr, v.OpenId) } redirectUrl := utils.WX_MSG_PATH_TEMPLATE_MSG + "SourceId=" + strconv.Itoa(askId) + "&SourceType=2" //redirectUrl = "" sendInfo := new(SendWxCategoryTemplate) sendInfo.Keywords = keywords sendInfo.TemplateId = utils.WxCategoryMsgTemplateIdApplyXzs sendInfo.RedirectTarget = 4 sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD sendInfo.OpenIdArr = openIdArr sendInfo.RedirectUrl = redirectUrl err = SendCategoryTemplateMsg(sendInfo) return } // 线下调研活动扫码签到给对应销售发类目模班消息 func SendActivitieSignCategoryTemplateMsg(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) } }() var mobiles []string //获取销售信息 sellerItem, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2) if err != nil && err.Error() != utils.ErrNoRow() { return } if sellerItem != nil { mobiles = append(mobiles, sellerItem.Mobile, utils.MobileWuAngDi, utils.MobileDongHeng, utils.MobileShenTao, utils.MobileZhangChuanXin) } var keyword1 string var keyword2 string var keyword3 string var keyword4 string var keyword5 string var redirectUrl string keyword1 = user.RealName keyword2 = user.CompanyName keyword3 = sellerItem.RealName keyword4 = activityDetail.ActivityName keyword5 = time.Now().Format(utils.FormatDateTimeMinute2) openIdList, e := models.GetMfyxUserRecordListByMobileArr(mobiles) if e != nil { err = errors.New("GetMfyxUserRecordListByMobileArr, Err: " + e.Error()) return } if len(openIdList) == 0 { return } openIdArr := make([]string, 0) for _, v := range openIdList { openIdArr = append(openIdArr, v.OpenId) } redirectUrl = utils.WX_MSG_PATH_YX_SIGNUP_DETAIL + strconv.Itoa(activityDetail.ActivityId) keywords := []string{keyword1, keyword2, keyword3, keyword4, keyword5} sendInfo := new(SendWxCategoryTemplate) sendInfo.Keywords = keywords sendInfo.TemplateId = utils.WxCategoryMsgTemplateIdActivitySign sendInfo.RedirectTarget = 4 sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD sendInfo.OpenIdArr = openIdArr sendInfo.RedirectUrl = redirectUrl err = SendCategoryTemplateMsg(sendInfo) if err != nil { return } return } // SendReviewCategoryTemplateMsgAdmin 提交审核时给王芳,汪洋发消息 func SendReviewCategoryTemplateMsgAdmin(specialId int) (err error) { defer func() { if err != nil { go utils.SendAlarmMsg(fmt.Sprint("处理试用申请给王芳,汪洋发消息失败, specialId:", specialId, "ErrMsg", err.Error()), 2) } }() var configCode string //研选专栏修改之后给这些手机号推送审核模版消息 configCode = utils.TPL_MSG_YAN_XUAN_SPECIAL_APPROVAL cnf, e := models.GetConfigByCode(configCode) if e != nil { err = errors.New("GetConfigByCode, Err: " + e.Error() + configCode) return } //王芳、葛琳和沈涛 openIdList, e := models.GetMfyxWxOpenIdByMobileList(cnf.ConfigValue) if e != nil && e.Error() != utils.ErrNoRow() { err = errors.New("GetUserRecordListByMobile, Err: " + e.Error()) return err } specialItem, e := models.GetYanxuanSpecialItemById(specialId) if e != nil { err = errors.New("GetYanxuanSpecialFollowUserById, Err: " + e.Error()) return } user, e := models.GetWxUserItemByUserId(specialItem.UserId) if e != nil { err = errors.New("GetWxUserItemByUserId, Err: " + e.Error()) return err } var keyword1 string var keyword2 string var keyword3 string var keyword4 string var keyword5 string keyword1 = checkNickNameString(specialItem.NickName) keyword2 = specialItem.RealName + "-" + user.CompanyName keyword2 = utils.TruncateActivityNameString(keyword2) keyword3 = checkSpecialNameString(specialItem.SpecialName) keyword4 = time.Now().Format(utils.FormatDateTimeMinute2) keyword5 = "研选专栏提交了内容待审核" openIdArr := make([]string, 0) for _, v := range openIdList { openIdArr = append(openIdArr, v.OpenId) } redirectUrl := "" redirectUrl = utils.WX_MSG_PATH_YX_SPECIAL_ENABLE_DETAIL + strconv.Itoa(specialId) keywords := []string{keyword1, keyword2, keyword3, keyword4, keyword5} sendInfo := new(SendWxCategoryTemplate) sendInfo.Keywords = keywords sendInfo.TemplateId = utils.WxCategoryMsgTemplateIdApplyXzs sendInfo.RedirectTarget = 4 sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD sendInfo.OpenIdArr = openIdArr sendInfo.RedirectUrl = redirectUrl err = SendCategoryTemplateMsg(sendInfo) if err != nil { return } return } func checkNickNameString(s string) string { var trimmedString string hanCount := 0 for _, r := range s { if unicode.Is(unicode.Han, r) { trimmedString += string(r) hanCount++ if hanCount == 5 { break } } else { return "作者昵称" } } if trimmedString == "" { trimmedString = "作者昵称" } return trimmedString } func checkSpecialNameString(s string) string { var trimmedString string hanCount := 0 for _, r := range s { if unicode.Is(unicode.Han, r) { trimmedString += string(r) hanCount++ if hanCount == 5 { break } } else { return "专栏名称" } } if trimmedString == "" { trimmedString = "专栏名称" } return trimmedString } // 研选专栏审核完成时,给提交人发送类目模板消息 func SendWxCategoryMsgSpecialAuthor(specialId, status int) (err error) { defer func() { if err != nil { go utils.SendAlarmMsg(fmt.Sprint("研选专栏审核完成时,给提交人发送模板消息", specialId, ", specialId", err.Error()), 2) } }() var keyword1 string var keyword2 string var keyword3 string var keyword4 string var redirectUrl string specialItem, e := models.GetYanxuanSpecialItemById(specialId) if e != nil { err = errors.New("GetYanxuanSpecialFollowUserById, Err: " + e.Error()) return } user, e := models.GetWxUserItemByUserId(specialItem.UserId) if e != nil { err = errors.New("GetWxUserItemByUserId, Err: " + e.Error()) return err } openIdList, err := models.GetMfyxWxOpenIdByMobileList(user.Mobile) if err != nil && err.Error() != utils.ErrNoRow() { return err } keyword1 = specialItem.Title keyword2 = specialItem.NickName if status == 1 { keyword3 = "文章已通过审核,点击查看详情" redirectUrl = utils.WX_MSG_PATH_YX_SPECIAL_DETAIL + strconv.Itoa(specialId) } else { keyword3 = "文章未通过审核,点击查看驳回原因" redirectUrl = utils.WX_MSG_PATH_YX_SPECIAL_CENTER } keyword4 = time.Now().Format(utils.FormatDateTimeMinute2) openIdArr := make([]string, 0) for _, v := range openIdList { openIdArr = append(openIdArr, v.OpenId) } keywords := []string{keyword1, keyword2, keyword3, keyword4} sendInfo := new(SendWxCategoryTemplate) sendInfo.Keywords = keywords sendInfo.TemplateId = utils.WxMsgCategoryTemplateIdCompanyApprovalMessageRai sendInfo.RedirectTarget = 4 sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD sendInfo.OpenIdArr = openIdArr sendInfo.RedirectUrl = redirectUrl err = SendCategoryTemplateMsg(sendInfo) if err != nil { return } return } // 研选专栏有新内容审核通过时,给关注此专栏的客户发送模板消息 func SendWxCategoryMsgSpecialFollow(specialId int) (err error) { defer func() { if err != nil { go utils.SendAlarmMsg(fmt.Sprint("研选专栏有新内容审核通过时,给关注此专栏的客户发送类目模板消息失败", specialId, ", specialId", err.Error()), 2) } }() var keyword1 string var keyword2 string var keyword3 string var keyword4 string followers, e := models.GetYanxuanSpecialFollowUserById(specialId) if e != nil { err = errors.New("GetYanxuanSpecialFollowUserById, Err: " + e.Error()) return } if len(followers) == 0 { return } specialItem, e := models.GetYanxuanSpecialItemById(specialId) if e != nil { err = errors.New("GetYanxuanSpecialFollowUserById, Err: " + e.Error()) return } var allInUserId string for _, v := range followers { allInUserId += strconv.Itoa(v) + "," } allInUserId = strings.TrimRight(allInUserId, ",") userList, err := models.GetWxUserListByUserIds(allInUserId) if err != nil && err.Error() != utils.ErrNoRow() { return err } mobile := `` for _, v := range userList { mobile += v.Mobile + "," } mobile = strings.TrimRight(mobile, ",") openIdList, e := models.GetMfyxWxOpenIdByMobileList(mobile) if e != nil { err = errors.New("GetMfyxWxOpenIdByMobileList, Err: " + e.Error()) return } if len(openIdList) == 0 { return } keyword1 = specialItem.SpecialName keyword2 = "发布了新报告" keyword3 = specialItem.Title keyword4 = time.Now().Format(utils.FormatDateTimeMinute2) openIdArr := make([]string, 0) for _, v := range openIdList { openIdArr = append(openIdArr, v.OpenId) } redirectUrl := "" redirectUrl = utils.WX_MSG_PATH_YX_SPECIAL_DETAIL + strconv.Itoa(specialId) keywords := []string{keyword1, keyword2, keyword3, keyword4} sendInfo := new(SendWxCategoryTemplate) sendInfo.Keywords = keywords sendInfo.TemplateId = utils.WxMsgCategoryTemplateIdActivityPublishApply sendInfo.RedirectTarget = 4 sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD sendInfo.OpenIdArr = openIdArr sendInfo.RedirectUrl = redirectUrl err = SendCategoryTemplateMsg(sendInfo) if err != nil { return } return } // 文章互动相关类目模版消息 func SendWxCategoryMsgInteractive(user *models.WxUserItem, interactive string, articleId int, title string) (err error) { if interactive == "阅读报告" { //30秒内阅读同一篇报告不做重复推送 key := "CYGX_" + utils.CYGX_OBJ_RESEARCHSUMMARY + "_READ" + strconv.Itoa(articleId) + "_" + strconv.Itoa(user.UserId) + "_CATEGORY" if utils.Rc.IsExist(key) { return } utils.Rc.Put(key, 1, 30*time.Second) } defer func() { if err != nil { go utils.SendAlarmMsg(fmt.Sprint("文章互动相关,类目模板消息推送 ,SendWxCategoryMsgInteractive"+err.Error(), "userId:", user.UserId, "互动类型:", interactive), 2) } }() countUser, err := models.GetUserRemind(user.UserId) if err != nil { return err } if err != nil { return err } if countUser == 0 { return err } var keyword1 string var keyword2 string var keyword3 string var keyword4 string //获取销售手机号 sellerItemQy, err := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2) if err != nil && err.Error() != utils.ErrNoRow() { return err } if sellerItemQy != nil { sllerAndShareMobileArr, e := GetCompanySellerAndShareMobileByRai(user.CompanyId) //获取所属销售以及对应销售的手机号 if e != nil { err = errors.New("GetCompanySellerAndShareMobileByRai, Err: " + e.Error()) return } sllerAndShareMobiles := strings.Join(sllerAndShareMobileArr, ",") openIdList, e := models.GetMfyxWxOpenIdByMobileList(sllerAndShareMobiles) if e != nil { err = errors.New("GetMfyxWxOpenIdByMobileList, Err: " + e.Error()) return } if len(openIdList) == 0 { return } keyword1 = user.RealName + "-" + user.CompanyName keyword1 = utils.TruncateActivityNameString(keyword1) keyword2 = fmt.Sprint("互动提醒:", interactive) keyword3 = utils.TruncateActivityNameString(title) keyword4 = time.Now().Format(utils.FormatDateTimeMinute2) openIdArr := make([]string, 0) for _, v := range openIdList { openIdArr = append(openIdArr, v.OpenId) } redirectUrl := `` redirectUrl = utils.WX_MSG_PATH_YX_ARTICLE_DETAIL + strconv.Itoa(articleId) keywords := []string{keyword1, keyword2, keyword3, keyword4} sendInfo := new(SendWxCategoryTemplate) sendInfo.Keywords = keywords sendInfo.TemplateId = utils.WxMsgCategoryTemplateIdActivityPublishApply sendInfo.RedirectTarget = 4 sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD sendInfo.OpenIdArr = openIdArr sendInfo.RedirectUrl = redirectUrl err = SendCategoryTemplateMsg(sendInfo) if err != nil { return } } return } // 用户搜索操作操作行为,类目模板消息推送 func SearchKeywordUserRmindCategoryMsg(user *models.WxUserItem, keyWord string) (err error) { defer func() { if err != nil { fmt.Println(err) go utils.SendAlarmMsg(fmt.Sprint("用户搜索操作操作行为,类目模板消息推送失败"+err.Error(), "UserId:", user.UserId, "keyWord:", keyWord), 2) } }() countUser, e := models.GetUserRemind(user.UserId) if e != nil { err = errors.New("GetUserRemind, Err: " + e.Error()) return } if countUser == 0 { return } var keyword1 string var keyword2 string var keyword3 string var keyword4 string //获取销售手机号 sellerItemQy, e := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2) if e != nil && e.Error() != utils.ErrNoRow() { err = errors.New("GetSellerByCompanyIdCheckFicc, Err: " + e.Error()) return } if sellerItemQy != nil { sllerAndShareMobileArr, e := GetCompanySellerAndShareMobileByRai(user.CompanyId) //获取所属销售以及对应销售的手机号 if e != nil { err = errors.New("GetCompanySellerAndShareMobileByRai, Err: " + e.Error()) return } sllerAndShareMobiles := strings.Join(sllerAndShareMobileArr, ",") openIdList, e := models.GetMfyxWxOpenIdByMobileList(sllerAndShareMobiles) if e != nil { err = errors.New("GetMfyxWxOpenIdByMobileList, Err: " + e.Error()) return } if len(openIdList) == 0 { return } keyword1 = user.RealName + "-" + user.CompanyName keyword2 = fmt.Sprint("互动提醒:搜索") keyword3 = keyWord keyword4 = time.Now().Format(utils.FormatDateTimeMinute2) openIdArr := make([]string, 0) for _, v := range openIdList { openIdArr = append(openIdArr, v.OpenId) } redirectUrl := "" keywords := []string{keyword1, keyword2, keyword3, keyword4} sendInfo := new(SendWxCategoryTemplate) sendInfo.Keywords = keywords sendInfo.TemplateId = utils.WxMsgCategoryTemplateIdCompanyApprovalMessageRai sendInfo.RedirectTarget = 4 sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD sendInfo.OpenIdArr = openIdArr sendInfo.RedirectUrl = redirectUrl err = SendCategoryTemplateMsg(sendInfo) if err != nil { return } } return } // 发送报告提问消息提醒 func SendWxCategoryMsgWithActivityAsk(companyName, realName, sellerName, askTime, askMsg string, openIdList []*models.OpenIdList, msgId int) (err error) { if len(openIdList) == 0 { return } 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("发送报告提问消息提醒类目模版消息失败,用户名", realName, "留言ID:", msgId), 2) utils.FileLog.Info("发送类目模版消息失败,Err:%s", err.Error()) } fmt.Println("line 21", err, msg) }() var keyword1 string var keyword2 string var keyword3 string var keyword4 string var keyword5 string keyword1 = realName keyword2 = companyName keyword3 = sellerName keyword4 = askTime keyword5 = utils.TruncateActivityNameString("活动提问:" + askMsg) keywords := []string{keyword1, keyword2, keyword3, keyword4, keyword5} openIdArr := make([]string, 0) for _, v := range openIdList { openIdArr = append(openIdArr, v.OpenId) } redirectUrl := utils.WX_MSG_PATH_YX_ACTIVITY_DETAIL + strconv.Itoa(msgId) sendInfo := new(SendWxCategoryTemplate) sendInfo.Keywords = keywords sendInfo.TemplateId = utils.WxCategoryMsgTemplateIdApplyXzs sendInfo.RedirectTarget = 4 sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD sendInfo.OpenIdArr = openIdArr sendInfo.RedirectUrl = redirectUrl err = SendCategoryTemplateMsg(sendInfo) if err != nil { return } return } // 研选类目模板消息 func SendPermissionApplyTemplateMsgAdminByYxCategory(req models.ApplyTryReq, companyId int, usermobile, applyMethod, redirectUrl string) (err error) { defer func() { if err != nil { go utils.SendAlarmMsg("研选类目模板消息消息失败, SendPermissionApplyTemplateMsgAdminByYxCategoryErrMsg: "+err.Error(), 3) } }() //潜在客户、未开权益,已开FICC的客户推给王芳,开通权益的客户推给对应销售 mobileOpenid := utils.WxMsgTemplateIdAskMsgMobilePublic if companyId > 1 { raiCount, e := models.GetCompanyProductCount(companyId, utils.COMPANY_PRODUCT_RAI_ID) if e != nil { err = errors.New("GetCompanyProductCount, Err: " + e.Error()) return } if raiCount > 0 { sealldetail, e := models.GetRaiSellerByCompanyId(companyId) if e != nil { err = errors.New("GetRaiSellerByCompanyId, Err: " + e.Error()) return } //如果有共享销售给共享销售也推模版消息 if sealldetail.ShareSellerId > 0 { adminShare, e := models.GetSellerByAdminId(sealldetail.ShareSellerId) if e != nil { err = errors.New("GetSellerByAdminId, Err: " + e.Error()) return } listSellerMobile, e := models.GetCygxSellerMobileMap() if e != nil { err = errors.New("GetCygxSellerMobileMap, Err: " + e.Error()) return } //mapMobile := make(map[string]string) for _, vm := range listSellerMobile { if adminShare.Mobile == vm.ServerMobile { adminShare.Mobile = vm.Mobile } } mobileOpenid = sealldetail.Mobile + "," + adminShare.Mobile } else { mobileOpenid = sealldetail.Mobile } } } fmt.Println("mobileOpenid", mobileOpenid) openIdList, e := models.GetMfyxWxOpenIdByMobileList(mobileOpenid) if e != nil { err = errors.New("GetMfyxWxOpenIdByMobileList, Err: " + e.Error()) return err } if len(openIdList) == 0 { return } var keyword1 string var keyword2 string var keyword3 string var keyword4 string var keyword5 string keyword1 = req.RealName keyword2 = utils.TruncateActivityNameString(req.CompanyName) keyword3 = "权限申请" keyword4 = time.Now().Format(utils.FormatDateTimeMinute2) keyword5 = applyMethod openIdArr := make([]string, 0) for _, v := range openIdList { openIdArr = append(openIdArr, v.OpenId) } detail, e := models.GetCygxApplyRecordByMobile(usermobile) if e != nil { err = errors.New("GetCygxApplyRecordByMobile" + e.Error()) return } //redirectUrl := "" //如果是潜在客户就推送可查看详情的模版消息 if detail.ApplyMethod == 2 { redirectUrl = utils.WX_MSG_PATH_APPLY_DETAIL + strconv.Itoa(detail.ApplyRecordId) } keywords := []string{keyword1, keyword2, keyword3, keyword4, keyword5} sendInfo := new(SendWxCategoryTemplate) sendInfo.Keywords = keywords sendInfo.TemplateId = utils.WxCategoryMsgTemplateIdApplyXzs sendInfo.RedirectTarget = 4 sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD sendInfo.OpenIdArr = openIdArr sendInfo.RedirectUrl = redirectUrl err = SendCategoryTemplateMsg(sendInfo) return } // 有人支付成功时,模板消息发送给王芳、沈涛和推荐人(没有推荐人时给销售) func SendWxCategoryMsgWxPay(orderDetail *order.CygxOrder) (err error) { orderCode := orderDetail.OrderCode defer func() { if err != nil { go utils.SendAlarmMsg(fmt.Sprint("有人支付成功时,模板消息发送给王芳、沈涛和推荐人(没有推荐人时给销售)发送模板消息失败:orderCode", orderCode, "Err:", err.Error()), 2) } }() var keyword1 string var keyword2 string var keyword3 string var keyword4 string var redirectUrl string var mobile string mobile = utils.WxMsgTemplateIdAskMsgMobilePublic keyword1 = orderDetail.CompanyName if orderDetail.RealName == "" { keyword2 = "--" } else { keyword2 = orderDetail.RealName } switch orderDetail.GoodsId { case 1: keyword3 = fmt.Sprint(orderDetail.OrderMoney, "元—畅读日卡") case 2: keyword3 = fmt.Sprint(orderDetail.OrderMoney, "元—畅读月卡") default: keyword3 = fmt.Sprint(orderDetail.OrderMoney, "元—", orderDetail.SourceTitle) } //判断跳转文章、活动、专栏详情 switch orderDetail.Source { case utils.CYGX_OBJ_ARTICLE: redirectUrl = utils.WX_MSG_PATH_YX_ARTICLE_DETAIL + strconv.Itoa(orderDetail.SourceId) case utils.CYGX_OBJ_ACTIVITY: redirectUrl = utils.WX_MSG_PATH_YX_ACTIVITY_DETAIL + strconv.Itoa(orderDetail.SourceId) case utils.CYGX_OBJ_ACTIVITYVIDEO: redirectUrl = utils.WX_MSG_PATH_YX_ACTIVITY_DETAIL + strconv.Itoa(orderDetail.SourceId) case utils.CYGX_OBJ_ACTIVITYVOICE: redirectUrl = utils.WX_MSG_PATH_YX_ACTIVITY_DETAIL + strconv.Itoa(orderDetail.SourceId) case utils.CYGX_OBJ_YANXUANSPECIAL: redirectUrl = utils.WX_MSG_PATH_YX_SPECIAL_DETAIL + strconv.Itoa(orderDetail.SourceId) } //判断是否有分享人或者销售 var adminId int if orderDetail.ShareId > 0 { adminId = orderDetail.ShareId } else if orderDetail.SellerId > 0 { adminId = orderDetail.SellerId } if adminId > 0 { adminDetail, e := models.GetSellerByAdminId(adminId) if e != nil { err = errors.New("GetYanxuanSpecialFollowUserById, Err: " + e.Error()) return } mobile += "," + adminDetail.Mobile } openIdList, err := models.GetMfyxWxOpenIdByMobileList(mobile) if err != nil && err.Error() != utils.ErrNoRow() { return err } if len(openIdList) == 0 { err = nil return } keyword4 = time.Now().Format(utils.FormatDateTimeMinute2) openIdArr := make([]string, 0) for _, v := range openIdList { openIdArr = append(openIdArr, v.OpenId) } keyword1 = utils.TruncateActivityNameString(keyword1) keyword3 = utils.TruncateActivityNameString(keyword3) keywords := []string{keyword1, keyword2, keyword3, keyword4} sendInfo := new(SendWxCategoryTemplate) sendInfo.Keywords = keywords sendInfo.TemplateId = utils.WxMsgCategoryTemplateIdCompanyApprovalMessageRai sendInfo.RedirectTarget = 4 sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD sendInfo.OpenIdArr = openIdArr sendInfo.RedirectUrl = redirectUrl err = SendCategoryTemplateMsg(sendInfo) if err != nil { return } return } // 专栏文章有留言时,模板消息通知作者 func SendWxCategoryMsgByYanxuanSpecialMassageByAuthor(content string, yanxuanSpecialBySpeciaDetail *models.CygxYanxuanSpecialItem) (err error) { defer func() { if err != nil { go utils.SendAlarmMsg(fmt.Sprint(" 作者回复读者留言时,模板消息通知作者 发送模板消息失败:yanxuanSpecialBySpeciaDetail , messageId :", "Err:", err.Error()), 2) } }() var keyword1 string var keyword2 string var redirectUrl string var mobile string //mobile = utils.WxMsgTemplateIdAskMsgMobilePublic keyword1 = "专栏文章有新的留言" keyword2 = utils.TruncateActivityNameString(content) userInfo, e := models.GetWxUserItemByUserId(yanxuanSpecialBySpeciaDetail.UserId) if e != nil { err = errors.New("GetMfyxWxOpenIdByMobileList, Err: " + e.Error()) return } mobile = userInfo.Mobile if utils.RunMode == "release" { redirectUrl = utils.WX_MSG_PATH_YX_SPECIAL_MASSAGE_MANAGE_LIST } openIdList, e := models.GetMfyxWxOpenIdByMobileList(mobile) if e != nil && e.Error() != utils.ErrNoRow() { err = errors.New("GetMfyxWxOpenIdByMobileList, Err: " + e.Error()) return } if len(openIdList) == 0 { err = nil return } openIdArr := make([]string, 0) for _, v := range openIdList { openIdArr = append(openIdArr, v.OpenId) } keyword1 = utils.TruncateActivityNameString(keyword1) keywords := []string{keyword1, keyword2} sendInfo := new(SendWxCategoryTemplate) sendInfo.Keywords = keywords sendInfo.TemplateId = utils.WxMsgCategoryTemplateIdAddMessage sendInfo.RedirectTarget = 4 sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD sendInfo.OpenIdArr = openIdArr sendInfo.RedirectUrl = redirectUrl err = SendCategoryTemplateMsg(sendInfo) if err != nil { return } return } // 作者回复读者留言时,模板消息通知读者 func SendWxCategoryMsgByYanxuanSpecialMassage(parentId, messageId int, content string, yanxuanSpecialBySpeciaDetail *models.CygxYanxuanSpecialItem) (err error) { defer func() { if err != nil { go utils.SendAlarmMsg(fmt.Sprint(" 作者回复读者留言时,模板消息通知读者 发送模板消息失败:SendWxCategoryMsgByYanxuanSpecialMassage , messageId :", messageId, "Err:", err.Error()), 2) } }() var keyword1 string var keyword2 string var keyword3 string var keyword4 string var redirectUrl string var mobile string //mobile = utils.WxMsgTemplateIdAskMsgMobilePublic keyword1 = yanxuanSpecialBySpeciaDetail.Title keyword2 = "作者回复了您的留言" keyword3 = utils.TruncateActivityNameString(content) keyword4 = time.Now().Format(utils.FormatDateTimeMinute2) if utils.RunMode == "release" { redirectUrl = utils.WX_MSG_PATH_YX_SPECIAL_MASSAGE_DETAIL + strconv.Itoa(messageId) } messagerDetail, e := models.GetCygxYanxuanSpecialMessagerDetailById(parentId) if e != nil { err = errors.New("GetMfyxWxOpenIdByMobileList, Err: " + e.Error()) return } mobile = messagerDetail.Mobile openIdList, e := models.GetMfyxWxOpenIdByMobileList(mobile) if e != nil && e.Error() != utils.ErrNoRow() { err = errors.New("GetMfyxWxOpenIdByMobileList, Err: " + e.Error()) return } if len(openIdList) == 0 { err = nil return } openIdArr := make([]string, 0) for _, v := range openIdList { openIdArr = append(openIdArr, v.OpenId) } keyword1 = utils.TruncateActivityNameString(keyword1) keyword3 = utils.TruncateActivityNameString(keyword3) keywords := []string{keyword1, keyword2, keyword3, keyword4} sendInfo := new(SendWxCategoryTemplate) sendInfo.Keywords = keywords sendInfo.TemplateId = utils.WxMsgCategoryTemplateIdActivityPublishApply sendInfo.RedirectTarget = 4 sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD sendInfo.OpenIdArr = openIdArr sendInfo.RedirectUrl = redirectUrl err = SendCategoryTemplateMsg(sendInfo) if err != nil { return } return }