From 21dc62ba8eb879402200de60f590ec48a39f79d2 Mon Sep 17 00:00:00 2001 From: henry Date: Wed, 13 Oct 2021 11:23:55 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E5=AE=8C=E5=96=84=E4=BF=A1?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/model/user_instance.go | 1 + .../model/{user_tenant.go => user_manage.go} | 30 ++++----- app/enterprise/api/account.go | 6 +- app/enterprise/api/technology.go | 24 +++++--- app/enterprise/controller/account/login.go | 40 ++++++------ app/enterprise/controller/account/logout.go | 11 ++-- app/enterprise/controller/account/register.go | 61 ++++++++++--------- .../controller/technology/patent.go | 15 ++++- app/enterprise/model/user_manage.go | 21 +++++++ app/enterprise/model/user_tenant.go | 20 ------ main.go | 2 +- 11 files changed, 129 insertions(+), 102 deletions(-) rename app/common/model/{user_tenant.go => user_manage.go} (72%) create mode 100644 app/enterprise/model/user_manage.go delete mode 100644 app/enterprise/model/user_tenant.go diff --git a/app/common/model/user_instance.go b/app/common/model/user_instance.go index e43eaa9..f0dc4f9 100644 --- a/app/common/model/user_instance.go +++ b/app/common/model/user_instance.go @@ -9,6 +9,7 @@ import ( // UserInstance 账号信息 type UserInstance struct { Model + Local UUID uint64 `gorm:"column:uuid;uniqueIndex:idx_tenant_user_uuid;type:int;default:0;comment:用户唯一UUID" json:"-"` Name string `gorm:"column:name;type:varchar(20);default:null;comment:真实姓名" json:"name"` Mobile string `gorm:"column:mobile;index:idx_user_instance_mobile;type:varchar(15);default:null;comment:联系方式" json:"mobile"` diff --git a/app/common/model/user_tenant.go b/app/common/model/user_manage.go similarity index 72% rename from app/common/model/user_tenant.go rename to app/common/model/user_manage.go index 3eb5c34..f860c82 100644 --- a/app/common/model/user_tenant.go +++ b/app/common/model/user_manage.go @@ -6,48 +6,48 @@ import ( "time" ) -// UserTenant 用户租户信息 -type UserTenant struct { +// UserManage 用户管理信息 +type UserManage struct { Model ModelTenant - UUID uint64 `gorm:"column:uuid;uniqueIndex:idx_user_tenant_uuid;type:int;default:0;comment:用户唯一UUID" json:"-"` UID uint64 `gorm:"column:uid;index:idx_user_tenant_uid;type:int;default:0;comment:用户表UUID" json:"-"` + UUID uint64 `gorm:"column:uuid;uniqueIndex:idx_user_tenant_uuid;type:int;default:0;comment:用户唯一UUID" json:"-"` Name string `gorm:"column:name;type:varchar(20);default:null;comment:真实姓名" json:"name"` Email string `gorm:"column:email;type:varchar(50);default:null;comment:邮箱" json:"email"` Job string `gorm:"column:job;type:varchar(50);default:null;comment:职务" json:"job"` Identity int `gorm:"column:identity;type:tinyint(3);default:0;comment:身份信息" json:"-"` FixedPhone string `gorm:"column:fixed_phone;type:varchar(20);default:null;comment:固定电话" json:"fixed_phone"` Address string `gorm:"column:address;type:varchar(255);default:null;comment:详细地址" json:"address"` - Selected UserTenantSelected `gorm:"column:selected;type:tinyint(1);default:0;comment:最后一次选中的身份状态,用于下次登陆展示" json:"-"` Other string `gorm:"column:other;type:varchar(255);default:null;comment:其他信息" json:"-"` + Selected UserManageSelected `gorm:"column:selected;type:tinyint(1);default:0;comment:最后一次选中的身份状态,用于下次登陆展示" json:"-"` ModelDeleted ModelAt } -type UserTenantSelected int +type UserManageSelected int const ( - // UserTenantSelectedForNo 未选中 - UserTenantSelectedForNo UserTenantSelected = iota - // UserTenantSelectedForYes 已选中 - UserTenantSelectedForYes + // UserManageSelectedForNo 未选中 + UserManageSelectedForNo UserManageSelected = iota + // UserManageSelectedForYes 已选中 + UserManageSelectedForYes ) -func (m *UserTenant) TableName() string { - return m.NewTableName("user_tenant") +func (m *UserManage) TableName() string { + return m.NewTableName("user_manage") } -func (m *UserTenant) BeforeCreate(db *gorm.DB) error { +func (m *UserManage) BeforeCreate(db *gorm.DB) error { snowflake, _ := utils.NewSnowflake(1) m.UUID = uint64(snowflake.GetID()) m.CreatedAt = time.Now() return nil } -func (m *UserTenant) GetOtherAttribute() { +func (m *UserManage) GetOtherAttribute() { } -func NewUserTenant() *UserTenant { - return &UserTenant{} +func NewUserManage() *UserManage { + return &UserManage{} } diff --git a/app/enterprise/api/account.go b/app/enterprise/api/account.go index ac114d6..ebe9527 100644 --- a/app/enterprise/api/account.go +++ b/app/enterprise/api/account.go @@ -33,7 +33,7 @@ func (a *Account) Login(c *gin.Context) { api.APIFailure(err.(error))(c) return } - data, err := account.NewLogin()().Launch(account.LoginMode(form.Mode), &account.LoginParams{ + data, err := account.NewLogin()(api.GetLocal()(c).(uint64)).Launch(account.LoginMode(form.Mode), &account.LoginParams{ Captcha: struct { Mobile string Captcha string @@ -53,7 +53,7 @@ func (a *Account) Register(c *gin.Context) { api.APIFailure(err.(error))(c) return } - data, err := account.NewRegister()().Launch(&account.RegisterParams{ + data, err := account.NewRegister()(api.GetLocal()(c).(uint64)).Launch(&account.RegisterParams{ Name: form.Name, Mobile: form.Mobile, Captcha: form.Captcha, Password: form.Password, RepeatPass: form.RepeatPass, Identity: form.Identity, }) @@ -72,6 +72,6 @@ func (a *Account) Logout(c *gin.Context) { if handle != nil { session = handle.(*service.SessionEnterprise) } - err := account.NewLogout()(session).Launch() + err := account.NewLogout()(session, api.GetLocal()(c).(uint64)).Launch() api.APIResponse(err)(c) } diff --git a/app/enterprise/api/technology.go b/app/enterprise/api/technology.go index 8cf711c..cb9d514 100644 --- a/app/enterprise/api/technology.go +++ b/app/enterprise/api/technology.go @@ -31,7 +31,8 @@ func (a *Technology) Paper(c *gin.Context) { api.APIFailure(err.(error))(c) return } - data, err := technology.NewPaper()(api.GetSession()(c).(*service.SessionEnterprise)).List(form.Title, form.Page, form.PageSize) + data, err := technology.NewPaper()(api.GetSession()(c).(*service.SessionEnterprise), api.GetLocal()(c).(uint64)). + List(form.Title, form.Page, form.PageSize) api.APIResponse(err, data) } @@ -42,10 +43,11 @@ func (a *Technology) PaperAdd(c *gin.Context) { api.APIFailure(err.(error))(c) return } - err := technology.NewPaper()(api.GetSession()(c).(*service.SessionEnterprise)).Form(&technology.PaperParams{ - Title: form.Title, Ext: form.Ext, Author: form.Author, PublishAt: form.PublishAt, - Keyword: form.Keyword, Tags: form.Tags, Remark: form.Remark, - }) + err := technology.NewPaper()(api.GetSession()(c).(*service.SessionEnterprise), api.GetLocal()(c).(uint64)). + Form(&technology.PaperParams{ + Title: form.Title, Ext: form.Ext, Author: form.Author, PublishAt: form.PublishAt, + Keyword: form.Keyword, Tags: form.Tags, Remark: form.Remark, + }) api.APIResponse(err) } @@ -58,10 +60,11 @@ func (a *Technology) PaperEdit(c *gin.Context) { api.APIFailure(err.(error))(c) return } - err := technology.NewPaper()(api.GetSession()(c).(*service.SessionEnterprise)).Form(&technology.PaperParams{ - ID: form.Convert(), Title: form.Title, Ext: form.Ext, Author: form.Author, PublishAt: form.PublishAt, - Keyword: form.Keyword, Tags: form.Tags, Remark: form.Remark, - }) + err := technology.NewPaper()(api.GetSession()(c).(*service.SessionEnterprise), api.GetLocal()(c).(uint64)). + Form(&technology.PaperParams{ + ID: form.Convert(), Title: form.Title, Ext: form.Ext, Author: form.Author, PublishAt: form.PublishAt, + Keyword: form.Keyword, Tags: form.Tags, Remark: form.Remark, + }) api.APIResponse(err) } @@ -72,6 +75,7 @@ func (a *Technology) PaperDelete(c *gin.Context) { api.APIFailure(err.(error))(c) return } - err := technology.NewPaper()(api.GetSession()(c).(*service.SessionEnterprise)).Delete(form.Convert()) + err := technology.NewPaper()(api.GetSession()(c).(*service.SessionEnterprise), api.GetLocal()(c).(uint64)). + Delete(form.Convert()) api.APIResponse(err) } diff --git a/app/enterprise/controller/account/login.go b/app/enterprise/controller/account/login.go index 420273d..4ccc20e 100644 --- a/app/enterprise/controller/account/login.go +++ b/app/enterprise/controller/account/login.go @@ -8,10 +8,10 @@ import ( "errors" ) -type Login struct{} +type Login struct{ local uint64 } type ( - LoginHandle func() *Login + LoginHandle func(local uint64) *Login ) type ( @@ -38,12 +38,12 @@ const ( LoginModeForQQ // QQ登陆 ) -var loginHandle = map[LoginMode]func(*LoginParams) (*InstanceLoginParams, error){ +var loginHandle = map[LoginMode]func(*LoginParams, uint64) (*InstanceLoginParams, error){ LoginModeForSmsCaptcha: loginForSmsCaptcha, LoginModeForPassword: loginForPassword, } // loginForSmsCaptcha 短信验证码登陆 -func loginForSmsCaptcha(params *LoginParams) (*InstanceLoginParams, error) { +func loginForSmsCaptcha(params *LoginParams, local uint64) (*InstanceLoginParams, error) { if !utils.ValidateMobile(params.Captcha.Mobile) { return nil, errors.New("手机号码格式异常") } @@ -56,18 +56,18 @@ func loginForSmsCaptcha(params *LoginParams) (*InstanceLoginParams, error) { return nil, errors.New("验证码错误或已过期") } var isExist bool - + // 查询账号信息 mUserInstance := model.NewUserInstance() if isExist, err = model2.FirstField(mUserInstance.UserInstance, []string{"id", "uuid", "name", "mobile", "status"}, - model2.NewWhere("mobile", params.Captcha.Mobile)); err != nil { + model2.NewWhere("mobile", params.Captcha.Mobile), model2.NewWhere("local", local)); err != nil { return nil, err } - - mUserTenant := model.NewUserTenant() + mUserManage := model.NewUserManage() if isExist { - if err = mUserTenant.LastChooseInfo(mUserInstance.UUID); err != nil { + // 查询该区域下最后一次选中的信息 + if err = mUserManage.LastChooseInfo(mUserInstance.UUID); err != nil { return nil, err } goto RETURNS @@ -79,22 +79,22 @@ func loginForSmsCaptcha(params *LoginParams) (*InstanceLoginParams, error) { } RETURNS: return &InstanceLoginParams{ - UID: mUserInstance.UUID, TenantID: mUserTenant.TenantID, TenantUID: mUserTenant.UUID, + UID: mUserInstance.UUID, TenantID: mUserManage.TenantID, TenantUID: mUserManage.UUID, Name: mUserInstance.Name, Mobile: mUserInstance.Mobile, - Identity: mUserInstance.Identity, SelectIdentity: mUserTenant.Identity, + Identity: mUserInstance.Identity, SelectIdentity: mUserManage.Identity, Status: mUserInstance.Status, }, nil } // loginForPassword 密码登陆 -func loginForPassword(params *LoginParams) (*InstanceLoginParams, error) { +func loginForPassword(params *LoginParams, local uint64) (*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)) + model2.NewWhere("mobile", params.Password.Mobile), model2.NewWhere("local", local)) if err != nil { return nil, err @@ -105,15 +105,15 @@ func loginForPassword(params *LoginParams) (*InstanceLoginParams, error) { return nil, errors.New("账户或密码错误") } // 最后一次选中的身份信息 - mUserTenant := model.NewUserTenant() + mUserManage := model.NewUserManage() - if err = mUserTenant.LastChooseInfo(mUserInstance.UUID); err != nil { + if err = mUserManage.LastChooseInfo(mUserInstance.UUID); err != nil { return nil, err } return &InstanceLoginParams{ - UID: mUserInstance.UUID, TenantID: mUserTenant.TenantID, TenantUID: mUserTenant.UUID, + UID: mUserInstance.UUID, TenantID: mUserManage.TenantID, TenantUID: mUserManage.UUID, Name: mUserInstance.Name, Mobile: mUserInstance.Mobile, - Identity: mUserInstance.Identity, SelectIdentity: mUserTenant.Identity, + Identity: mUserInstance.Identity, SelectIdentity: mUserManage.Identity, Status: mUserInstance.Status, }, nil } @@ -128,7 +128,7 @@ func (c *Login) Launch(mode LoginMode, params *LoginParams) (*InstanceLoginRetur if !has { return nil, errors.New("未知的登陆模式") } - ret, err := _handle(params) + ret, err := _handle(params, c.local) if err != nil { return nil, err @@ -140,7 +140,7 @@ func (c *Login) Launch(mode LoginMode, params *LoginParams) (*InstanceLoginRetur } func NewLogin() LoginHandle { - return func() *Login { - return &Login{} + return func(local uint64) *Login { + return &Login{local: local} } } diff --git a/app/enterprise/controller/account/logout.go b/app/enterprise/controller/account/logout.go index 8e9aa6f..7d4530e 100644 --- a/app/enterprise/controller/account/logout.go +++ b/app/enterprise/controller/account/logout.go @@ -6,9 +6,12 @@ import ( "SciencesServer/utils" ) -type Logout struct{ *service.SessionEnterprise } +type Logout struct { + *service.SessionEnterprise + local uint64 +} -type LogoutHandle func(*service.SessionEnterprise) *Logout +type LogoutHandle func(enterprise *service.SessionEnterprise, local uint64) *Logout func (c *Logout) Launch() error { service.Publish(config.EventForRedisHashDestroy, config.RedisKeyForAccount, utils.UintToString(c.UID)) @@ -16,7 +19,7 @@ func (c *Logout) Launch() error { } func NewLogout() LogoutHandle { - return func(enterprise *service.SessionEnterprise) *Logout { - return &Logout{enterprise} + return func(enterprise *service.SessionEnterprise, local uint64) *Logout { + return &Logout{enterprise, local} } } diff --git a/app/enterprise/controller/account/register.go b/app/enterprise/controller/account/register.go index aa7dde5..5ef4e8f 100644 --- a/app/enterprise/controller/account/register.go +++ b/app/enterprise/controller/account/register.go @@ -10,9 +10,9 @@ import ( "gorm.io/gorm" ) -type Register struct{} +type Register struct{ local uint64 } -type RegisterHandle func() *Register +type RegisterHandle func(local uint64) *Register type ( RegisterParams struct { @@ -25,65 +25,70 @@ func (c *RegisterParams) checkPassword() bool { return c.Password == c.RepeatPass } -func (c *RegisterParams) checkUserExist(mUserInstance *model2.UserInstance) (bool, error) { - var count int64 - - if err := model2.Count(mUserInstance, &count, model2.NewWhere("mobile", c.Mobile)); err != nil { - return false, err - } - return count > 0, nil -} - 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 uint64) (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("两次密码不一致") } - mUserInstance := model.NewUserInstance() - - pass, err := params.checkUserExist(mUserInstance.UserInstance) + // 验证验证码 + pass, err := params.checkCaptcha() if err != nil { return nil, err - } else if pass { - return nil, errors.New("当前手机号码已注册") - } - if pass, err = params.checkCaptcha(); err != nil { - return nil, err } else if !pass { return nil, errors.New("验证码错误或已过期") } + // 验证账号信息 + mUserInstance := model.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 - mUserTenant := model.NewUserTenant() + mUserManage := model.NewUserManage() 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.Identity = params.Identity - mUserTenant.Selected = model2.UserTenantSelectedForYes - return model2.Create(mUserTenant.UserTenant, tx) + 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: mUserTenant.UUID, Name: mUserTenant.Name, Mobile: mUserInstance.Mobile, + UID: mUserManage.UUID, Name: mUserManage.Name, Mobile: mUserInstance.Mobile, Identity: mUserInstance.Identity, SelectIdentity: params.Identity, }), err } func NewRegister() RegisterHandle { - return func() *Register { - return &Register{} + return func(local uint64) *Register { + return &Register{local: local} } } diff --git a/app/enterprise/controller/technology/patent.go b/app/enterprise/controller/technology/patent.go index 0d0a117..4c1a941 100644 --- a/app/enterprise/controller/technology/patent.go +++ b/app/enterprise/controller/technology/patent.go @@ -1,4 +1,17 @@ package technology +import "SciencesServer/app/service" + // Patent 专利管理 -type Patent struct{} +type Patent struct { + *service.SessionEnterprise + local uint64 +} + +type PatentHandle func(enterprise *service.SessionEnterprise, local uint64) *Patent + +func NewPatent() PatentHandle { + return func(enterprise *service.SessionEnterprise, local uint64) *Patent { + return &Patent{SessionEnterprise: enterprise, local: local} + } +} diff --git a/app/enterprise/model/user_manage.go b/app/enterprise/model/user_manage.go new file mode 100644 index 0000000..ba5fffd --- /dev/null +++ b/app/enterprise/model/user_manage.go @@ -0,0 +1,21 @@ +package model + +import ( + "SciencesServer/app/common/model" +) + +type UserManage struct { + *model.UserManage +} + +// LastChooseInfo 最后一次选中的信息 +func (m *UserManage) LastChooseInfo(uid uint64) error { + _, err := model.FirstField(m.UserManage, []string{"id", "tenant_id", "uuid", "identity"}, + model.NewWhere("uid", uid), + model.NewWhere("selected", model.UserManageSelectedForYes)) + return err +} + +func NewUserManage() *UserManage { + return &UserManage{UserManage: model.NewUserManage()} +} diff --git a/app/enterprise/model/user_tenant.go b/app/enterprise/model/user_tenant.go deleted file mode 100644 index 9c6577d..0000000 --- a/app/enterprise/model/user_tenant.go +++ /dev/null @@ -1,20 +0,0 @@ -package model - -import ( - "SciencesServer/app/common/model" -) - -type UserTenant struct { - *model.UserTenant -} - -func (m *UserTenant) LastChooseInfo(uid uint64) error { - _, err := model.FirstField(m.UserTenant, []string{"id", "tenant_id", "uuid", "identity"}, - model.NewWhere("uid", uid), - model.NewWhere("selected", model.UserTenantSelectedForYes)) - return err -} - -func NewUserTenant() *UserTenant { - return &UserTenant{UserTenant: model.NewUserTenant()} -} diff --git a/main.go b/main.go index 1657cc5..3de1771 100644 --- a/main.go +++ b/main.go @@ -24,6 +24,6 @@ var ( func main() { flag.Parse() runtime.GOMAXPROCS(runtime.NumCPU()) - figure.NewFigure("Tenant Serve", "", true).Print() + figure.NewFigure("SciencesServer", "", true).Print() serve.NewServe()(&serve.Option{Config: *ConfigFile, RpcConfig: *RpcConfigFile}).Run() }