瀏覽代碼

Merge branch 'fix/subscribe' into debug

Roc 1 年之前
父節點
當前提交
a022a29845
共有 3 個文件被更改,包括 177 次插入3 次删除
  1. 147 2
      controllers/wechat.go
  2. 15 1
      models/user_record.go
  3. 15 0
      utils/config.go

+ 147 - 2
controllers/wechat.go

@@ -8,6 +8,7 @@ import (
 	"hongze/hongze_api/services"
 	"hongze/hongze_api/utils"
 	"strconv"
+	"strings"
 	"time"
 )
 
@@ -399,7 +400,8 @@ func (this *WechatCommonController) Notify() {
 			switch item.Event {
 			case "subscribe":
 				fmt.Println("关注")
-				go models.UserSubscribe(1, openId)
+				go subscribe(openId)
+
 				break
 			case "unsubscribe":
 				fmt.Println("取消关注")
@@ -412,6 +414,54 @@ func (this *WechatCommonController) Notify() {
 				utils.FileLog.Info("wechat notify event:" + item.Event)
 			}
 			this.Ctx.WriteString(xmlTpl)
+		} else if item.MsgType == "text" {
+			textXmlTpl := `<xml>
+						  <ToUserName><![CDATA[%s]]></ToUserName>
+						  <FromUserName><![CDATA[%s]]></FromUserName>
+						  <CreateTime>%s</CreateTime>
+						  <MsgType><![CDATA[text]]></MsgType>
+						  <Content><![CDATA[%s]]></Content>
+						</xml>`
+			createTime := strconv.FormatInt(time.Now().Unix(), 10)
+			classifyArr := utils.ClassifyArr
+			var flag bool
+			for _, v := range classifyArr {
+				if strings.Contains(v, item.Content) || strings.Contains(item.Content, v) {
+					flag=true
+				}
+			}
+			if flag {
+				contactMsg=`请点击研究报告-FICC研报,查看报告`
+				textXmlTpl = fmt.Sprintf(textXmlTpl, item.FromUserName, utils.WxId, createTime, contactMsg)
+				this.Ctx.WriteString(textXmlTpl)
+				return
+			}
+			reportNameArr := utils.ReportNameArr
+			for _, v := range reportNameArr {
+				if strings.Contains(v, item.Content) || strings.Contains(item.Content, v) {
+					flag=true
+				}
+			}
+			if flag {
+				contactMsg=`请点击研究报告-FICC研报-研报(左上),查看报告`
+				textXmlTpl = fmt.Sprintf(textXmlTpl, item.FromUserName, utils.WxId, createTime, contactMsg)
+				this.Ctx.WriteString(textXmlTpl)
+				return
+			}
+
+			if strings.Contains("解绑", item.Content) || strings.Contains(item.Content, "解绑") ||
+				strings.Contains("手机号", item.Content) || strings.Contains(item.Content, "手机号") {
+				flag=true
+			}
+
+			if flag {
+				contactMsg=`请通过电话联系我们或者联系销售人员处理`
+				textXmlTpl = fmt.Sprintf(textXmlTpl, item.FromUserName, utils.WxId, createTime, contactMsg)
+				this.Ctx.WriteString(textXmlTpl)
+				return
+			}
+			returnResult = xmlTpl
+			this.Ctx.WriteString(returnResult)
 		} else {
 			returnResult = xmlTpl
 		}
@@ -428,4 +478,99 @@ type Notify struct {
 	MsgType      string `xml:"MsgType"`
 	Event        string `xml:"Event"`
 	EventKey     string `xml:"EventKey"`
-}
+	Content      string `xml:"Content"`
+}
+
+// subscribe 关注后的处理逻辑
+func subscribe(openId string) {
+	accessToken, err := services.WxGetAccessToken()
+	if err != nil {
+		fmt.Println("获取access_token失败,err:" + err.Error())
+		return
+	}
+	userRecord, err := models.GetUserRecordByOpenId(openId)
+	if err != nil && err.Error() != utils.ErrNoRow() {
+		fmt.Println("通过openid获取user_record记录失败,err:" + err.Error())
+		return
+	}
+	err = nil
+
+	// openId已存在
+	if userRecord != nil {
+		if userRecord.UserId > 0 { //已经绑定了的话,那么就去修改用户状态
+			models.UserSubscribe(1, openId)
+		} else {
+			// 没有绑定的话,那么校验下unionid,然后再去修改
+			unionId := userRecord.UnionId
+			if unionId == `` {
+				wxUserItem, err := services.WxGetUserInfo(openId, accessToken)
+				if err != nil {
+					fmt.Println("获取用户信息失败,err:" + err.Error())
+					return
+				}
+				if wxUserItem.Unionid != `` {
+					unionId = wxUserItem.Unionid
+				}
+			}
+
+			updateCol := make([]string, 0)
+			userRecord.Subscribe = 1
+			userRecord.SubscribeTime = time.Now()
+			updateCol = append(updateCol, "Subscribe")
+			if unionId != `` {
+				userRecord.UnionId = unionId
+				// 通过unionid获取已绑定用户的user_record信息
+				bindUserRecord, _ := models.GetBindUserRecordByUnionId(unionId)
+				if bindUserRecord != nil {
+					userRecord.UserId = bindUserRecord.UserId
+					userRecord.RealName = bindUserRecord.RealName
+					userRecord.BindAccount = bindUserRecord.BindAccount
+					updateCol = append(updateCol, "UserId", "RealName", "BindAccount")
+				}
+			}
+			err = userRecord.Update(updateCol)
+			if err != nil {
+				fmt.Println("关注后,通过openid更新user_record异常,ERR:", err)
+			}
+		}
+
+		return
+	}
+
+	// 没有记录,那么需要获取下unionid
+	wxUserItem, err := services.WxGetUserInfo(openId, accessToken)
+	if err != nil {
+		fmt.Println("获取用户信息失败,err:" + err.Error())
+		return
+	}
+	newUserRecord := &models.UserRecord{
+		UserRecordId:   0,
+		OpenId:         openId,
+		UnionId:        wxUserItem.Unionid,
+		Subscribe:      1,
+		SubscribeTime:  time.Now(),
+		NickName:       wxUserItem.Nickname,
+		Sex:            wxUserItem.Sex,
+		Province:       wxUserItem.Province,
+		City:           wxUserItem.City,
+		Country:        wxUserItem.Country,
+		Headimgurl:     wxUserItem.Headimgurl,
+		CreateTime:     time.Now(),
+		CreatePlatform: 1,
+		SessionKey:     "",
+	}
+	if wxUserItem.Unionid != `` {
+		// 通过unionid获取已绑定用户的user_record信息
+		bindUserRecord, _ := models.GetBindUserRecordByUnionId(wxUserItem.Unionid)
+		if bindUserRecord != nil {
+			newUserRecord.UserId = bindUserRecord.UserId
+			newUserRecord.RealName = bindUserRecord.RealName
+			newUserRecord.BindAccount = bindUserRecord.BindAccount
+		}
+	}
+	_, err = models.AddUserRecord(newUserRecord)
+	if err != nil {
+		fmt.Println("关注后,添加user_record信息失败,err:" + err.Error())
+		return
+	}
+}

+ 15 - 1
models/user_record.go

@@ -10,6 +10,7 @@ type UserRecord struct {
 	OpenId         string    `description:"用户openid,最大长度:32"`
 	UnionId        string    `description:"用户unionid,最大长度:64"`
 	Subscribe      int       `description:"是否关注"`
+	SubscribeTime     time.Time `description:""`
 	NickName       string    `descritpion:"用户昵称,最大长度:32"`
 	RealName       string    `descritpion:"用户实际名称,最大长度:32"`
 	BindAccount    string    `descritpion:"绑定时的账号,最大长度:128"`
@@ -38,10 +39,11 @@ func GetUserRecordByUserId(userId, platform int) (item *UserRecord, err error) {
 	return
 }
 
-//添加用户关系
+// AddUserRecord 添加用户关系
 func AddUserRecord(record *UserRecord) (recordId int64, err error) {
 	o := orm.NewOrm()
 	recordId, err = o.Insert(record)
+	record.UserRecordId = int(recordId)
 	return
 }
 
@@ -75,3 +77,15 @@ func GetUserThirdRecordByUserId(userId int) (item *UserRecord, err error) {
 	err = orm.NewOrm().Raw(sql, userId).QueryRow(&item)
 	return
 }
+
+// GetBindUserRecordByUnionId 通过unionid获取已绑定用户的user_record信息
+func GetBindUserRecordByUnionId(unionId string) (item *UserRecord, err error) {
+	sql := `SELECT * FROM user_record WHERE union_id=? AND user_id >0`
+	err = orm.NewOrm().Raw(sql, unionId).QueryRow(&item)
+	return
+}
+func (item *UserRecord) Update(cols []string) (err error) {
+	o := orm.NewOrm()
+	_, err = o.Update(item, cols...)
+	return
+}

+ 15 - 0
utils/config.go

@@ -8,6 +8,7 @@ import (
 	"github.com/beego/beego/v2/server/web"
 	"github.com/go-redis/redis/v8"
 	"strconv"
+	"strings"
 )
 
 var (
@@ -45,6 +46,11 @@ var (
 	WxPcPlatform          int    //用户来源,需要入库,用来保存该用户来自哪个平台,默认是:3
 )
 
+var (
+	ClassifyArr []string
+	ReportNameArr  []string
+)
+
 func init() {
 	tmpRunMode, err := web.AppConfig.String("run_mode")
 	if err != nil {
@@ -115,6 +121,7 @@ func init() {
 
 	// 初始化redis缓存
 	initRedis(config)
+	initAutoReply()
 }
 
 // initRedis 初始化redis配置
@@ -152,6 +159,14 @@ func initRedis(config map[string]string) {
 	Redis = client
 }
 
+//自动回复
+func initAutoReply() {
+	keyWord := `宏观经济、利率债、原油、PTA、MEG、织造终端、甲醇、聚烯烃、沥青、苯乙烯、聚酯、钢材、铁矿、玻璃纯碱、PVC、双焦、铜/铝、镍/不锈钢、锌、钴锂、策略`
+	ClassifyArr = strings.Split(keyWord, "、")
+	reportNameStr := `比如晨报、周报、宏观报告、从宏观看商品、大宗商品、数据点评、知白守黑日评、有声有色日评、化里化外日评、股债日评、草根调研`
+	ReportNameArr = strings.Split(reportNameStr, "、")
+}
+
 //http://webapi.brilliantstart.cn/api/
 //http://webapi.brilliantstart.cn/swagger/
 //http://139.196.122.219:8603/swagger/