CommonTable.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. import { method } from 'lodash-es';
  33. const slotArray = useSlots();
  34. let props = defineProps({
  35. pageConfig: {
  36. type: Object,
  37. default: () => ({
  38. cacheKey: 'recycleOrderTable',
  39. fileName: '回收订单查询',
  40. rowKey: 'id'
  41. })
  42. },
  43. pageUrl: { type: String, default: '/system/post/list' },
  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. console.log(initKeys, 'initKeys');
  65. return queryPage({ ...initKeys, ...where, ...orders, ...pages });
  66. };
  67. /** 搜索 */
  68. const reload = (where) => {
  69. tableRef.value?.reload?.({ where, page: 1 });
  70. };
  71. /** 批量操作 */
  72. const operatBatch = ({ method, row, url, title, data = {} }) => {
  73. const rows = row == null ? selections.value : [row];
  74. if (!rows.length) {
  75. EleMessage.error('请至少选择一条数据');
  76. return;
  77. }
  78. title = title || '是否确认当前操作?';
  79. ElMessageBox.confirm(title, '提示', {
  80. type: 'warning',
  81. draggable: true
  82. })
  83. .then(() => {
  84. const loading = EleMessage.loading({
  85. message: '请求中..',
  86. plain: true
  87. });
  88. proxy.$http[method](url, data)
  89. .then((res) => {
  90. if (res.data.code === 200) {
  91. EleMessage.success('操作成功');
  92. reload();
  93. } else {
  94. EleMessage.error(res.data.msg);
  95. }
  96. loading.close();
  97. })
  98. .catch((e) => {
  99. loading.close();
  100. EleMessage.error(e.message);
  101. });
  102. })
  103. .catch(() => {});
  104. };
  105. /// 导出数据
  106. async function exportPage(params, name, method = 'POST') {
  107. const res = await proxy.$http({
  108. url: props.pageConfig.exportUrl,
  109. method,
  110. data: toFormData(params),
  111. responseType: 'blob'
  112. });
  113. await checkDownloadRes(res);
  114. download(
  115. res.data,
  116. name ? `${name}_${Date.now()}.xlsx` : `post_${Date.now()}.xlsx`
  117. );
  118. }
  119. //导出数据 进导出记录
  120. function exportRecord(params, method = 'POST') {
  121. return proxy.$http({
  122. url: props.pageConfig.exportUrl,
  123. method,
  124. data: params
  125. }); // 导出记录
  126. }
  127. /** 导出数据 */
  128. const exportData = (name, method = 'POST') => {
  129. const loading = EleMessage.loading({
  130. message: '请求中..',
  131. plain: true
  132. });
  133. tableRef.value?.fetch?.(({ where, orders }) => {
  134. let initKeys = props.pageConfig.params || {};
  135. exportRecord({ ...where, ...orders,...initKeys }, method)
  136. .then((res) => {
  137. if (res.data.code === 200) {
  138. EleMessage.success('操作成功,请前往导出记录下载');
  139. } else {
  140. EleMessage.error(res.data.msg);
  141. }
  142. loading.close();
  143. })
  144. .catch((e) => {
  145. loading.close();
  146. EleMessage.error(e.message);
  147. });
  148. });
  149. };
  150. function messageBoxConfirm({ message, fetch }) {
  151. ElMessageBox.confirm(message, '提示', {
  152. confirmButtonText: '确定',
  153. cancelButtonText: '关闭',
  154. type: 'warning'
  155. }).then(() => {
  156. fetch().then((res) => {
  157. if (res.data.code === 200) {
  158. EleMessage.success('操作成功');
  159. reload();
  160. } else {
  161. EleMessage.error(res.data.msg);
  162. }
  163. });
  164. });
  165. }
  166. function getSelections() {
  167. console.log(selections.value, 'selections.value');
  168. return selections.value;
  169. }
  170. defineExpose({
  171. reload,
  172. exportData,
  173. operatBatch,
  174. getSelections,
  175. messageBoxConfirm
  176. });
  177. </script>