Переглянути джерело

Merge branch 'feature/bind_once_0327'

hsun 7 місяців тому
батько
коміт
24b514a782

+ 19 - 19
controller/pc/pc.go

@@ -567,25 +567,25 @@ QUERY_WX_USER:
 		goto QUERY_WX_USER
 	} else if wxUserErr == userService.ERR_USER_NOT_BIND {
 		// 未绑定则去查询unionId是否已经绑定了用户(其他平台,不区分平台),有相应的手机号邮箱信息则自动绑定
-		platformUser, platformErr := userService.GetFirstWxUserItemByUnionId(unionId)
-		if platformErr == nil {
-			// 当公众号用户存在时
-			if platformUser.Mobile != "" || platformUser.Email != "" {
-				// 有手机号或邮箱则绑定信息则自动绑定并新增wx_user
-				countryCode := 0
-				if platformUser.CountryCode != "" {
-					countryCode, _ = strconv.Atoi(platformUser.CountryCode)
-				}
-				tempToken, tempUser, tempErr, errMsg := userService.BindWxUser(openId, platformUser.Mobile, platformUser.Email, "", 3, countryCode, 1)
-				if tempErr != nil {
-					err = errors.New("自动绑定公众号用户失败,Err:" + tempErr.Error() + ", errMsg:" + errMsg)
-					return
-				}
-				token = tempToken
-				userId = int(tempUser.UserID)
-				isBind = true
-			}
-		}
+		//platformUser, platformErr := userService.GetFirstWxUserItemByUnionId(unionId)
+		//if platformErr == nil {
+		//	// 当公众号用户存在时
+		//	if platformUser.Mobile != "" || platformUser.Email != "" {
+		//		// 有手机号或邮箱则绑定信息则自动绑定并新增wx_user
+		//		countryCode := 0
+		//		if platformUser.CountryCode != "" {
+		//			countryCode, _ = strconv.Atoi(platformUser.CountryCode)
+		//		}
+		//		tempToken, tempUser, tempErr, errMsg := userService.BindWxUser(openId, platformUser.Mobile, platformUser.Email, "", 3, countryCode, 1)
+		//		if tempErr != nil {
+		//			err = errors.New("自动绑定公众号用户失败,Err:" + tempErr.Error() + ", errMsg:" + errMsg)
+		//			return
+		//		}
+		//		token = tempToken
+		//		userId = int(tempUser.UserID)
+		//		isBind = true
+		//	}
+		//}
 	} else if wxUserErr != nil {
 		err = wxUserErr
 		return

+ 11 - 6
controller/user/user.go

@@ -35,15 +35,22 @@ func Login(c *gin.Context) {
 		response.Fail("参数异常", c)
 		return
 	}
+	if req.LoginType != 1 && req.LoginType != 2 {
+		response.Fail("无效的登录方式", c)
+		return
+	}
+
 	if req.LoginType == 1 {
 		//手机登录
+		req.Mobile = strings.Trim(req.Mobile, " ")
 		if req.Mobile == "" {
 			response.Fail("手机号不能为空,请输入手机号", c)
 			return
 		}
-		req.Mobile = strings.Trim(req.Mobile, " ")
-	} else if req.LoginType == 2 {
+	}
+	if req.LoginType == 2 {
 		//邮箱登录
+		req.Email = strings.Trim(req.Email, " ")
 		if req.Email == "" {
 			response.Fail("邮箱不能为空,请输入邮箱", c)
 			return
@@ -52,10 +59,8 @@ func Login(c *gin.Context) {
 			response.Fail("邮箱格式错误,请重新输入", c)
 			return
 		}
-	} else {
-		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 +322,4 @@ func GetTopTab(c *gin.Context) {
 	}
 
 	response.OkData("获取成功", tabBarList, c)
-}
+}

+ 11 - 0
models/tables/rddp/session/update.go

@@ -1,6 +1,7 @@
 package session
 
 import (
+	"fmt"
 	"time"
 )
 
@@ -18,3 +19,13 @@ func (session *Session) UpdateSession(userId int64, expireTime time.Time) (err e
 	err = session.Update(updateCols)
 	return
 }
+
+// ClearRepeatBindAccountToken 清除重复绑定账号的token
+func ClearRepeatBindAccountToken(openIds []string) (err error) {
+	if len(openIds) == 0 {
+		return
+	}
+	sql := fmt.Sprintf(` DELETE FROM session WHERE open_id IN ? AND expire_time >= NOW() `)
+	err = getDb().Exec(sql, openIds).Error
+	return
+}

+ 13 - 0
models/tables/user_record/query.go

@@ -31,3 +31,16 @@ func GetFirstByUnionID(unionID string) (item *UserRecord, err error) {
 	err = global.DEFAULT_MYSQL.Where("union_id = ? and user_id>0", unionID).Order("create_platform asc").First(&item).Error
 	return
 }
+
+// GetRepeatBindAccount 获取重复的绑定账号
+func GetRepeatBindAccount(account, unionId string) (items []*UserRecord, err error) {
+	err = global.DEFAULT_MYSQL.Model(UserRecord{}).Where("bind_account = ? AND union_id <> ? AND create_platform IN (3,6)", account, unionId).Find(&items).Error
+	return
+}
+
+// ClearRepeatBindAccount 清除重复的绑定账号
+func ClearRepeatBindAccount(account, unionId string) (err error) {
+	sql := ` UPDATE user_record SET bind_account = '', user_id = 0 WHERE bind_account = ? AND union_id <> ? AND create_platform IN (3,6)`
+	err = global.DEFAULT_MYSQL.Exec(sql, account, unionId).Error
+	return
+}

+ 28 - 29
services/user/user.go

@@ -28,15 +28,14 @@ type UserInfo struct {
 // GetWxUserItemByOpenId 通过openid获取用户信息
 func GetWxUserItemByOpenId(openid string) (userInfo UserInfo, err error) {
 	//通过openid获取用户关联信息
-	userRecord, userRecordErr := user_record.GetByOpenID(openid)
-	if userRecordErr != nil {
-		if userRecordErr == utils.ErrNoRow {
+	userRecord, e := user_record.GetByOpenID(openid)
+	if e != nil {
+		if e == utils.ErrNoRow {
 			err = ERR_NO_USER_RECORD
 			return
-		} else {
-			err = userRecordErr
-			return
 		}
+		err = e
+		return
 	}
 
 	//该openid在系统中没有关联关系
@@ -288,26 +287,26 @@ QUERY_WX_USER:
 		goto QUERY_WX_USER
 	} else if wxUserErr == ERR_USER_NOT_BIND {
 		// 未绑定则去查询unionId是否已经绑定了用户(其他平台,不区分平台),有相应的手机号邮箱信息则自动绑定
-		platformUser, platformErr := GetFirstWxUserItemByUnionId(unionId)
-		if platformErr == nil {
-			// 当公众号用户存在时
-			if platformUser.Mobile != "" || platformUser.Email != "" {
-				// 有手机号或邮箱则绑定信息则自动绑定并新增wx_user
-				countryCode := 0
-				if platformUser.CountryCode != "" {
-					countryCode, _ = strconv.Atoi(platformUser.CountryCode)
-				}
-				tempToken, tempUser, tempErr, errMsg := BindWxUser(openId, platformUser.Mobile, platformUser.Email, "", 3, countryCode, 1)
-				if tempErr != nil {
-					err = errors.New("自动绑定公众号用户失败,Err:" + tempErr.Error() + ", errMsg:" + errMsg)
-					return
-				}
-				token = tempToken
-				userId = int(tempUser.UserID)
-				isBind = true
-				return
-			}
-		}
+		//platformUser, platformErr := GetFirstWxUserItemByUnionId(unionId)
+		//if platformErr == nil {
+		//	// 当公众号用户存在时
+		//	if platformUser.Mobile != "" || platformUser.Email != "" {
+		//		// 有手机号或邮箱则绑定信息则自动绑定并新增wx_user
+		//		countryCode := 0
+		//		if platformUser.CountryCode != "" {
+		//			countryCode, _ = strconv.Atoi(platformUser.CountryCode)
+		//		}
+		//		tempToken, tempUser, tempErr, errMsg := BindWxUser(openId, platformUser.Mobile, platformUser.Email, "", 3, countryCode, 1)
+		//		if tempErr != nil {
+		//			err = errors.New("自动绑定公众号用户失败,Err:" + tempErr.Error() + ", errMsg:" + errMsg)
+		//			return
+		//		}
+		//		token = tempToken
+		//		userId = int(tempUser.UserID)
+		//		isBind = true
+		//		return
+		//	}
+		//}
 	} else if wxUserErr != nil {
 		err = wxUserErr
 		return
@@ -420,14 +419,14 @@ func GetInfoByClaims(c *gin.Context) (userInfo UserInfo) {
 	return
 }
 
-// GetAdminByUserId 判断当前用户是否为内部人员
+// GetAdminByUserInfo 判断当前用户是否为内部人员
 func GetAdminByUserInfo(userInfo UserInfo) (ok bool, adminInfo *admin2.Admin, err error) {
 	mobile := userInfo.Mobile
 	if mobile == "" {
 		// 用户有可能是通过邮箱登录
 		return
 	}
-	if userInfo.CompanyID != 16 {
+	if userInfo.CompanyID != utils.HzCompanyId {
 		return
 	}
 	adminInfo, err = admin2.GetAdminByMobile(mobile)
@@ -453,7 +452,7 @@ func GetResearcherByUserInfo(userInfo UserInfo) (ok bool, adminInfo *admin2.Admi
 		// 用户有可能是通过邮箱登录
 		return
 	}
-	if userInfo.CompanyID != 16 {
+	if userInfo.CompanyID != utils.HzCompanyId {
 		return
 	}
 	adminInfo, err = admin2.GetAdminByMobile(mobile)

+ 31 - 5
services/user/user_bind.go

@@ -122,8 +122,7 @@ func BindWxUser(openid, mobile, email, code string, bindType, areaNum, registerP
 
 // bindWxUser 用户注册/绑定
 func bindWxUser(openid, mobile, email string, areaNum, registerPlatform int) (userInfo UserInfo, errMsg string, err error) {
-	var source int8
-	source = 6 //绑定来源,1:微信端,2:pc网页端,3:查研观向小程序,4:每日咨询
+	source := int8(utils.USER_RECORD_PLATFORM_YB) //绑定来源,1:微信端,2:pc网页端,3:查研观向小程序,4:每日咨询
 	if mobile == "" && email == "" {
 		err = errors.New("手机号或邮箱必填一个")
 		return
@@ -219,13 +218,40 @@ func bindWxUser(openid, mobile, email string, areaNum, registerPlatform int) (us
 		}
 		return
 	}
+
+	// 未绑定
 	if userRecord.UserID == 0 {
+		// 校验该手机号/邮箱是否已绑定过微信, 若已绑定过则所有微信均进行解绑, 仅绑定当前微信
+		repeats, e := user_record.GetRepeatBindAccount(bindAccount, userRecord.UnionID)
+		if e != nil {
+			err = fmt.Errorf("获取重复的绑定账号数失败, err: %s", e.Error())
+			errMsg = "绑定异常"
+			return
+		}
+		if len(repeats) > 0 {
+			// 清除绑定关系
+			if e = user_record.ClearRepeatBindAccount(bindAccount, userRecord.UnionID); e != nil {
+				err = fmt.Errorf("重置重复的绑定账号失败, err: %s", e.Error())
+				errMsg = "绑定异常"
+				return
+			}
+			// 清除重复账号的session
+			repeatOpenIds := make([]string, 0)
+			for _, v := range repeats {
+				repeatOpenIds = append(repeatOpenIds, v.OpenID)
+			}
+			if e = session.ClearRepeatBindAccountToken(repeatOpenIds); e != nil {
+				err = fmt.Errorf("清除重复绑定账号Token失败, err: %v", e)
+				errMsg = "绑定异常"
+				return
+			}
+		}
+
 		userRecord.BindAccount = bindAccount
 		userRecord.UserID = userId
 		var updateCols = []string{"UserID", "BindAccount"}
-		tmpErr := userRecord.Update(updateCols)
-		if tmpErr != nil {
-			err = tmpErr
+		if e = userRecord.Update(updateCols); e != nil {
+			err = e
 			return
 		}
 	}