CommonTable.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <template>
  2. <ele-card :body-style="{ paddingTop: '8px' }" flex-table>
  3. <!-- 表格 -->
  4. <ele-pro-table
  5. ref="tableRef"
  6. :row-key="pageConfig.rowKey || 'id'"
  7. :columns="columns"
  8. :datasource="datasource"
  9. :show-overflow-tooltip="true"
  10. v-model:selections="selections"
  11. highlight-current-row
  12. :export-config="{ fileName: pageConfig.fileName }"
  13. :cache-key="pageConfig.cacheKey"
  14. border
  15. v-bind="$attrs"
  16. >
  17. <template v-for="(val, key) in slotArray" v-slot:[key]="{ row }">
  18. <slot :name="key" :row="row"></slot>
  19. </template>
  20. </ele-pro-table>
  21. </ele-card>
  22. </template>
  23. <script setup>
  24. import { ref, getCurrentInstance, useSlots } from 'vue';
  25. import { ElMessageBox } from 'element-plus/es';
  26. import { EleMessage } from 'ele-admin-plus/es';
  27. import { useDictData } from '@/utils/use-dict-data';
  28. import { download, toFormData, checkDownloadRes } from '@/utils/common';
  29. const slotArray = useSlots();
  30. let props = defineProps({
  31. pageConfig: {
  32. type: Object,
  33. default: () => ({
  34. cacheKey: 'recycleOrderTable',
  35. fileName: '回收订单查询',
  36. rowKey: 'id'
  37. })
  38. },
  39. pageUrl: { type: String, default: '/system/post/list' },
  40. exportUrl: { type: String, default: '/system/post/export' },
  41. columns: { type: Array, default: () => [] }
  42. });
  43. let { proxy } = getCurrentInstance();
  44. /** 表格实例 */
  45. const tableRef = ref(null);
  46. /** 表格选中数据 */
  47. const selections = ref([]);
  48. async function queryPage(params) {
  49. let url = props.pageConfig.pageUrl || props.pageUrl;
  50. const res = await proxy.$http.get(url, { params });
  51. if (res.data.code === 200) {
  52. return res.data;
  53. }
  54. return Promise.reject(new Error(res.data.msg));
  55. }
  56. /** 表格数据源 */
  57. const datasource = ({ pages, where, orders }) => {
  58. let initKeys = props.pageConfig.params || {};
  59. return queryPage({ ...initKeys, ...where, ...orders, ...pages });
  60. };
  61. /** 搜索 */
  62. const reload = (where) => {
  63. tableRef.value?.reload?.({ where });
  64. };
  65. /** 批量操作 */
  66. const operatBatch = ({ method, row, url, title }) => {
  67. const rows = row == null ? selections.value : [row];
  68. if (!rows.length) {
  69. EleMessage.error('请至少选择一条数据');
  70. return;
  71. }
  72. title = title || '是否确认当前操作?';
  73. ElMessageBox.confirm(title, '提示', {
  74. type: 'warning',
  75. draggable: true
  76. })
  77. .then(() => {
  78. const loading = EleMessage.loading({
  79. message: '请求中..',
  80. plain: true
  81. });
  82. proxy.$http[method](url)
  83. .then((res) => {
  84. if (res.data.code === 200) {
  85. EleMessage.success('操作成功');
  86. reload();
  87. } else {
  88. EleMessage.error(res.data.msg);
  89. }
  90. loading.close();
  91. })
  92. .catch((e) => {
  93. loading.close();
  94. EleMessage.error(e.message);
  95. });
  96. })
  97. .catch(() => {});
  98. };
  99. /// 导出数据
  100. async function exportPage(params, name) {
  101. const res = await proxy.$http({
  102. url: props.pageConfig.exportUrl,
  103. method: 'POST',
  104. data: toFormData(params),
  105. responseType: 'blob'
  106. });
  107. await checkDownloadRes(res);
  108. download(
  109. res.data,
  110. name ? `${name}_${Date.now()}.xlsx` : `post_${Date.now()}.xlsx`
  111. );
  112. }
  113. /** 导出数据 */
  114. const exportData = (name) => {
  115. const loading = EleMessage.loading({
  116. message: '请求中..',
  117. plain: true
  118. });
  119. tableRef.value?.fetch?.(({ where, orders }) => {
  120. exportPage({ ...where, ...orders }, name)
  121. .then(() => {
  122. loading.close();
  123. })
  124. .catch((e) => {
  125. loading.close();
  126. EleMessage.error(e.message);
  127. });
  128. });
  129. };
  130. function messageBoxConfirm({ message, fetch }) {
  131. ElMessageBox.confirm(message, '提示', {
  132. confirmButtonText: '确定',
  133. cancelButtonText: '关闭',
  134. type: 'warning'
  135. }).then(() => {
  136. fetch().then((res) => {
  137. if (res.data.code === 200) {
  138. EleMessage.success('操作成功');
  139. reload();
  140. } else {
  141. EleMessage.error(res.data.msg);
  142. }
  143. });
  144. });
  145. }
  146. function getSelections() {
  147. return selections.value;
  148. }
  149. defineExpose({
  150. reload,
  151. exportData,
  152. operatBatch,
  153. getSelections,
  154. messageBoxConfirm
  155. });
  156. </script>