hrms-checkin/src/stores/mixin.ts

123 lines
3.5 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-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
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
cancel?: CancelCallback
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',
color: 'public',
textOk: 'ตกลง',
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-07 11:17:13 +07:00
return {
date2Thai,
2023-11-07 16:35:39 +07:00
covertDateObject,
2023-11-08 11:15:17 +07:00
dialogConfirm,
2023-11-14 17:47:43 +07:00
}
})