package services import ( "bytes" "encoding/json" "fmt" "hongze/hongze_mobile_admin/models/tables/report" "hongze/hongze_mobile_admin/models/tables/user_template_record" "hongze/hongze_mobile_admin/models/tables/wx_user" "hongze/hongze_mobile_admin/services/alarm_msg" "hongze/hongze_mobile_admin/utils" "io/ioutil" "net/http" "strconv" "time" ) func SendWxTemplateMsg(reportId int) (err error) { var msg string defer func() { if err != nil { go alarm_msg.SendAlarmMsg(fmt.Sprintf("发送模版消息失败,Err:%s,%s", err.Error(), msg), 3) //go utils.SendEmail("发送模版消息失败"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers) utils.FileLog.Info("发送模版消息失败,Err:%s,%s", err.Error(), msg) } if msg != "" { utils.FileLog.Info("发送模版消息失败,msg:%s", msg) } }() utils.FileLog.Info("%s", "services SendMsg") report, err := report.GetReportById(reportId) if err != nil { msg = "GetReportInfo Err:" + err.Error() return } if report == nil { utils.FileLog.Info("报告信息不存在") return } accessToken, err := WxGetAccessToken() if err != nil { msg = "GetWxAccessToken Err:" + err.Error() return } if accessToken == "" { msg = "accessToken is empty" return } //获取openid列表 openIdStr := WxUsersGet() openIdList, err := wx_user.GetOpenIdList(openIdStr) if err != nil { msg = "get openIdList err:" + err.Error() return } fmt.Println("openIdListCount:", len(openIdList)) sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken fmt.Println("send start") utils.FileLog.Info("send start") //if report.MsgIsSend == 0 { sendMap := make(map[string]interface{}) sendData := make(map[string]interface{}) redirectUrl := utils.TemplateRedirectUrl + strconv.Itoa(reportId) keyword1 := "弘则日度点评" first := "Hi,最新一期日度点评已上线,欢迎查看" keyword2 := report.Title //keyword3 := report.PublishTime.Format(utils.FormatDateTime) keyword3 := report.PublishTime keyword4 := report.Abstract 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["keyword3"] = map[string]interface{}{"value": keyword3, "color": "#173177"} sendData["keyword4"] = map[string]interface{}{"value": keyword4, "color": "#173177"} sendMap["template_id"] = utils.TemplateIdByProduct sendMap["url"] = redirectUrl sendMap["data"] = sendData sendTemplateMsg(sendUrl, sendMap, openIdList, strconv.Itoa(reportId), utils.TEMPLATE_MSG_REPORT) //} fmt.Println("send end") utils.FileLog.Info("send end") return } func sendTemplateMsg(sendUrl string, sendMap map[string]interface{}, items []*wx_user.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()) utils.FileLog.Info(fmt.Sprintf("SendTemplateMsgOne Marshal Err:%s", err.Error())) return err } err = toSendTemplateMsg(sendUrl, data, resource, sendType, v) if err != nil { fmt.Println("send err:", err.Error()) utils.FileLog.Info(fmt.Sprintf("ToSendTemplateMsg Err:%s", err.Error())) } } return } func toSendTemplateMsg(sendUrl string, data []byte, resource string, sendType int, dataItem *wx_user.OpenIdList) (err error) { utils.FileLog.Info("Send:" + string(data)) 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("SendResult Unmarshal Err:%s", err.Error()) return err } //新增模板消息推送记录 { sendStatus := 0 if templateResponse.Errcode == 0 { sendStatus = 1 } else { sendStatus = 0 } tr := &user_template_record.UserTemplateRecord{ Id: 0, UserId: dataItem.UserId, OpenId: dataItem.OpenId, Resource: resource, SendData: string(data), Result: string(body), CreateDate: time.Now().Format(utils.FormatDate), CreateTime: time.Now().Format(utils.FormatDateTime), SendStatus: sendStatus, SendType: sendType, } go func() { err = user_template_record.AddUserTemplateRecord(tr) if err != nil { utils.FileLog.Info(fmt.Sprintf("AddUserTemplateRecord Err:%s", err.Error())) } }() } return } type SendTemplateResponse struct { Errcode int `json:"errcode"` Errmsg string `json:"errmsg"` MsgID int `json:"msgid"` } // SendCompanyApplyWxTemplateMsg 发送待办消息 func SendCompanyApplyWxTemplateMsg(mobile, redirectUrl, wxAppPath string, wxMsgMap map[int]string) (err error) { var msg string defer func() { if err != nil { go alarm_msg.SendAlarmMsg(fmt.Sprintf("发送模版消息失败,Err:%s,%s", err.Error(), msg), 3) //go utils.SendEmail("发送模版消息失败"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers) utils.FileLog.Info("发送模版消息失败,Err:%s,%s", err.Error(), msg) } if msg != "" { utils.FileLog.Info("发送模版消息失败,msg:%s", msg) } }() utils.FileLog.Info("%s", "services SendMsg") accessToken, err := WxGetAccessToken() if err != nil { msg = "GetWxAccessToken Err:" + err.Error() return } if accessToken == "" { msg = "accessToken is empty" return } utils.FileLog.Info("mobile:%s", mobile) //获取openid列表 openIdStr := WxUsersGet() openIdList, err := wx_user.GetOpenIdListByMobile(mobile, openIdStr) if err != nil { msg = "get openIdList err:" + err.Error() return } utils.FileLog.Info("openIdListCount:%s", len(openIdList)) //fmt.Println("openIdListCount:", len(openIdList)) if len(openIdList) > 0 && utils.TemplateIdByCompanyApply != "" { utils.FileLog.Info("start send") sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken fmt.Println("send start") utils.FileLog.Info("send start") sendMap := make(map[string]interface{}) sendData := make(map[string]interface{}) var first, keyword1, keyword2, keyword3, remark string if tmpStr, ok := wxMsgMap[0]; ok { first = tmpStr } else { first = "Hi,有新的订单需要审批" } if tmpStr, ok := wxMsgMap[1]; ok { keyword1 = tmpStr } if tmpStr, ok := wxMsgMap[2]; ok { keyword2 = tmpStr } if tmpStr, ok := wxMsgMap[3]; ok { keyword3 = tmpStr } if tmpStr, ok := wxMsgMap[4]; ok { remark = tmpStr } 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["keyword3"] = map[string]interface{}{"value": keyword3, "color": "#173177"} sendData["remark"] = map[string]interface{}{"value": remark, "color": "#173177"} sendMap["template_id"] = utils.TemplateIdByCompanyApply sendMap["url"] = redirectUrl sendMap["data"] = sendData //小程序信息 if wxAppPath != "" { sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxAppId2, "pagepath": wxAppPath} } sendTemplateMsg(sendUrl, sendMap, openIdList, mobile, utils.TEMPLATE_MSG_APPLY) } utils.FileLog.Info("send end") return } // 路演->销售收到处理结果 func SendWxMsgWithRoadshowDetailResult(first, keyword1, keyword2, remark, mobile, redirectUrl, wxAppPath string) (err error) { var msg string defer func() { if err != nil { go alarm_msg.SendAlarmMsg(fmt.Sprintf("发送模版消息失败,Err:%s,%s", err.Error(), msg), 3) //go utils.SendEmail("发送模版消息失败"+time.Now().Format("2006-01-02 15:04:05"), msg+";Err:"+err.Error(), utils.EmailSendToUsers) utils.FileLog.Info("发送模版消息失败,Err:%s,%s", err.Error(), msg) } if msg != "" { utils.FileLog.Info("发送模版消息失败,msg:%s", msg) } }() utils.FileLog.Info("%s", "services SendMsg") accessToken, err := WxGetAccessToken() if err != nil { msg = "GetWxAccessToken Err:" + err.Error() return } if accessToken == "" { msg = "accessToken is empty" return } utils.FileLog.Info("mobile:%s", mobile) //获取openid列表 openIdStr := WxUsersGet() openIdList, err := wx_user.GetOpenIdListByMobile(mobile, openIdStr) if err != nil { msg = "get openIdList err:" + err.Error() return } utils.FileLog.Info("openIdListCount:%s", len(openIdList)) //fmt.Println("openIdListCount:", len(openIdList)) if len(openIdList) > 0 && utils.TemplateIdByCompanyApply != "" { utils.FileLog.Info("start send") sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken fmt.Println("send start") utils.FileLog.Info("send start") sendMap := make(map[string]interface{}) sendData := make(map[string]interface{}) 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.WxMsgTemplateIdWithRoadshowDetailResult sendMap["url"] = redirectUrl sendMap["data"] = sendData //小程序信息 if wxAppPath != "" { sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxAppId2, "pagepath": wxAppPath} } sendTemplateMsg(sendUrl, sendMap, openIdList, wxAppPath, utils.TEMPLATE_MSG_ACTIVITY_APPOINTMENT) } utils.FileLog.Info("send end") return } // 路演->研究员收到待处理的申请 func SendWxMsgWithRoadshowPending(first, keyword1, keyword2, keyword3, keyword4, remark, wxAppPath, mobile string) (err error) { //utils.WxMsgTemplateIdWithRoadshowPending 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 alarm_msg.SendAlarmMsg("发送模版消息失败,Err:"+err.Error()+msg, 3) utils.FileLog.Info("发送模版消息失败,Err:" + err.Error() + msg) } if msg != "" { utils.FileLog.Info("发送模版消息失败,msg:%s", msg) } }() utils.FileLog.Info("%s", "services SendMsg") accessToken, err := WxGetAccessToken() if err != nil { msg = "GetWxAccessToken Err:" + err.Error() return } if accessToken == "" { msg = "accessToken is empty" return } //获取openid列表 openIdStr := WxUsersGet() openIdList, err := wx_user.GetOpenIdListByMobile(mobile, openIdStr) if err != nil { msg = "get openIdList err:" + err.Error() return } utils.FileLog.Info("openIdListCount:%s", len(openIdList)) //fmt.Println("openIdListCount:", len(openIdList)) if len(openIdList) > 0 && utils.TemplateIdByCompanyApply != "" { utils.FileLog.Info("start send") sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken fmt.Println("send start") utils.FileLog.Info("send start") sendMap := make(map[string]interface{}) sendData := make(map[string]interface{}) 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["keyword3"] = map[string]interface{}{"value": keyword3, "color": "#173177"} sendData["keyword4"] = map[string]interface{}{"value": keyword4, "color": "#173177"} sendData["remark"] = map[string]interface{}{"value": remark, "color": "#173177"} sendMap["template_id"] = utils.WxMsgTemplateIdWithRoadshowPending sendMap["data"] = sendData //小程序信息 if wxAppPath != "" { sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxAppId2, "pagepath": wxAppPath} } sendTemplateMsg(sendUrl, sendMap, openIdList, wxAppPath, utils.TEMPLATE_MSG_ACTIVITY_APPOINTMENT) } utils.FileLog.Info("send end") return } // 路演->研究员收到活动删除通知 func SendWxMsgWithRoadshowDeleteNotice(first, keyword1, keyword2, remark, wxAppPath, mobile string) (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 alarm_msg.SendAlarmMsg("发送模版消息失败"+time.Now().Format("2006-01-02 15:04:05")+";Msg:"+msg+";Err:"+err.Error(), 3) utils.FileLog.Info("发送模版消息失败,Err:%s,%s", err.Error(), msg) } if msg != "" { utils.FileLog.Info("发送模版消息失败,msg:%s", msg) } }() utils.FileLog.Info("%s", "services SendMsg") accessToken, err := WxGetAccessToken() if err != nil { msg = "GetWxAccessToken Err:" + err.Error() return } if accessToken == "" { msg = "accessToken is empty" return } //获取openid列表 openIdStr := WxUsersGet() openIdList, err := wx_user.GetOpenIdListByMobile(mobile, openIdStr) if err != nil { msg = "get openIdList err:" + err.Error() return } utils.FileLog.Info("openIdListCount:%s", len(openIdList)) //fmt.Println("openIdListCount:", len(openIdList)) if len(openIdList) > 0 && utils.TemplateIdByCompanyApply != "" { utils.FileLog.Info("start send") 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.WxMsgTemplateIdWithRoadshowDeleteNotice 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"} //小程序信息 if wxAppPath != "" { sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxAppId2, "pagepath": wxAppPath} } sendMap["data"] = sendData sendTemplateMsg(sendUrl, sendMap, openIdList, wxAppPath, utils.TEMPLATE_MSG_ACTIVITY_APPOINTMENT) } utils.FileLog.Info("send end") return } // SendYbQuestionDistributeWxMsg 推送研报小程序模板消息-问答社区分配 func SendYbQuestionDistributeWxMsg(questionId, userId int, openid, questionTitle string) (err error) { var errMsg string defer func() { if err != nil { alarmMsg := fmt.Sprintf("SendMiniProgramReportWxMsg-推送问答社区分配模版消息失败; QuestionId: %d; Err: %s; Msg: %s", questionId, err.Error(), errMsg) utils.FileLog.Error(alarmMsg) go alarm_msg.SendAlarmMsg(alarmMsg, 3) } }() accessToken, err := WxGetAccessToken() if err != nil { errMsg = "GetWxAccessToken Err:" + err.Error() return } if accessToken == "" { errMsg = "accessToken is empty" return } openIdList := make([]*wx_user.OpenIdList, 0) openIdList = append(openIdList, &wx_user.OpenIdList{ OpenId: openid, UserId: userId, }) 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 := "待回复" 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 wxAppPath != "" { sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxYbAppId, "pagepath": wxAppPath} } err = sendTemplateMsg(sendUrl, sendMap, openIdList, wxAppPath, utils.TEMPLATE_MSG_YB_COMMUNITY_QUESTION) return }