mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-29 17:41:26 +08:00
【feature】添加组织数据读取权限
This commit is contained in:
@@ -39,13 +39,14 @@ func (p *DeviceApi) GetDeviceList(rc *restfulx.ReqCtx) {
|
||||
data.DeviceType = restfulx.QueryParam(rc, "deviceType")
|
||||
data.ParentId = restfulx.QueryParam(rc, "parentId")
|
||||
data.LinkStatus = restfulx.QueryParam(rc, "linkStatus")
|
||||
|
||||
// 权限检查
|
||||
data.RoleId = rc.LoginAccount.RoleId
|
||||
list, total := p.DeviceApp.FindListPage(pageNum, pageSize, data)
|
||||
|
||||
rc.ResData = model.ResultPage{
|
||||
Total: total,
|
||||
PageNum: int64(pageNum),
|
||||
PageSize: int64(pageNum),
|
||||
PageSize: int64(pageSize),
|
||||
Data: list,
|
||||
}
|
||||
}
|
||||
@@ -135,6 +136,7 @@ func (p *DeviceApi) InsertDevice(rc *restfulx.ReqCtx) {
|
||||
biz.IsTrue(!(list != nil && len(*list) > 0), fmt.Sprintf("名称%s已存在,设置其他命名", data.Name))
|
||||
data.Id = kgo.KStr.Uniqid("d_")
|
||||
data.Owner = rc.LoginAccount.UserName
|
||||
data.OrgId = rc.LoginAccount.OrganizationId
|
||||
data.LinkStatus = global.INACTIVE
|
||||
data.LastAt = time.Now()
|
||||
p.DeviceApp.Insert(data)
|
||||
|
||||
@@ -31,7 +31,7 @@ func (p *DeviceAlarmApi) GetDeviceAlarmList(rc *restfulx.ReqCtx) {
|
||||
rc.ResData = model.ResultPage{
|
||||
Total: total,
|
||||
PageNum: int64(pageNum),
|
||||
PageSize: int64(pageNum),
|
||||
PageSize: int64(pageSize),
|
||||
Data: list,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ func (p *DeviceCmdLogApi) GetDeviceCmdLogList(rc *restfulx.ReqCtx) {
|
||||
rc.ResData = model.ResultPage{
|
||||
Total: total,
|
||||
PageNum: int64(pageNum),
|
||||
PageSize: int64(pageNum),
|
||||
PageSize: int64(pageSize),
|
||||
Data: list,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,6 +60,7 @@ func (p *DeviceGroupApi) InsertDeviceGroup(rc *restfulx.ReqCtx) {
|
||||
restfulx.BindJsonAndValid(rc, &data)
|
||||
data.Id = kgo.KStr.Uniqid("dg_")
|
||||
data.Owner = rc.LoginAccount.UserName
|
||||
data.OrgId = rc.LoginAccount.OrganizationId
|
||||
p.DeviceGroupApp.Insert(data)
|
||||
}
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ func (p *ProductApi) GetProductList(rc *restfulx.ReqCtx) {
|
||||
rc.ResData = model.ResultPage{
|
||||
Total: total,
|
||||
PageNum: int64(pageNum),
|
||||
PageSize: int64(pageNum),
|
||||
PageSize: int64(pageSize),
|
||||
Data: list,
|
||||
}
|
||||
}
|
||||
@@ -68,6 +68,7 @@ func (p *ProductApi) InsertProduct(rc *restfulx.ReqCtx) {
|
||||
restfulx.BindJsonAndValid(rc, &data)
|
||||
data.Id = kgo.KStr.Uniqid("p_")
|
||||
data.Owner = rc.LoginAccount.UserName
|
||||
data.OrgId = rc.LoginAccount.OrganizationId
|
||||
p.ProductApp.Insert(data)
|
||||
}
|
||||
|
||||
|
||||
@@ -61,6 +61,7 @@ func (p *ProductCategoryApi) InsertProductCategory(rc *restfulx.ReqCtx) {
|
||||
restfulx.BindJsonAndValid(rc, &data)
|
||||
data.Id = kgo.KStr.Uniqid("pc_")
|
||||
data.Owner = rc.LoginAccount.UserName
|
||||
data.OrgId = rc.LoginAccount.OrganizationId
|
||||
p.ProductCategoryApp.Insert(data)
|
||||
}
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ func (p *ProductOtaApi) GetProductOtaList(rc *restfulx.ReqCtx) {
|
||||
rc.ResData = model.ResultPage{
|
||||
Total: total,
|
||||
PageNum: int64(pageNum),
|
||||
PageSize: int64(pageNum),
|
||||
PageSize: int64(pageSize),
|
||||
Data: list,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ func (p *ProductTemplateApi) GetProductTemplateList(rc *restfulx.ReqCtx) {
|
||||
rc.ResData = model.ResultPage{
|
||||
Total: total,
|
||||
PageNum: int64(pageNum),
|
||||
PageSize: int64(pageNum),
|
||||
PageSize: int64(pageSize),
|
||||
Data: list,
|
||||
}
|
||||
}
|
||||
@@ -57,6 +57,7 @@ func (p *ProductTemplateApi) InsertProductTemplate(rc *restfulx.ReqCtx) {
|
||||
var data entity.ProductTemplate
|
||||
restfulx.BindJsonAndValid(rc, &data)
|
||||
data.Id = kgo.KStr.Uniqid("tm_")
|
||||
data.OrgId = rc.LoginAccount.OrganizationId
|
||||
p.ProductTemplateApp.Insert(data)
|
||||
// 向超级表中添加字段
|
||||
if data.Classify == entity.ATTRIBUTES_TSL {
|
||||
|
||||
@@ -19,6 +19,8 @@ type DeviceGroup struct {
|
||||
Status string `gorm:"status;type:varchar(1);comment:状态" json:"status"`
|
||||
Ext Ext `json:"ext" gorm:"type:json;comment:扩展"` //可扩展的kv map,承载设备组的外围信息
|
||||
Children []DeviceGroup `json:"children" gorm:"-"` //子节点
|
||||
|
||||
RoleId int64 `gorm:"-"` // 角色数据权限
|
||||
}
|
||||
|
||||
type DeviceGroupLabel struct {
|
||||
@@ -42,7 +44,10 @@ type Device struct {
|
||||
LastAt time.Time `gorm:"column:last_time;comment:最后一次在线时间" json:"lastTime"`
|
||||
OtaVersion string `json:"otaVersion" gorm:"type:varchar(64);comment:固件版本" ` //上一次固件升级的版本
|
||||
Ext Ext `json:"ext" gorm:"type:json;comment:扩展"` //可扩展的kv map,承载设备组的外围信息
|
||||
|
||||
RoleId int64 `gorm:"-"` // 角色数据权限
|
||||
}
|
||||
|
||||
type DeviceRes struct {
|
||||
Device
|
||||
DeviceGroup DeviceGroup `json:"deviceGroup" gorm:"foreignKey:Gid;references:Id"`
|
||||
|
||||
@@ -9,6 +9,7 @@ import "time"
|
||||
// DeviceAlarm 设备告警表 需要更改告警状态不能存在时序数据库中
|
||||
type DeviceAlarm struct {
|
||||
Id string `json:"id" gorm:"primary_key;"`
|
||||
OrgId int64 `json:"orgId" gorm:"type:int;comment:机构ID"`
|
||||
Time time.Time `gorm:"comment:告警时间" json:"time"`
|
||||
Name string `gorm:"type:varchar(64);comment:告警名称" json:"name"`
|
||||
DeviceId string `gorm:"type:varchar(64);comment:所属设备" json:"deviceId"`
|
||||
@@ -17,6 +18,8 @@ type DeviceAlarm struct {
|
||||
Level string `gorm:"type:varchar(64);comment:告警级别" json:"level"` // 危险 重要 次要 警告 不确定
|
||||
State string `gorm:"type:varchar(1);comment:告警状态" json:"state"` // 告警中 0 已确认 1 已清除 2 已关闭 3
|
||||
Details string `gorm:"type:varchar(255);comment:告警详情" json:"details"`
|
||||
|
||||
RoleId int64 `gorm:"-"` // 角色数据权限
|
||||
}
|
||||
|
||||
type DeviceAlarmForm struct {
|
||||
|
||||
@@ -49,6 +49,8 @@ type Product struct {
|
||||
SelfLearn bool `json:"selfLearn" gorm:"default:0;comment:自学习开关"`
|
||||
RuleChainId string `json:"ruleChainId" gorm:"type:varchar(64);comment:规则链Id"` //可空,如果空就走根规则链
|
||||
Status string `gorm:"status;type:varchar(1);comment:状态" json:"status"`
|
||||
|
||||
RoleId int64 `gorm:"-"` // 角色数据权限
|
||||
}
|
||||
|
||||
type ProductRes struct {
|
||||
|
||||
@@ -36,6 +36,7 @@ func (m *deviceModelImpl) Insert(data entity.Device) *entity.Device {
|
||||
biz.IsTrue(list != nil && len(*list) == 0, "设备名称已经存在")
|
||||
//2 创建认证TOKEN IOTHUB使用
|
||||
etoken := getDeviceToken(&data)
|
||||
// 子网关不需要设置token
|
||||
if data.DeviceType != global.GATEWAYS {
|
||||
data.Token = etoken.Token
|
||||
}
|
||||
@@ -56,6 +57,7 @@ func getDeviceToken(data *entity.Device) *tool.DeviceAuth {
|
||||
now := time.Now()
|
||||
etoken := &tool.DeviceAuth{
|
||||
DeviceId: data.Id,
|
||||
OrgId: data.OrgId,
|
||||
User: data.Owner,
|
||||
Name: data.Name,
|
||||
DeviceType: data.DeviceType,
|
||||
@@ -93,9 +95,6 @@ func (m *deviceModelImpl) FindListPage(page, pageSize int, data entity.Device) (
|
||||
if data.Gid != "" {
|
||||
db = db.Where("gid = ?", data.Gid)
|
||||
}
|
||||
if data.OrgId != "" {
|
||||
db = db.Where("org_id = ?", data.OrgId)
|
||||
}
|
||||
if data.Name != "" {
|
||||
db = db.Where("name like ?", "%"+data.Name+"%")
|
||||
}
|
||||
@@ -114,6 +113,9 @@ func (m *deviceModelImpl) FindListPage(page, pageSize int, data entity.Device) (
|
||||
if data.ParentId != "" {
|
||||
db = db.Where("parent_id = ?", data.ParentId)
|
||||
}
|
||||
// 组织数据访问权限
|
||||
tool.OrgAuthSet(db, data.RoleId)
|
||||
|
||||
err := db.Count(&total).Error
|
||||
err = db.Order("create_time").Preload("Product").Preload("DeviceGroup").Limit(pageSize).Offset(offset).Find(&list).Error
|
||||
biz.ErrIsNil(err, "查询设备分页列表失败")
|
||||
@@ -130,9 +132,6 @@ func (m *deviceModelImpl) FindList(data entity.Device) *[]entity.DeviceRes {
|
||||
if data.Gid != "" {
|
||||
db = db.Where("gid = ?", data.Gid)
|
||||
}
|
||||
if data.OrgId != "" {
|
||||
db = db.Where("org_id = ?", data.OrgId)
|
||||
}
|
||||
if data.Name != "" {
|
||||
db = db.Where("name like ?", "%"+data.Name+"%")
|
||||
}
|
||||
@@ -154,6 +153,7 @@ func (m *deviceModelImpl) FindList(data entity.Device) *[]entity.DeviceRes {
|
||||
if data.ParentId != "" {
|
||||
db = db.Where("parent_id = ?", data.ParentId)
|
||||
}
|
||||
tool.OrgAuthSet(db, data.RoleId)
|
||||
db.Preload("Product").Preload("DeviceGroup")
|
||||
biz.ErrIsNil(db.Order("create_time").Find(&list).Error, "查询设备列表失败")
|
||||
return &list
|
||||
|
||||
@@ -129,20 +129,20 @@ func (m *deviceGroupModelImpl) SelectDeviceGroup(data entity.DeviceGroup) []enti
|
||||
}
|
||||
|
||||
func (m *deviceGroupModelImpl) SelectDeviceGroupLabel(data entity.DeviceGroup) []entity.DeviceGroupLabel {
|
||||
deptlist := m.FindList(data)
|
||||
organizationlist := m.FindList(data)
|
||||
|
||||
dl := make([]entity.DeviceGroupLabel, 0)
|
||||
deptl := *deptlist
|
||||
for i := 0; i < len(deptl); i++ {
|
||||
if deptl[i].Pid != "0" {
|
||||
organizationl := *organizationlist
|
||||
for i := 0; i < len(organizationl); i++ {
|
||||
if organizationl[i].Pid != "0" {
|
||||
continue
|
||||
}
|
||||
e := entity.DeviceGroupLabel{}
|
||||
e.Id = deptl[i].Id
|
||||
e.Name = deptl[i].Name
|
||||
deptsInfo := DiGuiDeviceGroupLabel(deptlist, e)
|
||||
e.Id = organizationl[i].Id
|
||||
e.Name = organizationl[i].Name
|
||||
organizationsInfo := DiGuiDeviceGroupLabel(organizationlist, e)
|
||||
|
||||
dl = append(dl, deptsInfo)
|
||||
dl = append(dl, organizationsInfo)
|
||||
}
|
||||
return dl
|
||||
}
|
||||
@@ -170,12 +170,12 @@ func DiGuiDeviceGroup(sglist *[]entity.DeviceGroup, menu entity.DeviceGroup) ent
|
||||
menu.Children = min
|
||||
return menu
|
||||
}
|
||||
func DiGuiDeviceGroupLabel(sglist *[]entity.DeviceGroup, dept entity.DeviceGroupLabel) entity.DeviceGroupLabel {
|
||||
func DiGuiDeviceGroupLabel(sglist *[]entity.DeviceGroup, organization entity.DeviceGroupLabel) entity.DeviceGroupLabel {
|
||||
list := *sglist
|
||||
|
||||
min := make([]entity.DeviceGroupLabel, 0)
|
||||
for j := 0; j < len(list); j++ {
|
||||
if dept.Id != list[j].Pid {
|
||||
if organization.Id != list[j].Pid {
|
||||
continue
|
||||
}
|
||||
sg := entity.DeviceGroupLabel{list[j].Id, list[j].Name, []entity.DeviceGroupLabel{}}
|
||||
@@ -183,6 +183,6 @@ func DiGuiDeviceGroupLabel(sglist *[]entity.DeviceGroup, dept entity.DeviceGroup
|
||||
min = append(min, ms)
|
||||
|
||||
}
|
||||
dept.Children = min
|
||||
return dept
|
||||
organization.Children = min
|
||||
return organization
|
||||
}
|
||||
|
||||
@@ -77,7 +77,7 @@ func (m *productModelImpl) FindListPage(page, pageSize int, data entity.Product)
|
||||
if data.Name != "" {
|
||||
db = db.Where("name like ?", "%"+data.Name+"%")
|
||||
}
|
||||
if data.OrgId != "" {
|
||||
if data.OrgId != 0 {
|
||||
db = db.Where("org_id = ?", data.OrgId)
|
||||
}
|
||||
if data.RuleChainId != "" {
|
||||
@@ -114,7 +114,7 @@ func (m *productModelImpl) FindList(data entity.Product) *[]entity.ProductRes {
|
||||
if data.Name != "" {
|
||||
db = db.Where("name like ?", "%"+data.Name+"%")
|
||||
}
|
||||
if data.OrgId != "" {
|
||||
if data.OrgId != 0 {
|
||||
db = db.Where("org_id = ?", data.OrgId)
|
||||
}
|
||||
if data.RuleChainId != "" {
|
||||
|
||||
@@ -132,17 +132,17 @@ func (m *productCategoryModelImpl) SelectProductCategoryLabel(data entity.Produc
|
||||
list := m.FindList(data)
|
||||
|
||||
dl := make([]entity.ProductCategoryLabel, 0)
|
||||
deptl := *list
|
||||
for i := 0; i < len(deptl); i++ {
|
||||
if deptl[i].Pid != "0" {
|
||||
organizationl := *list
|
||||
for i := 0; i < len(organizationl); i++ {
|
||||
if organizationl[i].Pid != "0" {
|
||||
continue
|
||||
}
|
||||
e := entity.ProductCategoryLabel{}
|
||||
e.Id = deptl[i].Id
|
||||
e.Name = deptl[i].Name
|
||||
deptsInfo := DiGuiProductCategoryLabel(list, e)
|
||||
e.Id = organizationl[i].Id
|
||||
e.Name = organizationl[i].Name
|
||||
organizationsInfo := DiGuiProductCategoryLabel(list, e)
|
||||
|
||||
dl = append(dl, deptsInfo)
|
||||
dl = append(dl, organizationsInfo)
|
||||
}
|
||||
return dl
|
||||
}
|
||||
@@ -170,12 +170,12 @@ func DiGuiProductCategory(sglist *[]entity.ProductCategory, menu entity.ProductC
|
||||
menu.Children = min
|
||||
return menu
|
||||
}
|
||||
func DiGuiProductCategoryLabel(sglist *[]entity.ProductCategory, dept entity.ProductCategoryLabel) entity.ProductCategoryLabel {
|
||||
func DiGuiProductCategoryLabel(sglist *[]entity.ProductCategory, organization entity.ProductCategoryLabel) entity.ProductCategoryLabel {
|
||||
list := *sglist
|
||||
|
||||
min := make([]entity.ProductCategoryLabel, 0)
|
||||
for j := 0; j < len(list); j++ {
|
||||
if dept.Id != list[j].Pid {
|
||||
if organization.Id != list[j].Pid {
|
||||
continue
|
||||
}
|
||||
sg := entity.ProductCategoryLabel{}
|
||||
@@ -185,6 +185,6 @@ func DiGuiProductCategoryLabel(sglist *[]entity.ProductCategory, dept entity.Pro
|
||||
min = append(min, ms)
|
||||
|
||||
}
|
||||
dept.Children = min
|
||||
return dept
|
||||
organization.Children = min
|
||||
return organization
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user