index.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <template>
  2. <div>
  3. <BasicTable
  4. @register="registerTable"
  5. @selection-change="handleSelectionChange"
  6. @row-click="handleRowClick"
  7. @row-dbClick="handleRowDbClick"
  8. >
  9. <template #bodyCell="{ column, record }">
  10. <template v-if="column.key === 'disable'">
  11. <Tag :color="record.disable ? 'error' : 'success'">
  12. {{ commonDict(record.disable, 1) }}
  13. </Tag>
  14. </template>
  15. <template v-if="column.key === 'action'">
  16. <TableAction
  17. :actions="[
  18. {
  19. auth: ['system:sysDict:edit'],
  20. icon: 'icon-edit|iconfont',
  21. tooltip: '编辑',
  22. onClick: handleEdit.bind(null, record),
  23. },
  24. {
  25. auth: ['system:sysDict:remove'],
  26. icon: 'icon-delete|iconfont',
  27. tooltip: '删除',
  28. color: 'error',
  29. popConfirm: {
  30. title: '是否确认删除',
  31. confirm: handleDelete.bind(null, record),
  32. },
  33. },
  34. ]"
  35. />
  36. </template>
  37. </template>
  38. <template #toolbar>
  39. <a-button
  40. v-auth="['system:sysDict:add']"
  41. type="primary"
  42. @click="handleCreate"
  43. preIcon="icon-plus|iconfont"
  44. >新增</a-button
  45. >
  46. <!-- <a-button
  47. v-auth="['system:sysDict:export']"
  48. color="warning"
  49. @click="handleExport"
  50. preIcon="ant-design:download-outlined"
  51. >导出</a-button
  52. > -->
  53. <a-button
  54. v-auth="['system:sysDict:refresh']"
  55. color="success"
  56. @click="handleRefresh"
  57. preIcon="ant-design:redo-outlined"
  58. >刷新缓存</a-button
  59. >
  60. </template>
  61. </BasicTable>
  62. <FormModal @register="registerModal" @success="handleSuccess" />
  63. </div>
  64. </template>
  65. <script lang="ts" setup>
  66. import { onBeforeMount, ref } from 'vue';
  67. import { Tag } from 'ant-design-vue';
  68. import { BasicTable, useTable, TableAction } from '/@/components/Table';
  69. import { useModal } from '/@/components/Modal';
  70. import { useMessage } from '/@/hooks/web/useMessage';
  71. import FormModal from './FormModal.vue';
  72. import { columns, searchFormSchema } from './data';
  73. // import { fetchList, SysDictRemove, exportList, refresh } from '/@/api/modules/system/sysDictApi';
  74. import { SysDictQueryList, SysDictRemove } from '/@/api/sys/sysDictApi';
  75. import { commonDict } from '/@/utils';
  76. // import { listDictModel, downloadFile } from '/@/api/common';
  77. import { listDictModel } from '/@/api/common';
  78. const emit = defineEmits(['dict-change']);
  79. const sysStatusOptions = ref([]);
  80. onBeforeMount(async () => {
  81. const dictSysStatus = await listDictModel({ dictCode: 'sys_status' });
  82. sysStatusOptions.value = dictSysStatus.data || [];
  83. });
  84. const { createMessage, createConfirm } = useMessage();
  85. const [registerModal, { openModal }] = useModal();
  86. const [registerTable, { reload, getDataSource, getSelectRowKeys, setSelectedRowKeys }] = useTable(
  87. {
  88. title: '字典列表',
  89. api: SysDictQueryList,
  90. rowKey: 'dictId',
  91. columns,
  92. rowSelection: { type: 'radio' },
  93. clickToRowSelect: true,
  94. formConfig: {
  95. labelWidth: 120,
  96. schemas: searchFormSchema,
  97. autoSubmitOnEnter: true,
  98. baseColProps: { span: 12 },
  99. resetButtonOptions: {
  100. preIcon: 'icon-delete|iconfont',
  101. },
  102. submitButtonOptions: {
  103. preIcon: 'icon-search|iconfont',
  104. },
  105. },
  106. showIndexColumn: false,
  107. useSearchForm: true,
  108. showTableSetting: true,
  109. bordered: true,
  110. actionColumn: {
  111. auth: ['system:sysDict:edit', 'system:sysDict:remove'],
  112. width: 80,
  113. title: '操作',
  114. dataIndex: 'action',
  115. },
  116. afterFetch: handleAfterFetch,
  117. },
  118. );
  119. // 新增按钮事件
  120. function handleCreate() {
  121. openModal(true, {
  122. isUpdate: false,
  123. });
  124. }
  125. // 编辑按钮事件
  126. function handleEdit(record: Recordable) {
  127. console.log(record);
  128. openModal(true, {
  129. record,
  130. isUpdate: true,
  131. });
  132. }
  133. // 删除按钮事件
  134. async function handleDelete(record: Recordable) {
  135. console.log(record);
  136. await SysDictRemove([record.dictId]);
  137. createMessage.success('删除成功!');
  138. await reload();
  139. // after delete, select first row
  140. const list = getDataSource();
  141. if (list.length > 0) {
  142. setSelectedRowKeys([list[0].dictId]);
  143. } else {
  144. setSelectedRowKeys([]);
  145. }
  146. console.log('handleDelete');
  147. emitDictChange();
  148. }
  149. // 导出按钮事件
  150. // async function handleExport() {
  151. // createConfirm({
  152. // iconType: 'warning',
  153. // title: '提示',
  154. // content: '确认导出?',
  155. // onOk: async () => {
  156. // const params = getForm().getFieldsValue();
  157. // const filepath = await exportList(params);
  158. // downloadFile(filepath);
  159. // },
  160. // });
  161. // }
  162. // 刷新缓存按钮事件
  163. async function handleRefresh() {
  164. createConfirm({
  165. iconType: 'warning',
  166. title: '提示',
  167. content: '确认刷新所有数据字典缓存?',
  168. onOk: async () => {
  169. // await refresh();
  170. createMessage.success('刷新成功!');
  171. },
  172. });
  173. }
  174. // 弹窗回调事件
  175. async function handleSuccess({ isUpdate, values }) {
  176. console.log(isUpdate);
  177. console.log(values);
  178. await reload();
  179. if (isUpdate) {
  180. // after update, select updated row
  181. setSelectedRowKeys([values.id]);
  182. } else {
  183. // after create, select first row
  184. const list = getDataSource();
  185. if (list.length > 0) {
  186. setSelectedRowKeys([list[0].id]);
  187. } else {
  188. setSelectedRowKeys([]);
  189. }
  190. }
  191. console.log('handleSuccess', isUpdate ? 'update' : 'create');
  192. emitDictChange();
  193. }
  194. // 表格请求之后,对返回值进行处理
  195. function handleAfterFetch(data) {
  196. // after fetch, select first row
  197. if (data.length > 0) {
  198. setSelectedRowKeys([data[0].dictId]);
  199. } else {
  200. setSelectedRowKeys([]);
  201. }
  202. console.log('handleAfterFetch', data);
  203. emitDictChange();
  204. }
  205. // 表格行点击事件
  206. function handleRowClick(record: Recordable) {
  207. console.log('handleRowClick', record);
  208. setSelectedRowKeys([record.dictId]);
  209. emitDictChange();
  210. }
  211. // 表格行双击事件
  212. function handleRowDbClick(record: Recordable) {
  213. console.log('handleRowDbClick', record);
  214. setSelectedRowKeys([record.dictId]);
  215. emitDictChange();
  216. }
  217. // 表格行选中事件
  218. function handleSelectionChange({ keys, rows }) {
  219. console.log('handleSelectionChange', keys, rows);
  220. emitDictChange();
  221. }
  222. // 字典变化事件
  223. function emitDictChange() {
  224. const selectedKeys = getSelectRowKeys();
  225. console.log(selectedKeys);
  226. emit('dict-change', selectedKeys.length > 0 ? selectedKeys[0] : '');
  227. }
  228. </script>