Files

178 lines
4.9 KiB
Go
Raw Normal View History

2021-09-28 18:23:34 +08:00
package account
import (
model2 "SciencesServer/app/common/model"
"SciencesServer/app/enterprise/model"
"SciencesServer/app/handle"
2021-09-30 12:09:45 +08:00
"SciencesServer/serve/orm"
2021-09-28 18:23:34 +08:00
"SciencesServer/utils"
"errors"
2021-09-30 12:09:45 +08:00
"gorm.io/gorm"
2021-09-28 18:23:34 +08:00
)
type Login struct{}
type (
2021-09-29 16:25:56 +08:00
LoginHandle func() *Login
2021-09-28 18:23:34 +08:00
)
type (
2021-09-30 12:09:45 +08:00
LoginParams struct {
2021-09-28 18:23:34 +08:00
Captcha struct {
2021-09-30 12:09:45 +08:00
Mobile, Captcha string
2021-09-28 18:23:34 +08:00
}
Password struct {
2021-09-30 12:09:45 +08:00
Mobile, Password string
2021-09-28 18:23:34 +08:00
}
Platform struct {
2021-09-29 16:25:56 +08:00
OpenID string
2021-09-28 18:23:34 +08:00
}
}
)
// LoginMode 登陆模式
type LoginMode int
const (
2021-09-29 16:25:56 +08:00
LoginModeForSmsCaptcha LoginMode = iota + 1e2 + 1 // 短信验证码登陆
LoginModeForPassword // 密码登陆
LoginModeForWechat // 微信登陆
LoginModeForQQ // QQ登陆
2021-09-28 18:23:34 +08:00
)
2021-09-30 12:09:45 +08:00
var loginHandle = map[LoginMode]func(*LoginParams) (*InstanceLoginParams, error){
2021-09-29 16:25:56 +08:00
LoginModeForSmsCaptcha: loginForSmsCaptcha, LoginModeForPassword: loginForPassword,
2021-09-28 18:23:34 +08:00
}
2021-09-29 16:25:56 +08:00
// loginForSmsCaptcha 短信验证码登陆
2021-09-30 12:09:45 +08:00
func loginForSmsCaptcha(params *LoginParams) (*InstanceLoginParams, error) {
if !utils.ValidateMobile(params.Captcha.Mobile) {
2021-09-29 16:25:56 +08:00
return nil, errors.New("手机号码格式异常")
}
2021-09-28 18:23:34 +08:00
pass, err := handle.NewCaptcha().Validate(&handle.CaptchaSms{
2021-09-30 12:09:45 +08:00
Mobile: params.Captcha.Mobile, Captcha: params.Captcha.Captcha,
2021-09-28 18:23:34 +08:00
})
if err != nil {
return nil, err
} else if !pass {
return nil, errors.New("验证码错误或已过期")
}
var isExist bool
2021-09-30 12:09:45 +08:00
mUserInstance := model.NewUserInstance()
2021-09-28 18:23:34 +08:00
2021-09-30 12:09:45 +08:00
mUserTenant := model.NewUserTenant()
if isExist, err = model2.FirstField(mUserInstance.UserInstance, []string{"id", "uuid", "name", "mobile", "status"},
model2.NewWhere("mobile", params.Captcha.Mobile)); err != nil {
2021-09-28 18:23:34 +08:00
return nil, err
}
2021-09-30 12:09:45 +08:00
if isExist {
// 最后一次选中的身份信息
if _, err = model2.FirstField(mUserTenant.UserTenant, []string{"id", "uid", "uuid", "name", "identity"},
model2.NewWhere("uid", mUserInstance.UUID),
model2.NewWhere("selected", model2.UserTenantSelectedForYes)); err != nil {
return nil, err
}
} else {
mUserInstance.Password = utils.GetRandomString(12)
if err = orm.GetDB().Transaction(func(tx *gorm.DB) error {
if err = model2.Create(mUserInstance.UserInstance, tx); err != nil {
return err
}
mUserTenant.UID = mUserInstance.UUID
mUserTenant.Name = params.Captcha.Mobile
mUserTenant.Selected = model2.UserTenantSelectedForYes
return model2.Create(mUserTenant.UserTenant, tx)
}); err != nil {
return nil, err
}
}
return &InstanceLoginParams{
UID: mUserTenant.UUID, Name: mUserTenant.Name, Mobile: mUserInstance.Mobile,
Identity: mUserInstance.Identity, SelectIdentity: mUserTenant.Identity,
Status: mUserInstance.Status,
}, nil
2021-09-28 18:23:34 +08:00
}
// loginForPassword 密码登陆
2021-09-30 12:09:45 +08:00
func loginForPassword(params *LoginParams) (*InstanceLoginParams, error) {
if !utils.ValidateMobile(params.Password.Mobile) {
2021-09-29 16:25:56 +08:00
return nil, errors.New("手机号码格式异常")
}
2021-09-30 12:09:45 +08:00
mUserInstance := model.NewUserInstance()
2021-09-28 18:23:34 +08:00
2021-09-30 12:09:45 +08:00
isExist, err := model2.FirstField(mUserInstance.UserInstance, []string{"id", "uuid", "mobile", "password", "salt", "status"},
model2.NewWhere("mobile", params.Password.Mobile))
2021-09-28 18:23:34 +08:00
if err != nil {
return nil, err
} else if isExist {
return nil, errors.New("当前手机号码未注册")
}
2021-09-30 12:09:45 +08:00
if !mUserInstance.ValidatePassword(params.Password.Password) {
2021-09-28 18:23:34 +08:00
return nil, errors.New("账户或密码错误")
}
2021-09-30 12:09:45 +08:00
mUserTenant := model.NewUserTenant()
// 最后一次选中的身份信息
if _, err = model2.FirstField(mUserTenant.UserTenant, []string{"id", "uid", "uuid", "name", "identity"},
model2.NewWhere("uid", mUserInstance.UUID),
model2.NewWhere("selected", model2.UserTenantSelectedForYes)); err != nil {
return nil, err
}
return &InstanceLoginParams{
UID: mUserTenant.UUID, Name: mUserTenant.Name, Mobile: mUserInstance.Mobile,
Identity: mUserInstance.Identity, SelectIdentity: mUserTenant.Identity,
Status: mUserInstance.Status,
}, nil
2021-09-28 18:23:34 +08:00
}
2021-09-30 12:09:45 +08:00
func loginForPlatform(params *LoginParams) error {
2021-09-28 18:23:34 +08:00
return nil
}
2021-09-30 12:09:45 +08:00
func (c *Login) Launch(mode LoginMode, params *LoginParams) (*InstanceLoginReturn, error) {
2021-09-29 16:25:56 +08:00
handle, has := loginHandle[mode]
2021-09-28 18:23:34 +08:00
2021-09-29 16:25:56 +08:00
if !has {
return nil, errors.New("未知的登陆模式")
}
2021-09-30 12:09:45 +08:00
ret, err := handle(params)
2021-09-28 18:23:34 +08:00
2021-09-29 16:25:56 +08:00
if err != nil {
return nil, err
}
2021-09-30 12:09:45 +08:00
if ret.Status != model2.AccountStatusForEnable {
2021-09-29 16:25:56 +08:00
return nil, errors.New("该账号已禁止登陆,请联系管理员")
}
2021-09-30 12:09:45 +08:00
return NewInstance()().Login()(ret), err
2021-09-29 16:25:56 +08:00
}
func (c *Login) BindName(token, name string) {
// 解析token
tokenInfo := utils.JWTDecrypt(token)
if tokenInfo == nil {
//return errors.New("")
}
}
2021-09-30 12:09:45 +08:00
func (c *Login) BindMobile(token, mobile, captcha string) (*InstanceLoginReturn, error) {
2021-09-29 16:25:56 +08:00
pass, err := handle.NewCaptcha().Validate(&handle.CaptchaSms{
Mobile: mobile, Captcha: captcha,
})
if err != nil {
return nil, err
} else if !pass {
return nil, errors.New("验证码错误或已过期")
2021-09-28 18:23:34 +08:00
}
2021-09-29 16:25:56 +08:00
return nil, err
2021-09-28 18:23:34 +08:00
}
2021-09-29 16:25:56 +08:00
func NewLogin() LoginHandle {
return func() *Login {
return &Login{}
}
2021-09-28 18:23:34 +08:00
}