Browse Source

新增微信公众号关注回调

genlong 8 tháng trước cách đây
mục cha
commit
735d548212

+ 71 - 45
controllers/user/user_controller.go

@@ -1,6 +1,7 @@
 package user
 package user
 
 
 import (
 import (
+	"encoding/xml"
 	"eta/eta_mini_ht_api/common/component/cache"
 	"eta/eta_mini_ht_api/common/component/cache"
 	logger "eta/eta_mini_ht_api/common/component/log"
 	logger "eta/eta_mini_ht_api/common/component/log"
 	"eta/eta_mini_ht_api/common/exception"
 	"eta/eta_mini_ht_api/common/exception"
@@ -9,6 +10,8 @@ import (
 	"eta/eta_mini_ht_api/service/auth"
 	"eta/eta_mini_ht_api/service/auth"
 	"eta/eta_mini_ht_api/service/user"
 	"eta/eta_mini_ht_api/service/user"
 	"fmt"
 	"fmt"
+	"strconv"
+	"time"
 )
 )
 
 
 // UserController Operations about Users
 // UserController Operations about Users
@@ -298,9 +301,9 @@ func covertToUserProfile(user user.User) UserProfileReq {
 	}
 	}
 }
 }
 
 
-// ReadMessages  获取未读消息
-// @Summary 获取未读消息
-// @Description 获取未读消息
+// ReadMessages  绑定微信公众号
+// @Summary 绑定微信公众号
+// @Description 绑定微信公众号
 // @Success 200 {object} controllers.BaseResponse
 // @Success 200 {object} controllers.BaseResponse
 // @router /bind_gzh [post]
 // @router /bind_gzh [post]
 func (u *UserController) BindGzh() {
 func (u *UserController) BindGzh() {
@@ -322,48 +325,6 @@ func (u *UserController) BindGzh() {
 	})
 	})
 }
 }
 
 
-//
-//// RefreshToken 绑定微信公众号
-//// @Summary 绑定微信公众号
-//// @Success 200 {object} controllers.BaseResponse
-//// @Description 更新token
-//// @router /bind_gzh [post]
-//func (a *UserController) BindGzh() {
-//	//controllers.Wrap(&a.BaseController, func() (result *controllers.WrapData, err error) {
-//	//	fmt.Println("307")
-//	//	//bindReq := new(BindGzhReq)
-//	//	//fmt.Println("309")
-//	//	//body := a.Ctx.Input.RequestBody
-//	//	//err = json.Unmarshal(body, &bindReq)
-//	//	//if err != nil {
-//	//	//	logger.Error("解析参数失败,Err:" + err.Error())
-//	//	//	return result, exception.New(exception.WeChatCodeEmpty)
-//	//	//}
-//	//	//
-//	//	//code := bindReq.Code
-//	//	//fmt.Println("code:" + code)
-//	//	//result = a.InitWrapData("绑定公众号失败")
-//	//	//if code == "" {
-//	//	//	logger.Error("code不能为空")
-//	//	//	return result, exception.New(exception.WeChatCodeEmpty)
-//	//	//}
-//	//	//
-//	//	//logger.Info("bindGzh code:" + code)
-//	//	//
-//	//	////刷新token
-//	//	//isBind, err := auth.BindWxGzh(code)
-//	//	//if err != nil {
-//	//	//	logger.Error("绑定公众号失败:%v", err)
-//	//	//	a.FailedResult("绑定公众号失败", result)
-//	//	//	return
-//	//	//}
-//	//	//a.SuccessResult("绑定成功", IsBindGzhRes{
-//	//	//	IsBind: isBind,
-//	//	//}, result)
-//	//	return
-//	//})
-//}
-
 type IsBindGzhRes struct {
 type IsBindGzhRes struct {
 	IsBind bool `json:"isBind"`
 	IsBind bool `json:"isBind"`
 }
 }
@@ -371,3 +332,68 @@ type IsBindGzhRes struct {
 type BindGzhReq struct {
 type BindGzhReq struct {
 	Code string `json:"Code"`
 	Code string `json:"Code"`
 }
 }
+
+// ReadMessages  微信公众号回调
+// @Summary 微信公众号回调
+// @Description 微信公众号回调
+// @Success 200 {object} controllers.BaseResponse
+// @router /notify [get,post]
+func (u *UserController) Notify() {
+	echostr := u.GetString("echostr")
+	method := u.Ctx.Input.Method()
+	if method == "POST" {
+		body := u.Ctx.Input.RequestBody
+		item := new(Notify)
+		err := xml.Unmarshal(body, &item)
+		if err != nil {
+			logger.Info("xml.Unmarshal:" + err.Error())
+		}
+		contactMsg := ""
+
+		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)
+		// WxId := "gh_5dc508325c6f" // 弘则投研公众号原始id
+		xmlTpl = fmt.Sprintf(xmlTpl, openId, "gh_b2f5b115ec8d", createTime, contactMsg)
+
+		if item.MsgType == "event" {
+			switch item.Event {
+			case "subscribe":
+				fmt.Println("关注")
+				//go subscribe(openId)
+			case "unsubscribe":
+				fmt.Println("取消关注")
+				//go unsubscribe(openId)
+			case "CLICK":
+				returnResult = xmlTpl
+			default:
+				logger.Info("wechat notify event:" + item.Event)
+			}
+			u.Ctx.WriteString(xmlTpl)
+		} else {
+			returnResult = xmlTpl
+		}
+		u.Ctx.WriteString(returnResult)
+	} else {
+		u.Ctx.WriteString(echostr)
+	}
+}
+
+type Notify struct {
+	ToUserName   string `xml:"ToUserName"`
+	FromUserName string `xml:"FromUserName"`
+	CreateTime   int    `xml:"CreateTime"`
+	MsgType      string `xml:"MsgType"`
+	Event        string `xml:"Event"`
+	EventKey     string `xml:"EventKey"`
+	Content      string `xml:"Content"`
+}

+ 0 - 40
controllers/user/wechat.go

@@ -1,40 +0,0 @@
-package user
-
-import (
-	logger "eta/eta_mini_ht_api/common/component/log"
-	"eta/eta_mini_ht_api/common/exception"
-	"eta/eta_mini_ht_api/controllers"
-)
-
-// RefreshToken 绑定微信公众号
-// @Summary 绑定微信公众号
-// @Success 200 {object} controllers.BaseResponse
-// @Description 更新token
-// @router /bind_gzh [get]
-func (a *AuthController) BindGzh(code string) {
-	controllers.Wrap(&a.BaseController, func() (result *controllers.WrapData, err error) {
-		result = a.InitWrapData("刷新token失败")
-		if code == "" {
-			logger.Error("code不能为空")
-			return result, exception.New(exception.WeChatCodeEmpty)
-		}
-
-		logger.Info("bindGzh code:" + code)
-
-		//刷新token
-		//isBind, err := auth.BindWxGzh(code)
-		//if err != nil {
-		//	logger.Error("绑定公众号失败:%v", err)
-		//	a.FailedResult("绑定公众号失败", result)
-		//	return
-		//}
-		//a.SuccessResult("绑定成功", IsBindGzhRes{
-		//	IsBind: isBind,
-		//}, result)
-		return
-	})
-}
-
-//type IsBindGzhRes struct {
-//	IsBind bool `json:"isBind"`
-//}