feat:完善信息
This commit is contained in:
@ -4,7 +4,6 @@ import (
|
||||
model2 "SciencesServer/app/common/model"
|
||||
"SciencesServer/app/enterprise/model"
|
||||
"SciencesServer/app/handle"
|
||||
"SciencesServer/config"
|
||||
"SciencesServer/utils"
|
||||
"errors"
|
||||
)
|
||||
@ -12,48 +11,46 @@ import (
|
||||
type Login struct{}
|
||||
|
||||
type (
|
||||
LoginHandle func(LoginMode, *LoginRequest) (*LoginResponse, error)
|
||||
LoginHandle func() *Login
|
||||
)
|
||||
|
||||
type (
|
||||
LoginRequest struct {
|
||||
Captcha struct {
|
||||
Mobile string `json:"mobile"`
|
||||
Captcha string `json:"captcha"`
|
||||
Mobile string
|
||||
Captcha string
|
||||
}
|
||||
Password struct {
|
||||
Mobile string `json:"mobile"`
|
||||
Password string `json:"password"`
|
||||
Mobile string
|
||||
Password string
|
||||
}
|
||||
Platform struct {
|
||||
OpenID string `json:"open_id"`
|
||||
OpenID string
|
||||
}
|
||||
}
|
||||
LoginResponse struct {
|
||||
Token string `json:"token"`
|
||||
EffectTime int `json:"effect_time"`
|
||||
}
|
||||
)
|
||||
|
||||
// LoginMode 登陆模式
|
||||
type LoginMode int
|
||||
|
||||
const (
|
||||
LoginModeForCaptcha LoginMode = iota + 1e2 + 1 // 验证码登陆
|
||||
LoginModeForPassword // 密码登陆
|
||||
LoginModeForWechat // 微信登陆
|
||||
LoginModeForQQ // QQ登陆
|
||||
LoginModeForSmsCaptcha LoginMode = iota + 1e2 + 1 // 短信验证码登陆
|
||||
LoginModeForPassword // 密码登陆
|
||||
LoginModeForWechat // 微信登陆
|
||||
LoginModeForQQ // QQ登陆
|
||||
)
|
||||
|
||||
var loginHandle = map[LoginMode]func(*LoginRequest) (*model.TenantUser, error){
|
||||
LoginModeForCaptcha: loginForCaptcha, LoginModeForPassword: loginForPassword,
|
||||
LoginModeForSmsCaptcha: loginForSmsCaptcha, LoginModeForPassword: loginForPassword,
|
||||
}
|
||||
|
||||
// loginForCaptcha 验证码登陆
|
||||
func loginForCaptcha(req *LoginRequest) (*model.TenantUser, error) {
|
||||
// loginForSmsCaptcha 短信验证码登陆
|
||||
func loginForSmsCaptcha(req *LoginRequest) (*model.TenantUser, error) {
|
||||
if !utils.ValidateMobile(req.Captcha.Mobile) {
|
||||
return nil, errors.New("手机号码格式异常")
|
||||
}
|
||||
pass, err := handle.NewCaptcha().Validate(&handle.CaptchaSms{
|
||||
Mobile: req.Captcha.Mobile,
|
||||
Captcha: req.Captcha.Captcha,
|
||||
Mobile: req.Captcha.Mobile, Captcha: req.Captcha.Captcha,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -64,20 +61,26 @@ func loginForCaptcha(req *LoginRequest) (*model.TenantUser, error) {
|
||||
|
||||
mTenantUsr := model.NewTenantUser()
|
||||
|
||||
if isExist, err = model2.FirstField(mTenantUsr.TenantUser, []string{"id", "name", "status"},
|
||||
if isExist, err = model2.FirstField(mTenantUsr.TenantUser, []string{"id", "uuid", "name", "mobile", "status"},
|
||||
model2.NewWhere("mobile", req.Captcha.Mobile)); err != nil {
|
||||
return nil, err
|
||||
} else if isExist {
|
||||
return nil, errors.New("当前手机号码未注册")
|
||||
return mTenantUsr, nil
|
||||
}
|
||||
mTenantUsr.Name = req.Captcha.Mobile
|
||||
mTenantUsr.Password = utils.GetRandomString(12)
|
||||
return mTenantUsr, nil
|
||||
}
|
||||
|
||||
// loginForPassword 密码登陆
|
||||
func loginForPassword(req *LoginRequest) (*model.TenantUser, error) {
|
||||
if !utils.ValidateMobile(req.Password.Mobile) {
|
||||
return nil, errors.New("手机号码格式异常")
|
||||
}
|
||||
mTenantUsr := model.NewTenantUser()
|
||||
|
||||
isExist, err := model2.FirstField(mTenantUsr.TenantUser, []string{"id", "name", "status"},
|
||||
isExist, err := model2.FirstField(mTenantUsr.TenantUser, []string{"id", "uuid", "name", "mobile",
|
||||
"password", "salt", "status"},
|
||||
model2.NewWhere("mobile", req.Password.Mobile))
|
||||
|
||||
if err != nil {
|
||||
@ -95,27 +98,46 @@ func loginForPlatform(req *LoginRequest) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Login) Login() LoginHandle {
|
||||
return func(mode LoginMode, req *LoginRequest) (*LoginResponse, error) {
|
||||
handle, has := loginHandle[mode]
|
||||
func (c *Login) Launch(mode LoginMode, req *LoginRequest) (*LoginResponse, error) {
|
||||
handle, has := loginHandle[mode]
|
||||
|
||||
if !has {
|
||||
return nil, errors.New("未知的登陆模式")
|
||||
}
|
||||
user, err := handle(req)
|
||||
if !has {
|
||||
return nil, errors.New("未知的登陆模式")
|
||||
}
|
||||
user, err := handle(req)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
token := utils.JWTEncrypt(config.SettingInfo.TokenEffectTime, map[string]interface{}{
|
||||
config.TokenForUID: user.UUID,
|
||||
})
|
||||
return &LoginResponse{
|
||||
Token: token, EffectTime: config.SettingInfo.TokenEffectTime,
|
||||
}, nil
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if user.Status != model2.AccountStatusForEnable {
|
||||
return nil, errors.New("该账号已禁止登陆,请联系管理员")
|
||||
}
|
||||
return NewAccount().Login()(user), err
|
||||
}
|
||||
|
||||
func (c *Login) BindName(token, name string) {
|
||||
// 解析token
|
||||
tokenInfo := utils.JWTDecrypt(token)
|
||||
|
||||
if tokenInfo == nil {
|
||||
//return errors.New("")
|
||||
}
|
||||
}
|
||||
|
||||
func NewLogin() *Login {
|
||||
return &Login{}
|
||||
func (c *Login) BindMobile(token, mobile, captcha string) (*LoginResponse, error) {
|
||||
pass, err := handle.NewCaptcha().Validate(&handle.CaptchaSms{
|
||||
Mobile: mobile, Captcha: captcha,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !pass {
|
||||
return nil, errors.New("验证码错误或已过期")
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func NewLogin() LoginHandle {
|
||||
return func() *Login {
|
||||
return &Login{}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user