122 lines
2.9 KiB
Go
122 lines
2.9 KiB
Go
package account
|
|
|
|
import (
|
|
model2 "SciencesServer/app/common/model"
|
|
"SciencesServer/app/enterprise/model"
|
|
"SciencesServer/app/handle"
|
|
"SciencesServer/config"
|
|
"SciencesServer/utils"
|
|
"errors"
|
|
)
|
|
|
|
type Login struct{}
|
|
|
|
type (
|
|
LoginHandle func(LoginMode, *LoginRequest) (*LoginResponse, error)
|
|
)
|
|
|
|
type (
|
|
LoginRequest struct {
|
|
Captcha struct {
|
|
Mobile string `json:"mobile"`
|
|
Captcha string `json:"captcha"`
|
|
}
|
|
Password struct {
|
|
Mobile string `json:"mobile"`
|
|
Password string `json:"password"`
|
|
}
|
|
Platform struct {
|
|
OpenID string `json:"open_id"`
|
|
}
|
|
}
|
|
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登陆
|
|
)
|
|
|
|
var loginHandle = map[LoginMode]func(*LoginRequest) (*model.TenantUser, error){
|
|
LoginModeForCaptcha: loginForCaptcha, LoginModeForPassword: loginForPassword,
|
|
}
|
|
|
|
// loginForCaptcha 验证码登陆
|
|
func loginForCaptcha(req *LoginRequest) (*model.TenantUser, error) {
|
|
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", "name", "status"},
|
|
model2.NewWhere("mobile", req.Captcha.Mobile)); err != nil {
|
|
return nil, err
|
|
} else if isExist {
|
|
return nil, errors.New("当前手机号码未注册")
|
|
}
|
|
return mTenantUsr, nil
|
|
}
|
|
|
|
// loginForPassword 密码登陆
|
|
func loginForPassword(req *LoginRequest) (*model.TenantUser, error) {
|
|
mTenantUsr := model.NewTenantUser()
|
|
|
|
isExist, err := model2.FirstField(mTenantUsr.TenantUser, []string{"id", "name", "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) Login() LoginHandle {
|
|
return func(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
|
|
}
|
|
token := utils.JWTEncrypt(config.SettingInfo.TokenEffectTime, map[string]interface{}{
|
|
config.TokenForUID: user.UUID,
|
|
})
|
|
return &LoginResponse{
|
|
Token: token, EffectTime: config.SettingInfo.TokenEffectTime,
|
|
}, nil
|
|
}
|
|
}
|
|
|
|
func NewLogin() *Login {
|
|
return &Login{}
|
|
}
|