RoleSetFunction.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <template>
  2. <a-layout class="full-height">
  3. <a-layout-header style="height: 48px; background: white; line-height: 48px; text-align: center">
  4. <h3>{{ $t('system.views.role.title.setFunction') }}</h3>
  5. </a-layout-header>
  6. <Divider style="margin: 0" />
  7. <a-layout-content style=" overflow: auto;background: white">
  8. <Spin :spinning="dataLoading">
  9. <a-tree
  10. ref="treeRef"
  11. v-model:checkedKeys="checkedKeysModel"
  12. :tree-data="functionTreeData"
  13. checkable
  14. />
  15. </Spin>
  16. </a-layout-content>
  17. <Divider style="margin: 0" />
  18. <a-layout-footer style="height: 50px; padding: 10px 0; background: white; text-align: center">
  19. <div style="padding: 0 5px">
  20. <a-button
  21. v-permission="permissions.setFunction"
  22. :loading="saveLoading"
  23. block
  24. type="primary"
  25. @click="handleSave"
  26. >
  27. {{ $t('common.button.save') }}
  28. </a-button>
  29. </div>
  30. </a-layout-footer>
  31. </a-layout>
  32. </template>
  33. <script lang="ts">
  34. import { defineComponent, ref, onMounted, toRefs, watch, unref } from 'vue';
  35. import type { PropType } from 'vue';
  36. import { message, Spin, Divider } from 'ant-design-vue';
  37. import TreeUtils from '@/utils/TreeUtils';
  38. import { SystemPermissions } from '@/modules/system/constants/SystemConstants';
  39. import { ApiServiceEnum, defHttp } from '@/utils/http/axios';
  40. /**
  41. * 设置角色对应的功能
  42. */
  43. export default defineComponent({
  44. name: 'RoleSetFunction',
  45. components: {
  46. Spin,
  47. Divider,
  48. },
  49. props: {
  50. roleId: {
  51. type: Number as PropType<number>,
  52. default: null,
  53. },
  54. },
  55. setup(props) {
  56. const { roleId } = toRefs(props);
  57. const treeRef = ref();
  58. // 树形控件数据
  59. const functionTreeData = ref<Array<any>>([]);
  60. const dataLoading = ref(false);
  61. const saveLoading = ref(false);
  62. const checkedKeysModel = ref([]);
  63. /**
  64. * 加载功能树函数
  65. */
  66. const loadFunctionTreeData = async () => {
  67. dataLoading.value = true;
  68. try {
  69. const result = await defHttp.post({
  70. service: ApiServiceEnum.SMART_SYSTEM,
  71. url: 'sys/function/list',
  72. data: {
  73. sortName: 'seq',
  74. },
  75. });
  76. functionTreeData.value =
  77. TreeUtils.convertList2Tree(
  78. result.map(({ functionId, functionName, parentId }: any) => {
  79. return {
  80. key: functionId,
  81. title: functionName,
  82. parentId: parentId,
  83. };
  84. }),
  85. (item) => item.key,
  86. (item) => item.parentId,
  87. 0,
  88. ) || [];
  89. } finally {
  90. dataLoading.value = false;
  91. }
  92. };
  93. /**
  94. * 加载角色对应的功能ID
  95. */
  96. const loadRoleFunctions = async () => {
  97. if (roleId.value !== null) {
  98. dataLoading.value = true;
  99. try {
  100. checkedKeysModel.value = await defHttp.post({
  101. service: ApiServiceEnum.SMART_SYSTEM,
  102. url: 'sys/role/listFunctionId',
  103. params: {
  104. id: unref(roleId),
  105. },
  106. });
  107. } finally {
  108. dataLoading.value = false;
  109. }
  110. }
  111. };
  112. /**
  113. * 执行保存操作
  114. */
  115. const handleSave = async () => {
  116. const tree = unref(treeRef);
  117. if (roleId.value === null) {
  118. message.error('请先选定角色');
  119. return false;
  120. }
  121. saveLoading.value = true;
  122. try {
  123. await defHttp.post({
  124. service: ApiServiceEnum.SMART_SYSTEM,
  125. url: 'sys/role/saveRoleMenu',
  126. data: {
  127. roleId: roleId.value,
  128. functionIdList: tree.checkedKeys,
  129. halfFunctionIdList: tree.halfCheckedKeys,
  130. },
  131. });
  132. message.success('保存成功');
  133. } finally {
  134. saveLoading.value = false;
  135. }
  136. };
  137. watch(roleId, loadRoleFunctions);
  138. onMounted(loadFunctionTreeData);
  139. return {
  140. functionTreeData,
  141. dataLoading,
  142. saveLoading,
  143. checkedKeysModel,
  144. handleSave,
  145. permissions: SystemPermissions.role,
  146. treeRef,
  147. };
  148. },
  149. });
  150. </script>
  151. <style scoped></style>