feat:完善信息
This commit is contained in:
@ -1,115 +1,26 @@
|
||||
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 Instance struct{ *service.SessionEnterprise }
|
||||
|
||||
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 (
|
||||
// InstanceInfoReturn 基本信息返回
|
||||
InstanceInfoReturn struct {
|
||||
Name string `json:"name"`
|
||||
Avatar string `json:"avatar"` // 头像
|
||||
Identity string `json:"identity"`
|
||||
SelectIdentity string `json:"select_identity"` // 最后一次选中的身份信息
|
||||
Name string `json:"name"` // 名称
|
||||
Avatar string `json:"avatar"` // 头像
|
||||
Identity int `json:"identity"` // 具体身份
|
||||
SelectIdentity int `json:"select_identity"` // 最后一次选中的身份信息
|
||||
}
|
||||
)
|
||||
|
||||
// Info 基本信息
|
||||
func (c *Instance) Info() *InstanceInfoReturn {
|
||||
return &InstanceInfoReturn{Name: c.Name}
|
||||
}
|
||||
|
||||
// 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
|
||||
return &InstanceInfoReturn{Name: c.Name, Avatar: c.Avatar, Identity: c.Identity, SelectIdentity: c.SelectIdentity}
|
||||
}
|
||||
|
||||
func NewInstance() InstanceHandle {
|
||||
|
166
app/enterprise/controller/user/tenant.go
Normal file
166
app/enterprise/controller/user/tenant.go
Normal 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}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user