Files

174 lines
5.2 KiB
Go
Raw Normal View History

2021-09-28 18:23:34 +08:00
package account
import (
"SciencesServer/app/api/enterprise/model"
2021-09-28 18:23:34 +08:00
model2 "SciencesServer/app/common/model"
"SciencesServer/app/handle"
"SciencesServer/serve/orm"
2021-09-28 18:23:34 +08:00
"SciencesServer/utils"
"errors"
"gorm.io/gorm"
2021-09-28 18:23:34 +08:00
)
2022-01-15 16:48:49 +08:00
type Login struct{ tenantID uint64 }
2021-09-28 18:23:34 +08:00
type (
2022-01-15 16:48:49 +08:00
LoginHandle func(tenantID uint64) *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-11-24 09:59:29 +08:00
Account, 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
)
2022-01-15 16:48:49 +08:00
var loginHandle = map[LoginMode]func(*LoginParams, uint64) (*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 短信验证码登陆
2022-01-15 16:48:49 +08:00
func loginForSmsCaptcha(params *LoginParams, tenantID uint64) (*InstanceLoginParams, error) {
2021-09-30 12:09:45 +08:00
if !utils.ValidateMobile(params.Captcha.Mobile) {
2021-12-03 11:32:26 +08:00
return nil, errors.New("操作错误,手机号码格式异常")
2021-09-29 16:25:56 +08:00
}
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 {
2021-12-03 11:32:26 +08:00
return nil, errors.New("操作错误,验证码错误或已过期")
2021-09-28 18:23:34 +08:00
}
var isExist bool
2021-10-13 11:23:55 +08:00
// 查询账号信息
mUserInstance := model.NewUserInstance()
2021-09-28 18:23:34 +08:00
if isExist, err = model2.FirstField(mUserInstance.UserInstance, []string{"id", "uuid", "avatar", "name", "mobile",
"identity", "is_vip", "vip_deadline", "status"},
2021-12-03 11:32:26 +08:00
model2.NewWhere("mobile", params.Captcha.Mobile)); err != nil {
2021-09-28 18:23:34 +08:00
return nil, err
}
mUserIdentity := model.NewUserIdentity()
// 用户资产信息
mUserAssets := new(model.UserAssets)
2021-10-08 09:39:40 +08:00
2021-09-30 12:09:45 +08:00
if isExist {
2021-10-13 11:23:55 +08:00
// 查询该区域下最后一次选中的信息
2021-12-01 14:12:23 +08:00
if err = mUserIdentity.LastChooseInfo(mUserInstance.UUID); err != nil {
2021-09-30 12:09:45 +08:00
return nil, err
}
if mUserAssets, err = model.NewUserAssets().Assets(mUserInstance.UUID); err != nil {
return nil, err
}
2021-10-08 09:39:40 +08:00
goto RETURNS
2021-09-30 16:13:31 +08:00
}
if err = orm.GetDB().Transaction(func(tx *gorm.DB) error {
mUserInstance.Name = params.Captcha.Mobile
mUserInstance.Mobile = params.Captcha.Mobile
mUserInstance.Password = utils.GetRandomString(12)
if err = model2.Create(mUserInstance.UserInstance, tx); err != nil {
return err
}
mUserAssets.UID = mUserInstance.UUID
2021-09-30 12:09:45 +08:00
return model2.Create(mUserAssets.UserAssets, tx)
}); err != nil {
2021-09-30 16:13:31 +08:00
return nil, err
2021-09-30 12:09:45 +08:00
}
2021-09-30 16:13:31 +08:00
RETURNS:
2021-09-30 12:09:45 +08:00
return &InstanceLoginParams{
2021-12-03 11:50:57 +08:00
UID: mUserInstance.UUID,
2022-01-27 14:50:52 +08:00
Avatar: mUserInstance.Avatar, Name: mUserInstance.Name, Mobile: mUserInstance.Mobile,
2021-12-22 14:53:45 +08:00
Vip: mUserInstance.Vip, VipStatus: mUserInstance.VipStatus(), VipDeadline: mUserInstance.VipDeadline,
Currency: mUserAssets.Currency, Identity: mUserInstance.Identity, SelectIdentity: mUserIdentity.Identity,
2021-10-09 11:55:54 +08:00
Status: mUserInstance.Status,
2021-09-30 12:09:45 +08:00
}, nil
2021-09-28 18:23:34 +08:00
}
// loginForPassword 密码登陆
2022-01-15 16:48:49 +08:00
func loginForPassword(params *LoginParams, tenantID uint64) (*InstanceLoginParams, error) {
2021-11-24 09:59:29 +08:00
if !utils.ValidateMobile(params.Password.Account) {
2021-12-03 11:32:26 +08:00
return nil, errors.New("操作错误,手机号码格式异常")
2021-09-29 16:25:56 +08:00
}
mUserInstance := model.NewUserInstance()
2021-09-28 18:23:34 +08:00
isExist, err := model2.FirstField(mUserInstance.UserInstance, []string{"id", "uuid", "name", "avatar", "mobile",
"identity", "is_vip", "vip_deadline", "password", "salt", "status"},
2021-12-03 11:32:26 +08:00
model2.NewWhere("mobile", params.Password.Account))
2021-09-28 18:23:34 +08:00
if err != nil {
return nil, err
2021-12-03 11:32:26 +08:00
} else if !isExist {
return nil, errors.New("操作错误,当前手机号码未注册")
2021-09-28 18:23:34 +08:00
}
2021-09-30 12:09:45 +08:00
if !mUserInstance.ValidatePassword(params.Password.Password) {
2021-12-03 11:32:26 +08:00
return nil, errors.New("操作错误,账户或密码错误")
2021-09-28 18:23:34 +08:00
}
// 用户资产信息
mUserAssets := new(model.UserAssets)
if mUserAssets, err = model.NewUserAssets().Assets(mUserInstance.UUID); err != nil {
return nil, err
}
2021-09-30 12:09:45 +08:00
// 最后一次选中的身份信息
mUserIdentity := model.NewUserIdentity()
2021-10-08 09:39:40 +08:00
2021-12-01 14:12:23 +08:00
if err = mUserIdentity.LastChooseInfo(mUserInstance.UUID); err != nil {
2021-09-30 12:09:45 +08:00
return nil, err
}
return &InstanceLoginParams{
2021-12-03 11:50:57 +08:00
UID: mUserInstance.UUID,
2022-01-27 14:50:52 +08:00
Avatar: mUserInstance.Avatar, Name: mUserInstance.Name, Mobile: mUserInstance.Mobile,
2021-12-22 14:53:45 +08:00
Vip: mUserInstance.Vip, VipStatus: mUserInstance.VipStatus(), VipDeadline: mUserInstance.VipDeadline,
Currency: mUserAssets.Currency, Identity: mUserInstance.Identity, SelectIdentity: mUserIdentity.Identity,
2021-10-09 11:55:54 +08:00
Status: mUserInstance.Status,
2021-09-30 12:09:45 +08:00
}, 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-10-08 09:39:40 +08:00
_handle, has := loginHandle[mode]
2021-09-28 18:23:34 +08:00
2021-09-29 16:25:56 +08:00
if !has {
2021-12-03 11:32:26 +08:00
return nil, errors.New("操作错误,未知的登陆模式")
2021-09-29 16:25:56 +08:00
}
2022-01-15 16:48:49 +08:00
ret, err := _handle(params, c.tenantID)
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-12-03 11:32:26 +08:00
return nil, errors.New("操作错误,该账号已禁止登陆,请联系管理员")
2021-09-29 16:25:56 +08:00
}
2021-09-30 12:09:45 +08:00
return NewInstance()().Login()(ret), err
2021-09-29 16:25:56 +08:00
}
func NewLogin() LoginHandle {
2022-01-15 16:48:49 +08:00
return func(tenantID uint64) *Login {
return &Login{tenantID: tenantID}
2021-09-29 16:25:56 +08:00
}
2021-09-28 18:23:34 +08:00
}