123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- package services
- import (
- "bytes"
- "encoding/json"
- "errors"
- "fmt"
- "hongze/hongze_mfyx/models"
- "hongze/hongze_mfyx/utils"
- "io/ioutil"
- "net/http"
- "strconv"
- "time"
- )
- // 发送报告提问消息提醒
- func SendWxMsgWithAsk(name, askTime, askMsg, title string, openIdList []*models.OpenIdList, articleId 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("发送报告提问消息提醒模版消息失败,用户名", name, "文章ID:", articleId), 2)
- utils.FileLog.Info("发送模版消息失败,Err:%s", err.Error())
- }
- fmt.Println("line 21", err, msg)
- }()
- var first string
- var keyword1 string
- var keyword2 string
- var keyword3 string
- var keyword4 string
- var remark string
- first = "研选报告有新的提问,请及时处理"
- keyword1 = name
- keyword3 = askTime
- keyword2 = ""
- 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)
- if err != nil {
- return
- }
- 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)
- }
- }()
- //accessToken, err := models.GetWxAccessTokenByXzs()
- //if err != nil {
- // msg = "GetWxAccessToken Err:" + err.Error()
- // return
- //}
- //if accessToken == "" {
- // msg = "accessToken is empty"
- // return
- //}
- //var first string
- //first = keyWord1
- ////keyword1 := keyWord2
- ////keyword1 := keyWord2
- //remark := "点击查看报告详情"
- //sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
- //templateId := utils.WxMsgTemplateIdArticleUserRemind
- //sendMap := make(map[string]interface{})
- //sendData := make(map[string]interface{})
- //sendMap["template_id"] = templateId
- //sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxAppId, "pagepath": utils.WX_MSG_PATH_ARTICLE_DETAIL + strconv.Itoa(articleId)}
- //sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
- //sendData["keyword1"] = map[string]interface{}{"value": keyWord1, "color": "#173177"}
- //sendData["keyword2"] = map[string]interface{}{"value": keyWord2, "color": "#173177"}
- //sendData["remark"] = map[string]interface{}{"value": remark, "color": "#173177"}
- //sendMap["data"] = sendData
- //WxSendTemplateMsg(sendUrl, sendMap, openIdList, strconv.Itoa(articleId), utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD)
- 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
- }
- // 发送用户操作活动消息提醒
- func SendWxMsgWithActivityUserRemind(keyword1, keyword2 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)
- }
- }()
- //accessToken, err := models.GetWxAccessTokenByXzs()
- //if err != nil {
- // msg = "GetWxAccessToken Err:" + err.Error()
- // return
- //}
- //if accessToken == "" {
- // msg = "accessToken is empty"
- // return
- //}
- //var first string
- ////first = keyWord1
- ////keyword1 := keyWord2
- //sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
- //templateId := utils.WxMsgTemplateIdArticleUserRemind
- //sendMap := make(map[string]interface{})
- //sendData := make(map[string]interface{})
- //sendMap["template_id"] = templateId
- //sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxAppId, "pagepath": "activityPages/activityDetail/activityDetail?id=" + strconv.Itoa(activityId)}
- //sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
- //sendData["keyword1"] = map[string]interface{}{"value": keyWord1, "color": "#173177"}
- //sendData["keyword2"] = map[string]interface{}{"value": keyWord2, "color": "#173177"}
- //sendMap["data"] = sendData
- //WxSendTemplateMsg(sendUrl, sendMap, openIdList, strconv.Itoa(activityId), utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD)
- 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 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 models.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 SendCommentWxTemplateMsg(title, companyName, userName, sellerName, createTime, content string, openIdList []*models.OpenIdList, articleId int) (err error) {
- 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:活动音频
- if utils.RunMode != "release" {
- redirectUrl = utils.WX_MSG_PATH_ARTICLE_DETAIL + strconv.Itoa(articleInfo.ArticleId)
- } else {
- 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
- }
|