feat:完善信息

This commit is contained in:
henry
2021-09-30 12:09:45 +08:00
parent 3d84af2e0c
commit 022fef28f7
23 changed files with 1111 additions and 170 deletions

View File

@ -1,41 +0,0 @@
package account
import (
"SciencesServer/app/enterprise/model"
"SciencesServer/app/service"
"SciencesServer/config"
"SciencesServer/utils"
)
type Account struct{}
type LoginCallback func(user *model.TenantUser) *LoginResponse
type (
LoginResponse struct {
Token string `json:"token"`
EffectTime int `json:"effect_time"`
}
)
func (c *Account) Login() LoginCallback {
return func(mTenantUser *model.TenantUser) *LoginResponse {
token := utils.JWTEncrypt(config.SettingInfo.TokenEffectTime, map[string]interface{}{
config.TokenForUID: mTenantUser.UUID,
})
session := service.NewSessionEnterprise()
session.Token = token
session.UID = mTenantUser.UUID
session.Name = mTenantUser.Name
session.Mobile = mTenantUser.Mobile
session.Identity = mTenantUser.Identity
service.Publish(config.EventForRedisHashProduce, config.RedisKeyForAccount, mTenantUser.UUIDToString(), session)
return &LoginResponse{Token: token, EffectTime: config.SettingInfo.TokenEffectTime}
}
}
func NewAccount() *Account {
return &Account{}
}

View File

@ -0,0 +1,52 @@
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 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.Name = params.Name
session.Mobile = params.Mobile
session.Identity = params.Identity
session.CurrentIdentity = params.SelectIdentity
service.Publish(config.EventForRedisHashProduce, config.RedisKeyForAccount, utils.UintToString(params.UID), session)
return &InstanceLoginReturn{Token: token, EffectTime: config.SettingInfo.TokenEffectTime}
}
}
func NewInstance() InstanceHandle {
return func() *Instance {
return &Instance{}
}
}

View File

@ -4,8 +4,10 @@ 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{}
@ -15,14 +17,12 @@ type (
)
type (
LoginRequest struct {
LoginParams struct {
Captcha struct {
Mobile string
Captcha string
Mobile, Captcha string
}
Password struct {
Mobile string
Password string
Mobile, Password string
}
Platform struct {
OpenID string
@ -40,17 +40,17 @@ const (
LoginModeForQQ // QQ登陆
)
var loginHandle = map[LoginMode]func(*LoginRequest) (*model.TenantUser, error){
var loginHandle = map[LoginMode]func(*LoginParams) (*InstanceLoginParams, error){
LoginModeForSmsCaptcha: loginForSmsCaptcha, LoginModeForPassword: loginForPassword,
}
// loginForSmsCaptcha 短信验证码登陆
func loginForSmsCaptcha(req *LoginRequest) (*model.TenantUser, error) {
if !utils.ValidateMobile(req.Captcha.Mobile) {
func loginForSmsCaptcha(params *LoginParams) (*InstanceLoginParams, error) {
if !utils.ValidateMobile(params.Captcha.Mobile) {
return nil, errors.New("手机号码格式异常")
}
pass, err := handle.NewCaptcha().Validate(&handle.CaptchaSms{
Mobile: req.Captcha.Mobile, Captcha: req.Captcha.Captcha,
Mobile: params.Captcha.Mobile, Captcha: params.Captcha.Captcha,
})
if err != nil {
return nil, err
@ -59,60 +59,94 @@ func loginForSmsCaptcha(req *LoginRequest) (*model.TenantUser, error) {
}
var isExist bool
mTenantUsr := model.NewTenantUser()
mUserInstance := model.NewUserInstance()
if isExist, err = model2.FirstField(mTenantUsr.TenantUser, []string{"id", "uuid", "name", "mobile", "status"},
model2.NewWhere("mobile", req.Captcha.Mobile)); err != nil {
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
} else if isExist {
return mTenantUsr, nil
}
mTenantUsr.Name = req.Captcha.Mobile
mTenantUsr.Password = utils.GetRandomString(12)
return mTenantUsr, nil
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
}
// loginForPassword 密码登陆
func loginForPassword(req *LoginRequest) (*model.TenantUser, error) {
if !utils.ValidateMobile(req.Password.Mobile) {
func loginForPassword(params *LoginParams) (*InstanceLoginParams, error) {
if !utils.ValidateMobile(params.Password.Mobile) {
return nil, errors.New("手机号码格式异常")
}
mTenantUsr := model.NewTenantUser()
mUserInstance := model.NewUserInstance()
isExist, err := model2.FirstField(mTenantUsr.TenantUser, []string{"id", "uuid", "name", "mobile",
"password", "salt", "status"},
model2.NewWhere("mobile", req.Password.Mobile))
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 !mTenantUsr.ValidatePassword(req.Password.Password) {
if !mUserInstance.ValidatePassword(params.Password.Password) {
return nil, errors.New("账户或密码错误")
}
return mTenantUsr, nil
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
}
func loginForPlatform(req *LoginRequest) error {
func loginForPlatform(params *LoginParams) error {
return nil
}
func (c *Login) Launch(mode LoginMode, req *LoginRequest) (*LoginResponse, error) {
func (c *Login) Launch(mode LoginMode, params *LoginParams) (*InstanceLoginReturn, error) {
handle, has := loginHandle[mode]
if !has {
return nil, errors.New("未知的登陆模式")
}
user, err := handle(req)
ret, err := handle(params)
if err != nil {
return nil, err
}
if user.Status != model2.AccountStatusForEnable {
if ret.Status != model2.AccountStatusForEnable {
return nil, errors.New("该账号已禁止登陆,请联系管理员")
}
return NewAccount().Login()(user), err
return NewInstance()().Login()(ret), err
}
func (c *Login) BindName(token, name string) {
@ -124,7 +158,7 @@ func (c *Login) BindName(token, name string) {
}
}
func (c *Login) BindMobile(token, mobile, captcha string) (*LoginResponse, error) {
func (c *Login) BindMobile(token, mobile, captcha string) (*InstanceLoginReturn, error) {
pass, err := handle.NewCaptcha().Validate(&handle.CaptchaSms{
Mobile: mobile, Captcha: captcha,
})

View File

@ -4,7 +4,10 @@ import (
model2 "SciencesServer/app/common/model"
"SciencesServer/app/enterprise/model"
"SciencesServer/app/handle"
"SciencesServer/serve/orm"
"SciencesServer/utils"
"errors"
"gorm.io/gorm"
)
type Register struct{}
@ -12,53 +15,66 @@ type Register struct{}
type RegisterHandle func() *Register
type (
RegisterRequest struct {
RegisterParams struct {
Name, Mobile, Captcha, Password, RepeatPass string
}
)
func (c *RegisterRequest) checkPassword() bool {
func (c *RegisterParams) checkPassword() bool {
return c.Password == c.RepeatPass
}
func (c *RegisterRequest) checkUserExist(mTenantUser *model.TenantUser) (bool, error) {
func (c *RegisterParams) checkUserExist(mUserInstance *model2.UserInstance) (bool, error) {
var count int64
if err := model2.Count(mTenantUser.TenantUser, &count, model2.NewWhere("mobile", c.Mobile)); err != nil {
if err := model2.Count(mUserInstance, &count, model2.NewWhere("mobile", c.Mobile)); err != nil {
return false, err
}
return count > 0, nil
}
func (c *RegisterRequest) checkCaptcha() (bool, error) {
func (c *RegisterParams) checkCaptcha() (bool, error) {
return handle.NewCaptcha().Validate(&handle.CaptchaSms{Captcha: c.Captcha, Mobile: c.Mobile})
}
func (c *Register) Launch(req *RegisterRequest) (*LoginResponse, error) {
if req.checkPassword() {
func (c *Register) Launch(params *RegisterParams) (*InstanceLoginReturn, error) {
if params.checkPassword() {
return nil, errors.New("两次密码不一致")
}
mTenantUser := model.NewTenantUser()
mUserInstance := model.NewUserInstance()
pass, err := req.checkUserExist(mTenantUser)
pass, err := params.checkUserExist(mUserInstance.UserInstance)
if err != nil {
return nil, err
} else if pass {
return nil, errors.New("当前手机号码已注册")
}
if pass, err = req.checkCaptcha(); err != nil {
if pass, err = params.checkCaptcha(); err != nil {
return nil, err
} else if !pass {
return nil, errors.New("验证码错误或已过期")
}
mTenantUser.Name = req.Name
mTenantUser.Mobile = req.Mobile
mTenantUser.Password = req.Password
mUserInstance.Password = utils.GetRandomString(12)
mUserInstance.Mobile = params.Mobile
mUserInstance.Password = params.Password
if err = model2.Create(mTenantUser.TenantUser); err != nil {
mUserTenant := model.NewUserTenant()
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.Name
mUserTenant.Selected = model2.UserTenantSelectedForYes
return model2.Create(mUserTenant.UserTenant, tx)
}); err != nil {
return nil, err
}
return NewAccount().Login()(mTenantUser), err
return NewInstance()().Login()(&InstanceLoginParams{
UID: mUserTenant.UUID, Name: mUserTenant.Name, Mobile: mUserInstance.Mobile,
Identity: mUserInstance.Identity,
}), err
}
func NewRegister() RegisterHandle {

View File

@ -0,0 +1,39 @@
package user
import (
"SciencesServer/app/basic/config"
"SciencesServer/app/service"
)
type Instance struct{ *service.SessionEnterprise }
type InstanceHandle func(enterprise *service.SessionEnterprise) *Instance
type InstanceBasic struct {
Avatar string `json:"avatar"` // 头像
Name string `json:"name"` // 名称
Email string `json:"email"` // 邮箱
}
type (
// InstancePerfectParams 完善信息参数
InstancePerfectParams struct {
*InstanceBasic
*config.Area
}
)
func (c *Instance) User() {
}
// Perfect 完善信息
func (c *Instance) Perfect(params *InstancePerfectParams) {
}
func NewInstance() InstanceHandle {
return func(enterprise *service.SessionEnterprise) *Instance {
return &Instance{enterprise}
}
}