| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <template>
- <ele-card :body-style="{ paddingTop: '8px', ...bodyStyle }" :flex-table="flexTable">
- <!-- 表格 -->
- <ele-pro-table
- ref="tableRef"
- :row-key="pageConfig.rowKey || 'id'"
- :columns="columns"
- :datasource="datasource"
- :show-overflow-tooltip="true"
- v-model:selections="selections"
- highlight-current-row
- :export-config="{ fileName: pageConfig.fileName }"
- :cache-key="pageConfig.cacheKey"
- border
- v-bind="$attrs"
- >
- <template v-for="(val, key) in slotArray" v-slot:[key]="{ row }">
- <slot :name="key" :row="row"></slot>
- </template>
- </ele-pro-table>
- </ele-card>
- </template>
- <script setup>
- import { ref, getCurrentInstance, useSlots } from 'vue';
- import { ElMessageBox } from 'element-plus/es';
- import { EleMessage } from 'ele-admin-plus/es';
- import { useDictData } from '@/utils/use-dict-data';
- import { download, toFormData, checkDownloadRes } from '@/utils/common';
- const slotArray = useSlots();
- let props = defineProps({
- pageConfig: {
- type: Object,
- default: () => ({
- cacheKey: 'recycleOrderTable',
- fileName: '回收订单查询',
- rowKey: 'id'
- })
- },
- pageUrl: { type: String, default: '/system/post/list' },
- exportUrl: { type: String, default: '/system/post/export' },
- columns: { type: Array, default: () => [] },
- bodyStyle: { type: Object, default: () => ({}) },
- flexTable: { type: Boolean, default: true }
- });
- let { proxy } = getCurrentInstance();
- /** 表格实例 */
- const tableRef = ref(null);
- /** 表格选中数据 */
- const selections = ref([]);
- async function queryPage(params) {
- let url = props.pageConfig.pageUrl || props.pageUrl;
- const res = await proxy.$http.get(url, { params });
- if (res.data.code === 200) {
- return res.data;
- }
- return Promise.reject(new Error(res.data.msg));
- }
- /** 表格数据源 */
- const datasource = ({ pages, where, orders }) => {
- let initKeys = props.pageConfig.params || {};
- return queryPage({ ...initKeys, ...where, ...orders, ...pages });
- };
- /** 搜索 */
- const reload = (where) => {
- console.log('where', where);
- tableRef.value?.reload?.({ where });
- };
- /** 批量操作 */
- const operatBatch = ({ method, row, url, title, data = {} }) => {
- const rows = row == null ? selections.value : [row];
- if (!rows.length) {
- EleMessage.error('请至少选择一条数据');
- return;
- }
- title = title || '是否确认当前操作?';
- ElMessageBox.confirm(title, '提示', {
- type: 'warning',
- draggable: true
- })
- .then(() => {
- const loading = EleMessage.loading({
- message: '请求中..',
- plain: true
- });
- proxy.$http[method](url, data)
- .then((res) => {
- if (res.data.code === 200) {
- EleMessage.success('操作成功');
- reload();
- } else {
- EleMessage.error(res.data.msg);
- }
- loading.close();
- })
- .catch((e) => {
- loading.close();
- EleMessage.error(e.message);
- });
- })
- .catch(() => {});
- };
- /// 导出数据
- async function exportPage(params, name) {
- const res = await proxy.$http({
- url: props.pageConfig.exportUrl,
- method: 'POST',
- data: toFormData(params),
- responseType: 'blob'
- });
- await checkDownloadRes(res);
- download(
- res.data,
- name ? `${name}_${Date.now()}.xlsx` : `post_${Date.now()}.xlsx`
- );
- }
- //导出数据 进导出记录
- function exportRecord(params, name) {
- return proxy.$http({
- url: props.pageConfig.exportUrl,
- method: 'POST',
- data: params
- }); // 导出记录
- }
- /** 导出数据 */
- const exportData = (name) => {
- const loading = EleMessage.loading({
- message: '请求中..',
- plain: true
- });
- tableRef.value?.fetch?.(({ where, orders }) => {
- exportRecord({ ...where, ...orders }, name)
- .then((res) => {
- if (res.data.code === 200) {
- EleMessage.success('操作成功,请前往导出记录下载');
- }else{
- EleMessage.error(res.data.msg);
- }
- loading.close();
- })
- .catch((e) => {
- loading.close();
- EleMessage.error(e.message);
- });
- });
- };
- function messageBoxConfirm({ message, fetch }) {
- ElMessageBox.confirm(message, '提示', {
- confirmButtonText: '确定',
- cancelButtonText: '关闭',
- type: 'warning'
- }).then(() => {
- fetch().then((res) => {
- if (res.data.code === 200) {
- EleMessage.success('操作成功');
- reload();
- } else {
- EleMessage.error(res.data.msg);
- }
- });
- });
- }
- function getSelections() {
- console.log(selections.value, 'selections.value');
- return selections.value;
- }
- defineExpose({
- reload,
- exportData,
- operatBatch,
- getSelections,
- messageBoxConfirm
- });
- </script>
|