From 6399e23d2825e5400fb1a2aefe39c870e2967fe4 Mon Sep 17 00:00:00 2001 From: "DESKTOP-1R2VSQH\\Lenovo ThinkPad E490" Date: Thu, 27 Jun 2024 10:25:38 +0700 Subject: [PATCH] =?UTF-8?q?=E0=B8=AD=E0=B8=81=E0=B8=81=E0=B8=84=E0=B8=B3?= =?UTF-8?q?=E0=B8=AA=E0=B8=B1=E0=B9=88=E0=B8=87=20=3D>=20report?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../10_order/components/step/step04.vue | 166 ++++++------------ 1 file changed, 49 insertions(+), 117 deletions(-) diff --git a/src/modules/10_order/components/step/step04.vue b/src/modules/10_order/components/step/step04.vue index b2ab5709f..9b382c6da 100644 --- a/src/modules/10_order/components/step/step04.vue +++ b/src/modules/10_order/components/step/step04.vue @@ -39,13 +39,11 @@ const fileTailer = ref(null); const OrderPDFUpload = ref(""); const TailerPDFUpload = ref(""); -const orderCoverPdf = ref(); const orderAttachmentPdf = ref(""); const statusOrder = ref(); const orderName = ref(""); const orderStatusName = ref(""); -const dataGen = ref(); const orderId = ref(orderId_params.toString()); onMounted(async () => { @@ -108,132 +106,81 @@ const getCommandDetail = async () => { orderStatusName.value = data.orderStatusName; fetchReportCover("pdf", orderId.value); - - if (attachmentStatus.value) { - fetchReportAttachment("pdf", orderId.value); - } }) .catch((e) => { messageError($q, e); + hideLoader(); }); }; + // โหลดคำสั่ง const downloadCover = async (type: string) => { - showLoader(); - - if (!genReportStatus.value) { - await http - .get(config.API.fileCover(code.value, type, orderId.value), { - responseType: "blob", - }) - .then(async (res) => { - downloadFile(res, `คำสั่ง ${orderName.value}.${type}`); - }) - .catch(async (e) => { - messageError($q, JSON.parse(await e.response.data.text())); - }) - .finally(() => { - hideLoader(); - }); - } else if (type === "docx") { - genReport(dataGen.value, `คำสั่ง ${orderName.value}`, "docx"); - } else { - const link = document.createElement("a"); - var fileName = `คำสั่ง ${orderName.value}.${ - type === "docx" ? "docx" : "pdf" - }`; - link.href = orderCoverPdf.value; - link.setAttribute("download", fileName); - document.body.appendChild(link); - link.click(); - document.body.removeChild(link); - hideLoader(); - } + genReport(dataCover.value, `คำสั่ง ${orderName.value}`, type); }; // โหลด เอกสารแนบท้าย const downloadAttachment = async (type: string) => { + genReport(dataAttachment.value, `คำสั่ง ${orderName.value}`, type); +}; + +const dataCover = ref(); +// เรียกไฟล์ คำสั่ง +const fetchReportCover = async (type: string, orderId: string) => { showLoader(); await http - .get(config.API.fileAttachment(code.value, type, orderId.value), { - responseType: "blob", - }) - .then(async (res) => { - downloadFile(res, `เอกสารแนบท้าย ${orderName.value}.${type}`); + .get(config.API.reportOrderCover(type, orderId, code.value)) + .then(async (res: any) => { + genPDf(res.data.result); + dataCover.value = res.data.result; }) .catch(async (e) => { - messageError($q, JSON.parse(await e.response.data.text())); - }) - .finally(() => { + messageError($q, e); hideLoader(); }); }; -// เรียกไฟล์ คำสั่ง -const fetchReportCover = async (type: string, orderId: string) => { - await http - .get( - config.API.reportOrderCover(type, orderId, code.value), - !genReportStatus.value - ? { - responseType: "blob", - } - : {} - ) - .then(async (res: any) => { - if (!genReportStatus.value) { - const blob = new Blob([res.data]); - const objectUrl = URL.createObjectURL(blob); - orderCoverPdf.value = objectUrl; - if (tab.value == "main") { - viewPDF(orderCoverPdf.value); - } - } else { - // report template - dataGen.value = res.data.result; - await axios - .post(`${config.API.reportTemplate}/docx`, res.data.result, { - headers: { - accept: "application/pdf", - "content-Type": "application/json", - }, - responseType: "blob", - }) - .then((resReport) => { - const data = resReport.data; - if (data) { - const blob = new Blob([data]); - const objectUrl = URL.createObjectURL(blob); - orderCoverPdf.value = objectUrl; - viewPDF(orderCoverPdf.value); - } - }) - .catch(async (e) => { - messageError($q, JSON.parse(await e.response.data.text())); - }) - .finally(() => {}); - } - }) - .catch(async (e) => { - !genReportStatus.value - ? messageError($q, JSON.parse(await e.response.data.text())) - : messageError($q, e); - }); -}; + +const dataAttachment = ref(); // เรียกไฟล์ เอกสารแนบท้าย const fetchReportAttachment = async (type: string, orderId: string) => { + showLoader(); await http - .get(config.API.reportOrderAttachment(type, orderId, code.value), { + .get(config.API.reportOrderAttachment(type, orderId, code.value)) + .then(async (res) => { + genPDf(res.data.result); + dataAttachment.value = res.data.result; + }) + .catch(async (e) => { + messageError($q, e); + hideLoader(); + }); +}; + +function genPDf(data: any) { + axios + .post(config.API.reportTemplate + `/docx`, data, { + headers: { + accept: "application/pdf", + "content-Type": "application/json", + }, responseType: "blob", }) .then(async (res) => { const blob = new Blob([res.data]); const objectUrl = URL.createObjectURL(blob); - orderAttachmentPdf.value = objectUrl; + + const pdfData = await usePDF(`${objectUrl}`); + showLoader(); + setTimeout(() => { + pdfSrc.value = pdfData.pdf.value; + numOfPages.value = pdfData.pages.value; + hideLoader(); + }, 1500); }) .catch(async (e) => { - messageError($q, JSON.parse(await e.response.data.text())); + messageError($q, e); + hideLoader(); }); -}; +} // Fetch file upload & detail const fetchAttachment = async (orderId: string) => { @@ -241,6 +188,7 @@ const fetchAttachment = async (orderId: string) => { .get(config.API.attachmentOrder(orderId)) .then(async (res) => { let response = res.data.result; + order.value = response.orderNo; years.value = Number(response.orderYear); if (response.signDate !== undefined && response.signDate != "") { @@ -292,10 +240,10 @@ const viewPDFUpload = async (pdf: string) => { watch(tab, () => { if (tab.value === "main") { - viewPDF(orderCoverPdf.value); + fetchReportCover("pdf", orderId.value); } if (tab.value === "second") { - viewPDF(orderAttachmentPdf.value); + fetchReportAttachment("pdf", orderId.value); } }); @@ -327,16 +275,6 @@ const onchangePage = () => { vuePDFRef.value.reload(); } }; -// ดาวน์โหลดไฟล์ -const downloadFile = (response: any, filename: string) => { - const link = document.createElement("a"); - var fileName = filename; - link.href = window.URL.createObjectURL(new Blob([response.data])); - link.setAttribute("download", fileName); - document.body.appendChild(link); - link.click(); - document.body.removeChild(link); -}; // การอัพโหลดไฟล์คำสั่งและเอกสารแนบท้าย const saveUpload = () => { @@ -526,9 +464,6 @@ const viewFileUpload = async (url: string) => { v-close-popup @click="downloadCover('pdf')" > - @@ -539,9 +474,6 @@ const viewFileUpload = async (url: string) => { v-close-popup @click="downloadCover('docx')" > -