|
| 1 | +<script setup lang="ts"> |
| 2 | +import { ref } from "vue"; |
| 3 | +import { transformI18n } from "@/plugins/i18n"; |
| 4 | +import ElTreeLine from "@/components/ReTreeLine"; |
| 5 | +import { getRolePermission, treeMenu } from "@/api/tableExample"; |
| 6 | +import { message } from "@/utils/message"; |
| 7 | +import { PermDialogProps } from "./utils/types"; |
| 8 | +
|
| 9 | +const loading = ref(true); |
| 10 | +
|
| 11 | +const props = withDefaults(defineProps<PermDialogProps>(), { |
| 12 | + formInline: () => ({ |
| 13 | + id: null, |
| 14 | + permissions: [] |
| 15 | + }) |
| 16 | +}); |
| 17 | +const newFormInline = ref(props.formInline); |
| 18 | +
|
| 19 | +// 获取菜单树状数据 |
| 20 | +const menuTree = ref([]); |
| 21 | +function getMenuTree() { |
| 22 | + treeMenu() |
| 23 | + .then(res => { |
| 24 | + menuTree.value = res.data; |
| 25 | + }) |
| 26 | + .catch(error => { |
| 27 | + message(`${error.data.ret}: ${error.data.msg}\n菜单数据获取失败`, { |
| 28 | + type: "error" |
| 29 | + }); |
| 30 | + }); |
| 31 | + loading.value = false; |
| 32 | +} |
| 33 | +
|
| 34 | +// 获取角色拥有的菜单权限 |
| 35 | +getRolePermission(newFormInline.value.id) |
| 36 | + .then(res => { |
| 37 | + newFormInline.value.permissions = res.data.permissions; |
| 38 | + getMenuTree(); |
| 39 | + }) |
| 40 | + .catch(error => { |
| 41 | + message(`${error.data.ret}: ${error.data.msg}\n角色权限获取失败`, { |
| 42 | + type: "error" |
| 43 | + }); |
| 44 | + }); |
| 45 | +
|
| 46 | +const ruleFormRef = ref(); |
| 47 | +function getRef() { |
| 48 | + return ruleFormRef.value; |
| 49 | +} |
| 50 | +defineExpose({ getRef }); |
| 51 | +
|
| 52 | +const dataProps = { |
| 53 | + value: "id", |
| 54 | + children: "children", |
| 55 | + disabled: "disabled" |
| 56 | +}; |
| 57 | +
|
| 58 | +const treeRef = ref(); |
| 59 | +const swCheckAll = ref(false); |
| 60 | +const swExpandTree = ref(true); |
| 61 | +// const swLinkage = ref(false); |
| 62 | +
|
| 63 | +function removePermission(permId) { |
| 64 | + const delIndex = newFormInline.value.permissions.indexOf(permId); |
| 65 | + if (delIndex >= 0) { |
| 66 | + newFormInline.value.permissions.splice(delIndex, 1); |
| 67 | + } |
| 68 | +} |
| 69 | +
|
| 70 | +function setPermission(permId) { |
| 71 | + if (!newFormInline.value.permissions.includes(permId)) { |
| 72 | + newFormInline.value.permissions.push(permId); |
| 73 | + } |
| 74 | +} |
| 75 | +
|
| 76 | +/*菜单权限树节点选中变化时*/ |
| 77 | +function menuCheckChange(obj, isChecked) { |
| 78 | + const permId = obj.id; |
| 79 | +
|
| 80 | + // 菜单上下级之间联动时半选中的父节点视为选择 |
| 81 | + // const halfCheckedMenuIds = treeRef.value.getHalfCheckedKeys(); |
| 82 | + // if (!isChecked && halfCheckedMenuIds.includes(permId)) { |
| 83 | + // isChecked = true; |
| 84 | + // } |
| 85 | + if (isChecked) { |
| 86 | + setPermission(permId); |
| 87 | + if (obj.parentId) { |
| 88 | + const parentNode = treeRef.value.store.nodesMap[obj.parentId]; |
| 89 | + if (!parentNode.checked) { |
| 90 | + parentNode.checked = true; |
| 91 | + } |
| 92 | + } |
| 93 | + } else { |
| 94 | + removePermission(permId); |
| 95 | + // 按钮 |
| 96 | + obj.buttons.forEach(button => { |
| 97 | + removePermission(button.id); |
| 98 | + }); |
| 99 | + obj.children.forEach(child => { |
| 100 | + const childNode = treeRef.value.store.nodesMap[child.id]; |
| 101 | + if (childNode.checked) { |
| 102 | + childNode.checked = false; |
| 103 | + } |
| 104 | + }); |
| 105 | + } |
| 106 | +} |
| 107 | +
|
| 108 | +/*菜单权限树展开/折叠*/ |
| 109 | +function expandTree(isExpand) { |
| 110 | + const nodes = treeRef.value.store.nodesMap; |
| 111 | + for (const node in nodes) { |
| 112 | + nodes[node].expanded = isExpand; |
| 113 | + } |
| 114 | +} |
| 115 | +
|
| 116 | +/*菜单权限树全选/不选*/ |
| 117 | +function checkAllTree(isCheckedAll) { |
| 118 | + const nodes = treeRef.value.store.nodesMap; |
| 119 | + for (const node in nodes) { |
| 120 | + if (!nodes[node].data.disabled) { |
| 121 | + if (nodes[node].checked != isCheckedAll) { |
| 122 | + nodes[node].checked = isCheckedAll; |
| 123 | + } |
| 124 | + const nodeButtons = nodes[node].data.buttons; |
| 125 | + for (const index in nodeButtons) { |
| 126 | + if (isCheckedAll && !nodeButtons[index].disabled) { |
| 127 | + setPermission(nodeButtons[index].id); |
| 128 | + } else { |
| 129 | + removePermission(nodeButtons[index].id); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | +
|
| 136 | +function buttonCheckChange(isChecked, nodeData: any) { |
| 137 | + // 按钮勾选时自动勾选按钮所属菜单 |
| 138 | + const menuId = nodeData.id; |
| 139 | + const node = treeRef.value.store.nodesMap[menuId]; |
| 140 | + if (isChecked && !node.checked) { |
| 141 | + node.checked = true; |
| 142 | + // setPermission(menuId); |
| 143 | + } |
| 144 | +} |
| 145 | +</script> |
| 146 | + |
| 147 | +<template> |
| 148 | + <el-form |
| 149 | + ref="ruleFormRef" |
| 150 | + :model="newFormInline" |
| 151 | + label-width="82px" |
| 152 | + v-loading="loading" |
| 153 | + > |
| 154 | + <el-col class="mb-[20px]"> |
| 155 | + <el-row> |
| 156 | + <el-col :span="6"> |
| 157 | + <el-switch |
| 158 | + v-model="swCheckAll" |
| 159 | + active-text="全选/不选" |
| 160 | + @change="checkAllTree" |
| 161 | + /> |
| 162 | + </el-col> |
| 163 | + <el-col :span="6"> |
| 164 | + <el-switch |
| 165 | + v-model="swExpandTree" |
| 166 | + active-text="展开/折叠" |
| 167 | + @change="expandTree" |
| 168 | + /> |
| 169 | + </el-col> |
| 170 | + <!-- <el-col :span="6">--> |
| 171 | + <!-- <el-switch v-model="swLinkage" active-text="父子联动" />--> |
| 172 | + <!-- </el-col>--> |
| 173 | + </el-row> |
| 174 | + <el-card shadow="never"> |
| 175 | + <div class="max-h-[550px] overflow-y-auto"> |
| 176 | + <el-tree |
| 177 | + ref="treeRef" |
| 178 | + :data="menuTree" |
| 179 | + :props="dataProps" |
| 180 | + show-checkbox |
| 181 | + check-strictly |
| 182 | + node-key="id" |
| 183 | + :render-after-expand="false" |
| 184 | + :indent="30" |
| 185 | + :default-checked-keys="newFormInline.permissions" |
| 186 | + default-expand-all |
| 187 | + @check-change="menuCheckChange" |
| 188 | + > |
| 189 | + <template v-slot:default="{ node, data }"> |
| 190 | + <el-tree-line :node="node" :showLabelLine="true"> |
| 191 | + <template v-slot:node-label> |
| 192 | + <span class="text-sm"> |
| 193 | + {{ transformI18n(node.label) }} |
| 194 | + </span> |
| 195 | + <div class="demo-button-style" style="margin-left: 20px"> |
| 196 | + <el-checkbox-group |
| 197 | + v-model="newFormInline.permissions" |
| 198 | + size="small" |
| 199 | + > |
| 200 | + <el-checkbox-button |
| 201 | + v-for="button in data.buttons" |
| 202 | + :value="button.id" |
| 203 | + :key="button.id" |
| 204 | + :disabled="button.disabled" |
| 205 | + @change="checked => buttonCheckChange(checked, data)" |
| 206 | + >{{ transformI18n(button.meta.title) }} |
| 207 | + </el-checkbox-button> |
| 208 | + </el-checkbox-group> |
| 209 | + </div> |
| 210 | + </template> |
| 211 | + </el-tree-line> |
| 212 | + </template> |
| 213 | + </el-tree> |
| 214 | + </div> |
| 215 | + </el-card> |
| 216 | + </el-col> |
| 217 | + </el-form> |
| 218 | +</template> |
0 commit comments