hrms-checkin/src/stores/mixin.ts

390 lines
11 KiB
TypeScript
Raw Normal View History

2023-11-14 17:47:43 +07:00
import { defineStore } from 'pinia'
import CustomComponent from '@/components/CustomDialog.vue'
2023-11-16 17:30:24 +07:00
import { Loading, QSpinnerCube } from 'quasar'
2024-08-28 13:43:21 +07:00
import { logout } from '@/plugins/auth'
2025-03-10 17:17:04 +07:00
import { format, utcToZonedTime } from 'date-fns-tz'
2023-11-07 11:17:13 +07:00
2023-11-14 17:47:43 +07:00
export const useCounterMixin = defineStore('mixin', () => {
2023-11-07 11:17:13 +07:00
function date2Thai(srcDate: Date, isFullMonth = false, isTime = false) {
if (srcDate == null) {
2023-11-14 17:47:43 +07:00
return null
;`
`
2023-11-07 11:17:13 +07:00
}
2023-11-14 17:47:43 +07:00
const date = new Date(srcDate)
const isValidDate = Boolean(+date)
if (!isValidDate) return srcDate.toString()
if (isValidDate && date.getFullYear() < 1000) return srcDate.toString()
2023-11-07 11:17:13 +07:00
const fullMonthThai = [
2023-11-14 17:47:43 +07:00
'มกราคม',
'กุมภาพันธ์',
'มีนาคม',
'เมษายน',
'พฤษภาคม',
'มิถุนายน',
'กรกฎาคม',
'สิงหาคม',
'กันยายน',
'ตุลาคม',
'พฤศจิกายน',
'ธันวาคม',
]
2023-11-07 11:17:13 +07:00
const abbrMonthThai = [
2023-11-14 17:47:43 +07:00
'ม.ค.',
'ก.พ.',
'มี.ค.',
'เม.ย.',
'พ.ค.',
'มิ.ย.',
'ก.ค.',
'ส.ค.',
'ก.ย.',
'ต.ค.',
'พ.ย.',
'ธ.ค.',
]
let dstYear = 0
2023-11-07 11:17:13 +07:00
if (date.getFullYear() > 2500) {
2023-11-14 17:47:43 +07:00
dstYear = date.getFullYear()
2023-11-07 11:17:13 +07:00
} else {
2023-11-14 17:47:43 +07:00
dstYear = date.getFullYear() + 543
2023-11-07 11:17:13 +07:00
}
2023-11-14 17:47:43 +07:00
let dstMonth = ''
2023-11-07 11:17:13 +07:00
if (isFullMonth) {
2023-11-14 17:47:43 +07:00
dstMonth = fullMonthThai[date.getMonth()]
2023-11-07 11:17:13 +07:00
} else {
2023-11-14 17:47:43 +07:00
dstMonth = abbrMonthThai[date.getMonth()]
2023-11-07 11:17:13 +07:00
}
2023-11-14 17:47:43 +07:00
let dstTime = ''
2023-11-07 11:17:13 +07:00
if (isTime) {
2023-11-14 17:47:43 +07:00
const H = date.getHours().toString().padStart(2, '0')
const M = date.getMinutes().toString().padStart(2, '0')
2023-11-07 11:17:13 +07:00
// const S = date.getSeconds().toString().padStart(2, "0")
// dstTime = " " + H + ":" + M + ":" + S + " น."
2023-11-14 17:47:43 +07:00
dstTime = ' ' + H + ':' + M + ' น.'
2023-11-07 11:17:13 +07:00
}
return (
2023-11-14 17:47:43 +07:00
date.getDate().toString().padStart(2, '0') +
' ' +
2023-11-07 11:17:13 +07:00
dstMonth +
2023-11-14 17:47:43 +07:00
' ' +
2023-11-07 11:17:13 +07:00
dstYear +
dstTime
2023-11-14 17:47:43 +07:00
)
2023-11-07 11:17:13 +07:00
}
2023-11-14 17:47:43 +07:00
const showLoader = () => {
Loading.show({
spinner: QSpinnerCube,
spinnerSize: 140,
2023-11-16 17:30:24 +07:00
spinnerColor: 'primary',
backgroundColor: 'white',
})
}
const hideLoader = () => {
2023-11-16 17:30:24 +07:00
Loading.hide()
}
2023-11-07 16:35:39 +07:00
function covertDateObject(date: string) {
if (date) {
2023-11-14 17:47:43 +07:00
const dateParts = date.split('/')
2023-11-07 16:35:39 +07:00
// ประกาศตัวแปรเพื่อเก็บค่าวันที่, เดือน, และ ปี
2023-11-14 17:47:43 +07:00
const day = parseInt(dateParts[0], 10)
const month = parseInt(dateParts[1], 10) - 1
const year = parseInt(dateParts[2], 10) + 2500
2023-11-07 16:35:39 +07:00
// สร้างอ็อบเจ็กต์ Date ด้วยค่าที่ได้
2023-11-14 17:47:43 +07:00
const dateObject = new Date(year, month, day)
return date2Thai(dateObject)
2023-11-07 16:35:39 +07:00
}
}
2023-11-14 17:47:43 +07:00
type OkCallback = () => void
type CancelCallback = () => void
function dialogConfirm(
2023-11-08 11:15:17 +07:00
q: any,
ok?: OkCallback,
title?: string, // ถ้ามี cancel action ใส่เป็น null
desc?: string, // ถ้ามี cancel action ใส่เป็น null
2025-03-20 16:22:01 +07:00
cancel?: CancelCallback,
color?: string,
okText?: string
2023-11-14 17:47:43 +07:00
) {
2023-11-08 11:15:17 +07:00
q.dialog({
component: CustomComponent,
componentProps: {
2023-11-14 17:47:43 +07:00
title: title && title != null ? title : 'ยืนยันการบันทึก',
2023-11-08 11:15:17 +07:00
message:
desc && desc != null
? desc
2023-11-14 17:47:43 +07:00
: 'ต้องการยืนยันการบันทึกข้อมูลนี้ใช่หรือไม่?',
icon: 'info',
2025-03-20 16:22:01 +07:00
color: color ? color : 'public',
textOk: okText ? okText : 'ตกลง',
2023-11-08 11:15:17 +07:00
onlycancel: false,
},
})
.onOk(() => {
2023-11-14 17:47:43 +07:00
if (ok) ok()
2023-11-08 11:15:17 +07:00
})
.onCancel(() => {
2023-11-14 17:47:43 +07:00
if (cancel) cancel()
})
}
2023-11-20 12:11:34 +07:00
2024-07-23 14:46:40 +07:00
const messageError = (q: any, e: any = '', msg: string = '') => {
2023-11-20 12:11:34 +07:00
// q.dialog.hide();
if (e.response !== undefined) {
if (e.response.data.status !== undefined) {
if (e.response.data.status == 401) {
//invalid_token
q.dialog({
component: CustomComponent,
componentProps: {
title: `พบข้อผิดพลาด`,
message: `ล็อกอินหมดอายุ กรุณาล็อกอินใหม่อีกครั้ง`,
icon: 'warning',
color: 'red',
onlycancel: true,
},
2024-08-16 17:00:33 +07:00
}).onCancel(async () => {
showLoader()
2024-08-28 13:43:21 +07:00
await logout()
2024-08-16 17:00:33 +07:00
setTimeout(() => {
hideLoader()
}, 1000)
2023-11-20 12:11:34 +07:00
})
} else {
const message = e.response.data.result ?? e.response.data.message
q.dialog({
component: CustomComponent,
componentProps: {
title: `พบข้อผิดพลาด`,
message: `${message}`,
icon: 'warning',
color: 'red',
onlycancel: true,
},
})
}
} else {
if (e.response.status == 401) {
2024-07-23 14:46:40 +07:00
if (msg !== '') {
q.dialog({
component: CustomComponent,
componentProps: {
title: `พบข้อผิดพลาด`,
message: msg,
icon: 'warning',
color: 'red',
onlycancel: true,
},
2024-08-16 17:00:33 +07:00
}).onCancel(async () => {
showLoader()
2024-08-28 13:43:21 +07:00
await logout()
2024-08-16 17:00:33 +07:00
setTimeout(() => {
hideLoader()
}, 1000)
2024-07-23 14:46:40 +07:00
})
} else {
//invalid_token
q.dialog({
component: CustomComponent,
componentProps: {
title: `พบข้อผิดพลาด`,
message: `ล็อกอินหมดอายุ กรุณาล็อกอินใหม่อีกครั้ง`,
icon: 'warning',
color: 'red',
onlycancel: true,
},
2024-08-16 17:00:33 +07:00
}).onCancel(async () => {
showLoader()
2024-08-28 13:43:21 +07:00
await logout()
2024-08-16 17:00:33 +07:00
setTimeout(() => {
hideLoader()
}, 1000)
2024-07-23 14:46:40 +07:00
})
}
2023-11-20 12:11:34 +07:00
} else if (e.response.data.successful === false) {
q.dialog({
component: CustomComponent,
componentProps: {
title: `พบข้อผิดพลาด`,
message: e.response.data.message,
icon: 'warning',
color: 'red',
onlycancel: true,
},
})
} else {
q.dialog({
component: CustomComponent,
componentProps: {
title: `พบข้อผิดพลาด`,
message: `ข้อมูลผิดพลาดทำให้เกิดการไม่ตอบสนองต่อการเรียกใช้งานดูเว็บไซต์`,
icon: 'warning',
color: 'red',
onlycancel: true,
},
})
}
}
} else {
2025-06-13 11:24:09 +07:00
if (msg !== '') {
return q.dialog({
component: CustomComponent,
componentProps: {
title: `พบข้อผิดพลาด`,
message: msg,
icon: 'warning',
color: 'red',
onlycancel: true,
},
})
}
2023-11-20 12:11:34 +07:00
q.dialog({
component: CustomComponent,
componentProps: {
title: `พบข้อผิดพลาด`,
message: `ข้อมูลผิดพลาดทำให้เกิดการไม่ตอบสนองต่อการเรียกใช้งานดูเว็บไซต์`,
icon: 'warning',
color: 'red',
onlycancel: true,
},
})
}
}
2023-11-07 11:17:13 +07:00
const success = (q: any, val: string) => {
// useQuasar ไม่สามารถใช้นอกไฟล์ .vue
if (val !== '') {
return q.notify({
message: val,
color: 'primary',
icon: 'mdi-information',
position: 'bottom-right',
multiLine: true,
timeout: 1000,
badgeColor: 'positive',
classes: 'my-notif-class',
})
}
}
function notify(q: any, val: string) {
if (val !== '') {
q.notify({
color: 'teal-10',
message: val,
icon: 'mdi-information',
position: 'bottom-right',
multiLine: true,
timeout: 7000,
actions: [{ label: 'ปิด', color: 'white', handler: () => {} }],
})
}
}
function dialogRemove(
q: any,
ok?: () => void,
title?: string, // ถ้ามี cancel action ใส่เป็น null
desc?: string, // ถ้ามี cancel action ใส่เป็น null
cancel?: () => void
) {
q.dialog({
component: CustomComponent,
componentProps: {
title: title && title != null ? title : 'ยืนยันการลบข้อมูล',
message:
desc && desc != null
? desc
: 'ต้องการยืนยันการลบข้อมูลนี้ใช่หรือไม่?',
icon: 'delete',
color: 'red',
textOk: 'ตกลง',
onlycancel: false,
},
})
.onOk(() => {
if (ok) ok()
})
.onCancel(() => {
if (cancel) cancel()
})
}
function monthYear2Thai(month: number, year: number, isFullMonth = false) {
if (
month < 0 ||
month > 11 ||
!Number.isFinite(month) ||
!Number.isFinite(year)
)
return ''
const fullMonthThai = [
'มกราคม',
'กุมภาพันธ์',
'มีนาคม',
'เมษายน',
'พฤษภาคม',
'มิถุนายน',
'กรกฎาคม',
'สิงหาคม',
'กันยายน',
'ตุลาคม',
'พฤศจิกายน',
'ธันวาคม',
]
const abbrMonthThai = [
'ม.ค.',
'ก.พ.',
'มี.ค.',
'เม.ย.',
'พ.ค.',
'มิ.ย.',
'ก.ค.',
'ส.ค.',
'ก.ย.',
'ต.ค.',
'พ.ย.',
'ธ.ค.',
]
// assume year is in BE if > 2500
let dstYear = year > 2500 ? year : year + 543
// month is already 0-based
let dstMonth = isFullMonth ? fullMonthThai[month] : abbrMonthThai[month]
return `${dstMonth} ${dstYear}`
}
2025-03-10 17:17:04 +07:00
// กรณีมีเฉพาะ date
function convertDateToAPI(date: Date | null) {
return date
? format(utcToZonedTime(date, 'Asia/Bangkok'), 'yyyy-MM-dd')
: null
}
// กรณี datetime
function convertDatetimeToAPI(date: Date | null) {
return date
? format(utcToZonedTime(date, 'Asia/Bangkok'), 'yyyy-MM-dd HH:mm:ss')
: null
}
2023-11-07 11:17:13 +07:00
return {
date2Thai,
monthYear2Thai,
showLoader,
hideLoader,
2023-11-07 16:35:39 +07:00
covertDateObject,
2023-11-08 11:15:17 +07:00
dialogConfirm,
2023-11-16 17:30:24 +07:00
messageError,
success,
notify,
dialogRemove,
2025-03-10 17:17:04 +07:00
convertDateToAPI,
convertDatetimeToAPI,
2023-11-14 17:47:43 +07:00
}
})