RegisterUser支持注册用户时,不指定手机号

This commit is contained in:
gazebo
2019-10-21 15:14:34 +08:00
parent d24b8ea53d
commit bed2d85833
4 changed files with 49 additions and 26 deletions

View File

@@ -279,20 +279,9 @@ func Login(ctx *Context, authType, authID, authIDType, authSecret string) (authI
// 通过临时TOKEN绑定新创建的用户
func BindUser(inauthInfo *AuthInfo, user IUser) (outauthInfo *AuthInfo, err error) {
if inauthInfo == nil || user == nil {
return nil, ErrInternalErrror
}
if !inauthInfo.IsUserEmpty() {
return nil, ErrUserAlreadyExist
}
if handler := authers[inauthInfo.AuthBindInfo.Type]; handler != nil {
if err = AddAuthBind(user, inauthInfo); err == nil {
inauthInfo.AuthBindInfo.UserID = user.GetID()
if err = handler.AddAuthBind(inauthInfo.AuthBindInfo, user.GetName()); err == nil {
RemoveUserInfo(inauthInfo.Token)
outauthInfo = createAuthInfo(user, inauthInfo.AuthBindInfo)
}
} else {
err = ErrIllegalAuthType
outauthInfo = createAuthInfo(user, inauthInfo.AuthBindInfo)
}
return outauthInfo, err
}
@@ -311,9 +300,13 @@ func AddAuthBind(user IUser, newAuthInfo *AuthInfo) (err error) {
} else if newAuthInfo.AuthBindInfo.Type == AuthTypeEmail {
err = userProvider.UpdateUserEmail(user.GetID(), newAuthInfo.AuthBindInfo.AuthID)
} else {
newAuthInfo.AuthBindInfo.UserID = user.GetID()
authers[newAuthInfo.AuthBindInfo.Type].UnbindAuth(user.GetID(), newAuthInfo.GetAuthType(), user.GetName())
err = authers[newAuthInfo.AuthBindInfo.Type].AddAuthBind(newAuthInfo.AuthBindInfo, user.GetName())
if handler := authers[newAuthInfo.AuthBindInfo.Type]; handler != nil {
newAuthInfo.AuthBindInfo.UserID = user.GetID()
handler.UnbindAuth(user.GetID(), newAuthInfo.GetAuthType(), user.GetName())
err = handler.AddAuthBind(newAuthInfo.AuthBindInfo, user.GetName())
} else {
err = ErrIllegalAuthType
}
}
return err
}

View File

@@ -115,26 +115,45 @@ func init() {
}
func RegisterUserWithMobile(ctx *jxcontext.Context, user *model.User, mobileVerifyCode string, inAuthInfo *auth2.AuthInfo) (outAuthInfo *auth2.AuthInfo, err error) {
mobileAuth, err2 := auth2.Login(ctx.Context, auth2.AuthTypeMobile, user.GetMobile(), auth2.UserIDMobile, mobileVerifyCode)
if err = err2; err == nil {
if !mobileAuth.IsUserEmpty() {
var mobileAuth *auth2.AuthInfo
if mobileVerifyCode != "" {
mobileAuth, err = auth2.Login(ctx.Context, auth2.AuthTypeMobile, user.GetMobile(), auth2.UserIDMobile, mobileVerifyCode)
if err != nil {
return nil, err
}
if mobileAuth != nil && !mobileAuth.IsUserEmpty() {
return nil, jsonerr.New(mobileAuth, model.ErrCodeJsonUserAlreadyExist)
}
} else {
if inAuthInfo == nil {
return nil, fmt.Errorf("短信验证码与其它认证方式至少要指定一种")
}
user.Mobile = nil
}
createName := ctx.GetRealRemoteIP()
authType := auth2.AuthTypeMobile
if inAuthInfo != nil {
user.Type = model.UserTypeConsumer
if inAuthInfo.AuthBindInfo.Type == dingding.AuthTypeStaff {
user.Type |= model.UserTypeOperator
} else {
user.Type |= model.UserTypeStoreBoss
}
if err = CreateUser(user, utils.LimitUTF8StringLen(ctx.GetRealRemoteIP()+","+inAuthInfo.GetAuthID(), 32)); err == nil {
userProvider.UpdateLastLogin(user.GetID(), inAuthInfo.GetAuthType(), ctx.GetRealRemoteIP())
TryAddStoreBossRole4User(ctx, user)
createName += "," + inAuthInfo.GetAuthID()
authType = inAuthInfo.GetAuthType()
}
if err = CreateUser(user, utils.LimitUTF8StringLen(createName, 32)); err == nil {
userProvider.UpdateLastLogin(user.GetID(), authType, ctx.GetRealRemoteIP())
TryAddStoreBossRole4User(ctx, user)
if mobileAuth != nil {
if outAuthInfo, err = auth2.BindUser(mobileAuth, user); err == nil && inAuthInfo != nil {
err = auth2.AddAuthBind(&outAuthInfo.UserBasic, inAuthInfo)
}
} else if dao.IsDuplicateError(err) {
err = auth2.ErrUserID2AlreadyExist
} else {
outAuthInfo, err = auth2.BindUser(inAuthInfo, user)
}
} else if dao.IsDuplicateError(err) {
err = auth2.ErrUserID2AlreadyExist
}
return outAuthInfo, err
}

View File

@@ -145,3 +145,14 @@ func (ctx *Context) GetMobileAndUserID() (mobile, userID string) {
}
return mobile, userID
}
func (ctx *Context) GetUserID() (userID string) {
token := ctx.GetToken()
authInfo, err2 := auth2.GetTokenInfo(token)
if err2 == nil {
if authInfo.TokenType == auth2.TokenTypeNormal {
userID = authInfo.GetID()
}
}
return userID
}

View File

@@ -18,8 +18,8 @@ type User2Controller struct {
// @Title 用户注册
// @Description 用户注册
// @Param payload formData string true "json数据User对象(手机号必填)"
// @Param mobileVerifyCode formData string true "手机验证码通过auth2.SendVerifyCode获得"
// @Param authToken formData string false "之前通过login得到的认证TOKEN可以为空)"
// @Param mobileVerifyCode formData string false "手机验证码通过auth2.SendVerifyCode获得mobileVerifyCode与authToken不能同时为空"
// @Param authToken formData string false "之前通过login得到的认证TOKENmobileVerifyCode与authToken不能同时为空)"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /RegisterUser [post]