123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- package services
- import (
- "encoding/json"
- "errors"
- "fmt"
- "hongze/hongze_task/models"
- "hongze/hongze_task/services/alarm_msg"
- "hongze/hongze_task/utils"
- "io/ioutil"
- "net/http"
- "strings"
- "time"
- )
- func SendWxMsgWithFrequency(first, keyword1, keyword2, remark string, openIdList []*models.OpenIdList) (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)
- utils.FileLog.Info("发送模版消息失败,Err:%s", err.Error())
- }
- fmt.Println("line 21", err, msg)
- }()
- utils.FileLog.Info("services SendMsg")
- /*accessToken, err := models.GetWxAccessToken()
- if err != nil {
- msg = "GetWxAccessToken Err:" + err.Error()
- return
- }
- if accessToken == "" {
- msg = "accessToken is empty"
- return
- }
- sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
- templateId := utils.TemplateId
- //if true{
- sendMap := make(map[string]interface{})
- sendData := make(map[string]interface{})
- sendMap["template_id"] = templateId
- //redirectUrl:="report.hzinsights.com/#/allindex/"+strconv.Itoa(v.ResearchReportId)+"/"+v.ReportType;
- //sendMap["url"]=redirectUrl
- 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)*/
- wxAppPath := ""
- openIdArr := make([]string, len(openIdList))
- for i, v := range openIdList {
- openIdArr[i] = v.OpenId
- }
- sendInfo := new(SendWxTemplate)
- sendInfo.WxAppId = utils.AdminWxAppId
- sendInfo.First = first
- sendInfo.Keyword1 = keyword1
- sendInfo.Keyword2 = keyword2
- sendInfo.RedirectUrl = wxAppPath
- sendInfo.RedirectTarget = 0
- sendInfo.TemplateId = utils.TemplateId
- sendInfo.Resource = wxAppPath
- sendInfo.OpenIdArr = openIdArr
- sendInfo.Remark = remark
- err = SendTemplateMsgV2(sendInfo)
- return
- }
- // 到期提醒模板消息
- func SendWxMsgWithCompanyRemind(first, keyword1, keyword2, remark, code string, openIdList []*models.OpenIdList) (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)
- utils.FileLog.Info("发送模版消息失败,Err:%s", err.Error())
- }
- }()
- utils.FileLog.Info("%s", "services SendMsg")
- /*accessToken, err := models.GetWxAccessToken()
- if err != nil {
- msg = "GetWxAccessToken Err:" + err.Error()
- return
- }
- if accessToken == "" {
- msg = "accessToken is empty"
- return
- }
- sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
- sendMap := make(map[string]interface{})
- sendData := make(map[string]interface{})
- sendMap["template_id"] = utils.RemindTemplateId
- sendData["first"] = map[string]interface{}{"value": first, "color": "#173177"}
- sendData["productname"] = map[string]interface{}{"value": keyword1, "color": "#173177"}
- sendData["date"] = map[string]interface{}{"value": keyword2, "color": "#173177"}
- sendData["remark"] = map[string]interface{}{"value": remark, "color": "#173177"}
- sendMap["data"] = sendData
- WxSendTemplateMsg(sendUrl, sendMap, openIdList)*/
- var wxAppPath string
- if utils.RunMode == `debug` {
- wxAppPath = `http://msgsendtest.hzinsights.com/productExpirationHint.html?code=` + code
- } else {
- wxAppPath = `https://msgsend.hzinsights.com//productExpirationHint.html?code=` + code
- }
- openIdArr := make([]string, len(openIdList))
- for i, v := range openIdList {
- openIdArr[i] = v.OpenId
- }
- sendInfo := new(SendWxTemplate)
- sendInfo.WxAppId = utils.AdminWxAppId
- sendInfo.First = first
- sendInfo.Productname = keyword1
- sendInfo.Date = keyword2
- sendInfo.RedirectUrl = wxAppPath
- sendInfo.RedirectTarget = 0
- sendInfo.TemplateId = utils.RemindTemplateId
- sendInfo.Resource = wxAppPath
- sendInfo.OpenIdArr = openIdArr
- sendInfo.Remark = remark
- err = SendTemplateMsgV2(sendInfo)
- return
- }
- // SendWxMsgWithActivityAppointmentRemind 活动预约/报名时间通知
- func SendWxMsgWithActivityAppointmentRemind(first, keyword1, keyword2, keyword3, keyword4, remark, wxAppPath string, openIdList []*models.OpenIdList) (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)
- utils.FileLog.Info(fmt.Sprintf("发送模版消息失败,Err:%s", err.Error()))
- }
- }()
- utils.FileLog.Info("%s", "services SendMsg")
- openIdArr := make([]string, len(openIdList))
- for i, v := range openIdList {
- openIdArr[i] = v.OpenId
- }
- sendInfo := new(SendWxTemplate)
- sendInfo.WxAppId = utils.WxAppId
- sendInfo.First = first
- sendInfo.Keyword1 = keyword1
- sendInfo.Keyword2 = keyword2
- sendInfo.RedirectUrl = wxAppPath
- sendInfo.RedirectTarget = 1
- sendInfo.TemplateId = utils.AppointmentRemindTemplateId
- sendInfo.Resource = wxAppPath
- sendInfo.OpenIdArr = openIdArr
- sendInfo.Remark = remark
- err = SendTemplateMsgV2(sendInfo)
- return
- }
- type SendWxTemplate struct {
- WxAppId string `description:"公众号appId"`
- First string `description:"模板消息first字段"`
- Keyword1 string `description:"模板消息keyword1字段"`
- Keyword2 string `description:"模板消息keyword2字段"`
- Keyword3 string `description:"模板消息keyword3字段"`
- Keyword4 string `description:"模板消息keyword4字段"`
- Productname string `description:"模板消息productname字段"`
- Date string `description:"模板消息date字段"`
- Remark string `description:"模板消息remark字段"`
- TemplateId string `description:"模板id"`
- RedirectUrl string `description:"跳转地址"`
- RedirectTarget int `description:"小程序跳转目标:1:弘则研报小程序,2:随手办公小程序"`
- Resource string `description:"资源唯一标识"`
- SendType int `description:"发送的消息类型:1:报告,2:指标更新提醒,3:审批通知,4:销售领取客户通知,5:活动取消通知,6活动更改时间通知,7:关注的作者发布报告通知,8:发送日报(周报、双周报、月报)模板消息,9:活动预约/报名时间通知"`
- OpenIdArr []string `description:"消息接收者openid"`
- }
- // 推送模板消息
- func SendTemplateMsgV2(sendInfo *SendWxTemplate) (err error) {
- postData, err := json.Marshal(sendInfo)
- if err != nil {
- alarm_msg.SendAlarmMsg("SendTemplateMsg json.Marshal Err:"+err.Error(), 1)
- return err
- }
- body := ioutil.NopCloser(strings.NewReader(string(postData)))
- client := &http.Client{}
- req, err := http.NewRequest("POST", utils.SendWxTemplateMsgUrl, body)
- if err != nil {
- alarm_msg.SendAlarmMsg("SendTemplateMsg 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
- }
- // SendYbVoiceBroadcastWxMsg 推送研报语音播报模板消息
- func SendYbVoiceBroadcastWxMsg(broadcastId int, sectionName, broadcastName string) (err error) {
- var msg string
- defer func() {
- if err != nil {
- errMsg := "SendYbVoiceBroadcastWxMsg-定时任务发送语音播报模版消息失败; Err: " + err.Error() + "; Msg: " + msg
- utils.FileLog.Info("%s", errMsg)
- go alarm_msg.SendAlarmMsg(errMsg, 3)
- }
- }()
- openIdList, err := models.GetOpenIdListV2()
- if err != nil {
- msg = "get openIdList err:" + err.Error()
- return
- }
- openIdArr := make([]string, 0)
- for _, v := range openIdList {
- openIdArr = append(openIdArr, v.OpenId)
- }
- first := "您好,有新的语音播报待查看"
- //keyword1 := fmt.Sprint(sectionName, ":", broadcastName)
- //keyword2 := "待查看"
- keyword1 := broadcastName
- keyword2 := fmt.Sprintf("【%s】 待查看", sectionName)
- remark := "请点击详情查看"
- wxAppPath := fmt.Sprintf("pages-voice/voiceDetail?voiceId=%d", broadcastId)
- sendInfo := new(SendWxTemplate)
- sendInfo.First = first
- sendInfo.Keyword1 = keyword1
- sendInfo.Keyword2 = keyword2
- sendInfo.RedirectUrl = wxAppPath
- sendInfo.TemplateId = utils.YbCommonTemplateId
- sendInfo.RedirectTarget = 1
- sendInfo.Resource = wxAppPath
- sendInfo.SendType = utils.TEMPLATE_MSG_YB_VOICE_BROADCAST
- sendInfo.OpenIdArr = openIdArr
- sendInfo.Remark = remark
- err = SendTemplateMsgV2(sendInfo)
- return
- }
- // SendWxMsgWithEtaBusinessRemind ETA商家-到期提醒
- func SendWxMsgWithEtaBusinessRemind(first, keyword1, keyword2, remark, code string, openIdList []*models.OpenIdList) (err error) {
- defer func() {
- if err != nil {
- tips := "ETA商家-推送到期提醒模板消息失败, Err: " + err.Error()
- utils.FileLog.Info("%s", tips)
- go alarm_msg.SendAlarmMsg(tips, 3)
- //go utils.SendEmail("发送模版消息失败"+"【"+utils.APPNAME+"】"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers)
- }
- }()
- utils.FileLog.Info("%s", "services SendMsg")
- var wxAppPath string
- if utils.RunMode == `debug` {
- wxAppPath = `http://msgsendtest.hzinsights.com/ETAExpirationHint.html?code=` + code
- } else {
- wxAppPath = `https://msgsend.hzinsights.com/ETAExpirationHint.html?code=` + code
- }
- openIdArr := make([]string, len(openIdList))
- for i, v := range openIdList {
- openIdArr[i] = v.OpenId
- }
- if len(openIdArr) == 0 {
- return
- }
- sendInfo := new(SendWxTemplate)
- sendInfo.WxAppId = utils.AdminWxAppId
- sendInfo.First = first
- sendInfo.Productname = keyword1
- sendInfo.Date = keyword2
- sendInfo.RedirectUrl = wxAppPath
- sendInfo.RedirectTarget = 0
- sendInfo.TemplateId = utils.RemindTemplateId
- sendInfo.Resource = wxAppPath
- sendInfo.OpenIdArr = openIdArr
- sendInfo.Remark = remark
- err = SendTemplateMsgV2(sendInfo)
- return
- }
- // SendYbQuestionToResearcher 给弘则FICC公众号,推送研报小程序模板消息-用户提问时,通知到研究员
- func SendYbQuestionToResearcher(questionId, adminId int, openid, questionTitle, remark string) (err error) {
- var errMsg string
- defer func() {
- if err != nil {
- alarmMsg := fmt.Sprintf("SendYbQuestionToResearcher-用户提问时,通知到研究员; QuestionId: %d; Err: %s; Msg: %s", questionId, err.Error(), errMsg)
- utils.FileLog.Error(alarmMsg)
- go alarm_msg.SendAlarmMsg(alarmMsg, 3)
- }
- }()
- //accessToken, err := models.GetWxAccessToken()
- //if err != nil {
- // errMsg = "GetWxAccessToken Err:" + err.Error()
- // return
- //}
- //if accessToken == "" {
- // errMsg = "accessToken is empty"
- // return
- //}
- openIdArr := make([]string, 0)
- openIdArr = append(openIdArr, openid)
- //sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
- //sendMap := make(map[string]interface{})
- //sendData := make(map[string]interface{})
- first := "您好,有新的提问待回复"
- keyword1 := questionTitle
- keyword2 := "待回复"
- if remark == "" {
- remark = "请点击详情尽快处理"
- keyword2 = remark
- }
- //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["template_id"] = utils.WxMsgTemplateIdWithYbCommunityQuestion
- //sendMap["data"] = sendData
- wxAppPath := fmt.Sprintf("pages-question/answerDetail?id=%d", questionId)
- if utils.RunMode == "debug" {
- // 仅测试环境测试用
- wxAppPath = "pages-report/chapterList?reportId=3800"
- }
- sendInfo := new(SendWxTemplate)
- sendInfo.WxAppId = utils.AdminWxAppId
- sendInfo.First = first
- sendInfo.Keyword1 = keyword1
- sendInfo.Keyword2 = keyword2
- sendInfo.Remark = remark
- sendInfo.RedirectUrl = wxAppPath
- sendInfo.TemplateId = utils.WxMsgTemplateIdWithYbCommunityQuestion
- sendInfo.RedirectTarget = 1
- sendInfo.Resource = wxAppPath
- sendInfo.SendType = utils.TEMPLATE_MSG_YB_COMMUNITY_QUESTION //研报问答社区
- sendInfo.OpenIdArr = openIdArr
- err = SendTemplateMsgV2(sendInfo)
- return
- }
|