permission.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <template>
  2. <el-dialog :title="title" v-model="isShowDialog" width="1100px">
  3. <div class="mb-4 tr">
  4. <el-dropdown>
  5. <el-button plain class="mr-3">
  6. 操作<el-icon>
  7. <ele-ArrowDown />
  8. </el-icon>
  9. </el-button>
  10. <template #dropdown>
  11. <el-dropdown-menu>
  12. <el-dropdown-item @click.native="checkAll(true)">全部勾选</el-dropdown-item>
  13. <el-dropdown-item @click.native="checkAll(false)">取消全选</el-dropdown-item>
  14. <el-dropdown-item @click.native="expand(true)">展开所有</el-dropdown-item>
  15. <el-dropdown-item @click.native="expand(false)">折叠所有</el-dropdown-item>
  16. </el-dropdown-menu>
  17. </template>
  18. </el-dropdown>
  19. <el-button :disabled="step <= 0" @click="prev">上一步</el-button>
  20. <el-button :disabled="step >= 3" @click="next">下一步</el-button>
  21. <el-button type="primary" :loading="btnLoading" :disabled="step < 3" @click="submit">确定</el-button>
  22. <el-button @click="cancel">取消</el-button>
  23. </div>
  24. <el-steps :active="step" simple>
  25. <el-step name title="菜单权限" />
  26. <el-step title="按钮权限" />
  27. <el-step title="列表权限" />
  28. <el-step title="接口权限" />
  29. </el-steps>
  30. <div class="scroll-part mt-3">
  31. <el-tree ref="treeRef" :data="treeData" show-checkbox default-expand-all node-key="id" highlight-current :props="defaultProps" check-on-click-node :expand-on-click-node="false" @check-change="checkChange" />
  32. </div>
  33. </el-dialog>
  34. </template>
  35. <script lang="ts" setup>
  36. import { ref } from 'vue';
  37. import api from '/@/api/system';
  38. import { ElMessage } from 'element-plus';
  39. const isShowDialog = ref(false);
  40. const btnLoading = ref(false);
  41. const step = ref(0);
  42. const treeRef = ref();
  43. const title = ref('角色权限设置');
  44. const roleId = ref(0);
  45. const treeData = ref([]);
  46. const menuData = ref([]);
  47. const buttonData = ref([]);
  48. const listData = ref([]);
  49. const apiData = ref([]);
  50. const menuIds = ref([]);
  51. const buttonIds = ref([]);
  52. const columnIds = ref([]);
  53. const apiIds = ref([]);
  54. const typeList = ['menu', 'button', 'column', 'api'];
  55. const idsList = [menuIds, buttonIds, columnIds, apiIds];
  56. const treeDataList = [menuData, buttonData, listData, apiData];
  57. const defaultProps = {
  58. children: 'children',
  59. label: 'title',
  60. };
  61. const openDialog = async (row: any) => {
  62. title.value = '角色权限设置 - ' + row.name;
  63. roleId.value = row.id;
  64. isShowDialog.value = true;
  65. let res = await api.role.auth.getList(typeList[step.value]);
  66. // console.log(res);
  67. treeData.value = res;
  68. menuData.value = res;
  69. };
  70. const cancel = () => {
  71. isShowDialog.value = false;
  72. };
  73. const expand = (expand: boolean) => {
  74. const nodes = treeRef.value.store.nodesMap;
  75. for (let i in nodes) {
  76. nodes[i].expanded = expand;
  77. }
  78. };
  79. const prev = async () => {
  80. const currentStep = step.value;
  81. const prevStep = step.value - 1;
  82. // 获取选中id
  83. const val = treeRef.value.getCheckedKeys(currentStep === 0 ? false : true);
  84. idsList[currentStep].value = val;
  85. treeData.value = treeDataList[prevStep].value;
  86. treeRef.value.setCheckedKeys(idsList[prevStep].value);
  87. step.value = prevStep;
  88. };
  89. const next = async () => {
  90. const nextStep = step.value + 1;
  91. let res = await api.role.auth.getList(typeList[nextStep], menuIds.value);
  92. // console.log(res);
  93. treeData.value = res;
  94. treeDataList[nextStep].value = res;
  95. treeRef.value.setCheckedKeys(idsList[nextStep].value);
  96. step.value = nextStep;
  97. };
  98. // 选中状态变化
  99. const checkChange = () => {
  100. idsList[step.value].value = treeRef.value.getCheckedKeys(step.value === 0 ? false : true);
  101. };
  102. // 全选取消全选
  103. const checkAll = (all: boolean) => {
  104. if (!all) {
  105. treeRef.value.setCheckedKeys([]);
  106. } else {
  107. const ids = deepTree(treeDataList[step.value].value, []);
  108. treeRef.value.setCheckedKeys(ids);
  109. }
  110. };
  111. const submit = async () => {
  112. const data = {
  113. menuIds: menuIds.value,
  114. buttonIds: buttonIds.value,
  115. columnIds: columnIds.value,
  116. apiIds: apiIds.value,
  117. roleId: roleId.value,
  118. };
  119. // console.log(data);
  120. btnLoading.value = true;
  121. api.role.auth
  122. .set(data)
  123. .then(() => {
  124. ElMessage.success('权限设置成功');
  125. })
  126. .finally(() => {
  127. btnLoading.value = false;
  128. isShowDialog.value = false;
  129. });
  130. };
  131. function deepTree(tree: any[], arr: number[]) {
  132. for (let item of tree) {
  133. arr.push(item.id);
  134. if (item.children?.length) deepTree(item.children, arr);
  135. }
  136. return arr;
  137. }
  138. // openDialog({ name: '超级管理员', id: 3 });
  139. defineExpose({ openDialog });
  140. </script>
  141. <style scoped lang="scss">
  142. .scroll-part {
  143. height: calc(80vh - 200px);
  144. overflow-x: hidden;
  145. overflow-y: auto;
  146. }
  147. </style>