set-role-menu.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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/role/components/set-role-menu">
  35. import { ref, reactive, getCurrentInstance, computed } from 'vue'
  36. import { RoleGetListOutput, PermissionAssignInput } from '/@/api/admin/data-contracts'
  37. import { PermissionApi } from '/@/api/admin/Permission'
  38. import { ElTree } from 'element-plus'
  39. import { listToTree } from '/@/utils/tree'
  40. import { cloneDeep } from 'lodash-es'
  41. const props = defineProps({
  42. title: {
  43. type: String,
  44. default: '',
  45. },
  46. })
  47. const innerTitle = computed(() => {
  48. return props.title ? props.title : state.roleName ? `设置【${state.roleName}】菜单权限` : '设置菜单权限'
  49. })
  50. const state = reactive({
  51. showDialog: false,
  52. loading: false,
  53. sureLoading: false,
  54. permissionTreeData: [],
  55. roleId: 0 as number | undefined,
  56. roleName: '' as string | undefined | null,
  57. checkedKeys: [],
  58. appid:0
  59. })
  60. const { proxy } = getCurrentInstance() as any
  61. const permissionTreeRef = ref<InstanceType<typeof ElTree>>()
  62. const getRolePermissionList = async () => {
  63. const res = await new PermissionApi().getMenuInfo( {roleid:state.roleId,applyid:state.appid} )
  64. state.checkedKeys = res?.success ? (res.data as never[]) : []
  65. }
  66. // 打开对话框
  67. const open = async (role: RoleGetListOutput,appId:any) => {
  68. state.roleId = role.id
  69. state.roleName = role.name
  70. state.appid=appId
  71. proxy.$modal.loading()
  72. await onQuery()
  73. await getRolePermissionList()
  74. proxy.$modal.closeLoading()
  75. state.showDialog = true
  76. }
  77. // 关闭对话框
  78. const close = () => {
  79. state.showDialog = false
  80. }
  81. const onQuery = async () => {
  82. state.loading = true
  83. const res = await new PermissionApi().getPermissionInfo({appid:state.appid}).catch(() => {
  84. state.loading = false
  85. })
  86. res.data=res.data.map((item)=>{
  87. item.parentId=Number(item.parentId)
  88. return item
  89. })
  90. if (res && res.data && res.data.length > 0) {
  91. state.permissionTreeData = listToTree(cloneDeep(res.data))
  92. } else {
  93. state.permissionTreeData = []
  94. }
  95. state.loading = false
  96. }
  97. const customNodeClass = (data: any) => {
  98. return data.row ? 'is-penultimate' : ''
  99. }
  100. // 取消
  101. const onCancel = () => {
  102. state.showDialog = false
  103. }
  104. // 确定
  105. const onSure = async () => {
  106. state.sureLoading = true
  107. const permissionIds = permissionTreeRef.value?.getCheckedKeys(true)
  108. const input = { strings: permissionIds }
  109. const res = await new PermissionApi().roleMenuAssign(input,{applyid:String(state.appid),roleId: String(state.roleId)} ,{ showSuccessMessage: true }).catch(() => {
  110. state.sureLoading = false
  111. })
  112. state.sureLoading = false
  113. if (res?.success) {
  114. state.showDialog = false
  115. }
  116. }
  117. defineExpose({
  118. open,
  119. close,
  120. })
  121. </script>
  122. <style scoped lang="scss">
  123. :deep(.el-dialog__body) {
  124. padding: 5px 10px;
  125. }
  126. :deep(.is-penultimate) {
  127. .el-tree-node__children {
  128. padding-left: 65px;
  129. white-space: pre-wrap;
  130. line-height: 100%;
  131. .el-tree-node {
  132. display: inline-block;
  133. }
  134. .el-tree-node__content {
  135. padding-left: 12px !important;
  136. padding-right: 12px;
  137. .el-tree-node__expand-icon.is-leaf {
  138. display: none;
  139. }
  140. }
  141. }
  142. }
  143. </style>