feat:完善信息
This commit is contained in:
@ -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 {
|
||||
|
Reference in New Issue
Block a user