diff --git a/app/api/admin/api/role.go b/app/api/admin/api/role.go index 7d2fb66..5d96b34 100644 --- a/app/api/admin/api/role.go +++ b/app/api/admin/api/role.go @@ -32,7 +32,7 @@ func (*Role) Select(c *gin.Context) { func (*Role) Add(c *gin.Context) { form := &struct { Name string `json:"name" form:"name" binding:"required"` - Remark string `json:"remark" form:"remark" binding:"required"` + Remark string `json:"remark" form:"remark"` Sort int `json:"sort" form:"sort"` }{} if err := api.Bind(form)(c); err != nil { @@ -47,7 +47,7 @@ func (*Role) Edit(c *gin.Context) { form := &struct { api.IDStringForm Name string `json:"name" form:"name" binding:"required"` - Remark string `json:"remark" form:"remark" binding:"required"` + Remark string `json:"remark" form:"remark"` Sort int `json:"sort" form:"sort"` }{} if err := api.Bind(form)(c); err != nil { diff --git a/app/api/admin/controller/role/auth.go b/app/api/admin/controller/role/auth.go index fffe811..6902fb1 100644 --- a/app/api/admin/controller/role/auth.go +++ b/app/api/admin/controller/role/auth.go @@ -92,11 +92,14 @@ func (c *Auth) Bind(roleID uint64, authIDs []uint64) error { if err = model2.Creates(mSysRoleAuth.SysRoleAuth, data); err != nil { return err } - permission.AddRequest(request) + if len(request) > 0 { + permission.AddRequest(request) - _, err = permission.AddPolicies() - - return err + if _, err = permission.AddPolicies(); err != nil { + return err + } + } + return nil }) } diff --git a/app/api/admin/controller/role/menu.go b/app/api/admin/controller/role/menu.go index 07ea47e..7733c80 100644 --- a/app/api/admin/controller/role/menu.go +++ b/app/api/admin/controller/role/menu.go @@ -85,11 +85,14 @@ func (c *Menu) Bind(roleID uint64, menuIDs []uint64) error { if err = model2.Creates(mSysRoleMenu.SysRoleMenu, data, tx); err != nil { return err } - permission.AddRequest(request) + if len(request) > 0 { + permission.AddRequest(request) - _, err = permission.AddPolicies() - - return err + if _, err = permission.AddPolicies(); err != nil { + return err + } + } + return nil }) } diff --git a/app/api/admin/controller/tenant/instance.go b/app/api/admin/controller/tenant/instance.go index c4519f7..d4d0f5a 100644 --- a/app/api/admin/controller/tenant/instance.go +++ b/app/api/admin/controller/tenant/instance.go @@ -8,8 +8,12 @@ import ( "SciencesServer/app/service" "SciencesServer/app/session" config2 "SciencesServer/config" + "SciencesServer/serve/logger" + "SciencesServer/serve/orm" "SciencesServer/utils" "errors" + "fmt" + "gorm.io/gorm" ) type Instance struct { @@ -115,8 +119,19 @@ func (c *Instance) Delete(id uint64) error { } else if !isExist { return errors.New("操作错误,平台信息不存在或已被删除") } - if err = model2.Delete(mSysTenant.SysTenant); err != nil { - return err + if err = orm.GetDB().Transaction(func(tx *gorm.DB) error { + if err = model2.Delete(mSysTenant.SysTenant); err != nil { + return err + } + if _, err = service.NewPermission( + service.WithAuthTenant(fmt.Sprintf("%d", id)), + ).RemoveFilteredGroupingPolicy(); err != nil { + logger.ErrorF("Casbin 删除租户【%d】下权限信息错误:%v", id, err) + return err + } + return nil + }); err != nil { + return nil } service.Publish(config2.EventForRedisHashDestroy, config2.RedisKeyForTenant, mSysTenant.Domain) return nil diff --git a/app/api/website/api/config.go b/app/api/website/api/config.go index 1eb8a7e..414bfcc 100644 --- a/app/api/website/api/config.go +++ b/app/api/website/api/config.go @@ -8,6 +8,18 @@ import ( type Config struct{} +func (*Config) Index(c *gin.Context) { + form := &struct { + Kind int `json:"kind" form:"kind" binding:"required"` + }{} + if err := api.Bind(form)(c); err != nil { + api.APIFailure(err.(error))(c) + return + } + data, err := controller.NewConfig()(nil).Instance(form.Kind) + api.APIResponse(err, data)(c) +} + func (*Config) Area(c *gin.Context) { form := &struct { Code string `json:"code" form:"code"` diff --git a/app/api/website/controller/config.go b/app/api/website/controller/config.go index 428af62..1450ad0 100644 --- a/app/api/website/controller/config.go +++ b/app/api/website/controller/config.go @@ -1,7 +1,9 @@ package controller import ( + "SciencesServer/app/api/admin/model" "SciencesServer/app/basic/config" + model2 "SciencesServer/app/common/model" "SciencesServer/app/session" config2 "SciencesServer/config" ) @@ -10,8 +12,25 @@ type Config struct{ *session.Admin } type ConfigHandle func(session *session.Admin) *Config -func (c *Config) Basic() { +type ( + ConfigInfo struct { + Kind int `json:"kind"` + Name string `json:"name"` + Key string `json:"key"` + Value string `json:"value"` + } +) +func (c *Config) Instance(kind int) ([]*ConfigInfo, error) { + mSysConfig := model.NewSysConfig() + + out := make([]*ConfigInfo, 0) + + if err := model2.ScanFields(mSysConfig.SysConfig, &out, []string{"kind", "name", "`key`", "`value`"}, + &model2.ModelWhereOrder{Where: model2.NewWhere("kind", kind)}); err != nil { + return nil, err + } + return out, nil } // Area 区域信息 diff --git a/app/api/website/controller/platform.go b/app/api/website/controller/platform.go index 8b7c60a..6550515 100644 --- a/app/api/website/controller/platform.go +++ b/app/api/website/controller/platform.go @@ -2,7 +2,8 @@ package controller import ( "SciencesServer/app/api/website/model" - model2 "SciencesServer/app/common/model" + "SciencesServer/app/basic/config" + config2 "SciencesServer/config" ) type Platform struct{} @@ -10,36 +11,44 @@ type Platform struct{} type PlatformHandle func() *Platform type PlatformInfo struct { - Title string `json:"title"` - Link string `json:"link"` + Name string `json:"name"` + Code string `json:"code"` + Domain string `json:"domain"` Children []*PlatformInfo `json:"children"` } -// tree 树状 -func (c *Platform) tree(src []*model2.SysPlatform, parentID uint64) []*PlatformInfo { - out := make([]*PlatformInfo, 0) - - for _, v := range src { - if v.ParentID == parentID { - out = append(out, &PlatformInfo{ - Title: v.Title, - Link: v.Link, - Children: c.tree(src, v.ID), - }) - } - } - return out -} - // Instance 平台信息 -func (c *Platform) Instance() ([]*PlatformInfo, error) { - mSysPlatform := model.NewSysPlatform() - out, err := mSysPlatform.Platform() +func (c *Platform) Instance() (map[string]*PlatformInfo, error) { + mSysPlatform := model.NewSysTenant() + out, err := mSysPlatform.Tenant() if err != nil { return nil, err } - return c.tree(out, 0), nil + ret := make(map[string]*PlatformInfo, 0) + + for _, v := range out { + if _, has := ret[v.Province]; !has { + ret[v.Province] = &PlatformInfo{ + Name: config.MemoryForAreaInfo[config2.DefaultChinaAreaCode][v.Province], + Code: v.Province, + Domain: "", + Children: []*PlatformInfo{&PlatformInfo{ + Name: config.MemoryForAreaInfo[v.Province][v.City], + Code: v.City, + Domain: v.Domain, + Children: nil, + }}, + } + continue + } + ret[v.Province].Children = append(ret[v.Province].Children, &PlatformInfo{ + Name: config.MemoryForAreaInfo[v.Province][v.City], + Domain: v.Domain, + Children: nil, + }) + } + return ret, nil } func NewPlatform() PlatformHandle { diff --git a/app/api/website/model/sys_platform.go b/app/api/website/model/sys_platform.go deleted file mode 100644 index fe233be..0000000 --- a/app/api/website/model/sys_platform.go +++ /dev/null @@ -1,27 +0,0 @@ -package model - -import ( - "SciencesServer/app/common/model" - "SciencesServer/serve/orm" -) - -type SysPlatform struct { - *model.SysPlatform -} - -// Platform 平台信息 -func (m *SysPlatform) Platform() ([]*model.SysPlatform, error) { - out := make([]*model.SysPlatform, 0) - - err := orm.GetDB().Table(m.TableName()). - Select("id", "parent_id", "`key`", "`code`", "title", "link"). - Where("is_deleted = ?", model.DeleteStatusForNot). - Order("sort " + model.OrderModeToDesc). - Scan(&out).Error - - return out, err -} - -func NewSysPlatform() *SysPlatform { - return &SysPlatform{model.NewSysPlatform()} -} diff --git a/app/api/website/model/sys_tenant.go b/app/api/website/model/sys_tenant.go new file mode 100644 index 0000000..3e3bb85 --- /dev/null +++ b/app/api/website/model/sys_tenant.go @@ -0,0 +1,27 @@ +package model + +import ( + "SciencesServer/app/common/model" + "SciencesServer/serve/orm" +) + +type SysTenant struct { + *model.SysTenant +} + +// Tenant 租户平台信息 +func (m *SysTenant) Tenant() ([]*model.SysTenant, error) { + out := make([]*model.SysTenant, 0) + + err := orm.GetDB().Table(m.TableName()). + Select("id", "`key`", "name", "domain", "province", "city"). + Where("is_deleted = ?", model.DeleteStatusForNot). + Order("province " + model.OrderModeToAsc).Order("city " + model.OrderModeToAsc). + Scan(&out).Error + + return out, err +} + +func NewSysTenant() *SysTenant { + return &SysTenant{model.NewSysTenant()} +} diff --git a/app/basic/config/memory.go b/app/basic/config/memory.go index 08bfac6..4424bae 100644 --- a/app/basic/config/memory.go +++ b/app/basic/config/memory.go @@ -16,8 +16,6 @@ var ( MemoryForAreaInfo map[string]map[string]string = make(map[string]map[string]string, 0) // MemoryForIndustryInfo 行业信息 MemoryForIndustryInfo map[string]string = make(map[string]string, 0) - // MemoryForPlatformInfo 平台信息 - MemoryForPlatformInfo map[string]string = make(map[string]string, 0) ) // GetIndustryInfo 获取行业信息 diff --git a/app/common/migrate/instance.go b/app/common/migrate/instance.go index cdb06e2..4efe852 100644 --- a/app/common/migrate/instance.go +++ b/app/common/migrate/instance.go @@ -118,7 +118,7 @@ func (this *Instance) Handle() { } return out }}, - &synchronized{iModel: model.NewSysPlatform()}, &synchronized{iModel: model.NewSysNavigation()}, + &synchronized{iModel: model.NewSysNavigation()}, &synchronized{iModel: model.NewSysAbout()}, // 日志管理 &synchronized{iModel: model.NewSysLog()}, &synchronized{iModel: model.NewSysUserLoginLog()}, diff --git a/app/common/model/sys_platform.go b/app/common/model/sys_platform.go deleted file mode 100644 index b7deb9e..0000000 --- a/app/common/model/sys_platform.go +++ /dev/null @@ -1,22 +0,0 @@ -package model - -// SysPlatform 平台配置数据模型 -type SysPlatform struct { - Model - ParentID uint64 `gorm:"column:parent_id;type:int;default:0;comment:父级ID" json:"parent_id"` - Key string `gorm:"column:key;uniqueIndex:idx_sys_platform_key;type:varchar(15);default:'';comment:唯一标识" json:"key"` - Code string `gorm:"column:code;type:varchar(10);default:'';comment:区域编码" json:"code"` - Title string `gorm:"column:title;type:varchar(10);default:'';comment:区域名称" json:"title"` - Link string `gorm:"column:link;type:varchar(255);default:'';comment:访问地址" json:"link"` - Sort int `gorm:"column:sort;type:tinyint(3);default:0;comment:排序,从大到小" json:"-"` - ModelDeleted - ModelAt -} - -func (m *SysPlatform) TableName() string { - return "sys_platform" -} - -func NewSysPlatform() *SysPlatform { - return &SysPlatform{} -} diff --git a/app/service/cache.go b/app/service/cache.go index 17125b8..52165fe 100644 --- a/app/service/cache.go +++ b/app/service/cache.go @@ -57,21 +57,6 @@ func (this *Cache) Init() { } }}, ) - function( - &caches{iModel: model.NewSysPlatform(), iValues: func() interface{} { - out := make([]*model.SysPlatform, 0) - _ = model.ScanFields(model.NewSysPlatform(), &out, []string{"id", "key", "link"}) - return out - }, toCache: func(values interface{}) { - out := values.([]*model.SysPlatform) - for _, v := range out { - if v.Link == "" { - continue - } - config2.MemoryForPlatformInfo[v.Link] = v.Key - } - }}, - ) } func NewCache() CacheHandle { diff --git a/router/address.go b/router/address.go index a81becb..0202111 100644 --- a/router/address.go +++ b/router/address.go @@ -38,6 +38,7 @@ func registerAPI(app *gin.Engine) { configV1 := v1.Group("/config") { _api := new(api2.Config) + configV1.GET("", _api.Index) configV1.POST("/area", _api.Area) } // User 用户信息管理