เเก้ ลงเวลาเข้างาน

This commit is contained in:
setthawutttty 2025-03-21 16:52:05 +07:00
parent e38c1210f9
commit 4bbaa52a4e

View file

@ -20,14 +20,16 @@ const modalTime = ref<boolean>(false) // Dailog ลงเวลาเข้า
const checkStatus = ref<string>('') const checkStatus = ref<string>('')
const statusCheckin = ref<boolean>(true) // , const statusCheckin = ref<boolean>(true) // ,
const checkDate = ref<boolean | null>(null)
const msgCheckTime = ref<string>('') // const msgCheckTime = ref<string>('') //
const isDisabledCheckTime = ref<boolean>(false) // const isDisabledCheckTime = ref<boolean>(false) //
const isErr = ref<boolean | null>(null) //
const endTimeAfternoon = ref<string>('12:00:00') // const endTimeAfternoon = ref<string>('12:00:00') //
/** /**
* fetch เชคเวลาตองลงเวลาเขาหรอออกงาน * fetch เชคเวลาตองลงเวลาเขาหรอออกงาน
*/ */
async function fetchCheckTime() { async function fetchCheckTime(load: any = true) {
showLoader() load && showLoader()
await http await http
.get(config.API.checkTime()) .get(config.API.checkTime())
.then(async (res) => { .then(async (res) => {
@ -39,12 +41,13 @@ async function fetchCheckTime() {
}) })
.catch((err) => { .catch((err) => {
if (err.response.status === 500) { if (err.response.status === 500) {
isErr.value = true
isDisabledCheckTime.value = true isDisabledCheckTime.value = true
msgCheckTime.value = err.response.data.message msgCheckTime.value = err.response.data.message
} else messageError($q, err) } else messageError($q, err)
}) })
.finally(() => { .finally(() => {
hideLoader() load && hideLoader()
}) })
} }
@ -128,6 +131,109 @@ const img = ref<any>(undefined)
const photoWidth = ref<number>(350) const photoWidth = ref<number>(350)
const photoHeight = ref<number>(350) const photoHeight = ref<number>(350)
const intervalId = ref<number | undefined>(undefined) // interval
/**
* เรมจาก onMounted #1 เช status
*
*/
async function startChecking() {
showLoader()
fetchCheckStatus()
if (intervalId.value === undefined) {
// intervalId == undefined
intervalId.value = setInterval(async () => {
await fetchCheckStatus() // fetchCheckStatus
}, 3000)
}
}
/**
* เรมจาก onMounted #2 เช status
*
*/
async function fetchCheckStatus() {
try {
const res = await http.get(config.API.checkStatus())
inQueue.value = res.data.result.inQueue
if (res.data.result.inQueue) {
/** inQueue เป็น true */
isDisabledCheckTime.value = true
msgCheckTime.value = 'ระบบกำลังประมวลผล'
hideLoader()
} else {
/** inQueue เป็น false */
isDisabledCheckTime.value = false
msgCheckTime.value = ''
stopChecking() //
console.log('Response เป็น false, หยุด interval')
}
} catch (error) {
console.log('เกิดข้อผิดพลาด', error)
stopChecking() //
}
}
/** ตัวเก่าก่อนเปลี่ยน */
// async function stopChecking() {
// if (intervalId.value !== undefined) {
// clearInterval(intervalId.value) // interval
// setTimeout(() => {
// fetchCheckTime()
// }, 1000)
// intervalId.value = undefined //
// }
// }
/**
* เรมจาก onMounted #3 เช status
*
*/
async function stopChecking() {
if (intervalId.value !== undefined) {
await fetchCheckTime()
clearInterval(intervalId.value)
intervalId.value = undefined // interval
async function repeatFetch() {
await fetchCheckTime(false)
if (statusCheckin.value && checkDate.value === true) {
setTimeout(repeatFetch, 1000)
} else {
checkDate.value = null
isErr.value = false
}
}
async function repeatFetchOut() {
await fetchCheckTime(false)
if (
isErr.value === false &&
statusCheckin.value === false &&
checkDate.value === true
) {
setTimeout(repeatFetchOut, 1000) //
} else {
checkDate.value = null
isErr.value = null
}
}
// fetch
if (statusCheckin.value && checkDate.value === true) {
repeatFetch()
} else if (
isErr.value === false &&
statusCheckin.value === false &&
checkDate.value === true
) {
repeatFetchOut()
}
}
}
/** function เปิดกล้อง*/ /** function เปิดกล้อง*/
async function openCamera() { async function openCamera() {
// change camera device // change camera device
@ -221,6 +327,7 @@ async function confirm() {
.then(async (res) => { .then(async (res) => {
const data = await res.data.result const data = await res.data.result
const dateObject = new Date(data.date) const dateObject = new Date(data.date)
checkDate.value = data.date ? true : false
const options: Intl.DateTimeFormatOptions = { const options: Intl.DateTimeFormatOptions = {
hour12: false, hour12: false,
hour: '2-digit', hour: '2-digit',
@ -289,6 +396,7 @@ async function onClickConfirm() {
// await fetchCheckTime() // await fetchCheckTime()
// fetchCheckStatus() // fetchCheckStatus()
startChecking() startChecking()
cameraIsOn.value = false cameraIsOn.value = false
@ -321,53 +429,11 @@ const getClassXS = (val: boolean) => {
} }
const inQueue = ref<boolean>(false) const inQueue = ref<boolean>(false)
async function fetchCheckStatus() {
try {
const res = await http.get(config.API.checkStatus())
inQueue.value = res.data.result.inQueue
if (res.data.result.inQueue) {
isDisabledCheckTime.value = true
msgCheckTime.value = 'ระบบกำลังประมวลผล'
hideLoader()
} else {
isDisabledCheckTime.value = false
msgCheckTime.value = ''
stopChecking() //
console.log('Response เป็น false, หยุด interval')
}
} catch (error) {
console.log('เกิดข้อผิดพลาด', error)
stopChecking() //
}
}
const intervalId = ref<number | undefined>(undefined) // interval
async function startChecking() {
showLoader()
fetchCheckStatus()
if (intervalId.value === undefined) {
// interval
intervalId.value = setInterval(async () => {
await fetchCheckStatus() // fetchCheckStatus
}, 3000)
}
}
async function stopChecking() {
if (intervalId.value !== undefined) {
clearInterval(intervalId.value) // interval
setTimeout(() => {
fetchCheckTime()
}, 1000)
intervalId.value = undefined //
}
}
/** Hook*/ /** Hook*/
onMounted(async () => { onMounted(async () => {
// await fetchCheckTime() // await fetchCheckTime()
updateClock() updateClock()
startChecking() startChecking() // status #1
}) })
</script> </script>