RegisterUser支持token
This commit is contained in:
@@ -44,6 +44,8 @@ const (
|
|||||||
MinCaptchaLen = 4
|
MinCaptchaLen = 4
|
||||||
MaxCaptchaWidth = 400
|
MaxCaptchaWidth = 400
|
||||||
MaxCaptchaHeight = 400
|
MaxCaptchaHeight = 400
|
||||||
|
|
||||||
|
InternalAuthSecret = "a36ca416-c85e-4dcf-aff9-590be3d2f8a2"
|
||||||
)
|
)
|
||||||
|
|
||||||
type IUser interface {
|
type IUser interface {
|
||||||
@@ -224,7 +226,7 @@ func SendVerifyCode(authToken, captchaID, captchaValue, authID string) (verfifyC
|
|||||||
// 公众号登录:authIDTypeD是UserIDEmpty,authSecret是code(这个函数是被微信的回调调用,不是直接被客户端调用)
|
// 公众号登录:authIDTypeD是UserIDEmpty,authSecret是code(这个函数是被微信的回调调用,不是直接被客户端调用)
|
||||||
// 微信登录:authIDType是UserIDEmpty,authSecret是code(这个函数是被微信的回调调用,不是直接被客户端调用)
|
// 微信登录:authIDType是UserIDEmpty,authSecret是code(这个函数是被微信的回调调用,不是直接被客户端调用)
|
||||||
// 小程序登录:authIDType是UserIDEmpty,authSecret是jsCode
|
// 小程序登录:authIDType是UserIDEmpty,authSecret是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)
|
authType = strings.ToLower(authType)
|
||||||
authIDType = strings.ToLower(authIDType)
|
authIDType = strings.ToLower(authIDType)
|
||||||
if handler := authers[authType]; handler != nil {
|
if handler := authers[authType]; handler != nil {
|
||||||
@@ -279,6 +281,13 @@ func Login(ctx *Context, authType, authID, authIDType, authSecret string) (authI
|
|||||||
return authInfo, err
|
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绑定新创建的用户
|
// 通过临时TOKEN绑定新创建的用户
|
||||||
func BindUser(inauthInfo *AuthInfo, user IUser) (outauthInfo *AuthInfo, err error) {
|
func BindUser(inauthInfo *AuthInfo, user IUser) (outauthInfo *AuthInfo, err error) {
|
||||||
if err = AddAuthBind(user, inauthInfo); err == nil {
|
if err = AddAuthBind(user, inauthInfo); err == nil {
|
||||||
|
|||||||
@@ -71,7 +71,9 @@ func (a *Auther) VerifySecret(mobileNumber, code string) (authBindEx *auth2.Auth
|
|||||||
globals.SugarLogger.Debugf("VerifySecret mobileNumber:%s, code:%s", mobileNumber, code)
|
globals.SugarLogger.Debugf("VerifySecret mobileNumber:%s, code:%s", mobileNumber, code)
|
||||||
|
|
||||||
err = ErrVerifyCodeIsWrong
|
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
|
err = nil
|
||||||
}
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -146,26 +146,48 @@ func init() {
|
|||||||
auth2.Init(userProvider)
|
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
|
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 != "" {
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if mobileAuth != nil && !mobileAuth.IsUserEmpty() {
|
if mobileAuth != nil && !mobileAuth.IsUserEmpty() {
|
||||||
return nil, jsonerr.New(mobileAuth, model.ErrCodeJsonUserAlreadyExist)
|
return nil, jsonerr.New(mobileAuth, model.ErrCodeJsonUserAlreadyExist)
|
||||||
}
|
}
|
||||||
} else {
|
} else if inAuthInfo != nil {
|
||||||
if inAuthInfo == nil {
|
|
||||||
return nil, fmt.Errorf("短信验证码与其它认证方式至少要指定一种")
|
|
||||||
}
|
|
||||||
user.Mobile = nil
|
user.Mobile = nil
|
||||||
|
} else {
|
||||||
|
return nil, fmt.Errorf("短信验证码与其它认证方式至少要指定一种")
|
||||||
}
|
}
|
||||||
createName := ctx.GetRealRemoteIP()
|
|
||||||
authType := auth2.AuthTypeMobile
|
|
||||||
if inAuthInfo != nil {
|
if inAuthInfo != nil {
|
||||||
user.Type = model.UserTypeConsumer
|
|
||||||
if inAuthInfo.AuthBindInfo.Type == dingding.AuthTypeStaff {
|
if inAuthInfo.AuthBindInfo.Type == dingding.AuthTypeStaff {
|
||||||
user.Type |= model.UserTypeOperator
|
user.Type |= model.UserTypeOperator
|
||||||
} else if user.Mobile != nil {
|
} else if user.Mobile != nil {
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ import (
|
|||||||
"git.rosy.net.cn/jx-callback/business/auth2/authprovider/password"
|
"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/auth2/authprovider/weixin"
|
||||||
"git.rosy.net.cn/jx-callback/business/model"
|
"git.rosy.net.cn/jx-callback/business/model"
|
||||||
"git.rosy.net.cn/jx-callback/business/model/dao"
|
|
||||||
"git.rosy.net.cn/jx-callback/globals"
|
"git.rosy.net.cn/jx-callback/globals"
|
||||||
"github.com/astaxie/beego"
|
"github.com/astaxie/beego"
|
||||||
)
|
)
|
||||||
@@ -67,13 +66,13 @@ func (c *Auth2Controller) CreateCaptcha() {
|
|||||||
// @router /SendVerifyCode [post]
|
// @router /SendVerifyCode [post]
|
||||||
func (c *Auth2Controller) SendVerifyCode() {
|
func (c *Auth2Controller) SendVerifyCode() {
|
||||||
c.callSendVerifyCode(func(params *tAuth2SendVerifyCodeParams) (retVal interface{}, errCode string, err error) {
|
c.callSendVerifyCode(func(params *tAuth2SendVerifyCodeParams) (retVal interface{}, errCode string, err error) {
|
||||||
code, authInfo, err := auth2.SendVerifyCode(params.AuthToken, params.CaptchaID, params.CaptchaValue, params.AuthID)
|
_, _, err = auth2.SendVerifyCode(params.AuthToken, params.CaptchaID, params.CaptchaValue, params.AuthID)
|
||||||
if err == nil && authInfo != nil {
|
// if err == nil && authInfo != nil {
|
||||||
user, err2 := dao.GetUserByID(dao.GetDB(), "user_id", authInfo.GetID())
|
// user, err2 := dao.GetUserByID(dao.GetDB(), "user_id", authInfo.GetID())
|
||||||
if err2 == nil && user.Type&(model.UserTypeBoss|model.UserTypeOperator) != 0 {
|
// if err2 == nil && user.Type&(model.UserTypeBoss|model.UserTypeOperator) != 0 {
|
||||||
retVal = code
|
// retVal = code
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
return retVal, "", err
|
return retVal, "", err
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ type User2Controller struct {
|
|||||||
|
|
||||||
// @Title 用户注册
|
// @Title 用户注册
|
||||||
// @Description 用户注册
|
// @Description 用户注册
|
||||||
|
// @Param token header string false "管理员token"
|
||||||
// @Param payload formData string true "json数据,User对象(手机号必填)"
|
// @Param payload formData string true "json数据,User对象(手机号必填)"
|
||||||
// @Param mobileVerifyCode formData string false "手机验证码(通过auth2.SendVerifyCode获得)(mobileVerifyCode与authToken不能同时为空)"
|
// @Param mobileVerifyCode formData string false "手机验证码(通过auth2.SendVerifyCode获得)(mobileVerifyCode与authToken不能同时为空)"
|
||||||
// @Param authToken formData string false "之前通过login得到的认证TOKEN(mobileVerifyCode与authToken不能同时为空)"
|
// @Param authToken formData string false "之前通过login得到的认证TOKEN(mobileVerifyCode与authToken不能同时为空)"
|
||||||
@@ -29,16 +30,19 @@ type User2Controller struct {
|
|||||||
func (c *User2Controller) RegisterUser() {
|
func (c *User2Controller) RegisterUser() {
|
||||||
c.callRegisterUser(func(params *tUser2RegisterUserParams) (retVal interface{}, errCode string, err error) {
|
c.callRegisterUser(func(params *tUser2RegisterUserParams) (retVal interface{}, errCode string, err error) {
|
||||||
var (
|
var (
|
||||||
user model.User
|
user model.User
|
||||||
inAuthInfo *auth2.AuthInfo
|
inAuthInfo, manTokenInfo *auth2.AuthInfo
|
||||||
)
|
)
|
||||||
if params.AuthToken != "" {
|
if params.AuthToken != "" {
|
||||||
inAuthInfo, err = auth2.GetTokenInfo(params.AuthToken)
|
inAuthInfo, err = auth2.GetTokenInfo(params.AuthToken)
|
||||||
}
|
}
|
||||||
|
if params.Token != "" {
|
||||||
|
manTokenInfo, err = auth2.GetTokenInfo(params.Token)
|
||||||
|
}
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if err = jxutils.Strings2Objs(params.Payload, &user); err == nil {
|
if err = jxutils.Strings2Objs(params.Payload, &user); err == nil {
|
||||||
user.Type = 0
|
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
|
return retVal, errCode, err
|
||||||
|
|||||||
Reference in New Issue
Block a user