Files
2021-09-30 16:13:31 +08:00

169 lines
4.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package account
import (
model2 "SciencesServer/app/common/model"
"SciencesServer/app/enterprise/model"
"SciencesServer/app/handle"
"SciencesServer/serve/orm"
"SciencesServer/utils"
"errors"
"gorm.io/gorm"
)
type Login struct{}
type (
LoginHandle func() *Login
)
type (
LoginParams struct {
Captcha struct {
Mobile, Captcha string
}
Password struct {
Mobile, 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(*LoginParams) (*InstanceLoginParams, error){
LoginModeForSmsCaptcha: loginForSmsCaptcha, LoginModeForPassword: loginForPassword,
}
// loginForSmsCaptcha 短信验证码登陆
func loginForSmsCaptcha(params *LoginParams) (*InstanceLoginParams, error) {
if !utils.ValidateMobile(params.Captcha.Mobile) {
return nil, errors.New("手机号码格式异常")
}
pass, err := handle.NewCaptcha().Validate(&handle.CaptchaSms{
Mobile: params.Captcha.Mobile, Captcha: params.Captcha.Captcha,
})
if err != nil {
return nil, err
} else if !pass {
return nil, errors.New("验证码错误或已过期")
}
var isExist bool
mUserInstance := model.NewUserInstance()
mUserTenant := model.NewUserTenant()
if isExist, err = model2.FirstField(mUserInstance.UserInstance, []string{"id", "uuid", "name", "mobile", "status"},
model2.NewWhere("mobile", params.Captcha.Mobile)); err != nil {
return nil, err
}
if isExist {
// TODO未处理删除用户身份的管理信息
// 最后一次选中的身份信息
if isExist, 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 if isExist {
goto RETURNS
}
}
mUserInstance.Password = utils.GetRandomString(12)
if err = orm.GetDB().Transaction(func(tx *gorm.DB) error {
if mUserInstance.ID <= 0 {
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
}
RETURNS:
return &InstanceLoginParams{
UID: mUserTenant.UUID, Name: mUserTenant.Name, Mobile: mUserInstance.Mobile,
Identity: mUserInstance.Identity, SelectIdentity: mUserTenant.Identity,
Status: mUserInstance.Status,
}, nil
}
// loginForPassword 密码登陆
func loginForPassword(params *LoginParams) (*InstanceLoginParams, error) {
if !utils.ValidateMobile(params.Password.Mobile) {
return nil, errors.New("手机号码格式异常")
}
mUserInstance := model.NewUserInstance()
isExist, err := model2.FirstField(mUserInstance.UserInstance, []string{"id", "uuid", "mobile", "password", "salt", "status"},
model2.NewWhere("mobile", params.Password.Mobile))
if err != nil {
return nil, err
} else if isExist {
return nil, errors.New("当前手机号码未注册")
}
if !mUserInstance.ValidatePassword(params.Password.Password) {
return nil, errors.New("账户或密码错误")
}
mUserTenant := model.NewUserTenant()
// 最后一次选中的身份信息
if isExist, 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 if !isExist {
mUserTenant.UID = mUserInstance.UUID
mUserTenant.Name = params.Password.Mobile
mUserTenant.Selected = model2.UserTenantSelectedForYes
if err = model2.Create(mUserTenant.UserTenant); 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
}
func loginForPlatform(params *LoginParams) error {
return nil
}
func (c *Login) Launch(mode LoginMode, params *LoginParams) (*InstanceLoginReturn, error) {
handle, has := loginHandle[mode]
if !has {
return nil, errors.New("未知的登陆模式")
}
ret, err := handle(params)
if err != nil {
return nil, err
}
if ret.Status != model2.AccountStatusForEnable {
return nil, errors.New("该账号已禁止登陆,请联系管理员")
}
return NewInstance()().Login()(ret), err
}
func NewLogin() LoginHandle {
return func() *Login {
return &Login{}
}
}