codes.vue 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 } from 'vue'
  18. import type { ElTreeV2 } from 'element-plus'
  19. import { clone } from 'lodash'
  20. import { ElMessage } from 'element-plus'
  21. const { modelValue, role } = defineProps<{
  22. modelValue: boolean,
  23. role?: Role
  24. }>()
  25. const emit = defineEmits(['update:modelValue'])
  26. const codes = ref()
  27. const tree = ref()
  28. const submitLoading = ref(false)
  29. const treeProps = {
  30. value: 'id',
  31. label: 'label',
  32. children: 'children',
  33. }
  34. onMounted(async () => {
  35. codes.value = (await getCodes()).data
  36. })
  37. const submitForm = async () => {
  38. const checkedCodes = tree.value?.getCheckedNodes(true).map(node => node.lable)
  39. const checkedKeys = tree.value?.getCheckedKeys()
  40. const updateRole = clone(role) as Role
  41. updateRole.codes = checkedKeys
  42. updateRole.codes_cn = checkedCodes
  43. submitLoading.value = true
  44. const result = await update(updateRole)
  45. if (result.code != 0) {
  46. return
  47. }
  48. submitLoading.value = false
  49. ElMessage({ message: '更新成功' })
  50. emit('update:modelValue', false)
  51. }
  52. </script>