三端用户手机号联通,增加临时用户
This commit is contained in:
		| @@ -4,6 +4,7 @@ import ( | ||||
| 	"context" | ||||
| 	"database/sql" | ||||
| 	"fmt" | ||||
| 	"time" | ||||
|  | ||||
| 	"qnc-server/app/main/api/internal/svc" | ||||
| 	"qnc-server/app/main/api/internal/types" | ||||
| @@ -15,7 +16,6 @@ import ( | ||||
| 	"github.com/pkg/errors" | ||||
| 	"github.com/zeromicro/go-zero/core/logx" | ||||
| 	"github.com/zeromicro/go-zero/core/stores/redis" | ||||
| 	"github.com/zeromicro/go-zero/core/stores/sqlx" | ||||
| ) | ||||
|  | ||||
| type BindMobileLogic struct { | ||||
| @@ -33,22 +33,15 @@ func NewBindMobileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BindMo | ||||
| } | ||||
|  | ||||
| func (l *BindMobileLogic) BindMobile(req *types.BindMobileReq) (resp *types.BindMobileResp, err error) { | ||||
| 	userID, getUserIdErr := ctxdata.GetUidFromCtx(l.ctx) | ||||
| 	if getUserIdErr != nil { | ||||
| 		return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "绑定手机号, %v", getUserIdErr) | ||||
| 	claims, err := ctxdata.GetClaimsFromCtx(l.ctx) | ||||
| 	if err != nil && !errors.Is(err, ctxdata.ErrNoInCtx) { | ||||
| 		return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "绑定手机号, %v", err) | ||||
| 	} | ||||
| 	secretKey := l.svcCtx.Config.Encrypt.SecretKey | ||||
| 	encryptedMobile, err := crypto.EncryptMobile(req.Mobile, secretKey) | ||||
| 	if err != nil { | ||||
| 		return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "绑定手机号, 加密手机号失败: %v", err) | ||||
| 	} | ||||
| 	user, err := l.svcCtx.UserModel.FindOneByMobile(l.ctx, sql.NullString{String: encryptedMobile, Valid: true}) | ||||
| 	if err != nil && !errors.Is(err, model.ErrNotFound) { | ||||
| 		return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "绑定手机号, %v", err) | ||||
| 	} | ||||
| 	if user != nil { | ||||
| 		return nil, errors.Wrapf(xerr.NewErrMsg("该手机号已绑定"), "绑定手机号, %v", err) | ||||
| 	} | ||||
| 	// 检查手机号是否在一分钟内已发送过验证码 | ||||
| 	redisKey := fmt.Sprintf("%s:%s", "bindMobile", encryptedMobile) | ||||
| 	cacheCode, err := l.svcCtx.Redis.Get(redisKey) | ||||
| @@ -61,44 +54,38 @@ func (l *BindMobileLogic) BindMobile(req *types.BindMobileReq) (resp *types.Bind | ||||
| 	if cacheCode != req.Code { | ||||
| 		return nil, errors.Wrapf(xerr.NewErrMsg("验证码不正确"), "手机登录, 验证码不正确: %s", encryptedMobile) | ||||
| 	} | ||||
|  | ||||
| 	userModel, err := l.svcCtx.UserModel.FindOne(l.ctx, userID) | ||||
| 	if err != nil { | ||||
| 		return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "绑定手机号, %v", err) | ||||
| 	} | ||||
| 	if userModel.Mobile.Valid && userModel.Mobile.String != "" { | ||||
| 		return nil, errors.Wrapf(xerr.NewErrMsg("账号已绑定手机号,无法再次绑定"), "绑定手机号, %v", err) | ||||
| 	} | ||||
| 	userAuthModel, err := l.svcCtx.UserAuthModel.FindOneByUserIdAuthType(l.ctx, userID, model.UserAuthTypeH5Mobile) | ||||
| 	var userID int64 | ||||
| 	user, err := l.svcCtx.UserModel.FindOneByMobile(l.ctx, sql.NullString{String: encryptedMobile, Valid: true}) | ||||
| 	if err != nil && !errors.Is(err, model.ErrNotFound) { | ||||
| 		return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "绑定手机号, %v", err) | ||||
| 	} | ||||
| 	if userAuthModel != nil { | ||||
| 		return nil, errors.Wrapf(xerr.NewErrMsg("账号已绑定手机号,无法再次绑定"), "绑定手机号, %v", err) | ||||
| 	if user != nil { | ||||
| 		// 进行平台绑定 | ||||
| 		if claims != nil { | ||||
| 			if claims.UserType == model.UserTypeTemp { | ||||
| 				err = l.svcCtx.UserService.TempUserBindUser(l.ctx, nil, user.Id) | ||||
| 				if err != nil { | ||||
| 					return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "绑定手机号, 临时用户绑定用户失败: %+v", err) | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
| 		userID = user.Id | ||||
| 	} else { | ||||
| 		// 创建账号,并绑定手机号 | ||||
| 		userID, err = l.svcCtx.UserService.RegisterUser(l.ctx, encryptedMobile) | ||||
| 		if err != nil { | ||||
| 			return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "绑定手机号, 注册用户失败: %+v", err) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	var userAuth model.UserAuth | ||||
| 	userAuth.UserId = userID | ||||
| 	userAuth.AuthType = model.UserAuthTypeH5Mobile | ||||
| 	userAuth.AuthKey = encryptedMobile | ||||
| 	transErr := l.svcCtx.UserAuthModel.Trans(l.ctx, func(ctx context.Context, session sqlx.Session) error { | ||||
| 		_, err = l.svcCtx.UserAuthModel.Insert(ctx, session, &userAuth) | ||||
| 		if err != nil { | ||||
| 			return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "绑定手机号, %v", err) | ||||
| 		} | ||||
| 		userModel.Mobile = sql.NullString{ | ||||
| 			String: encryptedMobile, | ||||
| 			Valid:  true, | ||||
| 		} | ||||
| 		_, err = l.svcCtx.UserModel.Update(l.ctx, session, userModel) | ||||
| 		if err != nil { | ||||
| 			return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "绑定手机号, %v", err) | ||||
| 		} | ||||
| 		return nil | ||||
| 	}) | ||||
| 	if transErr != nil { | ||||
| 		return nil, transErr | ||||
| 	token, err := l.svcCtx.UserService.GeneralUserToken(l.ctx, userID) | ||||
| 	if err != nil { | ||||
| 		return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "绑定手机号, 生成token失败: %+v", err) | ||||
| 	} | ||||
|  | ||||
| 	return &types.BindMobileResp{}, nil | ||||
| 	now := time.Now().Unix() | ||||
| 	return &types.BindMobileResp{ | ||||
| 		AccessToken:  token, | ||||
| 		AccessExpire: now + l.svcCtx.Config.JwtAuth.AccessExpire, | ||||
| 		RefreshAfter: now + l.svcCtx.Config.JwtAuth.RefreshAfter, | ||||
| 	}, nil | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user