Browse Source

fix:测试模板

zqbao 9 months ago
parent
commit
d8bddbf6c0
4 changed files with 25 additions and 21 deletions
  1. 21 12
      controllers/wechat.go
  2. 1 1
      models/user.go
  3. 0 5
      models/user_record.go
  4. 3 3
      utils/config.go

+ 21 - 12
controllers/wechat.go

@@ -9,6 +9,7 @@ import (
 	"fmt"
 	"html"
 	"strconv"
+	"time"
 )
 
 type WeChatController struct {
@@ -88,13 +89,15 @@ func (this *WeChatController) SendTemplateMsg() {
 		br.ErrMsg = "获取用户列表失败,Err:" + err.Error()
 		return
 	}
+	userUnionMap := make(map[int]string)
+	for _, v := range userList {
+		userUnionMap[v.UserId] = v.UnionId
+	}
 	var openIds []*services.OpenIdList
 	if IsPublic {
 		var unionIds []string
-		userMap := make(map[int]*models.User)
 		for _, v := range userList {
 			unionIds = append(unionIds, v.UnionId)
-			userMap[v.UserId] = v
 		}
 		// 给所有人,发送模板消息
 		records, err := models.GetUserRecordByUnionids(unionIds)
@@ -124,11 +127,12 @@ func (this *WeChatController) SendTemplateMsg() {
 				sendUserIds = append(sendUserIds, permissionMap[v]...)
 			}
 		}
-		unionIds, err := models.GetUserUnionIdListByIds(sendUserIds)
-		if err != nil {
-			br.Msg = "获取用户信息失败"
-			br.ErrMsg = "获取用户unionId失败,Err:" + err.Error()
-			return
+		sendUserIds = utils.Unique(sendUserIds)
+		var unionIds []string
+		for _, v := range sendUserIds {
+			if id, ok := userUnionMap[v]; ok {
+				unionIds = append(unionIds, id)
+			}
 		}
 		records, err := models.GetUserRecordByUnionids(unionIds)
 		if err != nil {
@@ -145,11 +149,15 @@ func (this *WeChatController) SendTemplateMsg() {
 	}
 	sendMap := make(map[string]interface{})
 	sendMap["template_id"] = utils.TEMPLATE_ID_BY_PRODUCT
-	sendMap["first"] = fmt.Sprintf("Hi,最新一期%s已上线,欢迎查看", report.ClassifyNameFirst)
-	sendMap["keyword1"] = map[string]interface{}{"value": fmt.Sprintf("弘则%s", report.ClassifyNameSecond), "color": "#173177"}
-	sendMap["keyword2"] = map[string]interface{}{"value": report.ContentSub, "color": "#173177"}
-	sendMap["keyword3"] = map[string]interface{}{"value": report.PublishTime, "color": "#173177"}
-	sendMap["keyword4"] = map[string]interface{}{"value": report.Abstract, "color": "#173177"}
+	// sendMap["first"] = fmt.Sprintf("Hi,最新一期%s已上线,欢迎查看", report.ClassifyNameFirst)
+	// sendMap["keyword1"] = map[string]interface{}{"value": fmt.Sprintf("弘则%s", report.ClassifyNameSecond), "color": "#173177"}
+	// sendMap["keyword2"] = map[string]interface{}{"value": report.ContentSub, "color": "#173177"}
+	sendMap["first"] = "测试first"
+	sendMap["keyword1"] = "测试k1"
+	sendMap["keyword2"] = "测试k2"
+	sendMap["remark"] = time.Now().Format("2006-01-02")
+	// sendMap["keyword3"] = map[string]interface{}{"value": report.PublishTime, "color": "#173177"}
+	// sendMap["keyword4"] = map[string]interface{}{"value": report.Abstract, "color": "#173177"}
 	var WxAppId = `wxb059c872d79b9967`
 	// var WxAppSecret = `1737c73e9f69a21de1a345b8f0800258`
 	var wxAppPath string
@@ -162,6 +170,7 @@ func (this *WeChatController) SendTemplateMsg() {
 		"appid":    WxAppId,
 		"pagepath": wxAppPath,
 	}
+	alarm_msg.SendAlarmMsg(fmt.Sprintf("发送模板消息,报告id:%d", report.Id), 1)
 	err = services.SendMultiTemplateMsg(sendMap, openIds, 1)
 	if err != nil {
 		br.Msg = "发送失败"

+ 1 - 1
models/user.go

@@ -63,7 +63,7 @@ func ModifyUserRegisterStatus(userId int, status bool, registerTime, modifyTime
 
 func GetUserBySubscribe() (user []*User, err error) {
 	o := orm.NewOrm()
-	sql := `SELECT * FROM user WHERE is_subscribed=1 AND is_registered=1 `
+	sql := `SELECT * FROM user WHERE is_subscribed=1 AND is_registered=1 AND union_id IS NOT NULL `
 	_, err = o.Raw(sql).QueryRows(&user)
 	return
 }

+ 0 - 5
models/user_record.go

@@ -36,14 +36,9 @@ func GetUserRecordByUnionids(unionIds []string) (items []*UserRecord, err error)
 		return
 	}
 	sql := `SELECT * FROM user_record WHERE union_id IN (`
-	// var idsStr string
 	for range unionIds {
 		sql += "?,"
 	}
-	// if len(unionIds) > 0 {
-	// 	idsStr = strings.Join(unionIds, ",")
-	// 	sql += idsStr
-	// }
 	sql = sql[:len(sql)-1]
 	sql += `) `
 	_, err = orm.NewOrm().Raw(sql, unionIds).QueryRows(&items)

+ 3 - 3
utils/config.go

@@ -101,13 +101,13 @@ func init() {
 	DW_WX_APPID = config["dw_wx_appid"]
 	DW_WX_APP_SECRET = config["dw_wx_app_secret"]
 	TEMPLATE_ID_BY_PRODUCT = config["template_id_by_product"]
-	// 仅测试
-	TEMPLATE_ID_BY_PRODUCT = `-YjuPOB7Fqd-S3ilabYa6wvjDY9aXmeEfPN6DCiy-EY`
 
 	// 测试微信配置
 	// WX_APPID = config["wx_appid"]
 	// WX_APP_SECRET = config["wx_app_secret"]
-	WX_APPID = "wx9b5d7291e581233a"
+	// 仅测试
+	TEMPLATE_ID_BY_PRODUCT = `CB7bOl7f3viMG4s1uhRo7WM0Jbx3WvodKuIZ8A_z8fM`
+	WX_APPID = "wx9b5d7291e581233a" // 投研公众号
 	WX_APP_SECRET = "f4d52e34021eee262dce9682b31f8861"
 	AlarmMsgUrl = "http://127.0.0.1:8606/api/alarm/send"
 	// redis缓存配置