feat:完善信息

This commit is contained in:
henry
2021-10-15 15:06:02 +08:00
parent af8691e943
commit ae9fb8ea0f
86 changed files with 215 additions and 216 deletions

View File

@ -0,0 +1,54 @@
package account
import (
"SciencesServer/app/common/model"
"SciencesServer/app/service"
"SciencesServer/config"
"SciencesServer/utils"
)
type Instance struct{}
type InstanceHandle func() *Instance
type InstanceLoginCallback func(params *InstanceLoginParams) *InstanceLoginReturn
type (
InstanceLoginParams struct {
UID, TenantID, ManageUID uint64
Name, Mobile string
Identity, SelectIdentity int
Status model.AccountStatusKind
}
InstanceLoginReturn struct {
Token string `json:"token"`
EffectTime int `json:"effect_time"`
}
)
func (c *Instance) Login() InstanceLoginCallback {
return func(params *InstanceLoginParams) *InstanceLoginReturn {
token := utils.JWTEncrypt(config.SettingInfo.TokenEffectTime, map[string]interface{}{
config.TokenForUID: params.UID,
})
session := service.NewSessionEnterprise()
session.Token = token
session.UID = params.UID
session.TenantID = params.TenantID
session.ManageUID = params.ManageUID
session.Name = params.Name
session.Mobile = params.Mobile
session.Identity = params.Identity
session.SelectIdentity = params.SelectIdentity
service.Publish(config.EventForRedisHashProduce, config.RedisKeyForAccount, session.UIDToString(), session)
return &InstanceLoginReturn{Token: token, EffectTime: config.SettingInfo.TokenEffectTime}
}
}
func NewInstance() InstanceHandle {
return func() *Instance {
return &Instance{}
}
}

View 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}
}
}

View File

@ -0,0 +1,25 @@
package account
import (
"SciencesServer/app/service"
"SciencesServer/config"
"SciencesServer/utils"
)
type Logout struct {
*service.SessionEnterprise
local uint64
}
type LogoutHandle func(enterprise *service.SessionEnterprise, local uint64) *Logout
func (c *Logout) Launch() error {
service.Publish(config.EventForRedisHashDestroy, config.RedisKeyForAccount, utils.UintToString(c.UID))
return nil
}
func NewLogout() LogoutHandle {
return func(enterprise *service.SessionEnterprise, local uint64) *Logout {
return &Logout{enterprise, local}
}
}

View File

@ -0,0 +1,33 @@
package account
import (
"SciencesServer/app/handle"
"SciencesServer/utils"
"errors"
)
type Other struct{}
type OtherHandle func() *Other
// BindMobile 绑定手机号码
func (c *Other) BindMobile(token, mobile, captcha string) (interface{}, error) {
if !utils.ValidateMobile(mobile) {
return nil, errors.New("手机号码格式异常")
}
pass, err := handle.NewCaptcha().Validate(&handle.CaptchaSms{
Mobile: mobile, Captcha: captcha,
})
if err != nil {
return nil, err
} else if !pass {
return nil, errors.New("验证码错误或已过期")
}
return nil, nil
}
func NewOther() OtherHandle {
return func() *Other {
return &Other{}
}
}

View File

@ -0,0 +1,94 @@
package account
import (
model3 "SciencesServer/app/api/enterprise/model"
model2 "SciencesServer/app/common/model"
"SciencesServer/app/handle"
"SciencesServer/serve/orm"
"SciencesServer/utils"
"errors"
"gorm.io/gorm"
)
type Register struct{ local string }
type RegisterHandle func(local string) *Register
type (
RegisterParams struct {
Name, Mobile, Captcha, Password, RepeatPass string
Identity int
}
)
func (c *RegisterParams) checkPassword() bool {
return c.Password == c.RepeatPass
}
func (c *RegisterParams) checkCaptcha() (bool, error) {
return handle.NewCaptcha().Validate(&handle.CaptchaSms{Captcha: c.Captcha, Mobile: c.Mobile})
}
func (c *RegisterParams) checkUserExist(mUserInstance *model2.UserInstance, local string) (bool, error) {
var count int64
if err := model2.Count(mUserInstance, &count, model2.NewWhere("mobile", c.Mobile),
model2.NewWhere("local", local)); err != nil {
return false, err
}
return count <= 0, nil
}
// Launch 发起注册
func (c *Register) Launch(params *RegisterParams) (*InstanceLoginReturn, error) {
// 验证密码
if params.checkPassword() {
return nil, errors.New("两次密码不一致")
}
// 验证验证码
pass, err := params.checkCaptcha()
if err != nil {
return nil, err
} else if !pass {
return nil, errors.New("验证码错误或已过期")
}
// 验证账号信息
mUserInstance := model3.NewUserInstance()
if pass, err = params.checkUserExist(mUserInstance.UserInstance, c.local); err != nil {
return nil, err
} else if !pass {
return nil, errors.New("当前手机号码已注册")
}
mUserInstance.Local.Local = c.local
mUserInstance.Password = utils.GetRandomString(12)
mUserInstance.Mobile = params.Mobile
mUserInstance.Password = params.Password
mUserInstance.Identity = params.Identity
mUserManage := model3.NewUserManage()
if err = orm.GetDB().Transaction(func(tx *gorm.DB) error {
if err = model2.Create(mUserInstance.UserInstance, tx); err != nil {
return err
}
mUserManage.UID = mUserInstance.UUID
mUserManage.Name = params.Name
mUserManage.Identity = params.Identity
mUserManage.Selected = model2.UserManageSelectedForYes
return model2.Create(mUserManage.UserManage, tx)
}); err != nil {
return nil, err
}
return NewInstance()().Login()(&InstanceLoginParams{
UID: mUserManage.UUID, Name: mUserManage.Name, Mobile: mUserInstance.Mobile,
Identity: mUserInstance.Identity, SelectIdentity: params.Identity,
}), err
}
func NewRegister() RegisterHandle {
return func(local string) *Register {
return &Register{local: local}
}
}