codes.vue 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <template>
  2. <el-dialog :modelValue="modelValue" @close="$emit('update:modelValue', false)" title="编辑权限">
  3. <el-tree-v2 ref="tree" :data="codes" :props="treeProps" :default-checked-keys="role.codes" show-checkbox :height="500">
  4. <template #default="{ node }">
  5. <span>{{ node.data.lable }}</span>
  6. </template>
  7. </el-tree-v2>
  8. <template #footer>
  9. <el-button type="default" @click="$emit('update:modelValue', false)">取消</el-button>
  10. <el-button v-loading="submitLoading" type="primary" @click="submitForm()">提交</el-button>
  11. </template>
  12. </el-dialog>
  13. </template>
  14. <script setup lang="ts">
  15. import { update, codes as getCodes } from '/@/api/role'
  16. import type { Role } from '/@/api/role'
  17. import { ref, onMounted, inject } from 'vue';
  18. import { ElMessage } from 'element-plus'
  19. import { throttle } from 'lodash'
  20. import { codes as symbolCode } from '/@/types/di';
  21. import { CodeTree } from '/@/api/role';
  22. const { modelValue, role } = defineProps<{
  23. modelValue: boolean,
  24. role: Role
  25. }>()
  26. console.log(role)
  27. const emit = defineEmits<{
  28. (e: 'update:modelValue', value: boolean): void
  29. (e: 'submit', result: any): void
  30. }>()
  31. const codes = ref<CodeTree[]>([])
  32. const tree = ref()
  33. const submitLoading = ref(false)
  34. const treeProps = {
  35. value: 'id',
  36. label: 'label',
  37. children: 'children',
  38. }
  39. onMounted(async () => {
  40. codes.value = await inject(symbolCode, () => getCodes().then(res => res.data))
  41. })
  42. const submitForm = throttle(async () => {
  43. const checkedCodes = tree.value?.getCheckedNodes(true).map(node => node.lable)
  44. const checkedKeys = tree.value?.getCheckedKeys()
  45. role.codes = checkedKeys
  46. role.codes_cn = checkedCodes
  47. submitLoading.value = true
  48. const result = await update(role)
  49. if (result.code != 0) {
  50. return
  51. }
  52. submitLoading.value = false
  53. ElMessage({ message: '更新成功' })
  54. emit('update:modelValue', false)
  55. emit('submit', result)
  56. })
  57. </script>