CommonTable.vue 5.0 KB

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