From 36e21c2e4ee172efe575e20b3a001a937cf1c15c Mon Sep 17 00:00:00 2001 From: "DESKTOP-1R2VSQH\\Lenovo ThinkPad E490" Date: Fri, 22 Nov 2024 11:07:30 +0700 Subject: [PATCH] fix file .txt --- .../03_logs/components/DialogDataDiff.vue | 2 +- src/modules/03_logs/components/LogTable.vue | 23 ++++------- src/plugins/generateTxt.ts | 39 ++++++++++++------- 3 files changed, 33 insertions(+), 31 deletions(-) diff --git a/src/modules/03_logs/components/DialogDataDiff.vue b/src/modules/03_logs/components/DialogDataDiff.vue index 4b580d43..4c1bbdeb 100644 --- a/src/modules/03_logs/components/DialogDataDiff.vue +++ b/src/modules/03_logs/components/DialogDataDiff.vue @@ -31,7 +31,7 @@ defineProps<{ (null); // เวลาเริ่มต้น const endTime = ref(null); //เวลาสินสุด @@ -463,21 +463,12 @@ function onSendCSV() { } async function downloadTxt() { - const queryString = { - id: currentlogData.value?.id, - }; - await http - .get(`${config.API.log}/report/logsDetail`, { - params: queryString, - }) - .then(async (res) => { - const data = res.data; - await generateTxt(data, `LOG_${date2Thai(new Date(startDate.value))}`); - }) - .catch((e) => { - messageError($q, e); - }) - .finally(() => {}); + if (currentlogData.value?.id) { + await generateTxt( + currentlogData.value?.id, + `LOG_${date2Thai(new Date(startDate.value))}` + ); + } } onBeforeMount(async () => { diff --git a/src/plugins/generateTxt.ts b/src/plugins/generateTxt.ts index 0ce9f93d..9ae01aa3 100644 --- a/src/plugins/generateTxt.ts +++ b/src/plugins/generateTxt.ts @@ -1,32 +1,32 @@ -import axios from "axios"; import config from "@/app.config"; import { useQuasar } from "quasar"; import { useCounterMixin } from "@/stores/mixin"; +import http from "@/plugins/http"; const $q = useQuasar(); const mixin = useCounterMixin(); const { showLoader, hideLoader, messageError } = mixin; -async function generateTxt(data: any, fileName: string) { +async function generateTxt(id: string, fileName: string) { showLoader(); - await axios - .post(`${config.API.reportTemplate}/docx`, data, { + await http + .post(`${config.API.log}/report/logsDetail?id=${id}`, "", { headers: { - accept: - "application/vnd.openxmlformats-officedocument.wordprocessingml.document", - "content-Type": "application/json", - // "Content-Type": "text/plain;charset=utf-8", + "Content-Type": "text/plain;charset=utf-8", }, - // responseType: "blob", - responseType: "arraybuffer", + responseType: "blob", }) - .then((res) => { - const data = res.data; + .then(async (res) => { + const data = await res.data.text(); if (data) { + const formattedData = formatData(data); + // สร้าง Blob จาก array buffer - const blob = new Blob([data], { type: "text/plain;charset=utf-8" }); + const blob = new Blob([formattedData], { + type: "text/plain;charset=utf-8", + }); // สร้าง URL สำหรับไฟล์ Blob const url = URL.createObjectURL(blob); @@ -34,7 +34,7 @@ async function generateTxt(data: any, fileName: string) { // สร้างลิงก์เพื่อดาวน์โหลดไฟล์ const link = document.createElement("a"); link.href = url; - link.download = `${fileName}.docx`; // กำหนดชื่อไฟล์ที่จะดาวน์โหลด + link.download = `${fileName}.txt`; // กำหนดชื่อไฟล์ที่จะดาวน์โหลด document.body.appendChild(link); link.click(); @@ -50,4 +50,15 @@ async function generateTxt(data: any, fileName: string) { }); } +function formatData(data: any) { + // ตรวจสอบว่าเป็น JSON และจัดรูปแบบ + try { + const jsonData = JSON.parse(data); // แปลงข้อมูลเป็น JSON + return JSON.stringify(jsonData, null, 2); // จัดให้สวยงาม (2 ช่องว่าง) + } catch { + // ถ้าไม่ใช่ JSON ให้คืนค่าข้อมูลดั้งเดิม + return data; + } +} + export default generateTxt;