Files
2021-09-29 16:25:56 +08:00

144 lines
3.4 KiB
Go

package account
import (
model2 "SciencesServer/app/common/model"
"SciencesServer/app/enterprise/model"
"SciencesServer/app/handle"
"SciencesServer/utils"
"errors"
)
type Login struct{}
type (
LoginHandle func() *Login
)
type (
LoginRequest struct {
Captcha struct {
Mobile string
Captcha string
}
Password struct {
Mobile string
Password string
}
Platform struct {
OpenID string
}
}
)
// LoginMode 登陆模式
type LoginMode int
const (
LoginModeForSmsCaptcha LoginMode = iota + 1e2 + 1 // 短信验证码登陆
LoginModeForPassword // 密码登陆
LoginModeForWechat // 微信登陆
LoginModeForQQ // QQ登陆
)
var loginHandle = map[LoginMode]func(*LoginRequest) (*model.TenantUser, error){
LoginModeForSmsCaptcha: loginForSmsCaptcha, LoginModeForPassword: loginForPassword,
}
// 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,
})
if err != nil {
return nil, err
} else if !pass {
return nil, errors.New("验证码错误或已过期")
}
var isExist bool
mTenantUsr := model.NewTenantUser()
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 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", "uuid", "name", "mobile",
"password", "salt", "status"},
model2.NewWhere("mobile", req.Password.Mobile))
if err != nil {
return nil, err
} else if isExist {
return nil, errors.New("当前手机号码未注册")
}
if !mTenantUsr.ValidatePassword(req.Password.Password) {
return nil, errors.New("账户或密码错误")
}
return mTenantUsr, nil
}
func loginForPlatform(req *LoginRequest) error {
return nil
}
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 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 (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{}
}
}