genlong 8 mēneši atpakaļ
vecāks
revīzija
1f7cf04641

+ 52 - 25
controllers/user/user_controller.go

@@ -1,7 +1,6 @@
 package user
 
 import (
-	"encoding/json"
 	"eta/eta_mini_ht_api/common/component/cache"
 	logger "eta/eta_mini_ht_api/common/component/log"
 	"eta/eta_mini_ht_api/common/exception"
@@ -299,48 +298,76 @@ func covertToUserProfile(user user.User) UserProfileReq {
 	}
 }
 
-// RefreshToken 绑定微信公众号
-// @Summary 绑定微信公众号
+// ReadMessages  获取未读消息
+// @Summary 获取未读消息
+// @Description 获取未读消息
 // @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) {
+func (u *UserController) BindGzh() {
+	controllers.Wrap(&u.BaseController, func() (result *controllers.WrapData, err error) {
+		result = u.InitWrapData("获取我的未读消息失败")
 		bindReq := new(BindGzhReq)
-		body := a.Ctx.Input.RequestBody
-		err = json.Unmarshal(body, &bindReq)
-		if err != nil {
-			logger.Error("解析参数失败,Err:" + err.Error())
-			return result, exception.New(exception.WeChatCodeEmpty)
-		}
-
+		u.GetPostParams(bindReq)
 		code := bindReq.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)
+			u.FailedResult("绑定公众号失败", result)
 			return
 		}
-		a.SuccessResult("绑定成功", IsBindGzhRes{
+		u.SuccessResult("绑定成功", IsBindGzhRes{
 			IsBind: isBind,
 		}, result)
 		return
 	})
 }
 
+//
+//// 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 {
 	IsBind bool `json:"isBind"`
 }
 
 type BindGzhReq struct {
-	Code string
+	Code string `json:"Code"`
 }

+ 2 - 2
models/wx_token.go

@@ -7,7 +7,7 @@ type WxToken struct {
 }
 
 func RegisterWxToken(wxToken *WxToken) (err error) {
-	err = Main().Create(&wxToken).Error
+	err = Main().Table("wx_token").Create(&wxToken).Error
 	return
 }
 
@@ -18,6 +18,6 @@ func UpdateWxToken(accessToken string, expiresIn int64) (err error) {
 
 // GetById 根据id获取accessToken信息
 func GetWxTokenById() (info WxToken, err error) {
-	err = Main().Unscoped().Where("id", 0).First(&info).Error
+	err = Main().Table("wx_token").Unscoped().Where("id", 0).First(&info).Error
 	return
 }

+ 1 - 3
routers/commentsRouter.go

@@ -260,9 +260,7 @@ func init() {
             Method: "BindGzh",
             Router: `/bind_gzh`,
             AllowHTTPMethods: []string{"post"},
-            MethodParams: param.Make(
-				param.New("code"),
-			),
+            MethodParams: param.Make(),
             Filters: nil,
             Params: nil})
 

+ 6 - 1
service/auth/auth_service.go

@@ -305,7 +305,7 @@ func BindWxGzh(code string) (isBind bool, err error) {
 		err = exception.New(exception.TemplateUserNotFound)
 		return
 	}
-
+	logger.Info("GetWxUserInfo:")
 	if wxUser.ErrCode != 0 {
 		logger.Error("获取微信公众号用户信息失败" + wxUser.ErrMsg)
 		err = exception.New(exception.TemplateUserNotFound)
@@ -313,6 +313,8 @@ func BindWxGzh(code string) (isBind bool, err error) {
 	}
 
 	openId := wxUser.OpenId
+
+	logger.Info("openId:" + openId)
 	wxUserDetail, err := GetWxUserDetail(openId)
 	if err != nil {
 		logger.Error("获取微信公众号用户详细信息失败" + err.Error())
@@ -322,6 +324,7 @@ func BindWxGzh(code string) (isBind bool, err error) {
 
 	unionId := wxUserDetail.UnionID
 
+	logger.Info("unionId:" + unionId)
 	user, err := userService.GetTemplateUserByGzhOpenId(openId)
 	var isAdd bool
 	if err != nil {
@@ -333,6 +336,8 @@ func BindWxGzh(code string) (isBind bool, err error) {
 		}
 	}
 
+	logger.Info("IsAdd", isAdd)
+
 	if isAdd {
 		user.GzhOpenId = openId
 		user.UnionId = unionId