refactor(role-edit): 优化菜单树选择和角色关联逻辑

- 将菜单树的 `checkbox-config` 配置改为严格模式
- 添加 `relationTypes` 变量以明确关联类型
- 在菜单服务中优化 SQL 查询逻辑,根据 `roleId` 动态构建条件
This commit is contained in:
user 2025-04-12 11:44:00 +08:00
parent e0781b0f8c
commit 408e02e38d
3 changed files with 19 additions and 5 deletions

View File

@ -131,12 +131,24 @@ public class MenuService {
public Flux<Map<String, Object>> getRelation(String table, Map<String, Object> params, Pageable pageable) {
return SecurityUtils.getCurrentUserLogin()
.flatMapMany(login -> {
// 构建基础SQL查询
String conditionSql =
"(select GROUP_CONCAT(data_scope, ',') from jhi_relation where jhi_relation.relation_type = 'menu' and jhi_relation.number = jhi_menu.number";
// 根据roleId[value]是否存在构建不同的条件部分,设置完整的SQL查询
params.put(
"dataScope[field]",
"(select GROUP_CONCAT(data_scope, ',') from jhi_relation where jhi_relation.relation_type = 'menu' and jhi_relation.number = jhi_menu.number and exists(select 1 from jhi_relation r where r.role_number = jhi_relation.role_number and relation_type = 'user' and number = '" +
login +
"'))"
params.get("roleId[value]") == null
? conditionSql +
" and exists(select 1 from jhi_relation r where r.role_number = jhi_relation.role_number and relation_type = 'user' and number = '" +
login +
"'))"
: conditionSql +
" and exists(select 1 from jhi_role where number = jhi_relation.role_number and id = " +
params.get("roleId[value]").toString() +
"))"
);
return getTree(table, params, pageable);
});
}

View File

@ -19,6 +19,7 @@ export default defineComponent({
const statusDicts = ref([]);
const menuTreeData = ref([]);
const checkNodeKeys = ref([]);
const relationTypes = ref('menu');
const roleForm = ref<VxeFormInstance>();
const formRules = {
number: [{ required: true, message: $t('entity.validation.required') }],
@ -59,6 +60,7 @@ export default defineComponent({
name: { name: 'title' },
icon: { name: '' },
dataScope: { name: '' },
roleId: { value: route.params?.id },
parentNumber: { op: 'IFNULL', value: '' },
status: { op: '=', value: '1' },
},
@ -141,7 +143,7 @@ export default defineComponent({
// 创建一个包含所有菜单编号的关联对象
const relation = {
number: checkNodeKeys.value.join(','), // 将所有菜单编号合并为一个逗号分隔的字符串
relationType: 'menu',
relationType: relationTypes.value,
roleNumber: role.value.number,
status: '1',
};

View File

@ -62,7 +62,7 @@
ref="menuTree"
v-model:check-node-keys="checkNodeKeys"
:data="menuTreeData"
:checkbox-config="{ checkStrictly: false }"
:checkbox-config="{ checkStrictly: true }"
trigger="node"
key-field="number"
show-overflow