hrms-user/src/modules/10_registry/05_Other/03_FileOther.vue

156 lines
4.1 KiB
Vue
Raw Normal View History

2025-03-14 11:48:52 +07:00
<script setup lang="ts">
import { ref, onMounted, watch } from "vue";
2025-03-14 11:48:52 +07:00
import { useCounterMixin } from "@/stores/mixin";
import { useQuasar } from "quasar";
import http from "@/plugins/http";
import config from "@/app.config";
import { useRegistryInFormationStore } from "@/modules/10_registry/store/registry";
import type { FileFormType } from "@/modules/10_registry/interface/index/Main";
const $q = useQuasar();
2025-08-05 15:15:49 +07:00
const store = useRegistryInFormationStore();
const { showLoader, hideLoader, messageError } = useCounterMixin();
2025-03-14 11:48:52 +07:00
2025-08-05 15:15:49 +07:00
const isLoading = ref<boolean>(false);
const fileList = ref<FileFormType[]>([]);
2025-03-14 11:48:52 +07:00
2025-08-05 15:15:49 +07:00
/** ฟังก์ชันดึงข้อมูลไฟล์ */
async function getData() {
if (!store.citizenId) return;
2025-08-05 15:15:49 +07:00
isLoading.value = true;
await http
2025-03-14 11:48:52 +07:00
.get(
config.API.fileByFileUser(
"ระบบทะเบียนประวัติ",
"เอกสารหลักฐานเพิ่มเติม",
store.citizenId,
),
2025-03-14 11:48:52 +07:00
)
.then((res) => {
const data = res.data;
fileList.value = data;
})
.catch((e) => {
messageError($q, e);
})
.finally(() => {
2025-08-05 15:15:49 +07:00
isLoading.value = false;
2025-03-14 11:48:52 +07:00
});
}
/**
* ดาวนโหลดลงคไฟล
* @param fileName file name
*/
2025-08-05 15:15:49 +07:00
async function downloadFile(fileName: string) {
2025-03-14 11:48:52 +07:00
showLoader();
2025-08-05 15:15:49 +07:00
await http
2025-03-14 11:48:52 +07:00
.get(
config.API.fileByFile(
"ระบบทะเบียนประวัติ",
"เอกสารหลักฐานเพิ่มเติม",
store.citizenId,
fileName,
),
2025-03-14 11:48:52 +07:00
)
.then(async (res) => {
const downloadUrl = res.data.downloadUrl;
const response = await fetch(downloadUrl);
const blob = await response.blob();
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = fileName;
document.body.appendChild(link);
link.click();
setTimeout(() => {
document.body.removeChild(link);
URL.revokeObjectURL(url);
}, 100);
2025-03-14 11:48:52 +07:00
})
.catch((e) => {
messageError($q, e);
})
2025-08-05 15:15:49 +07:00
.finally(() => {
2025-03-14 11:48:52 +07:00
hideLoader();
});
}
onMounted(() => {
getData();
});
watch(
() => store.citizenId,
(newVal) => {
if (newVal) {
getData();
}
},
);
2025-03-14 11:48:52 +07:00
</script>
<template>
<div class="col-12">
<q-toolbar class="q-px-none q-mt-md">
<span class="text-blue-6 text-weight-bold text-body1">เอกสาร ..7 </span>
</q-toolbar>
<q-card bordered class="row col-12" style="border: 1px solid #d6dee1">
<div class="col-12 text-weight-medium bg-grey-1 q-py-sm q-px-md">
ไฟลเอกสาร ..7
</div>
<div class="col-12"><q-separator /></div>
2025-08-05 15:15:49 +07:00
<div v-if="isLoading" class="col-12 q-pa-sm">
<q-skeleton type="QSlider" />
</div>
<div v-else class="row col-12 q-col-gutter-y-sm q-pa-sm">
2025-03-14 11:48:52 +07:00
<div v-if="fileList.length > 0" class="col-xs-12 row">
<q-list class="full-width rounded-borders" bordered separator>
<q-item
clickable
v-ripple
v-for="data in fileList"
:key="data.id"
class="items-center"
>
<q-item-section>{{ data.fileName }}</q-item-section>
<q-space />
<div>
<q-btn
size="12px"
flat
round
dense
color="blue"
icon="mdi-download"
@click="downloadFile(data.fileName)"
><q-tooltip>ดาวนโหลดไฟล</q-tooltip></q-btn
>
</div>
</q-item>
</q-list>
</div>
<div class="col-12" v-else>
<q-card class="q-pa-md" bordered> ไมรายการเอกสาร </q-card>
</div>
</div>
</q-card>
</div>
</template>
<style scoped>
.absolute_button {
position: absolute;
right: 5px;
top: -20px;
}
.fix_top {
justify-content: start !important;
}
</style>