feat:完善信息

This commit is contained in:
henry
2021-10-08 17:33:19 +08:00
parent fe75ad1aec
commit 2191f0ea3f
15 changed files with 288 additions and 215 deletions

View File

@ -9,7 +9,6 @@ import (
"SciencesServer/app/service" "SciencesServer/app/service"
"SciencesServer/config" "SciencesServer/config"
"SciencesServer/serve/orm" "SciencesServer/serve/orm"
"SciencesServer/utils"
"errors" "errors"
"fmt" "fmt"
"time" "time"
@ -25,7 +24,6 @@ type (
// InstanceInfo 租户信息 // InstanceInfo 租户信息
InstanceInfo struct { InstanceInfo struct {
*model.SysTenantInfo *model.SysTenantInfo
Config *model2.SysTenantConfig `json:"config"`
} }
// InstanceParams 租户参数信息 // InstanceParams 租户参数信息
InstanceParams struct { InstanceParams struct {
@ -72,7 +70,7 @@ func basic(id uint64) func(args ...interface{}) (interface{}, error) {
} }
return &InstanceInfo{SysTenantInfo: &model.SysTenantInfo{ return &InstanceInfo{SysTenantInfo: &model.SysTenantInfo{
SysTenant: mSysTenant.SysTenant, SysTenant: mSysTenant.SysTenant,
}, Config: mSysTenant.ConfigInfo()}, nil }}, nil
} }
} }
@ -165,7 +163,7 @@ func (c *Instance) List(name string, status, page, pageSize int) (*controller.Re
for _, v := range out { for _, v := range out {
v.Image.Image = v.Image.Analysis(config.SettingInfo.Domain) v.Image.Image = v.Image.Analysis(config.SettingInfo.Domain)
list = append(list, &InstanceInfo{SysTenantInfo: v, Config: v.ConfigInfo()}) list = append(list, &InstanceInfo{SysTenantInfo: v})
} }
return &controller.ReturnPages{Data: list, Page: page, TotalCount: count}, nil return &controller.ReturnPages{Data: list, Page: page, TotalCount: count}, nil
} }
@ -194,8 +192,6 @@ func (c *Instance) Add(params *InstanceParams) error {
if err = orm.GetDB().Transaction(func(tx *gorm.DB) error { if err = orm.GetDB().Transaction(func(tx *gorm.DB) error {
mSysTenant.Image.Image = params.Image mSysTenant.Image.Image = params.Image
mSysTenant.Name = params.Name mSysTenant.Name = params.Name
mSysTenant.Config = utils.AnyToJSON(&model2.SysTenantConfig{MaxDevices: 0, MaxCustomer: 0, Protocol: 0})
mSysTenant.Deadline = utils.DateTimeToTime(params.Deadline + " 23:59:59")
mSysTenant.Remark = params.Remark mSysTenant.Remark = params.Remark
if err = model2.Create(mSysTenant.SysTenant, tx); err != nil { if err = model2.Create(mSysTenant.SysTenant, tx); err != nil {
@ -220,7 +216,6 @@ func (c *Instance) Add(params *InstanceParams) error {
}); err != nil { }); err != nil {
return err return err
} }
service.Publish(config.EventForRedisListProduce, config.RedisKeyForTenant, mSysTenant.Key)
return nil return nil
} }
@ -338,20 +333,9 @@ func (c *Instance) Renewal(id uint64, deadline string) error {
updates := make(map[string]interface{}, 0) updates := make(map[string]interface{}, 0)
updates["updated_at"] = now updates["updated_at"] = now
_deadline := utils.DateTimeToTime(deadline + " 23:59:59")
if _deadline.Before(mSysTenant.Deadline) {
return errors.New("续期时间异常")
}
updates["deadline"] = _deadline
if mSysTenant.Status != model2.SysTenantStatusForNormal && _deadline.After(now) {
updates["status"] = model2.SysTenantStatusForNormal
}
if err = model2.Updates(mSysTenant.SysTenant, updates); err != nil { if err = model2.Updates(mSysTenant.SysTenant, updates); err != nil {
return err return err
} }
service.Publish(config.EventForRedisListProduce, config.RedisKeyForTenant, mSysTenant.Key)
return nil return nil
} }
@ -372,19 +356,8 @@ func (c *Instance) StartUp(id uint64) error {
} else if c.TenantID != mSysTenant.ParentID { } else if c.TenantID != mSysTenant.ParentID {
return errors.New("不可启用其他租户/公司客户") return errors.New("不可启用其他租户/公司客户")
} }
} else if mSysTenant.Status == model2.SysTenantStatusForNormal {
return errors.New("该租户/公司/客户已是启用状态")
} }
status := model2.SysTenantStatusForNormal if err = model2.Updates(mSysTenant.SysTenant, map[string]interface{}{}); err != nil {
now := time.Now()
if mSysTenant.Deadline.Before(now) {
status = model2.SysTenantStatusForExpired
}
if err = model2.Updates(mSysTenant.SysTenant, map[string]interface{}{
"status": status, "updated_at": now,
}); err != nil {
return err return err
} }
return nil return nil
@ -392,31 +365,31 @@ func (c *Instance) StartUp(id uint64) error {
// Disable 禁用处理 // Disable 禁用处理
func (c *Instance) Disable(id uint64) error { func (c *Instance) Disable(id uint64) error {
mSysTenant := model.NewSysTenant() //mSysTenant := model.NewSysTenant()
mSysTenant.ID = id //mSysTenant.ID = id
//
//isExist, err := model2.FirstWhere(mSysTenant.SysTenant)
isExist, err := model2.FirstWhere(mSysTenant.SysTenant) //if err != nil {
// return err
if err != nil { //} else if !isExist {
return err // return errors.New("租户/公司/客户信息不存在")
} else if !isExist { //} else if c.TenantID > 0 {
return errors.New("租户/公司/客户信息不存在") // if mSysTenant.ParentID <= 0 {
} else if c.TenantID > 0 { // return errors.New("非超级管理员,不可操作")
if mSysTenant.ParentID <= 0 { // } else if c.TenantID != mSysTenant.ParentID {
return errors.New("非超级管理员,不可操作") // return errors.New("不可启用其他租户/公司/客户")
} else if c.TenantID != mSysTenant.ParentID { // }
return errors.New("不可启用其他租户/公司/客户") //} else if mSysTenant.Status == model2.SysTenantStatusForDisable {
} // return errors.New("该租户/公司/客户已是禁用状态")
} else if mSysTenant.Status == model2.SysTenantStatusForDisable { //}
return errors.New("该租户/公司/客户已是禁用状态") //if err = model2.Updates(mSysTenant.SysTenant, map[string]interface{}{
} // "status": model2.SysTenantStatusForDisable, "updated_at": time.Now(),
if err = model2.Updates(mSysTenant.SysTenant, map[string]interface{}{ //}); err != nil {
"status": model2.SysTenantStatusForDisable, "updated_at": time.Now(), // return err
}); err != nil { //}
return err //// TODO推送用户强制退出并强行删除所有产品数据
} //service.Publish(config.EventForRedisListDestroy, config.RedisKeyForTenant, mSysTenant.Key)
// TODO推送用户强制退出并强行删除所有产品数据
service.Publish(config.EventForRedisListDestroy, config.RedisKeyForTenant, mSysTenant.Key)
return nil return nil
} }

View File

@ -4,7 +4,6 @@ import (
"SciencesServer/app/common/model" "SciencesServer/app/common/model"
"SciencesServer/serve/orm" "SciencesServer/serve/orm"
"fmt" "fmt"
"time"
) )
type SysTenant struct { type SysTenant struct {
@ -38,7 +37,7 @@ type (
// IsInvalid 判断是否有效 // IsInvalid 判断是否有效
func (m *SysTenant) IsInvalid() bool { func (m *SysTenant) IsInvalid() bool {
return m.Status == model.SysTenantStatusForDisable || m.Status == model.SysTenantStatusForExpired || m.Deadline.Before(time.Now()) return false
} }
// Tenants 租户信息 // Tenants 租户信息

View File

@ -14,7 +14,7 @@ const (
// TenantUserIdentityData 用户身份信息 // TenantUserIdentityData 用户身份信息
var TenantUserIdentityData = map[int]string{ var TenantUserIdentityData = map[int]string{
TenantUserIdentityForPerson: "个人", TenantUserIdentityForPerson: "游客",
TenantUserIdentityForCompany: "企业", TenantUserIdentityForExpert: "专家", TenantUserIdentityForCompany: "企业", TenantUserIdentityForExpert: "专家",
TenantUserIdentityForResearch: "研究机构", TenantUserIdentityForLaboratory: "实验室", TenantUserIdentityForResearch: "研究机构", TenantUserIdentityForLaboratory: "实验室",
TenantUserIdentityForAgent: "科技经纪人", TenantUserIdentityForAgent: "科技经纪人",

View File

@ -0,0 +1 @@
package controller

View File

@ -0,0 +1,12 @@
package model
type SysIndustry struct {
}
func (*SysIndustry) TableName() string {
return "sys_industry"
}
func NewSysIndustry() *SysIndustry {
return &SysIndustry{}
}

View File

@ -1,69 +1,25 @@
package model package model
import (
"SciencesServer/utils"
"time"
"gorm.io/gorm"
)
type SysTenant struct { type SysTenant struct {
Model Model
ParentID uint64 `gorm:"column:parent_id;type:int;default:0;comment:父级ID" json:"-"` ParentID uint64 `gorm:"column:parent_id;type:int;default:0;comment:父级ID" json:"-"`
Key string `gorm:"column:key;type:varchar(100);default:null;comment:租户/租户客户标识" json:"key"`
Image Image
Name string `gorm:"column:name;type:varchar(30);default:null;comment:租户名称/租户客户名称" json:"name"` Name string `gorm:"column:name;type:varchar(30);default:null;comment:名称" json:"name"`
Config string `gorm:"column:config;type:text;comment:租户/租户客户配置信息" json:"-"` Code string `gorm:"column:code;type:varchar(30);default:null;comment:信用代码" json:"code"`
Deadline time.Time `gorm:"column:deadline;type:datetime;default:null;comment:租户/租户客户协议截止时间" json:"deadline"` Images
Status SysTenantStatus `gorm:"column:status;type:tinyint(1);default:1;comment:租户/租户客户状态" json:"status"` Area
Remark string `gorm:"column:remark;type:varchar(255);default:null;comment:租户/租户客户备注" json:"remark"` Position string `gorm:"column:position;type:varchar(255);default:null;comment:坐标" json:"-"`
Industry uint64 `gorm:"column:industry;type:int(11);default:0;comment:所属领域" json:"industry"`
Introduce string `gorm:"column:introduce;type:text;comment:介绍描述" json:"introduce"`
Config string `gorm:"column:config;type:varchar(255);default:null;comment:配置信息" json:"-"`
ModelDeleted ModelDeleted
ModelAt ModelAt
} }
type SysTenantConfig struct {
MaxDevices int `json:"max_devices"` // 最大可连接设备数
MaxCustomer int `json:"max_customer"` // 最大可拥有的客户数
Protocol uint `json:"protocol"` // 支持的协议模式
}
type SysTenantStatus int
const (
// SysTenantStatusForNormal 正常
SysTenantStatusForNormal SysTenantStatus = iota + 1
// SysTenantStatusForWellExpire 协议将到期
SysTenantStatusForWellExpire
// SysTenantStatusForExpired 协议已过期
SysTenantStatusForExpired
// SysTenantStatusForDisable 已禁用
SysTenantStatusForDisable
)
func (m *SysTenant) TableName() string { func (m *SysTenant) TableName() string {
return "sys_tenant" return "sys_tenant"
} }
func (m *SysTenant) BeforeCreate(db *gorm.DB) error {
snowflake, _ := utils.NewSnowflake(1)
m.Key = utils.IntToString(snowflake.GetID())
m.CreatedAt = time.Now()
return nil
}
func (m *SysTenant) ConfigInfo() *SysTenantConfig {
config := new(SysTenantConfig)
_ = utils.FromJSON(m.Config, config)
return config
}
func (m *SysTenant) StatusTitle() string {
if m.Status == SysTenantStatusForDisable {
return "禁用"
}
return "正常"
}
func NewSysTenant() *SysTenant { func NewSysTenant() *SysTenant {
return &SysTenant{} return &SysTenant{}
} }

View File

@ -3,12 +3,12 @@ package model
type UserTenant struct { type UserTenant struct {
Model Model
ModelTenant ModelTenant
UID uint64 `gorm:"column:uid;index:idx_tenant_user_uuid;type:int;default:0;comment:用户表UUID" json:"-"` UID uint64 `gorm:"column:uid;index:idx_tenant_user_uuid;type:int;default:0;comment:用户表UUID" json:"-"`
Avatar string `gorm:"column:avatar;type:varchar(255);default:null;comment:头像" json:"avatar"` Avatar string `gorm:"column:avatar;type:varchar(255);default:null;comment:头像" json:"avatar"`
Name string `gorm:"column:name;type:varchar(20);default:null;comment:真实姓名" json:"name"` 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"` Email string `gorm:"column:email;type:varchar(50);default:null;comment:邮箱" json:"email"`
Identity int `gorm:"column:identity;type:tinyint(3);default:0;comment:身份信息" json:"-"` Identity int `gorm:"column:identity;type:tinyint(3);default:0;comment:身份信息" json:"-"`
Area 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:"-"` Selected UserTenantSelected `gorm:"column:selected;type:tinyint(1);default:0;comment:最后一次选中的身份状态,用于下次登陆展示" json:"-"`
Other string `gorm:"column:other;type:varchar(255);default:null;comment:其他信息" json:"-"` Other string `gorm:"column:other;type:varchar(255);default:null;comment:其他信息" json:"-"`
Status UserTenantStatus `gorm:"column:status;type:tinyint(0);default:0;comment:状态" json:"-"` Status UserTenantStatus `gorm:"column:status;type:tinyint(0);default:0;comment:状态" json:"-"`
@ -28,12 +28,10 @@ const (
type UserTenantStatus int type UserTenantStatus int
const ( const (
// UserTenantStatusForInit 初始化 // UserTenantStatusForExamineRefuse 审核拒绝
UserTenantStatusForInit UserTenantStatus = iota UserTenantStatusForExamineRefuse UserTenantStatus = iota
// UserTenantStatusForExamining 审核中 // UserTenantStatusForExamining 审核中
UserTenantStatusForExamining UserTenantStatusForExamining
// UserTenantStatusForExamineRefuse 审核拒绝
UserTenantStatusForExamineRefuse
// UserTenantStatusForExaminePass 审核通过 // UserTenantStatusForExaminePass 审核通过
UserTenantStatusForExaminePass UserTenantStatusForExaminePass
) )

View File

@ -12,3 +12,7 @@ func (a *Config) Identity(c *gin.Context) {
data := controller.NewConfig().Identity() data := controller.NewConfig().Identity()
api.APISuccess(data) api.APISuccess(data)
} }
func (a *Config) Industry(c *gin.Context) {
}

View File

@ -1,3 +1,41 @@
package api package api
import (
"SciencesServer/app/basic/api"
"SciencesServer/app/enterprise/controller/user"
"SciencesServer/app/service"
"github.com/gin-gonic/gin"
)
type User struct{} type User struct{}
func (a *User) Info(c *gin.Context) {
data := user.NewInstance()(api.GetSession()(c).(*service.SessionEnterprise)).Info()
api.APISuccess(data)
}
func (a *User) Perfect(c *gin.Context) {
form := &struct {
Avatar string `json:"avatar" form:"avatar" binding:"required"` // 头像
Name string `json:"name" form:"name" binding:"required"` // 名称
Email string `json:"email" form:"email" binding:"required"` // 邮箱
Job string `json:"job" form:"job" binding:"required"` // 职务
Address string `json:"address" form:"address" ` // 地址
Company user.TenantParamsForCompany `json:"company" form:"company"` // 公司属性
}{}
if err := api.Bind(form)(c); err != nil {
api.APIFailure(err.(error))(c)
return
}
err := user.NewTenant()(api.GetSession()(c).(*service.SessionEnterprise)).Perfect(&user.TenantPerfectParams{
TenantBasicParams: &user.TenantBasicParams{
Avatar: form.Avatar, Name: form.Name, Email: form.Email, Job: form.Job, Address: form.Address,
},
TenantParamsForCompany: &form.Company,
})
api.APIResponse(err)
}
func (a *User) SwitchIdentity(c *gin.Context) {
}

View File

@ -40,7 +40,7 @@ func (c *Instance) Login() InstanceLoginCallback {
session.Identity = params.Identity session.Identity = params.Identity
session.SelectIdentity = params.SelectIdentity session.SelectIdentity = params.SelectIdentity
service.Publish(config.EventForRedisHashProduce, config.RedisKeyForAccount, utils.UintToString(params.UID), session) service.Publish(config.EventForRedisHashProduce, config.RedisKeyForAccount, session.UIDToString(), session)
return &InstanceLoginReturn{Token: token, EffectTime: config.SettingInfo.TokenEffectTime} return &InstanceLoginReturn{Token: token, EffectTime: config.SettingInfo.TokenEffectTime}
} }

View File

@ -1,115 +1,26 @@
package user package user
import ( import (
"SciencesServer/app/basic/config"
model2 "SciencesServer/app/common/model"
"SciencesServer/app/enterprise/model"
"SciencesServer/app/service" "SciencesServer/app/service"
config2 "SciencesServer/config"
"SciencesServer/serve/orm"
"SciencesServer/utils"
"errors"
"gorm.io/gorm"
"time"
) )
type Instance struct{ *service.SessionEnterprise } type Instance struct{ *service.SessionEnterprise }
type InstanceHandle func(enterprise *service.SessionEnterprise) *Instance type InstanceHandle func(enterprise *service.SessionEnterprise) *Instance
type InstanceBasic struct {
Avatar string `json:"avatar"` // 头像
Name string `json:"name"` // 名称
Email string `json:"email"` // 邮箱
}
type (
// InstancePerfectParams 完善信息参数
InstancePerfectParams struct {
*InstanceBasic
*config.Area
}
)
type ( type (
// InstanceInfoReturn 基本信息返回 // InstanceInfoReturn 基本信息返回
InstanceInfoReturn struct { InstanceInfoReturn struct {
Name string `json:"name"` Name string `json:"name"` // 名称
Avatar string `json:"avatar"` // 头像 Avatar string `json:"avatar"` // 头像
Identity string `json:"identity"` Identity int `json:"identity"` // 具体身份
SelectIdentity string `json:"select_identity"` // 最后一次选中的身份信息 SelectIdentity int `json:"select_identity"` // 最后一次选中的身份信息
} }
) )
// Info 基本信息 // Info 基本信息
func (c *Instance) Info() *InstanceInfoReturn { func (c *Instance) Info() *InstanceInfoReturn {
return &InstanceInfoReturn{Name: c.Name} return &InstanceInfoReturn{Name: c.Name, Avatar: c.Avatar, Identity: c.Identity, SelectIdentity: c.SelectIdentity}
}
// Perfect 完善信息
func (c *Instance) Perfect(params *InstancePerfectParams) error {
// 查询用户身份信息
mUserTenant := model.NewUserTenant()
_, err := model2.FirstField(mUserTenant.UserTenant, []string{"id", "name", "identity"},
model2.NewWhere("uuid", c.UID))
if err != nil {
return err
}
mUserTenant.Avatar = params.Avatar
mUserTenant.Name = params.Name
mUserTenant.Email = params.Email
mUserTenant.Area = model2.Area{Province: params.Province, City: params.City, District: params.District, Address: params.Address}
// 更新身份信息,提交审核
if mUserTenant.Identity == 0 {
mUserTenant.Identity = c.CurrentIdentity
mUserTenant.Status = model2.UserTenantStatusForExamining
}
orm.GetDB().Transaction(func(tx *gorm.DB) error {
return nil
})
return nil
}
// ChangeIdentity 更改身份信息
func (c *Instance) ChangeIdentity(identity int) error {
if _, has := config.TenantUserIdentityData[identity]; !has {
return errors.New("未知的身份信息")
}
if c.CurrentIdentity == identity {
return nil
}
// 已存在相应的身份,更新最后
if c.Identity&identity > 0 {
mUserTenant := model.NewUserTenant()
// 查询用户身份信息
_, err := model2.FirstField(mUserTenant.UserTenant, []string{"id", "uuid", "identity"},
model2.NewWhere("uuid", c.UID), model2.NewWhere("identity", identity))
if err != nil {
return err
}
//c.UID = mUserTenant.UUID
now := time.Now()
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("uuid", 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("uuid", c.UID),
model2.NewWhere("identity", identity)}, tx)
}); err != nil {
return err
}
}
c.CurrentIdentity = identity
service.Publish(config2.EventForRedisHashProduce, config2.RedisKeyForAccount, utils.UintToString(c.UID), c.SessionEnterprise)
return nil
} }
func NewInstance() InstanceHandle { func NewInstance() InstanceHandle {

View File

@ -0,0 +1,166 @@
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"
"SciencesServer/utils"
"errors"
"gorm.io/gorm"
"time"
)
type Tenant struct{ *service.SessionEnterprise }
type TenantHandle func(session *service.SessionEnterprise) *Tenant
type TenantBasicParams struct {
Avatar string `json:"avatar"` // 头像
Name string `json:"name"` // 名称
Email string `json:"email"` // 邮箱
Job string `json:"job"` // 职务
Address string `json:"address"` // 详细地址
}
type TenantIndustryParams struct {
}
type (
// TenantPerfectParams 完善信息参数
TenantPerfectParams struct {
*TenantBasicParams // 基本信息
*TenantParamsForCompany // 公司身份信息
}
// TenantParamsForCompany 公司参数信息
TenantParamsForCompany struct {
Name string `json:"name"`
Code string `json:"code"`
config.Area
model2.Position
Industry string `json:"industry"`
Introduce string `json:"introduce"`
}
)
var tenantHandlePerfect = map[int]func(params *TenantPerfectParams) (string, error){
config.TenantUserIdentityForCompany: perfectForCompany,
}
func perfectForCompany(params *TenantPerfectParams) (string, error) {
if params.TenantParamsForCompany == nil {
return "", errors.New("企业信息异常")
}
return utils.AnyToJSON(params.TenantParamsForCompany), nil
}
func perfectForExpert() error {
return nil
}
func perfectForResearch() 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", "status"},
model2.NewWhere("uid", c.UID), model2.NewWhere("identity", c.Identity))
if err != nil {
return err
} else if isExist {
if mUserTenant.Status != model2.UserTenantStatusForExamining {
return errors.New("资料审核中,不可修改")
} else if mUserTenant.Status != model2.UserTenantStatusForExaminePass {
return errors.New("资料审核已通过,不可修改")
}
}
mUserTenant.Avatar = params.TenantBasicParams.Avatar
mUserTenant.Name = params.TenantBasicParams.Name
mUserTenant.Email = params.TenantBasicParams.Email
mUserTenant.Identity = c.SelectIdentity
mUserTenant.Address = params.TenantBasicParams.Address
if mUserTenant.Other, err = _handle(params); err != nil {
return err
}
mUserTenant.Status = model2.UserTenantStatusForExamining
if isExist {
if err = model2.Updates(mUserTenant.UserTenant, mUserTenant.UserTenant); err != nil {
return err
}
return nil
}
if err = orm.GetDB().Transaction(func(tx *gorm.DB) error {
if err = model2.Updates(mUserTenant.UserTenant, map[string]interface{}{
"selected": model2.UserTenantSelectedForNo, "updated_at": time.Now(),
}); err != nil {
return err
}
mUserTenant.Selected = model2.UserTenantSelectedForYes
return model2.Create(mUserTenant.UserTenant, tx)
}); err != nil {
return err
}
return nil
}
// 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}
}
}

View File

@ -0,0 +1,11 @@
package model
import "SciencesServer/app/common/model"
type SysIndustry struct {
*model.SysIndustry
}
func NewSysIndustry() *SysIndustry {
return &SysIndustry{SysIndustry: model.NewSysIndustry()}
}

View File

@ -9,7 +9,7 @@ type UserTenant struct {
} }
func (m *UserTenant) LastChooseInfo(uid uint64) error { func (m *UserTenant) LastChooseInfo(uid uint64) error {
_, err := model.FirstField(m.UserTenant, []string{"id", "uuid", "avatar", "identity"}, _, err := model.FirstField(m.UserTenant, []string{"id", "uid", "avatar", "identity"},
model.NewWhere("uid", uid), model.NewWhere("uid", uid),
model.NewWhere("selected", model.UserTenantSelectedForYes)) model.NewWhere("selected", model.UserTenantSelectedForYes))
return err return err

View File

@ -38,6 +38,10 @@ type SessionEnterprise struct {
SelectIdentity int `json:"select_identity"` // 选中身份信息 SelectIdentity int `json:"select_identity"` // 选中身份信息
} }
func (this *SessionEnterprise) UIDToString() string {
return utils.UintToString(this.UID)
}
func (this *SessionEnterprise) MarshalBinary() ([]byte, error) { func (this *SessionEnterprise) MarshalBinary() ([]byte, error) {
return json.Marshal(this) return json.Marshal(this)
} }