Browse Source

限制一个手机号/邮箱只能绑定一个微信号

hsun 11 months ago
parent
commit
329a517c25
2 changed files with 29 additions and 4 deletions
  1. 23 4
      controller/user/user.go
  2. 6 0
      models/tables/wx_user/query.go

+ 23 - 4
controller/user/user.go

@@ -6,6 +6,7 @@ import (
 	userLogic "hongze/hongze_yb/logic/user"
 	"hongze/hongze_yb/models/request/user"
 	userResp "hongze/hongze_yb/models/response/user"
+	"hongze/hongze_yb/models/tables/wx_user"
 	userService "hongze/hongze_yb/services/user"
 	"hongze/hongze_yb/utils"
 	"strings"
@@ -35,6 +36,12 @@ func Login(c *gin.Context) {
 		response.Fail("参数异常", c)
 		return
 	}
+	if req.LoginType != 1 && req.LoginType != 2 {
+		response.Fail("无效的登录方式", c)
+		return
+	}
+
+	var bindAccount string
 	if req.LoginType == 1 {
 		//手机登录
 		if req.Mobile == "" {
@@ -42,7 +49,9 @@ func Login(c *gin.Context) {
 			return
 		}
 		req.Mobile = strings.Trim(req.Mobile, " ")
-	} else if req.LoginType == 2 {
+		bindAccount = req.Mobile
+	}
+	if req.LoginType == 2 {
 		//邮箱登录
 		if req.Email == "" {
 			response.Fail("邮箱不能为空,请输入邮箱", c)
@@ -52,10 +61,20 @@ func Login(c *gin.Context) {
 			response.Fail("邮箱格式错误,请重新输入", c)
 			return
 		}
-	} else {
-		response.Fail("无效的登录方式", c)
+		bindAccount = req.Email
+	}
+
+	// 校验账号是否已有绑定微信
+	bindUser, e := wx_user.GetWxUserBindExist(bindAccount, openId)
+	if e != nil && e != utils.ErrNoRow {
+		response.FailMsg("登录失败", "获取账号是否已绑定微信失败, Err: "+e.Error(), c)
+		return
+	}
+	if bindUser != nil && bindUser.UserID > 0 {
+		response.Fail("登录失败, 该账号已绑定其他微信", c)
 		return
 	}
+
 	token, newUserInfo, err, errMsg := userService.BindWxUser(openId, req.Mobile, req.Email, req.VerifyCode, req.LoginType, req.AreaNum, 1)
 	if err != nil {
 		if errMsg == "" {
@@ -317,4 +336,4 @@ func GetTopTab(c *gin.Context) {
 	}
 
 	response.OkData("获取成功", tabBarList, c)
-}
+}

+ 6 - 0
models/tables/wx_user/query.go

@@ -76,3 +76,9 @@ ORDER BY FIELD(c.company_id, 16) DESC, ur.user_record_id ASC  `
 	err = global.DEFAULT_MYSQL.Raw(sql, varietyTagId).Scan(&items).Error
 	return
 }
+
+// GetWxUserBindExist 获取账号是否已绑定微信
+func GetWxUserBindExist(account, openId string) (wxUser *WxUser, err error) {
+	err = global.DEFAULT_MYSQL.Where("bind_account = ? AND open_id <> ?", account, openId).First(&wxUser).Error
+	return
+}