diff --git a/app/common/model/user_tenant.go b/app/common/model/user_tenant.go index 8f91f3c..3eb5c34 100644 --- a/app/common/model/user_tenant.go +++ b/app/common/model/user_tenant.go @@ -6,6 +6,7 @@ import ( "time" ) +// UserTenant 用户租户信息 type UserTenant struct { Model ModelTenant @@ -43,6 +44,10 @@ func (m *UserTenant) BeforeCreate(db *gorm.DB) error { return nil } +func (m *UserTenant) GetOtherAttribute() { + +} + func NewUserTenant() *UserTenant { return &UserTenant{} } diff --git a/app/enterprise/controller/user/instance.go b/app/enterprise/controller/user/instance.go index e35a8ab..8d7fe6b 100644 --- a/app/enterprise/controller/user/instance.go +++ b/app/enterprise/controller/user/instance.go @@ -1,7 +1,15 @@ package user import ( + "SciencesServer/app/basic/config" + model2 "SciencesServer/app/common/model" + "SciencesServer/app/enterprise/model" "SciencesServer/app/service" + config2 "SciencesServer/config" + "SciencesServer/serve/orm" + "errors" + "gorm.io/gorm" + "time" ) type Instance struct{ *service.SessionEnterprise } @@ -9,18 +17,87 @@ type Instance struct{ *service.SessionEnterprise } type InstanceHandle func(enterprise *service.SessionEnterprise) *Instance type ( - // InstanceInfoReturn 基本信息返回 - InstanceInfoReturn struct { + // InstanceInfo 基本信息 + InstanceInfo struct { Name string `json:"name"` // 名称 - Avatar string `json:"avatar"` // 头像 Identity int `json:"identity"` // 具体身份 SelectIdentity int `json:"select_identity"` // 最后一次选中的身份信息 } + // InstanceDetailInfo 详细信息 + InstanceDetailInfo struct { + InstanceInfo + Email string `json:"email"` // 邮箱 + Job string `json:"job"` // 职务 + FixedPhone string `json:"fixed_phone"` // 固定电话 + } ) // Info 基本信息 -func (c *Instance) Info() *InstanceInfoReturn { - return &InstanceInfoReturn{Name: c.Name, Avatar: c.Avatar, Identity: c.Identity, SelectIdentity: c.SelectIdentity} +func (c *Instance) Info() *InstanceInfo { + return &InstanceInfo{Name: c.Name, Identity: c.Identity, SelectIdentity: c.SelectIdentity} +} + +// Detail 详细信息 +func (c *Instance) Detail() *InstanceDetailInfo { + resp := &InstanceDetailInfo{InstanceInfo: InstanceInfo{Name: c.Name, Identity: c.Identity, SelectIdentity: c.SelectIdentity}} + + mUserTenant := model.NewUserTenant() + + isExist, err := model2.FirstField(mUserTenant.UserTenant, []string{"id", "tenant_id", "uuid", "name", + "email", "job", "fixed_phone", "other"}, + model2.NewWhere("uid", c.UID), model2.NewWhere("identity", c.SelectIdentity)) + + if err != nil || !isExist { + return resp + } + resp.Name = mUserTenant.Name + resp.Email = mUserTenant.Email + resp.Job = mUserTenant.Job + resp.FixedPhone = mUserTenant.FixedPhone + return resp +} + +// Switch 切换身份 +func (c *Instance) Switch(identity int) error { + if _, has := config.TenantUserIdentityData[identity]; !has { + return errors.New("未知的身份信息") + } + if c.SelectIdentity == identity { + return nil + } + // 已存在相应的身份,更新最后 + if c.Identity&identity > 0 { + mUserTenant := model.NewUserTenant() + // 查询用户身份 + _, err := model2.FirstField(mUserTenant.UserTenant, []string{"id", "tenant_id", "name", "uuid"}, + model2.NewWhere("uid", c.UID), model2.NewWhere("identity", identity)) + + if err != nil { + return err + } + // 查询用户身份信息 + now := time.Now() + + if mUserTenant.ID > 0 { + if err = orm.GetDB().Transaction(func(tx *gorm.DB) error { + if err = model2.UpdatesWhere(mUserTenant.UserTenant, map[string]interface{}{ + "selected": model2.UserTenantSelectedForNo, "updated_at": now, + }, []*model2.ModelWhere{model2.NewWhere("uid", c.TenantUID)}, tx); err != nil { + return err + } + return model2.Updates(mUserTenant.UserTenant, map[string]interface{}{ + "selected": model2.UserTenantSelectedForYes, "updated_at": now, + }, tx) + }); err != nil { + return err + } + } + c.TenantID = mUserTenant.TenantID + c.TenantUID = mUserTenant.UUID + } + c.SelectIdentity = identity + service.Publish(config2.EventForAccountLoginProduce, config2.RedisKeyForAccount, c.UIDToString(), c.SessionEnterprise) + return nil } func NewInstance() InstanceHandle { diff --git a/app/enterprise/controller/user/tenant.go b/app/enterprise/controller/user/tenant.go deleted file mode 100644 index 2e37c32..0000000 --- a/app/enterprise/controller/user/tenant.go +++ /dev/null @@ -1,139 +0,0 @@ -package user - -import ( - "SciencesServer/app/basic/config" - model2 "SciencesServer/app/common/model" - "SciencesServer/app/enterprise/model" - "SciencesServer/app/service" - config2 "SciencesServer/config" - "SciencesServer/serve/orm" - "errors" - "gorm.io/gorm" - "time" -) - -type Tenant struct{ *service.SessionEnterprise } - -type TenantHandle func(session *service.SessionEnterprise) *Tenant - -type TenantBasicParams struct { - Name string `json:"name"` // 名称 - Email string `json:"email"` // 邮箱 - Job string `json:"job"` // 职务 - FixedPhone string `json:"fixed_phone"` // 固定电话 -} - -type TenantIndustryParams struct { -} - -type ( - // TenantPerfectParams 完善信息参数 - TenantPerfectParams struct { - *TenantBasicParams // 基本信息 - } - // TenantParamsForCompany 公司参数信息 - TenantParamsForCompany struct { - } -) - -// tenantHandlePerfect 完善信息处理方式 -var tenantHandlePerfect = map[int]func(params *TenantPerfectParams) (string, error){ - config.TenantUserIdentityForCompany: perfectForCompany, - config.TenantUserIdentityForExpert: perfectForExpert, - config.TenantUserIdentityForResearch: perfectForResearch, - config.TenantUserIdentityForLaboratory: perfectForLaboratory, -} - -func perfectForCompany(params *TenantPerfectParams) (string, error) { - return "", nil -} - -func perfectForExpert(params *TenantPerfectParams) (string, error) { - return "", nil -} - -func perfectForResearch(params *TenantPerfectParams) (string, error) { - return "", nil -} - -func perfectForLaboratory(params *TenantPerfectParams) (string, error) { - return "", nil -} - -// Perfect 完善信息 -func (c *Tenant) Perfect(params *TenantPerfectParams) error { - // 事件处理 - _handle, has := tenantHandlePerfect[c.SelectIdentity] - - if !has { - return errors.New("未知的身份处理方式") - } - // 查询用户身份信息 - mUserTenant := model.NewUserTenant() - - isExist, err := model2.FirstField(mUserTenant.UserTenant, []string{"id", "name", "identity"}, - model2.NewWhere("uid", c.TenantUID), model2.NewWhere("identity", c.Identity)) - - if err != nil { - return err - } - mUserTenant.Name = params.TenantBasicParams.Name - mUserTenant.Email = params.TenantBasicParams.Email - mUserTenant.Job = params.TenantBasicParams.Job - mUserTenant.FixedPhone = params.TenantBasicParams.FixedPhone - - if mUserTenant.Other, err = _handle(params); err != nil { - return err - } - if !isExist { - mUserTenant.Identity = c.SelectIdentity - return model2.Create(mUserTenant.UserTenant) - } - return model2.Updates(mUserTenant.UserTenant, mUserTenant.UserTenant) -} - -// Auth 认证 -func (c *Tenant) Auth() { - -} - -// Switch 切换身份 -func (c *Tenant) Switch(identity int) error { - if _, has := config.TenantUserIdentityData[identity]; !has { - return errors.New("未知的身份信息") - } - if c.SelectIdentity == identity { - return nil - } - // 已存在相应的身份,更新最后 - if c.Identity&identity > 0 { - mUserTenant := model.NewUserTenant() - // 查询用户身份信息 - now := time.Now() - - var err error - - if err = orm.GetDB().Transaction(func(tx *gorm.DB) error { - if err = model2.UpdatesWhere(mUserTenant.UserTenant, map[string]interface{}{ - "selected": model2.UserTenantSelectedForNo, "updated_at": now, - }, []*model2.ModelWhere{model2.NewWhere("uid", c.TenantUID)}, tx); err != nil { - return err - } - return model2.UpdatesWhere(mUserTenant.UserTenant, map[string]interface{}{ - "selected": model2.UserTenantSelectedForYes, "updated_at": now, - }, []*model2.ModelWhere{model2.NewWhere("uid", c.TenantUID), - model2.NewWhere("identity", identity)}, tx) - }); err != nil { - return err - } - } - c.SelectIdentity = identity - service.Publish(config2.EventForRedisHashProduce, config2.RedisKeyForAccount, c.UIDToString(), c.SessionEnterprise) - return nil -} - -func NewTenant() TenantHandle { - return func(session *service.SessionEnterprise) *Tenant { - return &Tenant{session} - } -}