增加字典全局权限
This commit is contained in:
parent
8c5859bad5
commit
ad4cc5408d
@ -22,7 +22,10 @@ export function addDictType(data) {
|
|||||||
return request({
|
return request({
|
||||||
url: '/api/dict/types',
|
url: '/api/dict/types',
|
||||||
method: 'post',
|
method: 'post',
|
||||||
data
|
data: {
|
||||||
|
...data,
|
||||||
|
is_global: data.is_global !== undefined ? data.is_global : 0
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -31,7 +34,10 @@ export function updateDictType(id, data) {
|
|||||||
return request({
|
return request({
|
||||||
url: `/api/dict/types/${id}`,
|
url: `/api/dict/types/${id}`,
|
||||||
method: 'put',
|
method: 'put',
|
||||||
data
|
data: {
|
||||||
|
...data,
|
||||||
|
is_global: data.is_global !== undefined ? data.is_global : 0
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -32,6 +32,13 @@
|
|||||||
<el-radio :value="0">禁用</el-radio>
|
<el-radio :value="0">禁用</el-radio>
|
||||||
</el-radio-group>
|
</el-radio-group>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
|
<el-form-item v-if="shouldShowGlobalOption" label="全局展示" prop="is_global">
|
||||||
|
<el-radio-group v-model="dictTypeForm.is_global">
|
||||||
|
<el-radio :value="1">是</el-radio>
|
||||||
|
<el-radio :value="0">否</el-radio>
|
||||||
|
</el-radio-group>
|
||||||
|
<div class="form-tip">是:所有租户可见;否:仅租户内部可见</div>
|
||||||
|
</el-form-item>
|
||||||
<el-form-item label="排序序号" prop="sort">
|
<el-form-item label="排序序号" prop="sort">
|
||||||
<el-input-number
|
<el-input-number
|
||||||
v-model="dictTypeForm.sort"
|
v-model="dictTypeForm.sort"
|
||||||
@ -64,6 +71,7 @@
|
|||||||
import { ref, reactive, watch, computed, onMounted } from 'vue';
|
import { ref, reactive, watch, computed, onMounted } from 'vue';
|
||||||
import { ElMessage, type FormInstance, type FormRules } from 'element-plus';
|
import { ElMessage, type FormInstance, type FormRules } from 'element-plus';
|
||||||
import { addDictType, updateDictType, getDictTypes } from '@/api/dict';
|
import { addDictType, updateDictType, getDictTypes } from '@/api/dict';
|
||||||
|
import { useAuthStore } from '@/stores/auth';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
modelValue: boolean;
|
modelValue: boolean;
|
||||||
@ -87,6 +95,7 @@
|
|||||||
|
|
||||||
const submitting = ref(false);
|
const submitting = ref(false);
|
||||||
const dictTypeFormRef = ref<FormInstance>();
|
const dictTypeFormRef = ref<FormInstance>();
|
||||||
|
const authStore = useAuthStore();
|
||||||
// 已移除父级选择
|
// 已移除父级选择
|
||||||
|
|
||||||
// 判断是否为编辑模式
|
// 判断是否为编辑模式
|
||||||
@ -94,12 +103,19 @@
|
|||||||
return !!(props.dictType && props.dictType.id);
|
return !!(props.dictType && props.dictType.id);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 判断是否应该显示全局展示选项(只有 system_admin 和 admin 角色才能看到)
|
||||||
|
const shouldShowGlobalOption = computed(() => {
|
||||||
|
const roleCode = authStore.user.role_code;
|
||||||
|
return roleCode === 'system_admin' || roleCode === 'admin';
|
||||||
|
});
|
||||||
|
|
||||||
// 表单数据
|
// 表单数据
|
||||||
const dictTypeForm = reactive({
|
const dictTypeForm = reactive({
|
||||||
id: null as number | null,
|
id: null as number | null,
|
||||||
dict_code: '',
|
dict_code: '',
|
||||||
dict_name: '',
|
dict_name: '',
|
||||||
status: 1,
|
status: 1,
|
||||||
|
is_global: 0,
|
||||||
sort: 0,
|
sort: 0,
|
||||||
remark: '',
|
remark: '',
|
||||||
});
|
});
|
||||||
@ -140,6 +156,7 @@
|
|||||||
dictTypeForm.dict_code = newDictType.dict_code || '';
|
dictTypeForm.dict_code = newDictType.dict_code || '';
|
||||||
dictTypeForm.dict_name = newDictType.dict_name || '';
|
dictTypeForm.dict_name = newDictType.dict_name || '';
|
||||||
dictTypeForm.status = newDictType.status !== undefined ? newDictType.status : 1;
|
dictTypeForm.status = newDictType.status !== undefined ? newDictType.status : 1;
|
||||||
|
dictTypeForm.is_global = newDictType.is_global !== undefined ? newDictType.is_global : 0;
|
||||||
dictTypeForm.sort = newDictType.sort || 0;
|
dictTypeForm.sort = newDictType.sort || 0;
|
||||||
dictTypeForm.remark = newDictType.remark || '';
|
dictTypeForm.remark = newDictType.remark || '';
|
||||||
} else {
|
} else {
|
||||||
@ -157,6 +174,7 @@
|
|||||||
dictTypeForm.dict_code = '';
|
dictTypeForm.dict_code = '';
|
||||||
dictTypeForm.dict_name = '';
|
dictTypeForm.dict_name = '';
|
||||||
dictTypeForm.status = 1;
|
dictTypeForm.status = 1;
|
||||||
|
dictTypeForm.is_global = 0;
|
||||||
dictTypeForm.sort = 0;
|
dictTypeForm.sort = 0;
|
||||||
dictTypeForm.remark = '';
|
dictTypeForm.remark = '';
|
||||||
dictTypeFormRef.value?.clearValidate();
|
dictTypeFormRef.value?.clearValidate();
|
||||||
@ -184,6 +202,7 @@
|
|||||||
dict_code: dictTypeForm.dict_code,
|
dict_code: dictTypeForm.dict_code,
|
||||||
dict_name: dictTypeForm.dict_name,
|
dict_name: dictTypeForm.dict_name,
|
||||||
status: dictTypeForm.status,
|
status: dictTypeForm.status,
|
||||||
|
is_global: dictTypeForm.is_global,
|
||||||
sort: dictTypeForm.sort,
|
sort: dictTypeForm.sort,
|
||||||
remark: dictTypeForm.remark || '',
|
remark: dictTypeForm.remark || '',
|
||||||
};
|
};
|
||||||
|
|||||||
@ -60,6 +60,13 @@
|
|||||||
</el-tag>
|
</el-tag>
|
||||||
</template>
|
</template>
|
||||||
</el-table-column>
|
</el-table-column>
|
||||||
|
<el-table-column prop="is_global" label="全局展示" width="100" align="center">
|
||||||
|
<template #default="{ row }">
|
||||||
|
<el-tag :type="row.is_global === 1 ? 'success' : 'warning'" size="small">
|
||||||
|
{{ row.is_global === 1 ? '是' : '否' }}
|
||||||
|
</el-tag>
|
||||||
|
</template>
|
||||||
|
</el-table-column>
|
||||||
<el-table-column prop="sort" label="排序" width="80" align="center" />
|
<el-table-column prop="sort" label="排序" width="80" align="center" />
|
||||||
<el-table-column prop="remark" label="备注" min-width="200" show-overflow-tooltip />
|
<el-table-column prop="remark" label="备注" min-width="200" show-overflow-tooltip />
|
||||||
<el-table-column prop="create_time" label="创建时间" width="180" align="center">
|
<el-table-column prop="create_time" label="创建时间" width="180" align="center">
|
||||||
|
|||||||
@ -25,6 +25,15 @@ func (c *DictController) GetDictTypes() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 获取用户ID
|
||||||
|
userIdData := c.Ctx.Input.GetData("userId")
|
||||||
|
userId := 0
|
||||||
|
if userIdData != nil {
|
||||||
|
if uid, ok := userIdData.(int); ok {
|
||||||
|
userId = uid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
parentId, _ := c.GetInt("parent_id", -1)
|
parentId, _ := c.GetInt("parent_id", -1)
|
||||||
statusStr := c.GetString("status")
|
statusStr := c.GetString("status")
|
||||||
|
|
||||||
@ -35,7 +44,7 @@ func (c *DictController) GetDictTypes() {
|
|||||||
status = &statusVal
|
status = &statusVal
|
||||||
}
|
}
|
||||||
|
|
||||||
dictTypes, err := services.GetDictTypes(tenantId, parentId, status)
|
dictTypes, err := services.GetDictTypes(tenantId, userId, parentId, status)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Data["json"] = map[string]interface{}{
|
c.Data["json"] = map[string]interface{}{
|
||||||
"success": false,
|
"success": false,
|
||||||
@ -109,6 +118,13 @@ func (c *DictController) AddDictType() {
|
|||||||
username = u
|
username = u
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
userIdData := c.Ctx.Input.GetData("userId")
|
||||||
|
userId := 0
|
||||||
|
if userIdData != nil {
|
||||||
|
if uid, ok := userIdData.(int); ok {
|
||||||
|
userId = uid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 设置租户ID和创建人
|
// 设置租户ID和创建人
|
||||||
dictType.TenantId = tenantId
|
dictType.TenantId = tenantId
|
||||||
@ -117,6 +133,17 @@ func (c *DictController) AddDictType() {
|
|||||||
dictType.Status = 1 // 默认启用
|
dictType.Status = 1 // 默认启用
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 根据用户角色设置是否全局展示
|
||||||
|
// system_admin 和 admin 角色默认是全局,其他角色默认是租户私有
|
||||||
|
dictType.IsGlobal = 0 // 默认为租户私有
|
||||||
|
if userId > 0 {
|
||||||
|
// 获取用户的角色信息
|
||||||
|
roleCode, err := services.GetUserRoleCode(userId)
|
||||||
|
if err == nil && (roleCode == "system_admin" || roleCode == "admin") {
|
||||||
|
dictType.IsGlobal = 1 // system_admin 和 admin 角色默认全局
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
id, err := services.AddDictType(&dictType)
|
id, err := services.AddDictType(&dictType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Data["json"] = map[string]interface{}{
|
c.Data["json"] = map[string]interface{}{
|
||||||
@ -508,4 +535,3 @@ func (c *DictController) BatchUpdateDictItemSort() {
|
|||||||
}
|
}
|
||||||
c.ServeJSON()
|
c.ServeJSON()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -12,6 +12,7 @@ type DictType struct {
|
|||||||
DictName string `orm:"column(dict_name);size(100)" json:"dict_name"` // 字典名称
|
DictName string `orm:"column(dict_name);size(100)" json:"dict_name"` // 字典名称
|
||||||
ParentId int `orm:"column(parent_id);default(0)" json:"parent_id"` // 父级字典ID(支持多级)
|
ParentId int `orm:"column(parent_id);default(0)" json:"parent_id"` // 父级字典ID(支持多级)
|
||||||
Status int8 `orm:"column(status);default(1)" json:"status"` // 0-禁用,1-启用
|
Status int8 `orm:"column(status);default(1)" json:"status"` // 0-禁用,1-启用
|
||||||
|
IsGlobal int8 `orm:"column(is_global);default(0)" json:"is_global"` // 0-租户私有,1-全局展示
|
||||||
Sort int `orm:"column(sort);default(0)" json:"sort"` // 排序号
|
Sort int `orm:"column(sort);default(0)" json:"sort"` // 排序号
|
||||||
Remark string `orm:"column(remark);size(500);null" json:"remark"` // 备注
|
Remark string `orm:"column(remark);size(500);null" json:"remark"` // 备注
|
||||||
CreateBy string `orm:"column(create_by);size(50);null" json:"create_by"`
|
CreateBy string `orm:"column(create_by);size(50);null" json:"create_by"`
|
||||||
|
|||||||
@ -58,7 +58,7 @@ func UpdateDictType(dictType *models.DictType) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 更新字段(排除 dict_code)
|
// 更新字段(排除 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 {
|
if err != nil {
|
||||||
return fmt.Errorf("更新字典类型失败: %v", err)
|
return fmt.Errorf("更新字典类型失败: %v", err)
|
||||||
}
|
}
|
||||||
@ -121,14 +121,21 @@ func GetDictTypeById(id int) (*models.DictType, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetDictTypes 获取字典类型列表
|
// 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()
|
o := orm.NewOrm()
|
||||||
qs := o.QueryTable("sys_dict_type").Filter("is_deleted", 0)
|
qs := o.QueryTable("sys_dict_type").Filter("is_deleted", 0)
|
||||||
|
|
||||||
// 租户过滤:0表示平台字典,>0表示租户字典
|
// 租户过滤:0表示平台字典,>0表示租户字典
|
||||||
if tenantId > 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 {
|
} else {
|
||||||
// 平台用户只能看到平台字典
|
// 平台用户只能看到平台字典
|
||||||
qs = qs.Filter("tenant_id", 0)
|
qs = qs.Filter("tenant_id", 0)
|
||||||
@ -370,4 +377,3 @@ func BatchUpdateDictItemSort(items []struct {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -317,3 +317,24 @@ func ChangePassword(username, oldPassword, newPassword string, tenantId int) err
|
|||||||
return 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
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user