zhanbo.xu

优化导出并修改代码

......@@ -64,3 +64,29 @@ export function getCustomizeExcel(data) {
data: data,
});
}
// 查询导出的任务ID
// POST
// /exportList/submitExportTask
export function getSubmitExportTask(data) {
return request({
url: "/bus-customer/exportList/submitExportTask",
method: "post",
data: data,
});
}
// /asyncExport/taskStatus/{taskId}
export function getTaskStatus(data) {
return request({
url: "/bus-customer/asyncExport/taskStatus/" + data.taskId,
method: "get",
});
}
// /asyncExport/download/{taskId}
export function getDownload(data) {
return request({
url: "/bus-customer/asyncExport/download/" + data.taskId,
method: "get",
});
}
......
This diff is collapsed. Click to expand it.
......@@ -20,6 +20,8 @@ const apiUrl = {
declareDataModel: "/bus-customer/declareData/downloadTemplate",
// 清单查询下载
listExport: "/bus-customer/exportList/listExport",
// 根据id导出
customDownloadXls: "/bus-customer/asyncExport/download",
};
const baseUrl = process.env.VUE_APP_BASE_API;
......@@ -31,6 +33,8 @@ export function downLoadZip(urlStatus, str, filename, type, method, data) {
url = baseUrl + apiUrl[str];
} else if (urlStatus == "res") {
url = str;
} else if (urlStatus == "task") {
url = baseUrl + apiUrl[str] + "/" + data.taskId;
}
console.log(url);
......
......@@ -215,6 +215,7 @@
size="small"
icon="el-icon-download"
@click="downloadData"
:loading="isPolling || exportLoading"
>
导出
</el-button>
......@@ -282,7 +283,12 @@
</template>
<script>
import { getAllexportList, getListReceiptData } from "@/api/exportList-api.js";
import {
getAllexportList,
getListReceiptData,
getSubmitExportTask,
getTaskStatus,
} from "@/api/exportList-api.js";
import CustomDialog from "./custom-dialog.vue";
import { formatDateNew } from "../../utils";
......@@ -415,6 +421,11 @@ export default {
formItemconsignmentHeadCode: false,
},
tableMaxheight: null,
exportLoading: false,
pollTimer: null, // 定时器实例
pollInterval: 5000, // 轮询间隔(5秒)
isPolling: false, // 轮询状态标识
currentTaskId: null,
};
},
created() {
......@@ -423,9 +434,13 @@ export default {
mounted() {
this.tableMaxheight = window.innerHeight - 360;
},
beforeDestroy() {
this.stopPolling(); // 组件销毁前清理定时器
},
watch: {
$route() {
this.popoverClose();
this.stopPolling(); // 组件销毁前清理定时器
},
},
methods: {
......@@ -496,6 +511,68 @@ export default {
summaryApplicationStatus: "",
};
},
// 查询成功后调用的其他接口
async callOtherApi() {
try {
const fileName = `${formatDateNew(new Date())}.xlsx`;
this.fileDownload
.downLoadZip("task", "customDownloadXls", fileName, "xlsx", "get", {
taskId: this.currentTaskId,
})
.finally(() => {
// this.cancelDownload();
this.exportLoading = false;
});
} catch (error) {
console.error("后续接口调用失败:", error);
}
},
// 轮询查询状态
async pollStatus() {
try {
const result = await getTaskStatus({ taskId: this.currentTaskId });
if (+result.code === 200) {
// PENDING, PROCESSING, COMPLETED, FAILED
if (result.data.status === "COMPLETED") {
this.stopPolling(); // 停止轮询
await this.callOtherApi(); // 调用其他接口
} else if (result.data.status === "FAILED") {
this.stopPolling();
this.handleFailure();
this.alertWarningMsg(result.data.errorMessage);
} else {
this.startPolling(); // 启动轮询
}
} else {
this.stopPolling();
this.handleFailure();
}
// 其他状态继续轮询
} catch (error) {
console.error("轮询查询失败:", error);
this.stopPolling();
}
},
// 启动轮询
startPolling() {
if (this.isPolling) return;
this.isPolling = true;
this.pollTimer = setInterval(() => {
this.pollStatus();
}, this.pollInterval);
},
// 停止轮询
stopPolling() {
if (this.pollTimer) {
clearInterval(this.pollTimer);
this.pollTimer = null;
}
this.isPolling = false;
this.exportLoading = false;
},
//导出
downloadData() {
let obj = { ...this.sreachFormData };
......@@ -527,13 +604,32 @@ export default {
// // 2. 清单查询
// console.log(res);
// });
const fileName = `${formatDateNew(new Date())}.xlsx`;
this.fileDownload
.downLoadZip("api", "listExport", fileName, "xlsx", "post", {
...obj,
this.exportLoading = true;
this.$confirm(`正在导出中,请稍候!`, "提示", {
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning",
})
.then(() => {
getSubmitExportTask(obj).then((res) => {
if (+res.code === 200) {
console.log(res);
this.currentTaskId = res.data;
this.startPolling();
}
});
// this.fileDownload
// .downLoadZip("api", "listExport", fileName, "xlsx", "post", {
// ...obj,
// })
// .finally(() => {
// // this.cancelDownload();
// this.exportLoading = false;
// });
})
.finally(() => {
this.cancelDownload();
.catch(() => {
this.exportLoading = false;
});
},
//查询详情
......