CommonTable.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <template>
  2. <ele-card :body-style="{ paddingTop: '8px', ...bodyStyle }" :flex-table="flexTable">
  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. bodyStyle: { type: Object, default: () => ({}) },
  43. flexTable: { type: Boolean, default: true }
  44. });
  45. let { proxy } = getCurrentInstance();
  46. /** 表格实例 */
  47. const tableRef = ref(null);
  48. /** 表格选中数据 */
  49. const selections = ref([]);
  50. async function queryPage(params) {
  51. let url = props.pageConfig.pageUrl || props.pageUrl;
  52. const res = await proxy.$http.get(url, { params });
  53. if (res.data.code === 200) {
  54. return res.data;
  55. }
  56. return Promise.reject(new Error(res.data.msg));
  57. }
  58. /** 表格数据源 */
  59. const datasource = ({ pages, where, orders }) => {
  60. let initKeys = props.pageConfig.params || {};
  61. return queryPage({ ...initKeys, ...where, ...orders, ...pages });
  62. };
  63. /** 搜索 */
  64. const reload = (where) => {
  65. console.log('where', where);
  66. tableRef.value?.reload?.({ where });
  67. };
  68. /** 批量操作 */
  69. const operatBatch = ({ method, row, url, title, data = {} }) => {
  70. const rows = row == null ? selections.value : [row];
  71. if (!rows.length) {
  72. EleMessage.error('请至少选择一条数据');
  73. return;
  74. }
  75. title = title || '是否确认当前操作?';
  76. ElMessageBox.confirm(title, '提示', {
  77. type: 'warning',
  78. draggable: true
  79. })
  80. .then(() => {
  81. const loading = EleMessage.loading({
  82. message: '请求中..',
  83. plain: true
  84. });
  85. proxy.$http[method](url, data)
  86. .then((res) => {
  87. if (res.data.code === 200) {
  88. EleMessage.success('操作成功');
  89. reload();
  90. } else {
  91. EleMessage.error(res.data.msg);
  92. }
  93. loading.close();
  94. })
  95. .catch((e) => {
  96. loading.close();
  97. EleMessage.error(e.message);
  98. });
  99. })
  100. .catch(() => {});
  101. };
  102. /// 导出数据
  103. async function exportPage(params, name) {
  104. const res = await proxy.$http({
  105. url: props.pageConfig.exportUrl,
  106. method: 'POST',
  107. data: toFormData(params),
  108. responseType: 'blob'
  109. });
  110. await checkDownloadRes(res);
  111. download(
  112. res.data,
  113. name ? `${name}_${Date.now()}.xlsx` : `post_${Date.now()}.xlsx`
  114. );
  115. }
  116. //导出数据 进导出记录
  117. function exportRecord(params, name) {
  118. return proxy.$http({
  119. url: props.pageConfig.exportUrl,
  120. method: 'POST',
  121. data: params
  122. }); // 导出记录
  123. }
  124. /** 导出数据 */
  125. const exportData = (name) => {
  126. const loading = EleMessage.loading({
  127. message: '请求中..',
  128. plain: true
  129. });
  130. tableRef.value?.fetch?.(({ where, orders }) => {
  131. exportRecord({ ...where, ...orders }, name)
  132. .then((res) => {
  133. if (res.data.code === 200) {
  134. EleMessage.success('操作成功,请前往导出记录下载');
  135. }else{
  136. EleMessage.error(res.data.msg);
  137. }
  138. loading.close();
  139. })
  140. .catch((e) => {
  141. loading.close();
  142. EleMessage.error(e.message);
  143. });
  144. });
  145. };
  146. function messageBoxConfirm({ message, fetch }) {
  147. ElMessageBox.confirm(message, '提示', {
  148. confirmButtonText: '确定',
  149. cancelButtonText: '关闭',
  150. type: 'warning'
  151. }).then(() => {
  152. fetch().then((res) => {
  153. if (res.data.code === 200) {
  154. EleMessage.success('操作成功');
  155. reload();
  156. } else {
  157. EleMessage.error(res.data.msg);
  158. }
  159. });
  160. });
  161. }
  162. function getSelections() {
  163. console.log(selections.value, 'selections.value');
  164. return selections.value;
  165. }
  166. defineExpose({
  167. reload,
  168. exportData,
  169. operatBatch,
  170. getSelections,
  171. messageBoxConfirm
  172. });
  173. </script>