fix
This commit is contained in:
@@ -63,17 +63,38 @@ func (s *UserService) RegisterUUIDUser(ctx context.Context) (string, error) {
|
||||
return userId, nil
|
||||
}
|
||||
|
||||
// GeneralUserToken 生成用户token
|
||||
func (s *UserService) GeneralUserToken(ctx context.Context, userID string, userType int64) (string, error) {
|
||||
// GetUserType 根据user.Mobile字段动态计算用户类型
|
||||
// 如果有mobile,则为正式用户(UserTypeNormal),否则为临时用户(UserTypeTemp)
|
||||
func (s *UserService) GetUserType(ctx context.Context, userID string) (int64, error) {
|
||||
user, err := s.userModel.FindOne(ctx, userID)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if user.Mobile.Valid && user.Mobile.String != "" {
|
||||
return model.UserTypeNormal, nil
|
||||
}
|
||||
return model.UserTypeTemp, nil
|
||||
}
|
||||
|
||||
// GeneralUserToken 生成用户token(动态计算userType)
|
||||
func (s *UserService) GeneralUserToken(ctx context.Context, userID string) (string, error) {
|
||||
platform, err := ctxdata.GetPlatformFromCtx(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// 动态计算userType
|
||||
userType, err := s.GetUserType(ctx, userID)
|
||||
if err != nil {
|
||||
return "", errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "获取用户信息失败: %v", err)
|
||||
}
|
||||
|
||||
var isAgent int64
|
||||
var agentID string
|
||||
var authType string
|
||||
var authKey string
|
||||
|
||||
// 只有正式用户(有mobile)才可能是代理
|
||||
if userType == model.UserTypeNormal {
|
||||
agent, err := s.agentModel.FindOneByUserId(ctx, userID)
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||
@@ -89,6 +110,7 @@ func (s *UserService) GeneralUserToken(ctx context.Context, userID string, userT
|
||||
authKey = userAuth.AuthKey
|
||||
}
|
||||
} else {
|
||||
// 临时用户获取其他平台的auth信息
|
||||
platAuthType := s.getAuthTypeByPlatform(platform)
|
||||
ua, err := s.userAuthModel.FindOneByUserIdAuthType(ctx, userID, platAuthType)
|
||||
if err == nil && ua != nil {
|
||||
@@ -159,12 +181,16 @@ func (s *UserService) RegisterUser(ctx context.Context, mobile string) (string,
|
||||
return userId, nil
|
||||
}
|
||||
|
||||
// 双重判断是否已经注册
|
||||
if claims.UserType == model.UserTypeNormal {
|
||||
// 双重判断是否已经注册(根据mobile判断,而不是userType)
|
||||
currentUser, err := s.userModel.FindOne(ctx, claims.UserId)
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||
return "", err
|
||||
}
|
||||
if currentUser != nil && currentUser.Mobile.Valid && currentUser.Mobile.String != "" {
|
||||
return "", errors.New("用户已注册")
|
||||
}
|
||||
var userId string
|
||||
// 临时转正式注册
|
||||
// 临时用户绑定mobile转正式注册
|
||||
err = s.userModel.Trans(ctx, func(ctx context.Context, session sqlx.Session) error {
|
||||
user := &model.User{Id: uuid.NewString(), Mobile: sql.NullString{String: mobile, Valid: true}}
|
||||
if _, userInsertErr := s.userModel.Insert(ctx, session, user); userInsertErr != nil {
|
||||
@@ -187,17 +213,26 @@ func (s *UserService) RegisterUser(ctx context.Context, mobile string) (string,
|
||||
return userId, nil
|
||||
}
|
||||
|
||||
// TempUserBindUser 临时用户绑定用户
|
||||
// TempUserBindUser 临时用户绑定用户(添加mobile使其变为正式用户)
|
||||
func (s *UserService) TempUserBindUser(ctx context.Context, session sqlx.Session, normalUserID string) error {
|
||||
claims, err := ctxdata.GetClaimsFromCtx(ctx)
|
||||
if err != nil && !errors.Is(err, ctxdata.ErrNoInCtx) {
|
||||
return err
|
||||
}
|
||||
|
||||
if claims == nil || claims.UserType != model.UserTypeTemp {
|
||||
if claims == nil {
|
||||
return errors.New("无临时用户")
|
||||
}
|
||||
|
||||
// 检查当前用户是否已经绑定了mobile(根据mobile判断,而不是userType)
|
||||
tempUser, err := s.userModel.FindOne(ctx, claims.UserId)
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||
return err
|
||||
}
|
||||
if tempUser != nil && tempUser.Mobile.Valid && tempUser.Mobile.String != "" {
|
||||
return errors.New("临时用户已注册")
|
||||
}
|
||||
|
||||
existingAuth, err := s.userAuthModel.FindOneByAuthTypeAuthKey(ctx, claims.AuthType, claims.AuthKey)
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user