fix(drivers): isolate cached detail requests and reject absent route IDs

This commit is contained in:
2026-10-04 01:02:28 +08:00
parent 5c4a381d87
commit f4d58e669b
2 changed files with 102 additions and 7 deletions
@@ -1,6 +1,6 @@
<script setup lang="ts">
import '@/styles/adminDensity.css'
import { computed, onBeforeUnmount, ref, watch } from 'vue'
import { computed, onActivated, onBeforeUnmount, onDeactivated, onMounted, ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ArrowLeft } from '@lucide/vue'
import { ElMessage, ElMessageBox } from 'element-plus'
@@ -27,6 +27,12 @@ const fileInput = ref<HTMLInputElement | null>(null), qualificationLoading = ref
const qualificationStale = ref(false)
const historyOpen = ref(false), historyRows = ref<DriverHistory[]>([]), historyPage = ref(1), historySize = ref(20), historyTotal = ref(0), historyLoading = ref(false), historyError = ref<ApiErrorDetails | null>(null)
let request = 0, historyRequest = 0, qualificationRequest = 0
let detailActive = true, initialActivation = true
function routeDriverId(): string | null {
const id = route.params.driverId
return route.name === 'overseas-master-data-driver-detail' && typeof id === 'string' && /^\d+$/.test(id) ? id : null
}
const instanceDriverId = routeDriverId()
const browserZone = Intl.DateTimeFormat().resolvedOptions().timeZone
const basic = computed(() => detail.value ? [
['司机编号', detail.value.driverNo], ['司机姓名', detail.value.details.name], ['联系电话', detail.value.details.phone],
@@ -46,13 +52,20 @@ const historyColumns: DataTableColumn[] = [
{ key: 'reviewStatus', label: '当时审核状态', slotName: 'historyReview', minWidth: 120 },
]
const actionLabels: Record<DriverHistory['action'], string> = { CREATE: '创建档案', UPDATE: '编辑档案', ACTIVATE: '启用', DEACTIVATE: '停用', UPLOAD_LICENSE: '上传驾驶证', APPROVED: '审核通过', REJECTED: '审核打回', RESUBMIT: '重新提交' }
async function load() {
function resetDetailRequests() {
const current = ++request
historyRequest++; qualificationRequest++; detail.value = null; error.value = null; loading.value = true
historyRequest++; qualificationRequest++; detail.value = null; error.value = null; loading.value = false
historyOpen.value = false; historyRows.value = []; historyPage.value = 1; reviewReason.value = ''; expectedEnd.value = null
historyLoading.value = false; historyError.value = null
previewOpen.value = false; editorOpen.value = false; qualificationLoading.value = false
qualificationStale.value = false
try { const result = await getDriver(String(route.params.driverId)); if (current === request) detail.value = result }
return current
}
async function load() {
const current = resetDetailRequests()
if (!detailActive || !instanceDriverId || routeDriverId() !== instanceDriverId) return
loading.value = true
try { const result = await getDriver(instanceDriverId); if (current === request) detail.value = result }
catch (cause) { if (current === request) error.value = getApiErrorDetails(cause, '司机详情加载失败') }
finally { if (current === request) loading.value = false }
}
@@ -116,10 +129,19 @@ async function upload(event: Event) {
finally { busy.value = false }
}
function showLicense(fileId: string) { previewFile.value = fileId; previewOpen.value = true }
watch(() => route.params.driverId, load, { immediate: true })
watch(() => [route.name, route.params.driverId], () => {
if (routeDriverId() !== instanceDriverId) resetDetailRequests()
}, { flush: 'sync' })
onMounted(() => void load())
onActivated(() => {
detailActive = true
if (initialActivation) { initialActivation = false; return }
void load()
})
onDeactivated(() => { detailActive = false; resetDetailRequests() })
watch([historyPage, historySize], () => void loadHistory())
watch(historyOpen, value => { if (value) void loadHistory() })
onBeforeUnmount(() => { request++; historyRequest++; qualificationRequest++ })
onBeforeUnmount(() => { detailActive = false; resetDetailRequests() })
</script>
<template>