fix fack location complated
All checks were successful
Build & Deploy on Dev / build (push) Successful in 2m22s
All checks were successful
Build & Deploy on Dev / build (push) Successful in 2m22s
This commit is contained in:
parent
859d74056a
commit
3ae6e6eeac
4 changed files with 287 additions and 115 deletions
|
|
@ -8,6 +8,7 @@ import config from '@/app.config'
|
|||
import http from '@/plugins/http'
|
||||
import { useCounterMixin } from '@/stores/mixin'
|
||||
import { usePermissions } from '@/composables/usePermissions'
|
||||
import { useLocationValidation } from '@/composables/useLocationValidation'
|
||||
import { usePrivacyStore } from '@/stores/privacy'
|
||||
|
||||
import type { FormRef, OptionReason } from '@/interface/response/checkin'
|
||||
|
|
@ -18,7 +19,10 @@ const mixin = useCounterMixin()
|
|||
const { date2Thai, showLoader, hideLoader, messageError, dialogConfirm } = mixin
|
||||
const $q = useQuasar()
|
||||
const { checkPrivacyAccepted } = usePermissions()
|
||||
const { validateLocation, showMockWarning, resetValidation } =
|
||||
useLocationValidation()
|
||||
const privacyStore = usePrivacyStore()
|
||||
const MOCK_CHECK_DELAY_MS = 800
|
||||
|
||||
const modalTime = ref<boolean>(false) // Dailog ลงเวลาเข้างานของคุณ
|
||||
const checkStatus = ref<string>('')
|
||||
|
|
@ -120,14 +124,110 @@ async function updateLocation(
|
|||
*/
|
||||
function onLocationStatus(status: boolean) {
|
||||
locationGranted.value = status
|
||||
if (status) {
|
||||
isMockLocationDetected.value = false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* รับค่า mock location detection จาก AscGISMap
|
||||
*/
|
||||
function onMockDetected(result: any) {
|
||||
isMockLocationDetected.value = true
|
||||
disabledBtn.value = true
|
||||
isMockLocationDetected.value = !!result?.isMockDetected
|
||||
disabledBtn.value = false
|
||||
}
|
||||
|
||||
function resetLocationForRetry() {
|
||||
locationGranted.value = false
|
||||
formLocation.lat = 0
|
||||
formLocation.lng = 0
|
||||
formLocation.POI = ''
|
||||
}
|
||||
|
||||
function getCurrentPositionAsync(options?: PositionOptions) {
|
||||
return new Promise<GeolocationPosition>((resolve, reject) => {
|
||||
navigator.geolocation.getCurrentPosition(resolve, reject, options)
|
||||
})
|
||||
}
|
||||
|
||||
function wait(ms: number) {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms))
|
||||
}
|
||||
|
||||
async function getDelayedFreshPosition() {
|
||||
const firstPosition = await getCurrentPositionAsync({
|
||||
enableHighAccuracy: true,
|
||||
timeout: 10000,
|
||||
maximumAge: 0,
|
||||
})
|
||||
|
||||
await wait(MOCK_CHECK_DELAY_MS)
|
||||
|
||||
try {
|
||||
return await getCurrentPositionAsync({
|
||||
enableHighAccuracy: true,
|
||||
timeout: 10000,
|
||||
maximumAge: 0,
|
||||
})
|
||||
} catch (error) {
|
||||
return firstPosition
|
||||
}
|
||||
}
|
||||
|
||||
async function revalidateLocationBeforeSubmit() {
|
||||
if (!navigator.geolocation) {
|
||||
messageError(
|
||||
$q,
|
||||
'',
|
||||
'ไม่สามารถระบุตำแหน่งปัจจุบันได้ เบราว์เซอร์ของคุณไม่รองรับ Geolocation'
|
||||
)
|
||||
return false
|
||||
}
|
||||
|
||||
try {
|
||||
// If previous attempt was mock, clear history so fresh GPS is not compared
|
||||
// against spoofed coordinates and incorrectly flagged as impossible speed.
|
||||
if (isMockLocationDetected.value) {
|
||||
resetValidation()
|
||||
}
|
||||
|
||||
const position = await getDelayedFreshPosition()
|
||||
|
||||
const validationResult = validateLocation(position)
|
||||
|
||||
if (validationResult.isMockDetected) {
|
||||
isMockLocationDetected.value = true
|
||||
disabledBtn.value = false
|
||||
resetValidation()
|
||||
resetLocationForRetry()
|
||||
showMockWarning(validationResult)
|
||||
mapRef.value?.requestLocationPermission()
|
||||
return false
|
||||
}
|
||||
|
||||
if (validationResult.errors.length > 0) {
|
||||
disabledBtn.value = false
|
||||
resetValidation()
|
||||
resetLocationForRetry()
|
||||
messageError($q, '', validationResult.errors[0])
|
||||
mapRef.value?.requestLocationPermission()
|
||||
return false
|
||||
}
|
||||
|
||||
locationGranted.value = true
|
||||
isMockLocationDetected.value = false
|
||||
return true
|
||||
} catch (error) {
|
||||
disabledBtn.value = false
|
||||
resetLocationForRetry()
|
||||
messageError(
|
||||
$q,
|
||||
'',
|
||||
'ไม่สามารถตรวจสอบตำแหน่งล่าสุดก่อนลงเวลาได้ กรุณาลองใหม่อีกครั้ง'
|
||||
)
|
||||
mapRef.value?.requestLocationPermission()
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
const location = ref<string>('') // พื้นที่ใกล้เคียง
|
||||
|
|
@ -348,7 +448,7 @@ const objectRef: FormRef = {
|
|||
}
|
||||
|
||||
/** function ตรวจสอบค่าว่างของ input*/
|
||||
function validateForm() {
|
||||
async function validateForm() {
|
||||
const hasError = []
|
||||
for (const key in objectRef) {
|
||||
if (Object.prototype.hasOwnProperty.call(objectRef, key)) {
|
||||
|
|
@ -360,6 +460,11 @@ function validateForm() {
|
|||
}
|
||||
}
|
||||
if (hasError.every((result) => result === true)) {
|
||||
const isLocationValid = await revalidateLocationBeforeSubmit()
|
||||
if (!isLocationValid) {
|
||||
return
|
||||
}
|
||||
|
||||
if (statusCheckin.value == false) {
|
||||
getCheck()
|
||||
} else if (statusCheckin.value) {
|
||||
|
|
@ -397,6 +502,12 @@ async function confirm() {
|
|||
mapRef.value?.requestLocationPermission()
|
||||
return
|
||||
}
|
||||
|
||||
const isLocationValid = await revalidateLocationBeforeSubmit()
|
||||
if (!isLocationValid) {
|
||||
return
|
||||
}
|
||||
|
||||
disabledBtn.value = true
|
||||
showLoader()
|
||||
const isLocation = workplace.value === 'in-place' //*true คือ ณ สถานที่ตั้ง, false คือ นอกสถานที่ตั้ง
|
||||
|
|
@ -1016,7 +1127,13 @@ watch(
|
|||
push
|
||||
size="18px"
|
||||
:class="$q.screen.gt.xs ? 'q-px-md' : 'full-width q-pa-sm'"
|
||||
:disable="disabledBtn || !locationGranted || isMockLocationDetected ? true : camera && img ? false : true"
|
||||
:disable="
|
||||
disabledBtn || !locationGranted || isMockLocationDetected
|
||||
? true
|
||||
: camera && img
|
||||
? false
|
||||
: true
|
||||
"
|
||||
@click="validateForm"
|
||||
:loading="inQueue"
|
||||
/>
|
||||
|
|
@ -1131,7 +1248,13 @@ watch(
|
|||
push
|
||||
size="18px"
|
||||
:class="$q.screen.gt.xs ? 'q-px-md' : 'full-width q-pa-sm'"
|
||||
:disable="disabledBtn || !locationGranted || isMockLocationDetected ? true : camera && img ? false : true"
|
||||
:disable="
|
||||
disabledBtn || !locationGranted || isMockLocationDetected
|
||||
? true
|
||||
: camera && img
|
||||
? false
|
||||
: true
|
||||
"
|
||||
@click="validateForm"
|
||||
:loading="inQueue"
|
||||
/>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue