2021-10-08 17:33:19 +08:00
|
|
|
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 {
|
2021-10-11 16:30:53 +08:00
|
|
|
Name string `json:"name"` // 名称
|
|
|
|
Email string `json:"email"` // 邮箱
|
|
|
|
Job string `json:"job"` // 职务
|
|
|
|
FixedPhone string `json:"fixed_phone"` // 固定电话
|
2021-10-08 17:33:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type TenantIndustryParams struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
type (
|
|
|
|
// TenantPerfectParams 完善信息参数
|
|
|
|
TenantPerfectParams struct {
|
2021-10-11 16:30:53 +08:00
|
|
|
*TenantBasicParams // 基本信息
|
2021-10-08 17:33:19 +08:00
|
|
|
}
|
|
|
|
// TenantParamsForCompany 公司参数信息
|
|
|
|
TenantParamsForCompany struct {
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-10-11 16:30:53 +08:00
|
|
|
// tenantHandlePerfect 完善信息处理方式
|
2021-10-08 17:33:19 +08:00
|
|
|
var tenantHandlePerfect = map[int]func(params *TenantPerfectParams) (string, error){
|
2021-10-11 16:30:53 +08:00
|
|
|
config.TenantUserIdentityForCompany: perfectForCompany,
|
|
|
|
config.TenantUserIdentityForExpert: perfectForExpert,
|
|
|
|
config.TenantUserIdentityForResearch: perfectForResearch,
|
|
|
|
config.TenantUserIdentityForLaboratory: perfectForLaboratory,
|
2021-10-08 17:33:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func perfectForCompany(params *TenantPerfectParams) (string, error) {
|
2021-10-11 16:30:53 +08:00
|
|
|
return "", nil
|
2021-10-08 17:33:19 +08:00
|
|
|
}
|
|
|
|
|
2021-10-09 11:55:54 +08:00
|
|
|
func perfectForExpert(params *TenantPerfectParams) (string, error) {
|
|
|
|
return "", nil
|
2021-10-08 17:33:19 +08:00
|
|
|
}
|
|
|
|
|
2021-10-11 16:30:53 +08:00
|
|
|
func perfectForResearch(params *TenantPerfectParams) (string, error) {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func perfectForLaboratory(params *TenantPerfectParams) (string, error) {
|
|
|
|
return "", nil
|
2021-10-08 17:33:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Perfect 完善信息
|
|
|
|
func (c *Tenant) Perfect(params *TenantPerfectParams) error {
|
|
|
|
// 事件处理
|
|
|
|
_handle, has := tenantHandlePerfect[c.SelectIdentity]
|
|
|
|
|
|
|
|
if !has {
|
|
|
|
return errors.New("未知的身份处理方式")
|
|
|
|
}
|
|
|
|
// 查询用户身份信息
|
|
|
|
mUserTenant := model.NewUserTenant()
|
|
|
|
|
2021-10-11 16:30:53 +08:00
|
|
|
isExist, err := model2.FirstField(mUserTenant.UserTenant, []string{"id", "name", "identity"},
|
2021-10-08 17:33:19 +08:00
|
|
|
model2.NewWhere("uid", c.UID), model2.NewWhere("identity", c.Identity))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
mUserTenant.Name = params.TenantBasicParams.Name
|
|
|
|
mUserTenant.Email = params.TenantBasicParams.Email
|
2021-10-11 16:30:53 +08:00
|
|
|
mUserTenant.Job = params.TenantBasicParams.Job
|
|
|
|
mUserTenant.FixedPhone = params.TenantBasicParams.FixedPhone
|
2021-10-08 17:33:19 +08:00
|
|
|
|
|
|
|
if mUserTenant.Other, err = _handle(params); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-10-11 16:30:53 +08:00
|
|
|
if !isExist {
|
|
|
|
mUserTenant.Identity = c.SelectIdentity
|
|
|
|
return model2.Create(mUserTenant.UserTenant)
|
2021-10-08 17:33:19 +08:00
|
|
|
}
|
2021-10-11 16:30:53 +08:00
|
|
|
return model2.Updates(mUserTenant.UserTenant, mUserTenant.UserTenant)
|
2021-10-08 17:33:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.UID)}, 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.UID),
|
|
|
|
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}
|
|
|
|
}
|
|
|
|
}
|