set-tenant-menu.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <template>
  2. <el-dialog v-model="state.showDialog" destroy-on-close :title="innerTitle" append-to-body draggable width="780px">
  3. <div>
  4. <el-tree
  5. ref="permissionTreeRef"
  6. :data="state.permissionTreeData"
  7. node-key="id"
  8. show-checkbox
  9. highlight-current
  10. default-expand-all
  11. check-on-click-node
  12. :expand-on-click-node="false"
  13. :props="{ class: customNodeClass }"
  14. :default-checked-keys="state.checkedKeys"
  15. />
  16. </div>
  17. <template #footer>
  18. <span class="dialog-footer">
  19. <el-button @click="onCancel" size="default">取 消</el-button>
  20. <el-button type="primary" @click="onSure" size="default" :loading="state.sureLoading">确 定</el-button>
  21. </span>
  22. </template>
  23. </el-dialog>
  24. </template>
  25. <script lang="ts" setup name="admin/tenant/components/set-tenant-menu">
  26. import { ref, reactive, getCurrentInstance, computed } from 'vue'
  27. import { TenantListOutput, PermissionSaveTenantPermissionsInput } from '/@/api/admin/data-contracts'
  28. import { PermissionApi } from '/@/api/admin/Permission'
  29. import { ElTree } from 'element-plus'
  30. import { listToTree } from '/@/utils/tree'
  31. import { cloneDeep } from 'lodash-es'
  32. const props = defineProps({
  33. title: {
  34. type: String,
  35. default: '',
  36. },
  37. })
  38. const innerTitle = computed(() => {
  39. return props.title ? props.title : state.name ? `设置【${state.name}】菜单权限` : '设置菜单权限'
  40. })
  41. const state = reactive({
  42. showDialog: false,
  43. loading: false,
  44. sureLoading: false,
  45. permissionTreeData: [],
  46. id: 0 as number | undefined,
  47. name: '' as string | undefined | null,
  48. checkedKeys: [],
  49. })
  50. const { proxy } = getCurrentInstance() as any
  51. const permissionTreeRef = ref<InstanceType<typeof ElTree>>()
  52. const getTenantPermissionList = async () => {
  53. const res = await new PermissionApi().getTenantPermissionList({ tenantId: state.id })
  54. state.checkedKeys = res?.success ? (res.data as never[]) : []
  55. }
  56. // 打开对话框
  57. const open = async (tenat: TenantListOutput) => {
  58. state.id = tenat.id
  59. state.name = tenat.name
  60. proxy.$modal.loading()
  61. await onQuery()
  62. await getTenantPermissionList()
  63. proxy.$modal.closeLoading()
  64. state.showDialog = true
  65. }
  66. // 关闭对话框
  67. const close = () => {
  68. state.showDialog = false
  69. }
  70. const onQuery = async () => {
  71. state.loading = true
  72. const res = await new PermissionApi().getPermissionList().catch(() => {
  73. state.loading = false
  74. })
  75. if (res && res.data && res.data.length > 0) {
  76. state.permissionTreeData = listToTree(cloneDeep(res.data))
  77. } else {
  78. state.permissionTreeData = []
  79. }
  80. state.loading = false
  81. }
  82. const customNodeClass = (data: any) => {
  83. return data.row ? 'is-penultimate' : ''
  84. }
  85. // 取消
  86. const onCancel = () => {
  87. state.showDialog = false
  88. }
  89. // 确定
  90. const onSure = async () => {
  91. state.sureLoading = true
  92. const permissionIds = permissionTreeRef.value?.getCheckedKeys(true)
  93. const input = { tenantId: state.id, permissionIds: permissionIds } as PermissionSaveTenantPermissionsInput
  94. const res = await new PermissionApi().saveTenantPermissions(input, { showSuccessMessage: true }).catch(() => {
  95. state.sureLoading = false
  96. })
  97. state.sureLoading = false
  98. if (res?.success) {
  99. state.showDialog = false
  100. }
  101. }
  102. defineExpose({
  103. open,
  104. close,
  105. })
  106. </script>
  107. <style scoped lang="scss">
  108. :deep(.el-dialog__body) {
  109. padding: 5px 10px;
  110. }
  111. :deep(.is-penultimate) {
  112. .el-tree-node__children {
  113. padding-left: 65px;
  114. white-space: pre-wrap;
  115. line-height: 100%;
  116. .el-tree-node {
  117. display: inline-block;
  118. }
  119. .el-tree-node__content {
  120. padding-left: 12px !important;
  121. padding-right: 12px;
  122. .el-tree-node__expand-icon.is-leaf {
  123. display: none;
  124. }
  125. }
  126. }
  127. }
  128. </style>