RegisterUser支持token

This commit is contained in:
gazebo
2020-01-06 16:36:52 +08:00
parent fda22fa2d4
commit 170cd63936
5 changed files with 58 additions and 22 deletions

View File

@@ -44,6 +44,8 @@ const (
MinCaptchaLen = 4
MaxCaptchaWidth = 400
MaxCaptchaHeight = 400
InternalAuthSecret = "a36ca416-c85e-4dcf-aff9-590be3d2f8a2"
)
type IUser interface {
@@ -224,7 +226,7 @@ func SendVerifyCode(authToken, captchaID, captchaValue, authID string) (verfifyC
// 公众号登录authIDTypeD是UserIDEmptyauthSecret是code这个函数是被微信的回调调用不是直接被客户端调用
// 微信登录authIDType是UserIDEmptyauthSecret是code这个函数是被微信的回调调用不是直接被客户端调用
// 小程序登录authIDType是UserIDEmptyauthSecret是jsCode
func Login(ctx *Context, authType, authID, authIDType, authSecret string) (authInfo *AuthInfo, err error) {
func LoginInternal(ctx *Context, authType, authID, authIDType, authSecret string) (authInfo *AuthInfo, err error) {
authType = strings.ToLower(authType)
authIDType = strings.ToLower(authIDType)
if handler := authers[authType]; handler != nil {
@@ -279,6 +281,13 @@ func Login(ctx *Context, authType, authID, authIDType, authSecret string) (authI
return authInfo, err
}
func Login(ctx *Context, authType, authID, authIDType, authSecret string) (authInfo *AuthInfo, err error) {
if authSecret == InternalAuthSecret {
authSecret = ""
}
return LoginInternal(ctx, authIDType, authID, authIDType, authSecret)
}
// 通过临时TOKEN绑定新创建的用户
func BindUser(inauthInfo *AuthInfo, user IUser) (outauthInfo *AuthInfo, err error) {
if err = AddAuthBind(user, inauthInfo); err == nil {

View File

@@ -71,7 +71,9 @@ func (a *Auther) VerifySecret(mobileNumber, code string) (authBindEx *auth2.Auth
globals.SugarLogger.Debugf("VerifySecret mobileNumber:%s, code:%s", mobileNumber, code)
err = ErrVerifyCodeIsWrong
if (auth2.TestMobileMap[mobileNumber] == 1 && code == TestVerifyCode) || a.VerifyCode(mobileNumber, code) {
if (code == auth2.InternalAuthSecret ||
auth2.TestMobileMap[mobileNumber] == 1 && code == TestVerifyCode) ||
a.VerifyCode(mobileNumber, code) {
err = nil
}
return nil, err

View File

@@ -146,26 +146,48 @@ func init() {
auth2.Init(userProvider)
}
func RegisterUserWithMobile(ctx *jxcontext.Context, user *model.User, mobileVerifyCode string, inAuthInfo *auth2.AuthInfo) (outAuthInfo *auth2.AuthInfo, err error) {
func RegisterUserWithMobile(ctx *jxcontext.Context, user *model.User, mobileVerifyCode string, inAuthInfo, manTokenInfo *auth2.AuthInfo) (outAuthInfo *auth2.AuthInfo, err error) {
var mobileAuth *auth2.AuthInfo
fakeMobile := false
user.Type = model.UserTypeConsumer
createName := ctx.GetRealRemoteIP()
authType := auth2.AuthTypeMobile
if manTokenInfo != nil && mobileVerifyCode == "" {
user, err2 := dao.GetUserByID(dao.GetDB(), "user_id", manTokenInfo.GetID())
if err = err2; err != nil {
return nil, err
}
if user.Type&(model.UserTypeOperator|model.UserTypeBoss) == 0 {
return nil, fmt.Errorf("管理员才能添加商户")
}
if utils.Pointer2String(user.Mobile) == "" {
return nil, fmt.Errorf("管理员添加必须指定用户手机号")
}
mobileVerifyCode = auth2.InternalAuthSecret
fakeMobile = true
user.Type |= model.UserTypeStoreBoss
createName = manTokenInfo.GetName()
}
if mobileVerifyCode != "" {
mobileAuth, err = auth2.Login(ctx.Context, auth2.AuthTypeMobile, user.GetMobile(), auth2.UserIDMobile, mobileVerifyCode)
if fakeMobile {
mobileAuth, err = auth2.LoginInternal(ctx.Context, auth2.AuthTypeMobile, user.GetMobile(), auth2.UserIDMobile, mobileVerifyCode)
} else {
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("短信验证码与其它认证方式至少要指定一种")
}
} else if inAuthInfo != nil {
user.Mobile = nil
} else {
return nil, fmt.Errorf("短信验证码与其它认证方式至少要指定一种")
}
createName := ctx.GetRealRemoteIP()
authType := auth2.AuthTypeMobile
if inAuthInfo != nil {
user.Type = model.UserTypeConsumer
if inAuthInfo.AuthBindInfo.Type == dingding.AuthTypeStaff {
user.Type |= model.UserTypeOperator
} else if user.Mobile != nil {

View File

@@ -13,7 +13,6 @@ import (
"git.rosy.net.cn/jx-callback/business/auth2/authprovider/password"
"git.rosy.net.cn/jx-callback/business/auth2/authprovider/weixin"
"git.rosy.net.cn/jx-callback/business/model"
"git.rosy.net.cn/jx-callback/business/model/dao"
"git.rosy.net.cn/jx-callback/globals"
"github.com/astaxie/beego"
)
@@ -67,13 +66,13 @@ func (c *Auth2Controller) CreateCaptcha() {
// @router /SendVerifyCode [post]
func (c *Auth2Controller) SendVerifyCode() {
c.callSendVerifyCode(func(params *tAuth2SendVerifyCodeParams) (retVal interface{}, errCode string, err error) {
code, authInfo, err := auth2.SendVerifyCode(params.AuthToken, params.CaptchaID, params.CaptchaValue, params.AuthID)
if err == nil && authInfo != nil {
user, err2 := dao.GetUserByID(dao.GetDB(), "user_id", authInfo.GetID())
if err2 == nil && user.Type&(model.UserTypeBoss|model.UserTypeOperator) != 0 {
retVal = code
}
}
_, _, err = auth2.SendVerifyCode(params.AuthToken, params.CaptchaID, params.CaptchaValue, params.AuthID)
// if err == nil && authInfo != nil {
// user, err2 := dao.GetUserByID(dao.GetDB(), "user_id", authInfo.GetID())
// if err2 == nil && user.Type&(model.UserTypeBoss|model.UserTypeOperator) != 0 {
// retVal = code
// }
// }
return retVal, "", err
})
}

View File

@@ -20,6 +20,7 @@ type User2Controller struct {
// @Title 用户注册
// @Description 用户注册
// @Param token header string false "管理员token"
// @Param payload formData string true "json数据User对象(手机号必填)"
// @Param mobileVerifyCode formData string false "手机验证码通过auth2.SendVerifyCode获得mobileVerifyCode与authToken不能同时为空"
// @Param authToken formData string false "之前通过login得到的认证TOKENmobileVerifyCode与authToken不能同时为空"
@@ -29,16 +30,19 @@ type User2Controller struct {
func (c *User2Controller) RegisterUser() {
c.callRegisterUser(func(params *tUser2RegisterUserParams) (retVal interface{}, errCode string, err error) {
var (
user model.User
inAuthInfo *auth2.AuthInfo
user model.User
inAuthInfo, manTokenInfo *auth2.AuthInfo
)
if params.AuthToken != "" {
inAuthInfo, err = auth2.GetTokenInfo(params.AuthToken)
}
if params.Token != "" {
manTokenInfo, err = auth2.GetTokenInfo(params.Token)
}
if err == nil {
if err = jxutils.Strings2Objs(params.Payload, &user); err == nil {
user.Type = 0
retVal, err = cms.RegisterUserWithMobile(params.Ctx, &user, params.MobileVerifyCode, inAuthInfo)
retVal, err = cms.RegisterUserWithMobile(params.Ctx, &user, params.MobileVerifyCode, inAuthInfo, manTokenInfo)
}
}
return retVal, errCode, err