增加字典全局权限

This commit is contained in:
2025-11-11 20:25:32 +08:00
parent 8c5859bad5
commit ad4cc5408d
7 changed files with 109 additions and 23 deletions
+23 -17
View File
@@ -20,10 +20,10 @@ func AddDictType(dictType *models.DictType) (int64, error) {
}
o := orm.NewOrm()
// 检查字典编码是否已存在(同一租户下唯一)
existType := &models.DictType{}
err = o.Raw("SELECT id FROM sys_dict_type WHERE dict_code = ? AND tenant_id = ? AND is_deleted = 0",
err = o.Raw("SELECT id FROM sys_dict_type WHERE dict_code = ? AND tenant_id = ? AND is_deleted = 0",
dictType.DictCode, dictType.TenantId).QueryRow(existType)
if err == nil {
return 0, fmt.Errorf("字典编码 %s 已存在", dictType.DictCode)
@@ -44,7 +44,7 @@ func AddDictType(dictType *models.DictType) (int64, error) {
// UpdateDictType 更新字典类型
func UpdateDictType(dictType *models.DictType) error {
o := orm.NewOrm()
// 获取原字典类型信息
oldType := &models.DictType{Id: dictType.Id}
err := o.Read(oldType)
@@ -58,7 +58,7 @@ func UpdateDictType(dictType *models.DictType) error {
}
// 更新字段(排除 dict_code
_, err = o.Update(dictType, "dict_name", "parent_id", "status", "sort", "remark", "update_by", "update_time")
_, err = o.Update(dictType, "dict_name", "parent_id", "status", "is_global", "sort", "remark", "update_by", "update_time")
if err != nil {
return fmt.Errorf("更新字典类型失败: %v", err)
}
@@ -72,7 +72,7 @@ func UpdateDictType(dictType *models.DictType) error {
// DeleteDictType 删除字典类型(逻辑删除)
func DeleteDictType(id int, tenantId int) error {
o := orm.NewOrm()
// 检查是否存在
dictType := &models.DictType{Id: id}
err := o.Read(dictType)
@@ -121,14 +121,21 @@ func GetDictTypeById(id int) (*models.DictType, error) {
}
// GetDictTypes 获取字典类型列表
func GetDictTypes(tenantId int, parentId int, status *int8) ([]*models.DictType, error) {
func GetDictTypes(tenantId int, userId int, parentId int, status *int8) ([]*models.DictType, error) {
o := orm.NewOrm()
qs := o.QueryTable("sys_dict_type").Filter("is_deleted", 0)
// 租户过滤:0表示平台字典,>0表示租户字典
if tenantId > 0 {
// 租户用户只能看到自己的字典和平台字典
qs = qs.Filter("tenant_id__in", 0, tenantId)
// 租户用户需要根据角色判断是否可以看到私有字典
roleCode, err := GetUserRoleCode(userId)
if err != nil || (roleCode != "system_admin" && roleCode != "admin") {
// 非admin用户只能看到全局字典和自己租户的全局字典
qs = qs.Filter("is_global", 1)
} else {
// admin用户可以看到自己租户的所有字典和全局字典
qs = qs.Filter("tenant_id__in", 0, tenantId)
}
} else {
// 平台用户只能看到平台字典
qs = qs.Filter("tenant_id", 0)
@@ -154,7 +161,7 @@ func GetDictTypes(tenantId int, parentId int, status *int8) ([]*models.DictType,
// AddDictItem 添加字典项
func AddDictItem(dictItem *models.DictItem) (int64, error) {
o := orm.NewOrm()
// 检查字典类型是否存在
dictType := &models.DictType{Id: dictItem.DictTypeId}
err := o.Read(dictType)
@@ -167,7 +174,7 @@ func AddDictItem(dictItem *models.DictItem) (int64, error) {
// 检查同一字典类型下 dict_value 是否已存在
existItem := &models.DictItem{}
err = o.Raw("SELECT id FROM sys_dict_item WHERE dict_type_id = ? AND dict_value = ? AND is_deleted = 0",
err = o.Raw("SELECT id FROM sys_dict_item WHERE dict_type_id = ? AND dict_value = ? AND is_deleted = 0",
dictItem.DictTypeId, dictItem.DictValue).QueryRow(existItem)
if err == nil {
return 0, fmt.Errorf("字典值 %s 已存在", dictItem.DictValue)
@@ -188,7 +195,7 @@ func AddDictItem(dictItem *models.DictItem) (int64, error) {
// UpdateDictItem 更新字典项
func UpdateDictItem(dictItem *models.DictItem) error {
o := orm.NewOrm()
// 获取原字典项信息
oldItem := &models.DictItem{Id: dictItem.Id}
err := o.Read(oldItem)
@@ -228,7 +235,7 @@ func UpdateDictItem(dictItem *models.DictItem) error {
// DeleteDictItem 删除字典项(逻辑删除)
func DeleteDictItem(id int) error {
o := orm.NewOrm()
// 检查是否存在
dictItem := &models.DictItem{Id: id}
err := o.Read(dictItem)
@@ -294,15 +301,15 @@ func GetDictItems(dictTypeId int, parentId *int, status *int8) ([]*models.DictIt
// GetDictItemsByCode 根据字典编码获取字典项列表(支持缓存)
func GetDictItemsByCode(dictCode string, tenantId int, includeDisabled bool) ([]*models.DictItem, error) {
o := orm.NewOrm()
// 先查询字典类型
dictType := &models.DictType{}
err := o.Raw("SELECT * FROM sys_dict_type WHERE dict_code = ? AND tenant_id = ? AND is_deleted = 0 AND status = 1",
err := o.Raw("SELECT * FROM sys_dict_type WHERE dict_code = ? AND tenant_id = ? AND is_deleted = 0 AND status = 1",
dictCode, tenantId).QueryRow(dictType)
if err != nil {
// 如果租户字典不存在,尝试查询平台字典
if tenantId > 0 {
err = o.Raw("SELECT * FROM sys_dict_type WHERE dict_code = ? AND tenant_id = 0 AND is_deleted = 0 AND status = 1",
err = o.Raw("SELECT * FROM sys_dict_type WHERE dict_code = ? AND tenant_id = 0 AND is_deleted = 0 AND status = 1",
dictCode).QueryRow(dictType)
if err != nil {
return []*models.DictItem{}, nil // 返回空列表而不是错误
@@ -353,7 +360,7 @@ func BatchUpdateDictItemSort(items []struct {
Sort int `json:"sort"`
}) error {
o := orm.NewOrm()
for _, item := range items {
dictItem := &models.DictItem{Id: item.Id}
err := o.Read(dictItem)
@@ -370,4 +377,3 @@ func BatchUpdateDictItemSort(items []struct {
return nil
}
+21
View File
@@ -317,3 +317,24 @@ func ChangePassword(username, oldPassword, newPassword string, tenantId int) err
return err
}
// GetUserRoleCode 根据用户ID获取用户的角色代码
func GetUserRoleCode(userId int) (string, error) {
o := orm.NewOrm()
user := &models.User{Id: userId}
err := o.Read(user)
if err != nil {
return "", err
}
// 根据用户的role字段获取角色信息
if user.Role <= 0 {
return "", errors.New("用户未设置角色")
}
role, err := models.GetRoleById(user.Role)
if err != nil {
return "", err
}
return role.RoleCode, nil
}