54 lines
2.0 KiB
TypeScript
54 lines
2.0 KiB
TypeScript
import { onScopeDispose, ref } from 'vue'
|
|
import { getApiErrorDetails, type ApiErrorDetails } from '@/shared/api/http'
|
|
import { getConfirmedLoadingList, getPreloadPlan, type LoadingDetail, type LoadingListMode } from './api/platformLoadingLists'
|
|
|
|
export function useLoadingListDetail(api = { getConfirmedLoadingList, getPreloadPlan }) {
|
|
const detail = ref<LoadingDetail | null>(null)
|
|
const loading = ref(false)
|
|
const error = ref<ApiErrorDetails | null>(null)
|
|
let sequence = 0
|
|
let currentKey = ''
|
|
|
|
function reset() {
|
|
++sequence
|
|
currentKey = ''
|
|
detail.value = null
|
|
loading.value = false
|
|
error.value = null
|
|
}
|
|
|
|
async function load(mode: LoadingListMode, planId: string, warehouseId: string) {
|
|
const key = `${mode}:${planId}:${warehouseId}`
|
|
if (key === currentKey && (detail.value || loading.value)) return detail.value
|
|
const request = ++sequence
|
|
currentKey = key
|
|
detail.value = null
|
|
error.value = null
|
|
loading.value = true
|
|
try {
|
|
if (!planId || !warehouseId) {
|
|
error.value = { code: 'MISSING_WAREHOUSE_CONTEXT', message: '该清单缺少计划或所属仓库信息,无法查询详情,请返回重新进入。' }
|
|
return null
|
|
}
|
|
const result = mode === 'confirmed'
|
|
? await api.getConfirmedLoadingList(planId, warehouseId)
|
|
: await api.getPreloadPlan(planId, warehouseId)
|
|
if (request !== sequence) return null
|
|
if (mode === 'confirmed' && result.summary.status !== 'COMPLETED') {
|
|
error.value = { code: 'MODE_STATUS_MISMATCH', message: '该计划尚未形成正式装柜清单,请从预装清单入口查看。' }
|
|
return null
|
|
}
|
|
detail.value = result
|
|
return detail.value
|
|
} catch (cause) {
|
|
if (request === sequence) error.value = getApiErrorDetails(cause, '装柜清单加载失败')
|
|
return null
|
|
} finally {
|
|
if (request === sequence) loading.value = false
|
|
}
|
|
}
|
|
|
|
onScopeDispose(reset)
|
|
return { detail, loading, error, load, reset }
|
|
}
|