feat:完善信息
This commit is contained in:
146
app/api/enterprise/controller/account/login.go
Normal file
146
app/api/enterprise/controller/account/login.go
Normal file
@ -0,0 +1,146 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
model3 "SciencesServer/app/api/enterprise/model"
|
||||
model2 "SciencesServer/app/common/model"
|
||||
"SciencesServer/app/handle"
|
||||
"SciencesServer/utils"
|
||||
"errors"
|
||||
)
|
||||
|
||||
type Login struct{ local string }
|
||||
|
||||
type (
|
||||
LoginHandle func(local string) *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, string) (*InstanceLoginParams, error){
|
||||
LoginModeForSmsCaptcha: loginForSmsCaptcha, LoginModeForPassword: loginForPassword,
|
||||
}
|
||||
|
||||
// loginForSmsCaptcha 短信验证码登陆
|
||||
func loginForSmsCaptcha(params *LoginParams, local string) (*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 := model3.NewUserInstance()
|
||||
|
||||
if isExist, err = model2.FirstField(mUserInstance.UserInstance, []string{"id", "uuid", "name", "mobile", "status"},
|
||||
model2.NewWhere("mobile", params.Captcha.Mobile), model2.NewWhere("local", local)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
mUserManage := model3.NewUserManage()
|
||||
|
||||
if isExist {
|
||||
// 查询该区域下最后一次选中的信息
|
||||
if err = mUserManage.LastChooseInfo(mUserInstance.UUID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
goto RETURNS
|
||||
}
|
||||
mUserInstance.Password = utils.GetRandomString(12)
|
||||
|
||||
if err = model2.Create(mUserInstance.UserInstance); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
RETURNS:
|
||||
return &InstanceLoginParams{
|
||||
UID: mUserInstance.UUID, TenantID: mUserManage.TenantID, ManageUID: mUserManage.UUID,
|
||||
Name: mUserInstance.Name, Mobile: mUserInstance.Mobile,
|
||||
Identity: mUserInstance.Identity, SelectIdentity: mUserManage.Identity,
|
||||
Status: mUserInstance.Status,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// loginForPassword 密码登陆
|
||||
func loginForPassword(params *LoginParams, local string) (*InstanceLoginParams, error) {
|
||||
if !utils.ValidateMobile(params.Password.Mobile) {
|
||||
return nil, errors.New("手机号码格式异常")
|
||||
}
|
||||
mUserInstance := model3.NewUserInstance()
|
||||
|
||||
isExist, err := model2.FirstField(mUserInstance.UserInstance, []string{"id", "uuid", "mobile", "password", "salt", "status"},
|
||||
model2.NewWhere("mobile", params.Password.Mobile), model2.NewWhere("local", local))
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if isExist {
|
||||
return nil, errors.New("当前手机号码未注册")
|
||||
}
|
||||
if !mUserInstance.ValidatePassword(params.Password.Password) {
|
||||
return nil, errors.New("账户或密码错误")
|
||||
}
|
||||
// 最后一次选中的身份信息
|
||||
mUserManage := model3.NewUserManage()
|
||||
|
||||
if err = mUserManage.LastChooseInfo(mUserInstance.UUID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &InstanceLoginParams{
|
||||
UID: mUserInstance.UUID, TenantID: mUserManage.TenantID, ManageUID: mUserManage.UUID,
|
||||
Name: mUserInstance.Name, Mobile: mUserInstance.Mobile,
|
||||
Identity: mUserInstance.Identity, SelectIdentity: mUserManage.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, c.local)
|
||||
|
||||
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(local string) *Login {
|
||||
return &Login{local: local}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user