set-pkg-menu.vue 3.8 KB

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