Browse Source

消息推送,主体模板修改

xingzai 3 years ago
parent
commit
0ef07f6342

+ 2 - 2
controllers/article.go

@@ -407,7 +407,7 @@ func (this *ArticleController) InterviewApply() {
 			}
 			sellerItem, _ := models.GetSellerByCompanyId(user.CompanyId)
 			if sellerItem != nil && sellerItem.AdminId > 0 && user.Mobile != "" {
-				openIpItem, _ := models.GetUserRecordByUserId(sellerItem.UserId, 1)
+				openIpItem, _ := models.GetUserRecordByUserIdByXzs(sellerItem.UserId, 1)
 				if openIpItem != nil && openIpItem.OpenId != "" {
 					go services.SendInterviewApplyTemplateMsg(user.RealName, sellerItem.CompanyName, mobile, article.Title, openIpItem.OpenId)
 				}
@@ -429,7 +429,7 @@ func (this *ArticleController) InterviewApply() {
 			}
 			sellerItem, _ := models.GetSellerByCompanyId(user.CompanyId)
 			if sellerItem != nil && sellerItem.AdminId > 0 && user.Mobile != "" {
-				openIpItem, _ := models.GetUserRecordByUserId(sellerItem.UserId, 1)
+				openIpItem, _ := models.GetUserRecordByUserIdByXzs(sellerItem.UserId, 1)
 				if openIpItem != nil && openIpItem.OpenId != "" {
 					go services.SendInterviewApplyCancelTemplateMsg(user.RealName, sellerItem.CompanyName, mobile, article.Title, openIpItem.OpenId)
 				}

+ 4 - 1
controllers/user.go

@@ -828,7 +828,7 @@ func (this *UserController) ApplyTryOut() {
 		return
 	}
 
-	if applyCount > 0 {
+	if applyCount < 0 {
 		br.Msg = "您已提交申请,请耐心等待。"
 		br.IsSendEmail = false
 		return
@@ -926,6 +926,9 @@ func (this *UserController) ApplyTryOut() {
 
 	cnf, _ := models.GetConfigByCode("tpl_msg")
 	if cnf != nil {
+		if req.ApplyMethod != 1 && sellerItem != nil {
+			cnf.ConfigValue = sellerItem.Mobile
+		}
 		openIpItem, _ := models.GetUserRecordByMobile(1, cnf.ConfigValue)
 		if openIpItem != nil && openIpItem.OpenId != "" {
 			fmt.Println("推送消息", req.RealName, req.CompanyName, mobile, openIpItem.OpenId, applyMethod)

+ 2 - 1
models/activity.go

@@ -301,12 +301,13 @@ func GetActivitySendMsgListAll(endDate string) (items []*WxMsgCygxActivityList,
 	sql := `SELECT
 	s.id,
 	s.fail_type,
-	c.open_id,
+	cr.open_id,
 	a.*
 FROM
 	cygx_activity AS a
 	INNER JOIN cygx_activity_signup AS s ON s.activity_id = a.activity_id
 	INNER   JOIN user_record AS c ON c.bind_account = s.mobile 
+	INNER JOIN cygx_user_record AS cr ON cr.union_id = c.union_id 
 WHERE
 	1 = 1 
 	AND s.is_send_wx_msg = 0 

+ 12 - 1
models/user_record.go

@@ -46,6 +46,15 @@ func GetUserRecordByUserId(userId, platform int) (item *UserRecord, err error) {
 	return
 }
 
+//根据用户id和平台id获取用户关系
+func GetUserRecordByUserIdByXzs(userId, platform int) (item *UserRecord, err error) {
+	sql := `SELECT cr.open_id FROM user_record  as u
+			INNER JOIN cygx_user_record AS cr ON cr.union_id = c.union_id 
+			WHERE user_id=? AND create_platform = ?`
+	err = orm.NewOrm().Raw(sql, userId, platform).QueryRow(&item)
+	return
+}
+
 //添加用户关系
 func AddUserRecord(record *UserRecord) (recordId int64, err error) {
 	o := orm.NewOrm()
@@ -87,7 +96,9 @@ func ModifyUserRecordSessionKey(openId, sessionKey string) (err error) {
 
 //根据用户id和平台id获取用户关系
 func GetUserRecordByMobile(platform int, bindAccount string) (item *UserRecord, err error) {
-	sql := `SELECT * FROM user_record WHERE create_platform=? AND bind_account = ?`
+	sql := `SELECT cr.open_id FROM user_record  as u 
+			INNER JOIN cygx_user_record AS cr ON cr.union_id = u.union_id 
+			WHERE create_platform=? AND bind_account = ?`
 	err = orm.NewOrm().Raw(sql, platform, bindAccount).QueryRow(&item)
 	return
 }

+ 37 - 0
models/wechat.go

@@ -81,6 +81,43 @@ func GetWxToken() (item *WxAccessToken, err error) {
 	return
 }
 
+func GetWxAccessTokenByXzs() (accessTokenStr string, err error) {
+	//缓存校验
+	cacheKey := "xygxxzs_wxtoken"
+	accessTokenStr, err = utils.Rc.RedisString(cacheKey)
+	if accessTokenStr != "" && err == nil {
+		fmt.Println("redis:", accessTokenStr)
+		return
+	} else {
+		WxAccessToken, errWx := GetWxTokenByXzs()
+		if errWx != nil {
+			fmt.Println(errWx)
+			return
+		}
+		err = errWx
+		accessTokenStr = WxAccessToken.AccessToken
+		utils.Rc.Put(cacheKey, WxAccessToken.AccessToken, time.Second*7000)
+	}
+	return
+}
+
+//获取小助手的微信Token
+func GetWxTokenByXzs() (item *WxAccessToken, err error) {
+	getUrl := "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + utils.WxPublicIdXzs + "&secret=" + utils.WxPublicSecretXzs
+	result, err := http.Get(getUrl)
+	if err != nil {
+		return
+	}
+	err = json.Unmarshal(result, &item)
+	if err != nil {
+		fmt.Println("GetWxToken Unmarshal Err:%s", err.Error())
+		return
+	}
+	if item.Errmsg != "" {
+	}
+	return
+}
+
 type WxAccessToken struct {
 	AccessToken  string `json:"access_token"`
 	ExpiresIn    int    `json:"expires_in"`

+ 3 - 1
models/wx_template_msg.go

@@ -37,7 +37,9 @@ func GetWxOpenIdList() (items []*OpenIdList, err error) {
 
 func GetWxOpenIdByMobileList(mobile string) (items []*OpenIdList, err error) {
 	//sql := `SELECT * FROM user_record WHERE bind_account IN (` + utils.WxMsgTemplateIdAskMsgMobile + `) AND create_platform = 1`
-	sql := `SELECT * FROM user_record WHERE bind_account IN (` + mobile + `) AND create_platform = 1`
+	sql := `SELECT cr.* FROM user_record  as c
+			INNER JOIN cygx_user_record AS cr ON cr.union_id = c.union_id
+			WHERE bind_account IN (` + mobile + `) AND create_platform = 1`
 	_, err = orm.NewOrm().Raw(sql).QueryRows(&items)
 	return
 }

+ 1 - 1
services/wechat.go

@@ -44,7 +44,7 @@ func WxGetUserOpenIdByCodeXzs(code string) (item *WxAccessToken, err error) {
 		return nil, err
 	}
 	requestUrl := `https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code`
-	requestUrl = fmt.Sprintf(requestUrl, utils.WxAppIdXzs, utils.WxAppSecretXzs, code)
+	requestUrl = fmt.Sprintf(requestUrl, utils.WxPublicIdXzs, utils.WxPublicSecretXzs, code)
 	result, err := http.Get(requestUrl)
 	if err != nil {
 		return nil, err

+ 7 - 6
services/wechat_send_msg.go

@@ -23,7 +23,7 @@ func SendInterviewApplyTemplateMsg(realName, companyName, mobile, articleTitle,
 		}
 	}()
 
-	accessToken, err := models.GetWxAccessToken()
+	accessToken, err := models.GetWxAccessTokenByXzs()
 	if err != nil {
 		msg = "GetWxAccessToken Err:" + err.Error()
 		return
@@ -50,7 +50,7 @@ func SendInterviewApplyTemplateMsg(realName, companyName, mobile, articleTitle,
 	sendData["keyword3"] = map[string]interface{}{"value": keyword3, "color": fontColor}
 	sendData["keyword4"] = map[string]interface{}{"value": keyword4, "color": fontColor}
 
-	sendMap["template_id"] = utils.WxMsgTemplateIdApply
+	sendMap["template_id"] = utils.WxMsgTemplateIdApplyXzs
 	sendMap["data"] = sendData
 	sendTemplateMsg(sendUrl, openId, sendMap)
 	fmt.Println("send end")
@@ -68,7 +68,7 @@ func SendInterviewApplyCancelTemplateMsg(realName, companyName, mobile, articleT
 			utils.FileLog.Info("发送模版消息失败,msg:%s", msg)
 		}
 	}()
-	accessToken, err := models.GetWxAccessToken()
+	accessToken, err := models.GetWxAccessTokenByXzs()
 	if err != nil {
 		msg = "GetWxAccessToken Err:" + err.Error()
 		return
@@ -91,7 +91,7 @@ func SendInterviewApplyCancelTemplateMsg(realName, companyName, mobile, articleT
 	sendData["keyword1"] = map[string]interface{}{"value": keyword1, "color": fontColor}
 	sendData["keyword2"] = map[string]interface{}{"value": keyword2, "color": fontColor}
 
-	sendMap["template_id"] = utils.WxMsgTemplateIdApplyCancel
+	sendMap["template_id"] = utils.WxMsgTemplateIdApplyCancelXzs
 	sendMap["data"] = sendData
 	sendTemplateMsg(sendUrl, openId, sendMap)
 	fmt.Println("send end")
@@ -109,7 +109,7 @@ func SendPermissionApplyTemplateMsg(realName, companyName, mobile, openId, apply
 			utils.FileLog.Info("发送模版消息失败,msg:%s", msg)
 		}
 	}()
-	accessToken, err := models.GetWxAccessToken()
+	accessToken, err := models.GetWxAccessTokenByXzs()
 	if err != nil {
 		msg = "GetWxAccessToken Err:" + err.Error()
 		return
@@ -136,7 +136,7 @@ func SendPermissionApplyTemplateMsg(realName, companyName, mobile, openId, apply
 	sendData["keyword3"] = map[string]interface{}{"value": keyword3, "color": fontColor}
 	sendData["keyword4"] = map[string]interface{}{"value": keyword4, "color": fontColor}
 
-	sendMap["template_id"] = utils.WxMsgTemplateIdApply
+	sendMap["template_id"] = utils.WxMsgTemplateIdApplyXzs
 	sendMap["data"] = sendData
 	sendTemplateMsg(sendUrl, openId, sendMap)
 	fmt.Println("send end")
@@ -168,6 +168,7 @@ func toSendTemplateMsg(sendUrl string, data []byte) (err error) {
 	body, _ := ioutil.ReadAll(resp.Body)
 	var templateResponse SendTemplateResponse
 	err = json.Unmarshal(body, &templateResponse)
+	fmt.Println(templateResponse)
 	utils.FileLog.Info("SendResult %s:", string(body))
 	if err != nil {
 		utils.FileLog.Info("SendResult Unmarshal Err:%s", err.Error())

+ 6 - 4
services/wx_template_msg.go

@@ -22,7 +22,7 @@ func SendWxMsgWithFrequency(first, activityName, reserveResults, activityTime, a
 		fmt.Println("line 21", err, msg)
 	}()
 	utils.FileLog.Info("%s", "services SendMsg")
-	accessToken, err := models.GetWxAccessToken()
+	accessToken, err := models.GetWxAccessTokenByXzs()
 	if err != nil {
 		msg = "GetWxAccessToken Err:" + err.Error()
 		return
@@ -33,7 +33,7 @@ func SendWxMsgWithFrequency(first, activityName, reserveResults, activityTime, a
 	}
 
 	sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
-	templateId := utils.WxMsgTemplateIdActivityApply
+	templateId := utils.WxMsgTemplateIdActivityApplyXzs
 	sendMap := make(map[string]interface{})
 	sendData := make(map[string]interface{})
 	sendMap["template_id"] = templateId
@@ -61,7 +61,8 @@ func SendWxMsgWithAsk(name, askTime, askMsg, title string, openIdList []*models.
 		fmt.Println("line 21", err, msg)
 	}()
 	utils.FileLog.Info("%s", "services SendMsg")
-	accessToken, err := models.GetWxAccessToken()
+	//accessToken, err := models.GetWxAccessToken()
+	accessToken, err := models.GetWxAccessTokenByXzs()
 	if err != nil {
 		msg = "GetWxAccessToken Err:" + err.Error()
 		return
@@ -81,7 +82,8 @@ func SendWxMsgWithAsk(name, askTime, askMsg, title string, openIdList []*models.
 	keyword4 = askMsg
 	remark = title
 	sendUrl := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
-	templateId := utils.WxMsgTemplateIdAskMsg
+	//templateId := utils.WxMsgTemplateIdAskMsg
+	templateId := utils.WxMsgTemplateIdAskMsgXzs
 	sendMap := make(map[string]interface{})
 	sendData := make(map[string]interface{})
 	sendMap["template_id"] = templateId

+ 21 - 13
utils/config.go

@@ -23,17 +23,21 @@ var (
 	WxAppId     string //查研观向小程序
 	WxAppSecret string //查研观向小程序
 
-	WxAppIdXzs     string //查研观向小助手公众号
-	WxAppSecretXzs string //查研观向小助手公众号
-
-	WxMsgTemplateIdApply           string //申请待处理
-	WxMsgTemplateIdApplyCancel     string //预约取消提醒
-	WxMsgTemplateIdPermissionApply string //预约取消提醒
-	WxMsgTemplateIdActivityApply   string //活动预约消息提醒
-	WxMsgTemplateIdActivityChange  string //活动预约变更提醒
-	WxMsgTemplateIdAskMsg          string //手机号用户【XXX】发送模板消息模板ID
-	WxMsgTemplateIdAskMsgMobile    string //手机号用户【XXX】发送模板消息
-	WxMsgTemplateIdAskMsgMobileAll string //手机号用户【XXX】发送模板消息
+	WxPublicIdXzs     string //查研观向小助手公众号
+	WxPublicSecretXzs string //查研观向小助手公众号
+
+	WxMsgTemplateIdApply            string //申请待处理
+	WxMsgTemplateIdApplyXzs         string //申请待处理(小助手)
+	WxMsgTemplateIdApplyCancel      string //预约取消提醒
+	WxMsgTemplateIdApplyCancelXzs   string //预约取消提醒(小助手)
+	WxMsgTemplateIdPermissionApply  string //预约取消提醒
+	WxMsgTemplateIdActivityApply    string //活动预约消息提醒
+	WxMsgTemplateIdActivityApplyXzs string //活动预约消息提醒(小助手)
+	WxMsgTemplateIdActivityChange   string //活动预约变更提醒
+	WxMsgTemplateIdAskMsg           string //手机号用户【XXX】发送模板消息模板ID
+	WxMsgTemplateIdAskMsgXzs        string //手机号用户【XXX】发送模板消息模板ID(小助手)
+	WxMsgTemplateIdAskMsgMobile     string //手机号用户【XXX】发送模板消息
+	WxMsgTemplateIdAskMsgMobileAll  string //手机号用户【XXX】发送模板消息
 )
 
 //微信公众号配置信息
@@ -77,8 +81,12 @@ func init() {
 		panic(Re)
 	}
 
-	WxAppIdXzs = "wxb7cb8a15abad5b8e"                   //查研观向小助手
-	WxAppSecretXzs = "4dd35cd1598b27bd1dc9a3b299b289fa" //查研观向小助手
+	WxPublicIdXzs = "wxb7cb8a15abad5b8e"                                            //查研观向小助手
+	WxPublicSecretXzs = "4dd35cd1598b27bd1dc9a3b299b289fa"                          //查研观向小助手
+	WxMsgTemplateIdActivityApplyXzs = "nsfOASdg2O5KNk8hnBvu8MFsoP9X0o8ED6yPLPvEkao" //活动预约消息提醒(小助手)
+	WxMsgTemplateIdAskMsgXzs = "IpS-yuNNQc8osCoy20jPHNkvBUyKRL1NGn7c0G9xmQA"        //手机号用户【XXX】发送模板消息模板ID(小助手)
+	WxMsgTemplateIdApplyXzs = "IpS-yuNNQc8osCoy20jPHNkvBUyKRL1NGn7c0G9xmQA"         //申请待处理(小助手)
+	WxMsgTemplateIdApplyCancelXzs = "gCSCAWNNhjkzE2V1cjbIV_Ex68R_8LM_u25qDlSKWyM"   ////预约取消提醒(小助手)
 
 	OnlineTime = "2021-06-01 00:00:01" //上线时间
 	SummaryArticleId = 1000000         //手动添加的纪要库开始ID