2021-09-30 12:09:45 +08:00
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
2021-10-12 14:47:50 +08:00
|
|
|
"SciencesServer/app/basic/config"
|
|
|
|
model2 "SciencesServer/app/common/model"
|
|
|
|
"SciencesServer/app/enterprise/model"
|
2021-09-30 12:09:45 +08:00
|
|
|
"SciencesServer/app/service"
|
2021-10-12 14:47:50 +08:00
|
|
|
config2 "SciencesServer/config"
|
|
|
|
"SciencesServer/serve/orm"
|
|
|
|
"errors"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"time"
|
2021-09-30 12:09:45 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Instance struct{ *service.SessionEnterprise }
|
|
|
|
|
|
|
|
type InstanceHandle func(enterprise *service.SessionEnterprise) *Instance
|
|
|
|
|
2021-10-08 09:39:40 +08:00
|
|
|
type (
|
2021-10-12 14:47:50 +08:00
|
|
|
// InstanceInfo 基本信息
|
|
|
|
InstanceInfo struct {
|
2021-10-08 17:33:19 +08:00
|
|
|
Name string `json:"name"` // 名称
|
|
|
|
Identity int `json:"identity"` // 具体身份
|
|
|
|
SelectIdentity int `json:"select_identity"` // 最后一次选中的身份信息
|
2021-10-08 09:39:40 +08:00
|
|
|
}
|
2021-10-12 14:47:50 +08:00
|
|
|
// InstanceDetailInfo 详细信息
|
|
|
|
InstanceDetailInfo struct {
|
|
|
|
InstanceInfo
|
|
|
|
Email string `json:"email"` // 邮箱
|
|
|
|
Job string `json:"job"` // 职务
|
|
|
|
FixedPhone string `json:"fixed_phone"` // 固定电话
|
|
|
|
}
|
2021-10-08 09:39:40 +08:00
|
|
|
)
|
2021-09-30 12:09:45 +08:00
|
|
|
|
2021-10-08 09:39:40 +08:00
|
|
|
// Info 基本信息
|
2021-10-12 14:47:50 +08:00
|
|
|
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
|
2021-09-30 12:09:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewInstance() InstanceHandle {
|
|
|
|
return func(enterprise *service.SessionEnterprise) *Instance {
|
|
|
|
return &Instance{enterprise}
|
|
|
|
}
|
|
|
|
}
|