Browse Source

fix:推送消息

Roc 2 years ago
parent
commit
4a665dba99

+ 23 - 0
models/tables/user_template_record/user_template_record.go

@@ -0,0 +1,23 @@
+package user_template_record
+
+import "github.com/beego/beego/v2/client/orm"
+
+type UserTemplateRecord struct {
+	Id         int `orm:"column(id);pk"`
+	UserId     int
+	OpenId     string
+	Resource   string
+	SendData   string
+	Result     string
+	CreateDate string
+	CreateTime string
+	SendStatus int
+	SendType   int
+}
+
+// AddUserTemplateRecord 添加记录
+func AddUserTemplateRecord(item *UserTemplateRecord) (err error) {
+	o := orm.NewOrm()
+	_, err = o.Insert(item)
+	return
+}

+ 2 - 1
models/tables/wx_user/wx_user.go

@@ -24,11 +24,12 @@ type WxUser struct {
 
 type OpenIdList struct {
 	OpenId string
+	UserId int
 }
 
 //获取所有的用户openid列表
 func GetOpenIdList(openIdStr string) (items []*OpenIdList, err error) {
-	sql := `SELECT DISTINCT ur.open_id FROM wx_user AS wu 
+	sql := `SELECT DISTINCT ur.open_id,wu.user_id FROM wx_user AS wu 
           INNER JOIN company AS c ON c.company_id = wu.company_id 
           INNER JOIN company_product AS d ON c.company_id=d.company_id
 		INNER join user_record  as ur on wu.user_id=ur.user_id

+ 96 - 19
services/wechat_send_msg.go

@@ -5,6 +5,7 @@ import (
 	"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"
@@ -78,14 +79,15 @@ func SendWxTemplateMsg(reportId int) (err error) {
 	sendMap["template_id"] = utils.TemplateIdByProduct
 	sendMap["url"] = redirectUrl
 	sendMap["data"] = sendData
-	sendTemplateMsg(sendUrl, sendMap, openIdList)
+	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) (err error) {
+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)
@@ -94,7 +96,7 @@ func sendTemplateMsg(sendUrl string, sendMap map[string]interface{}, items []*wx
 			utils.FileLog.Info(fmt.Sprintf("SendTemplateMsgOne Marshal Err:%s", err.Error()))
 			return err
 		}
-		err = toSendTemplateMsg(sendUrl, data)
+		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()))
@@ -103,8 +105,8 @@ func sendTemplateMsg(sendUrl string, sendMap map[string]interface{}, items []*wx
 	return
 }
 
-func toSendTemplateMsg(sendUrl string, data []byte) (err error) {
-	utils.FileLog.Info("Send:"+string(data))
+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 {
@@ -113,13 +115,42 @@ func toSendTemplateMsg(sendUrl string, data []byte) (err error) {
 	defer resp.Body.Close()
 
 	body, _ := ioutil.ReadAll(resp.Body)
-	utils.FileLog.Info("SendResult:"+string(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
 }
 
@@ -204,15 +235,14 @@ func SendCompanyApplyWxTemplateMsg(mobile, redirectUrl, wxAppPath string, wxMsgM
 		if wxAppPath != "" {
 			sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxAppId2, "pagepath": wxAppPath}
 		}
-		sendTemplateMsg(sendUrl, sendMap, openIdList)
+		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) {
+func SendWxMsgWithRoadshowDetailResult(first, keyword1, keyword2, remark, mobile, redirectUrl, wxAppPath string) (err error) {
 	var msg string
 	defer func() {
 		if err != nil {
@@ -254,7 +284,6 @@ func SendWxMsgWithRoadshowDetailResult(first, keyword1, keyword2, remark,mobile,
 		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"}
@@ -267,22 +296,21 @@ func SendWxMsgWithRoadshowDetailResult(first, keyword1, keyword2, remark,mobile,
 		if wxAppPath != "" {
 			sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxAppId2, "pagepath": wxAppPath}
 		}
-		sendTemplateMsg(sendUrl, sendMap, openIdList)
+		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) {
+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)
+			utils.FileLog.Info("发送模版消息失败,Err:" + err.Error() + msg)
 		}
 		if msg != "" {
 			utils.FileLog.Info("发送模版消息失败,msg:%s", msg)
@@ -317,7 +345,6 @@ func SendWxMsgWithRoadshowPending(first, keyword1, keyword2, keyword3, keyword4,
 		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"}
@@ -331,15 +358,14 @@ func SendWxMsgWithRoadshowPending(first, keyword1, keyword2, keyword3, keyword4,
 		if wxAppPath != "" {
 			sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxAppId2, "pagepath": wxAppPath}
 		}
-		sendTemplateMsg(sendUrl, sendMap, openIdList)
+		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) {
+func SendWxMsgWithRoadshowDeleteNotice(first, keyword1, keyword2, remark, wxAppPath, mobile string) (err error) {
 	var msg string
 	defer func() {
 		if err != nil {
@@ -388,8 +414,59 @@ func SendWxMsgWithRoadshowDeleteNotice(first, keyword1, keyword2, remark, wxAppP
 			sendMap["miniprogram"] = map[string]interface{}{"appid": utils.WxAppId2, "pagepath": wxAppPath}
 		}
 		sendMap["data"] = sendData
-		sendTemplateMsg(sendUrl, sendMap, openIdList)
+		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
+}

+ 6 - 5
services/yb/community_question.go

@@ -11,6 +11,7 @@ import (
 	"hongze/hongze_mobile_admin/models/tables/sys_role_admin"
 	"hongze/hongze_mobile_admin/models/tables/user_record"
 	"hongze/hongze_mobile_admin/models/tables/wx_user"
+	"hongze/hongze_mobile_admin/services"
 	"hongze/hongze_mobile_admin/services/alarm_msg"
 	"hongze/hongze_mobile_admin/utils"
 	"strconv"
@@ -318,11 +319,11 @@ func SendMsgToReplier(questionId int) (errMsg string, err error) {
 		updateCols = append(updateCols, "ReplierOpenid")
 		item.ReplierOpenid = userRecord.OpenId
 	}
-	// todo 推送消息
-	//if e := services.SendYbQuestionDistributeWxMsg(item.CommunityQuestionId, item.ReplierUserId, item.ReplierOpenid, item.QuestionContent); e != nil {
-	//	err = errors.New("推送模板消息失败, Err:" + e.Error())
-	//	return
-	//}
+
+	if e := services.SendYbQuestionDistributeWxMsg(item.CommunityQuestionId, item.ReplierUserId, item.ReplierOpenid, item.QuestionContent); e != nil {
+		err = errors.New("推送模板消息失败, Err:" + e.Error())
+		return
+	}
 	// 更新问答信息
 	updateCols = append(updateCols, "ModifyTime", "MsgSendStatus")
 	item.ModifyTime = time.Now().Local()

+ 46 - 27
utils/config.go

@@ -35,9 +35,12 @@ var (
 	WxAppSecret2 string
 	WxPlatform2  int //用户来源,需要入库,用来保存该用户来自哪个平台,默认是:1
 
+	WxYbAppId string //弘则研报小程序APPID
+
 	WxMsgTemplateIdWithRoadshowPending      string //路演->研究员收到待处理的申请
 	WxMsgTemplateIdWithRoadshowDetailResult string //路演->销售收到处理结果
 	WxMsgTemplateIdWithRoadshowDeleteNotice string //路演->研究员收到活动删除通知
+	WxMsgTemplateIdWithYbCommunityQuestion  string // 研报小程序->问答社区回复通知
 )
 
 var (
@@ -115,43 +118,17 @@ func init() {
 	WxId2 = `wx67b68e39913e511e`
 	WxAppSecret2 = `660b0375f701a19220bb8a662b41c77c`
 	WxPlatform2 = 5
+	WxYbAppId = `wxb059c872d79b9967` //弘则研报小程序
 
 	if RunMode == "release" {
-		WxAppId = "wx4a844c734d8c8e56"
-		WxAppSecret = "26c586e7ccb3c575433f0f37797b3eeb"
-		WxId = "gh_b67e0049fb8c"
-		TemplateIdByProduct = "Cp2wF8gvBtxyWV4DeYuI172oqwyYXVRSm3AyJO42d84"
-		TemplateRedirectUrl = "https://ficc.hzinsights.com/reportdtl?id="
-		WxPlatform = 1
-		TemplateIdByCompanyApply = "ZKcOfNIWBpwHJxpptufHIK1mp2nIwkT3cxub-35cFqI"
-		TemplateCompanyApplyRedirectUrl = "https://ficc.hzinsights.com/approval/approval/list"
-
 		//oss
 		STATIC_DIR = "/home/static/imgs/"
 		Endpoint = "oss-cn-shanghai-internal.aliyuncs.com"
 
-		//路演
-		WxMsgTemplateIdWithRoadshowPending = "PaoDanHGlt1kFw5q-4_ipJSwO3FyZpxSSNg4rwB7YCk"      //路演->研究员收到待处理的申请
-		WxMsgTemplateIdWithRoadshowDetailResult = "dYg6iHooRq74PyCXmw_Ns7qdJZmbtLoKS2p2FKeaXl0" //路演->销售收到处理结果
-		WxMsgTemplateIdWithRoadshowDeleteNotice = "dYg6iHooRq74PyCXmw_Ns7qdJZmbtLoKS2p2FKeaXl0" //路演->研究员收到活动删除通知
 	} else {
-		WxAppId = "wx9b5d7291e581233a"
-		WxAppSecret = "f4d52e34021eee262dce9682b31f8861"
-		WxId = "gh_5dc508325c6f"
-		TemplateIdByProduct = "-YjuPOB7Fqd-S3ilabYa6wvjDY9aXmeEfPN6DCiy-EY"
-		TemplateRedirectUrl = "http://rddpweb.brilliantstart.cn/reportdtl?id="
-		WxPlatform = 1
-		TemplateIdByCompanyApply = "eTalI1LbiT_B0mTaBeDTlfSMITenK8dQIgEB5yqjjvA"
-		TemplateCompanyApplyRedirectUrl = "http://advisoryadmin.brilliantstart.cn/approval/approval/list"
-
 		//oss配置
 		STATIC_DIR = "static/imgs/"
 		Endpoint = "oss-cn-shanghai.aliyuncs.com"
-
-		//路演
-		WxMsgTemplateIdWithRoadshowPending = "qfNuops-sKrfIkbA7U97A7gSrX03mUpoEpJksRUdloo"      //路演->研究员收到待处理的申请
-		WxMsgTemplateIdWithRoadshowDetailResult = "CB7bOl7f3viMG4s1uhRo7WM0Jbx3WvodKuIZ8A_z8fM" //路演->销售收到处理结果
-		WxMsgTemplateIdWithRoadshowDeleteNotice = "CB7bOl7f3viMG4s1uhRo7WM0Jbx3WvodKuIZ8A_z8fM" //路演->研究员收到活动删除通知
 	}
 
 	tmpLibreOfficePath, err := web.AppConfig.String("libreOfficePath")
@@ -170,6 +147,9 @@ func init() {
 
 	//上海crm开放api配置
 	CrmOpenApiConfig()
+
+	//微信配置
+	wxConfig()
 }
 
 // CrmOpenApiConfig 上海crm开放api配置
@@ -185,6 +165,45 @@ func CrmOpenApiConfig() {
 	}
 }
 
+func wxConfig() {
+	if RunMode == "release" {
+		WxAppId = "wx4a844c734d8c8e56"
+		WxAppSecret = "26c586e7ccb3c575433f0f37797b3eeb"
+		WxId = "gh_b67e0049fb8c"
+		TemplateIdByProduct = "Cp2wF8gvBtxyWV4DeYuI172oqwyYXVRSm3AyJO42d84"
+		TemplateRedirectUrl = "https://ficc.hzinsights.com/reportdtl?id="
+		WxPlatform = 1
+		TemplateIdByCompanyApply = "ZKcOfNIWBpwHJxpptufHIK1mp2nIwkT3cxub-35cFqI"
+		TemplateCompanyApplyRedirectUrl = "https://ficc.hzinsights.com/approval/approval/list"
+
+		//路演
+		WxMsgTemplateIdWithRoadshowPending = "PaoDanHGlt1kFw5q-4_ipJSwO3FyZpxSSNg4rwB7YCk"      //路演->研究员收到待处理的申请
+		WxMsgTemplateIdWithRoadshowDetailResult = "dYg6iHooRq74PyCXmw_Ns7qdJZmbtLoKS2p2FKeaXl0" //路演->销售收到处理结果
+		WxMsgTemplateIdWithRoadshowDeleteNotice = "dYg6iHooRq74PyCXmw_Ns7qdJZmbtLoKS2p2FKeaXl0" //路演->研究员收到活动删除通知
+
+		// 研报小程序
+		WxMsgTemplateIdWithYbCommunityQuestion = "dYg6iHooRq74PyCXmw_Ns7qdJZmbtLoKS2p2FKeaXl0" // 研报小程序->问答社区回复通知
+	} else {
+		WxAppId = "wx9b5d7291e581233a"
+		WxAppSecret = "f4d52e34021eee262dce9682b31f8861"
+		WxId = "gh_5dc508325c6f"
+		TemplateIdByProduct = "-YjuPOB7Fqd-S3ilabYa6wvjDY9aXmeEfPN6DCiy-EY"
+		TemplateRedirectUrl = "http://rddpweb.brilliantstart.cn/reportdtl?id="
+		WxPlatform = 1
+		TemplateIdByCompanyApply = "eTalI1LbiT_B0mTaBeDTlfSMITenK8dQIgEB5yqjjvA"
+		TemplateCompanyApplyRedirectUrl = "http://advisoryadmin.brilliantstart.cn/approval/approval/list"
+
+		//路演
+		WxMsgTemplateIdWithRoadshowPending = "qfNuops-sKrfIkbA7U97A7gSrX03mUpoEpJksRUdloo"      //路演->研究员收到待处理的申请
+		WxMsgTemplateIdWithRoadshowDetailResult = "CB7bOl7f3viMG4s1uhRo7WM0Jbx3WvodKuIZ8A_z8fM" //路演->销售收到处理结果
+		WxMsgTemplateIdWithRoadshowDeleteNotice = "CB7bOl7f3viMG4s1uhRo7WM0Jbx3WvodKuIZ8A_z8fM" //路演->研究员收到活动删除通知
+
+		// 研报小程序
+		WxMsgTemplateIdWithYbCommunityQuestion = "CB7bOl7f3viMG4s1uhRo7WM0Jbx3WvodKuIZ8A_z8fM" // 研报小程序->问答社区回复通知
+	}
+
+}
+
 //http://webapi.brilliantstart.cn/api/
 //http://webapi.brilliantstart.cn/swagger/
 //http://8.136.199.33:8607/swagger/

+ 18 - 0
utils/constants.go

@@ -108,3 +108,21 @@ const (
 	_
 	USER_RECORD_PLATFORM_YB // 研报小程序
 )
+
+//模板消息推送类型
+const (
+	TEMPLATE_MSG_REPORT                = iota + 1 //日度点评报告推送
+	TEMPLATE_MSG_INDEX                            //指标更新
+	TEMPLATE_MSG_APPLY                            //审批通知
+	TEMPLATE_MSG_RECEIVE                          //销售领取客户通知
+	TEMPLATE_MSG_CYGX_ACTIVITY_CACLE              //查研观向活动取消通知
+	TEMPLATE_MSG_CYGX_ACTIVITY_UPDATE             //查研观向活动更改时间通知
+	TEMPLATE_MSG_CYGX_ARTICLE                     //关注的作者发布报告通知
+	TEMPLATE_MSG_CYGX_DAY_REPORT                  //发送日报(周报、双周报、月报)
+	TEMPLATE_MSG_ACTIVITY_APPOINTMENT             //活动预约/报名时间通知
+	TEMPLATE_MSG_RECEIVE_PERMISSION               //销售"添加客户/领取客户"权限变更通知
+	TEMPLATE_MSG_YB_COMMUNITY_QUESTION            //研报问答社区
+	TEMPLATE_MSG_CYGX_ACTIVITY_ADD                //查研观向小程序活动更新推送 12
+	TEMPLATE_MSG_CYGX_ARTICLE_ADD                 //查研观向报告更新推送  13
+	TEMPLATE_MSG_YB_PRICE_DRIVEN                  //研报价格驱动 14
+)