Browse Source

新增用户是否关注逻辑

longyu 2 years ago
parent
commit
424ffdfb18
2 changed files with 56 additions and 19 deletions
  1. 49 19
      controllers/wechat.go
  2. 7 0
      models/wx_user.go

+ 49 - 19
controllers/wechat.go

@@ -2,11 +2,13 @@ package controllers
 
 import (
 	"encoding/json"
+	"encoding/xml"
 	"fmt"
 	"hongze/hongze_api/models"
 	"hongze/hongze_api/services"
 	"hongze/hongze_api/utils"
 	"strconv"
+	"time"
 )
 
 type WechatController struct {
@@ -367,28 +369,56 @@ func (this *WechatController) GetWxSign() {
 // @Success 200 {object} models.WechatSign
 // @router /notify [get,post]
 func (this *WechatCommonController) Notify() {
-	signature := this.GetString("signature")
-	timestamp := this.GetString("timestamp")
-	nonce := this.GetString("nonce")
 	echostr := this.GetString("echostr")
-	fmt.Println("signature:", signature)
-	fmt.Println("timestamp:", timestamp)
-	fmt.Println("nonce:", nonce)
-
 	method := this.Ctx.Input.Method()
-	fmt.Println("Method:", method)
-	body := this.Ctx.Input.RequestBody
-	paramsMap := this.Ctx.Input.Params()
-	fmt.Println("paramsMap")
-	fmt.Println(paramsMap)
-	paramsJson, err := json.Marshal(paramsMap)
-	if err != nil {
-		fmt.Println("json.Marshal Err:" + err.Error())
-	}
+	if method == "POST" {
+		body := this.Ctx.Input.RequestBody
+		utils.FileLog.Info("wechat notify:" + string(body))
+		item := new(Notify)
+		err := xml.Unmarshal(body, &item)
+		if err != nil {
+			utils.FileLog.Info("xml.Unmarshal:" + err.Error())
+		}
+		contactMsg := "感谢关注弘则研究。\r\n公司地址:上海市世纪大道210号21世纪中心大厦12层1206室\r\n\r\n业务合作:\r\n电话:86-21-61645300\r\n邮箱:service@hzinsights.com\r\n邮编:200120\r\n\r\n海外业务:\r\n邮箱:yyu@hzinsights.com  "
 
-	utils.FileLog.Info("notify result:" + string(body))
-	utils.FileLog.Info("notify paramsJson result:" + string(paramsJson))
-	this.Ctx.WriteString(echostr)
+		var openId, returnResult string
+		if item.MsgType != "" {
+			openId = item.FromUserName
+		}
+		xmlTpl := `<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)
+		xmlTpl = fmt.Sprintf(xmlTpl, openId, utils.WxId, createTime, contactMsg)
+
+		if item.MsgType == "event" {
+			switch item.Event {
+			case "subscribe":
+				fmt.Println("关注")
+				go models.UserSubscribe(1, openId)
+				break
+			case "unsubscribe":
+				fmt.Println("取消关注")
+				go models.UserSubscribe(0, openId)
+				break
+			case "CLICK":
+				returnResult = xmlTpl
+				break
+			default:
+				utils.FileLog.Info("wechat notify event:" + item.Event)
+			}
+			this.Ctx.WriteString(xmlTpl)
+		} else {
+			returnResult = xmlTpl
+		}
+		this.Ctx.WriteString(returnResult)
+	} else {
+		this.Ctx.WriteString(echostr)
+	}
 }
 
 type Notify struct {

+ 7 - 0
models/wx_user.go

@@ -476,3 +476,10 @@ func ModifyWxUserNameApplyMethod(userId int, realName, note string, isNote int8,
 	_, err = o.Raw(sql, realName, note, isNote, applyMethod, userId).Exec()
 	return
 }
+
+func UserSubscribe(subscribeType int, openId string) (err error) {
+	o := orm.NewOrm()
+	sql := `UPDATE user_record SET subscribe=?,subscribe_time=NOW() WHERE open_id = ? AND create_platform=1 `
+	_, err = o.Raw(sql, subscribeType, openId).Exec()
+	return
+}