import { ref } from 'vue' /** * T040: APS 排产进度 Hook * * 使用场景:详情页中查询排产进度数据,超时 5s 降级展示"排产进度暂不可用" * 不阻塞详情页整体渲染 */ export interface ApsScheduleResult { scheduleStatus: string scheduleProgress: number scheduleDate: string | null message: string | null } export function useApsSchedule() { const scheduleData = ref(null) const loading = ref(false) const timeout = ref(false) let timer: ReturnType | null = null /** * 查询排产进度 * @param forecastOrderId 预测单ID * @param timeoutMs 超时时间(毫秒),默认 5000ms */ async function fetchSchedule(forecastOrderId: number, timeoutMs = 5000) { loading.value = true timeout.value = false scheduleData.value = null // 模拟 API 调用 + 超时降级 const fetchPromise = new Promise((resolve, reject) => { // 模拟 APS 排产进度查询 timer = setTimeout(() => { // 模拟返回数据 resolve({ scheduleStatus: 'SCHEDULED', scheduleProgress: 75, scheduleDate: '2026-07-20', message: null, }) }, 1500) }) const timeoutPromise = new Promise((_, reject) => { setTimeout(() => { reject(new Error('TIMEOUT')) }, timeoutMs) }) try { const result = await Promise.race([fetchPromise, timeoutPromise]) scheduleData.value = result } catch { timeout.value = true scheduleData.value = null } finally { loading.value = false if (timer) clearTimeout(timer) } } /** * 手动重试 */ function retry(forecastOrderId: number) { return fetchSchedule(forecastOrderId) } return { scheduleData, loading, timeout, fetchSchedule, retry, } }