MenuPermissionForm.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <template>
  2. <Dialog :title="dialogScopeTitle" v-model="dialogScopeVisible" width="800">
  3. <el-form
  4. ref="menuPermissionFormRef"
  5. :model="dataScopeForm"
  6. :inline="true"
  7. label-width="80px"
  8. v-loading="formLoading"
  9. >
  10. <el-form-item label="角色名称">
  11. <el-tag>{{ dataScopeForm.name }}</el-tag>
  12. </el-form-item>
  13. <el-form-item label="角色标识">
  14. <el-tag>{{ dataScopeForm.code }}</el-tag>
  15. </el-form-item>
  16. <!-- 分配角色的菜单权限对话框 -->
  17. <el-row>
  18. <el-col :span="24">
  19. <el-form-item label="权限范围" style="display: flex">
  20. <el-card class="card" shadow="never">
  21. <template #header>
  22. 父子联动(选中父节点,自动选择子节点):
  23. <el-switch
  24. v-model="checkStrictly"
  25. inline-prompt
  26. active-text="是"
  27. inactive-text="否"
  28. />
  29. 全选/全不选:
  30. <el-switch
  31. v-model="treeNodeAll"
  32. inline-prompt
  33. active-text="是"
  34. inactive-text="否"
  35. @change="handleCheckedTreeNodeAll()"
  36. />
  37. </template>
  38. <el-tree
  39. ref="treeRef"
  40. node-key="id"
  41. show-checkbox
  42. :check-strictly="!checkStrictly"
  43. :props="defaultProps"
  44. :data="treeOptions"
  45. empty-text="加载中,请稍后"
  46. />
  47. </el-card>
  48. </el-form-item> </el-col
  49. ></el-row>
  50. </el-form>
  51. <!-- 操作按钮 -->
  52. <template #footer>
  53. <div class="dialog-footer">
  54. <el-button
  55. :title="t('action.save')"
  56. :loading="actionLoading"
  57. @click="submitScope()"
  58. type="primary"
  59. :disabled="formLoading"
  60. >
  61. 保存
  62. </el-button>
  63. <el-button
  64. :loading="actionLoading"
  65. :title="t('dialog.close')"
  66. @click="dialogScopeVisible = false"
  67. >取 消</el-button
  68. >
  69. </div>
  70. </template>
  71. </Dialog>
  72. </template>
  73. <script setup lang="ts">
  74. import * as RoleApi from '@/api/system/role'
  75. import type { ElTree } from 'element-plus'
  76. import type { FormExpose } from '@/components/Form'
  77. import { handleTree, defaultProps } from '@/utils/tree'
  78. import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
  79. import * as MenuApi from '@/api/system/menu'
  80. import * as PermissionApi from '@/api/system/permission'
  81. // ========== CRUD 相关 ==========
  82. const actionLoading = ref(false) // 遮罩层
  83. const menuPermissionFormRef = ref<FormExpose>() // 表单 Ref
  84. const { t } = useI18n() // 国际化
  85. const dialogScopeTitle = ref('菜单权限')
  86. const dataScopeDictDatas = ref()
  87. const message = useMessage() // 消息弹窗
  88. const actionScopeType = ref('')
  89. // 选项
  90. const checkStrictly = ref(true)
  91. const treeNodeAll = ref(false)
  92. const dialogScopeVisible = ref(false) // 弹窗的是否展示
  93. const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
  94. const treeOptions = ref<any[]>([]) // 菜单树形结构
  95. const treeRef = ref<InstanceType<typeof ElTree>>()
  96. // ========== 数据权限 ==========
  97. const dataScopeForm = reactive({
  98. id: 0,
  99. name: '',
  100. code: '',
  101. dataScope: 0,
  102. checkList: []
  103. })
  104. /** 打开弹窗 */
  105. const openModal = async (type: string, row: RoleApi.RoleVO) => {
  106. dataScopeForm.id = row.id
  107. dataScopeForm.name = row.name
  108. dataScopeForm.code = row.code
  109. actionScopeType.value = type
  110. dialogScopeVisible.value = true
  111. const menuRes = await MenuApi.getSimpleMenusList()
  112. treeOptions.value = handleTree(menuRes)
  113. const role = await PermissionApi.listRoleMenusApi(row.id)
  114. if (role) {
  115. role?.forEach((item: any) => {
  116. unref(treeRef)?.setChecked(item, true, false)
  117. })
  118. }
  119. }
  120. // 保存权限
  121. const submitScope = async () => {
  122. const data = ref<PermissionApi.PermissionAssignRoleMenuReqVO>({
  123. roleId: dataScopeForm.id,
  124. menuIds: [
  125. ...(treeRef.value!.getCheckedKeys(false) as unknown as Array<number>),
  126. ...(treeRef.value!.getHalfCheckedKeys() as unknown as Array<number>)
  127. ]
  128. })
  129. await PermissionApi.assignRoleMenuApi(data.value)
  130. message.success(t('common.updateSuccess'))
  131. dialogScopeVisible.value = false
  132. }
  133. // 全选/全不选
  134. const handleCheckedTreeNodeAll = () => {
  135. treeRef.value!.setCheckedNodes(treeNodeAll.value ? treeOptions.value : [])
  136. }
  137. const init = () => {
  138. dataScopeDictDatas.value = getIntDictOptions(DICT_TYPE.SYSTEM_DATA_SCOPE)
  139. }
  140. defineExpose({ openModal }) // 提供 openModal 方法,用于打开弹窗
  141. // ========== 初始化 ==========
  142. onMounted(() => {
  143. init()
  144. })
  145. </script>