2023-12-14 15:12:22 +07:00
|
|
|
import axios from "axios";
|
2024-11-18 10:22:56 +07:00
|
|
|
import config from "@/app.config";
|
|
|
|
|
|
2023-12-14 15:12:22 +07:00
|
|
|
import { useCounterMixin } from "@/stores/mixin";
|
2026-05-11 09:24:23 +07:00
|
|
|
import { downloadBlobFile } from "@/modules/10_registry/utils/downloadFile";
|
2023-12-14 15:12:22 +07:00
|
|
|
|
|
|
|
|
const mixin = useCounterMixin();
|
2025-06-06 10:38:41 +07:00
|
|
|
const { showLoader, hideLoader } = mixin;
|
2023-12-14 15:12:22 +07:00
|
|
|
|
2024-11-18 10:22:56 +07:00
|
|
|
async function genReport(data: any, fileName: string, type: string = "docx") {
|
2023-12-14 15:12:22 +07:00
|
|
|
showLoader();
|
|
|
|
|
await axios
|
2024-11-18 10:22:56 +07:00
|
|
|
.post(config.API.reportTemplate + `/docx`, data, {
|
2026-02-09 16:34:20 +07:00
|
|
|
headers: {
|
|
|
|
|
"content-Type": "application/json",
|
|
|
|
|
accept:
|
|
|
|
|
type === "docx"
|
|
|
|
|
? "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
|
|
|
|
: "application/pdf",
|
|
|
|
|
},
|
2023-12-14 15:12:22 +07:00
|
|
|
responseType: "arraybuffer",
|
|
|
|
|
})
|
2026-05-11 09:24:23 +07:00
|
|
|
.then(async (res) => {
|
2026-02-09 16:48:05 +07:00
|
|
|
const responseData = res.data;
|
|
|
|
|
if (responseData) {
|
|
|
|
|
const mimeType =
|
|
|
|
|
type === "docx"
|
|
|
|
|
? "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
|
|
|
|
: "application/pdf";
|
2023-12-14 15:12:22 +07:00
|
|
|
|
2026-02-09 16:48:05 +07:00
|
|
|
const blob = new Blob([responseData], { type: mimeType });
|
|
|
|
|
const url = URL.createObjectURL(blob);
|
2026-02-09 16:34:20 +07:00
|
|
|
|
2026-02-09 17:18:54 +07:00
|
|
|
const baseName = fileName.trim();
|
2026-05-11 09:24:23 +07:00
|
|
|
// const extension = type === "docx" ? "docx" : "pdf";
|
|
|
|
|
await downloadBlobFile({
|
|
|
|
|
downloadUrl: url,
|
|
|
|
|
fileName: baseName,
|
|
|
|
|
});
|
2023-12-14 15:12:22 +07:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
2025-06-06 10:38:41 +07:00
|
|
|
console.log(err);
|
2023-12-14 15:12:22 +07:00
|
|
|
})
|
|
|
|
|
.finally(() => {
|
|
|
|
|
hideLoader();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default genReport;
|