list.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <script lang="ts" setup>
  2. import type { Recordable } from '@vben/types';
  3. import type {
  4. OnActionClickParams,
  5. VxeTableGridOptions,
  6. } from '#/adapter/vxe-table';
  7. import type { SystemModel } from '#/api';
  8. import { Page, useVbenModal } from '@vben/common-ui';
  9. import { Plus } from '@vben/icons';
  10. import { Button, message, Modal, notification } from 'ant-design-vue';
  11. import { useVbenVxeGrid } from '#/adapter/vxe-table';
  12. import { $t } from '#/locales';
  13. import { useUserSearchFormSchema, useUserTableColumns } from './data';
  14. import { mockData } from './mock';
  15. import Form from './modules/form.vue';
  16. const [FormModal, formModalApi] = useVbenModal({
  17. connectedComponent: Form,
  18. destroyOnClose: true,
  19. });
  20. const [Grid, gridApi] = useVbenVxeGrid({
  21. formOptions: {
  22. schema: useUserSearchFormSchema(),
  23. submitOnChange: true,
  24. },
  25. gridOptions: {
  26. columns: useUserTableColumns(onActionClick, onStatusChange),
  27. height: 'auto',
  28. keepSource: true,
  29. // proxyConfig: {
  30. // ajax: {
  31. // query({ page }, formValues) {
  32. // return listUsersMethod(page.currentPage, page.pageSize, formValues);
  33. // },
  34. // },
  35. // },
  36. // rowConfig: {
  37. // keyField: 'id',
  38. // },
  39. proxyConfig: {
  40. response: {
  41. result: 'Data.Items',
  42. total: 'Data.TotalRecordCount',
  43. },
  44. ajax: {
  45. query() {
  46. return Promise.resolve({
  47. Data: mockData,
  48. ResultInfo: '操作成功',
  49. ResultCode: 0,
  50. });
  51. },
  52. },
  53. },
  54. rowConfig: {
  55. keyField: 'pid',
  56. },
  57. } as VxeTableGridOptions<SystemModel.User>,
  58. });
  59. function onActionClick(e: OnActionClickParams<SystemModel.User>) {
  60. switch (e.code) {
  61. case 'delete': {
  62. onDeleteHandle(e.row);
  63. break;
  64. }
  65. case 'edit': {
  66. onEditHandle(e.row);
  67. break;
  68. }
  69. }
  70. }
  71. /**
  72. * 将Antd的Modal.confirm封装为promise,方便在异步函数中调用。
  73. * @param content 提示内容
  74. * @param title 提示标题
  75. */
  76. function confirm(content: string, title: string) {
  77. return new Promise((reslove, reject) => {
  78. Modal.confirm({
  79. content,
  80. onCancel() {
  81. reject(new Error('已取消'));
  82. },
  83. onOk() {
  84. reslove(true);
  85. },
  86. title,
  87. });
  88. });
  89. }
  90. /**
  91. * 状态开关即将改变
  92. * @param newStatus 期望改变的状态值
  93. * @param row 行数据
  94. * @returns 返回false则中止改变,返回其他值(undefined、true)则允许改变
  95. */
  96. async function onStatusChange(newStatus: 0 | 1, row: SystemModel.User) {
  97. const status: Recordable<string> = {
  98. 0: '启用',
  99. 1: '禁用',
  100. };
  101. try {
  102. await confirm(
  103. `你要将${row.name}的状态切换为 【${status[newStatus.toString()]}】 吗?`,
  104. `切换状态`,
  105. );
  106. try {
  107. // await updateUserStatusMethod(row.pid!, { status: newStatus });
  108. mockData.Items = mockData.Items.map((item) => {
  109. if (item.pid === row.pid) {
  110. return {
  111. ...item,
  112. status: newStatus,
  113. };
  114. }
  115. return item;
  116. });
  117. notification.success({
  118. message: '切换状态成功',
  119. });
  120. } catch (error: any) {
  121. notification.error({
  122. message: error.message || '切换状态失败',
  123. });
  124. }
  125. return true;
  126. } catch {
  127. return false;
  128. }
  129. }
  130. function onRefresh() {
  131. gridApi.query();
  132. }
  133. function onEditHandle(row?: SystemModel.User) {
  134. formModalApi.setData(row ?? {}).open();
  135. console.log('row', row);
  136. console.log('mockdata', mockData)
  137. }
  138. async function onDeleteHandle(row: SystemModel.User) {
  139. const hideLoading = message.loading({
  140. content: $t('ui.actionMessage.deleting', [row.name]),
  141. duration: 0,
  142. key: 'action_process_msg',
  143. });
  144. try {
  145. console.log('删除', row);;
  146. // await deleteUserMethod(row);
  147. message.success({
  148. content: $t('ui.actionMessage.deleteSuccess', [row.name]),
  149. key: 'action_process_msg',
  150. });
  151. onRefresh();
  152. } finally {
  153. hideLoading();
  154. }
  155. }
  156. </script>
  157. <template>
  158. <Page auto-content-height>
  159. <FormModal @success="onRefresh" />
  160. <Grid>
  161. <template #toolbar-tools>
  162. <Button type="primary" @click="onEditHandle()">
  163. <Plus class="size-5" />
  164. {{ $t('ui.actionTitle.create', [$t('system.user._')]) }}
  165. </Button>
  166. </template>
  167. </Grid>
  168. </Page>
  169. </template>