index.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <template>
  2. <div>
  3. <BasicTable @register="registerTable">
  4. <template #bodyCell="{ column, record }">
  5. <template v-if="column.key === 'disable'">
  6. <Tag :color="formatDictColor(disableOptions, record.disable)">
  7. {{ formatDictValue(disableOptions, record.disable) }}
  8. </Tag>
  9. </template>
  10. <template v-if="column.key === 'action'">
  11. <TableAction
  12. :actions="[
  13. {
  14. auth: ['sys:org:edit'],
  15. icon: 'icon-edit|iconfont',
  16. tooltip: '编辑',
  17. onClick: handleEdit.bind(null, record),
  18. },
  19. {
  20. auth: ['sys:org:remove'],
  21. icon: 'icon-delete|iconfont',
  22. tooltip: '删除',
  23. color: 'error',
  24. popConfirm: {
  25. title: '是否确认删除',
  26. placement: 'left',
  27. confirm: handleDelete.bind(null, record),
  28. },
  29. },
  30. ]"
  31. />
  32. </template>
  33. </template>
  34. <template #toolbar>
  35. <a-button
  36. type="primary"
  37. @click="handleCreate"
  38. v-auth="['sys:org:add']"
  39. preIcon="icon-plus|iconfont"
  40. >新增</a-button
  41. >
  42. </template>
  43. </BasicTable>
  44. <FormModal @register="registerModal" @success="handleSuccess" />
  45. </div>
  46. </template>
  47. <script lang="ts" setup>
  48. import { nextTick, onBeforeMount, ref, watch } from 'vue';
  49. import { Tag } from 'ant-design-vue';
  50. import { BasicTable, useTable, TableAction } from '/@/components/Table';
  51. import { useModal } from '/@/components/Modal';
  52. import { useMessage } from '/@/hooks/web/useMessage';
  53. import FormModal from './FormModal.vue';
  54. import { columns, searchFormSchema } from './data';
  55. import { sysOrgQueryPage, sysOrgRemove } from '/@/api/sys/sysOrgApi';
  56. import { listDictModel } from '/@/api/common';
  57. import { formatDictColor, formatDictValue } from '/@/utils';
  58. const emit = defineEmits(['refresh-tree']);
  59. const props = defineProps({
  60. nodeId: { type: String },
  61. });
  62. const sysStatusOptions = ref([]);
  63. const sysOrgTypeOptions = ref([]);
  64. const disableOptions = ref();
  65. onBeforeMount(async () => {
  66. sysStatusOptions.value = await listDictModel({ dictCode: 'sys_status' });
  67. sysOrgTypeOptions.value = await listDictModel({ dictCode: 'sys_org_type' });
  68. disableOptions.value = await listDictModel({ dictCode: 'sys_disable_type' });
  69. });
  70. const { createMessage } = useMessage();
  71. const [registerModal, { openModal }] = useModal();
  72. const [registerTable, { reload }] = useTable({
  73. title: '部门列表',
  74. api: sysOrgQueryPage,
  75. rowKey: 'id',
  76. columns,
  77. formConfig: {
  78. labelWidth: 120,
  79. schemas: searchFormSchema,
  80. autoSubmitOnEnter: true,
  81. baseColProps: { xs: 24, sm: 12, md: 12, lg: 12, xl: 8 },
  82. resetButtonOptions: {
  83. preIcon: 'icon-delete|iconfont',
  84. },
  85. submitButtonOptions: {
  86. preIcon: 'icon-search|iconfont',
  87. },
  88. },
  89. showIndexColumn: false,
  90. useSearchForm: true,
  91. showTableSetting: true,
  92. bordered: true,
  93. actionColumn: {
  94. auth: ['sys:org:edit', 'sys:org:remove'],
  95. width: 80,
  96. title: '操作',
  97. dataIndex: 'action',
  98. },
  99. beforeFetch: handleBeforeFetch,
  100. afterFetch: handleAfterFetch,
  101. });
  102. // 新增按钮事件
  103. function handleCreate() {
  104. const record = {
  105. parentId: props.nodeId,
  106. };
  107. openModal(true, {
  108. record,
  109. isUpdate: false,
  110. });
  111. }
  112. // 编辑按钮事件
  113. function handleEdit(record: Recordable) {
  114. console.log(record);
  115. openModal(true, {
  116. record,
  117. isUpdate: true,
  118. });
  119. }
  120. // 删除按钮事件
  121. async function handleDelete(record: Recordable) {
  122. console.log(record);
  123. await sysOrgRemove([record.id]);
  124. createMessage.success('删除成功!');
  125. emit('refresh-tree', record.parentId === '0' ? undefined : record.parentId);
  126. // 表格刷新,在refresh-tree事件之后,执行
  127. // await reload();
  128. }
  129. // 弹窗回调事件
  130. function handleSuccess({ isUpdate, values }) {
  131. console.log(isUpdate);
  132. console.log(values);
  133. emit('refresh-tree', values.parentId);
  134. // 表格刷新,在refresh-tree事件之后,执行
  135. // reload();
  136. }
  137. // 表格请求之前,对参数进行处理
  138. function handleBeforeFetch(params) {
  139. return { ...params, parentId: !props.nodeId ? '0' : props.nodeId };
  140. }
  141. function handleAfterFetch(data) {
  142. const res = data.map(ele => {
  143. ele.children = null;
  144. return ele;
  145. });
  146. return res;
  147. }
  148. // 刷新表格
  149. function refresh() {
  150. reload();
  151. }
  152. watch(
  153. () => props.nodeId,
  154. () => {
  155. nextTick(() => {
  156. reload();
  157. });
  158. },
  159. );
  160. // 暴露内部方法
  161. defineExpose({ refresh });
  162. </script>