ปรับการอัปโหลรูป Profile

This commit is contained in:
DESKTOP-1R2VSQH\Lenovo ThinkPad E490 2024-10-31 17:13:48 +07:00
parent 728f45e489
commit d4696409b2

View file

@ -105,8 +105,7 @@ const itemsMenu = computed(() => {
if ( if (
leaveReason.value === leaveReason.value ===
"(พ้นจากราชการด้วยสาเหตุ: ได้รับโทษทางวินัย ให้ออกจากราชการไว้ก่อน)" || "(พ้นจากราชการด้วยสาเหตุ: ได้รับโทษทางวินัย ให้ออกจากราชการไว้ก่อน)" ||
leaveReason.value === leaveReason.value === "(พ้นจากราชการด้วยสาเหตุ: ลาออกจากราชการ)"
"(พ้นจากราชการด้วยสาเหตุ: ลาออกจากราชการ)"
) { ) {
return ( return (
baseItemsMenu.value?.filter( baseItemsMenu.value?.filter(
@ -140,7 +139,7 @@ const activeImage = ref<any | null>(null);
const images = ref<any[]>([]); const images = ref<any[]>([]);
const imagesAlldata = ref<any[]>([]); const imagesAlldata = ref<any[]>([]);
input.type = "file"; input.type = "file";
input.accept = ".jpg,.png,.tif,.pic"; input.accept = ".jpg,.png,.tif,.pic,.jfif";
input.addEventListener("change", (e) => { input.addEventListener("change", (e) => {
profileFile.value = (e.currentTarget as HTMLInputElement).files?.[0]; profileFile.value = (e.currentTarget as HTMLInputElement).files?.[0];
@ -156,31 +155,96 @@ function imageActive(n: any) {
activeImage.value = n; activeImage.value = n;
} }
//
function resizeImage(file: File): Promise<File> {
return new Promise((resolve, reject) => {
const img = new Image();
const reader = new FileReader();
reader.onload = (e) => {
img.src = e.target!.result as string; // string
img.onload = () => {
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
const width = 150;
const height = 200;
//
canvas.width = width;
canvas.height = height;
if (ctx) {
//
ctx.fillStyle = "white";
ctx.fillRect(0, 0, width, height); // canvas
// ctx null
const imgAspectRatio = img.width / img.height;
const targetAspectRatio = width / height;
let drawWidth: number, drawHeight: number;
if (imgAspectRatio > targetAspectRatio) {
drawWidth = width;
drawHeight = width / imgAspectRatio;
} else {
drawHeight = height;
drawWidth = height * imgAspectRatio;
}
const xOffset = (width - drawWidth) / 2;
const yOffset = (height - drawHeight) / 2;
ctx.drawImage(img, xOffset, yOffset, drawWidth, drawHeight);
// canvas Blob
canvas.toBlob((blob) => {
if (blob) {
resolve(new File([blob], file.name, { type: file.type })); // File Blob
} else {
reject(new Error("Failed to convert canvas to blob."));
}
}, "image/jpeg");
} else {
reject(new Error("Failed to get canvas context."));
}
};
img.onerror = (err) => reject(err);
};
reader.readAsDataURL(file);
});
}
const newProfileFile = ref<any>(null);
/** /**
* งกนอปโหลด * งกนอปโหลด
*/ */
function uploadImg() { async function uploadImg() {
showLoader();
newProfileFile.value = await resizeImage(profileFile.value);
closeImage();
http http
.post(config.API.orgProfileAvatarbyType(empType.value), { .post(config.API.orgProfileAvatarbyType(empType.value), {
profileId: empType.value == "" ? profileId.value : undefined, profileId: empType.value == "" ? profileId.value : undefined,
profileEmployeeId: empType.value !== "" ? profileId.value : undefined, profileEmployeeId: empType.value !== "" ? profileId.value : undefined,
}) })
.then((res) => { .then(async (res) => {
fileName.value = res.data.result.avatarName; fileName.value = res.data.result.avatarName;
uploadProfile(res.data.result.avatar); await uploadProfile(res.data.result.avatar);
}) })
.catch((e) => { .catch((e) => {
messageError($q, e); messageError($q, e);
}) hideLoader();
.finally(() => {}); });
} }
/** /**
* งกนสราง path ปโหลไฟล * งกนสราง path ปโหลไฟล
* @param path * @param path
*/ */
function uploadProfile(path: string) { async function uploadProfile(path: string) {
http await http
.post(config.API.fileByPath(path), { .post(config.API.fileByPath(path), {
replace: true, replace: true,
fileList: [ fileList: [
@ -189,15 +253,12 @@ function uploadProfile(path: string) {
}, },
], ],
}) })
.then((res) => { .then(async (res) => {
uploadUrl.value = res.data[fileName.value].uploadUrl; uploadUrl.value = res.data[fileName.value].uploadUrl;
uploadFileURL(uploadUrl.value, profileFile.value); await uploadFileURL(uploadUrl.value, newProfileFile.value);
closeImage();
}) })
.catch((err) => { .catch((err) => {
messageError($q, err); messageError($q, err);
})
.finally(() => {
hideLoader(); hideLoader();
}); });
} }
@ -207,7 +268,7 @@ function uploadProfile(path: string) {
* @param uploadUrl path ปโหลไฟล * @param uploadUrl path ปโหลไฟล
* @param file ไฟล * @param file ไฟล
*/ */
function uploadFileURL(uploadUrl: string, file: any) { async function uploadFileURL(uploadUrl: string, file: any) {
showLoader(); showLoader();
axios axios
.put(uploadUrl, file, { .put(uploadUrl, file, {
@ -215,8 +276,8 @@ function uploadFileURL(uploadUrl: string, file: any) {
"Content-Type": file.type, "Content-Type": file.type,
}, },
}) })
.then(() => { .then(async () => {
fetchProfile(profileId.value); await fetchProfile(profileId.value);
success($q, "อัปโหลดไฟล์สำเร็จ"); success($q, "อัปโหลดไฟล์สำเร็จ");
}) })
.catch((err) => { .catch((err) => {
@ -231,8 +292,8 @@ function uploadFileURL(uploadUrl: string, file: any) {
* งกนดงขอมลรปโปรไฟล * งกนดงขอมลรปโปรไฟล
* @param id โปรไฟล * @param id โปรไฟล
*/ */
function fetchProfile(id: string) { async function fetchProfile(id: string) {
http await http
.get(config.API.fileByFile("ทะเบียนประวัติ", "โปรไฟล์", id, fileName.value)) .get(config.API.fileByFile("ทะเบียนประวัติ", "โปรไฟล์", id, fileName.value))
.then(async (res) => { .then(async (res) => {
profilePicture.value = res.data.downloadUrl; profilePicture.value = res.data.downloadUrl;
@ -1028,7 +1089,7 @@ onMounted(async () => {
<!-- Dialog เลอก Image --> <!-- Dialog เลอก Image -->
<q-dialog v-model="dialogImage" persistent> <q-dialog v-model="dialogImage" persistent>
<q-card style="width: 100vw; max-width: 60vw"> <q-card style="width: 100vw; max-width: 60vw">
<DialogHeader :tittle="'เลือกรูปภาพ'" :close="closeImage" /> <DialogHeader :tittle="'เลือกรูปภาพ (150 x 200 px)'" :close="closeImage" />
<q-separator /> <q-separator />
<q-card-section class="col-12 row"> <q-card-section class="col-12 row">