import type { LocationQuery, Router } from 'vue-router'; import { useEdiAdvisorPanel } from '@/composables/useEdiAdvisorPanel'; export type WaybillSource = 'myOrders' | 'waybillManage' | 'h5Orders'; export type WaybillContext = { orderId?: string; waybillNo?: string; trackingNo?: string; customerOrderNo?: string; from?: WaybillSource; orderStatus?: string; }; function pickQueryValue(value: LocationQuery[string]): string | undefined { const raw = Array.isArray(value) ? value[0] : value; if (raw == null) return undefined; const text = String(raw).trim(); return text || undefined; } function pickWaybillSource(value: LocationQuery[string]): WaybillSource | undefined { const raw = pickQueryValue(value); if (raw === 'myOrders' || raw === 'waybillManage' || raw === 'h5Orders') return raw; return undefined; } export function buildWaybillContextFromRow( row: Record, from?: WaybillSource, ): WaybillContext { const orderId = row.id ?? row.Id; const waybillNo = row.waybillNo ?? row.serviceNo ?? row.ServiceNo; const trackingNo = row.trackingNo ?? row.trackNo ?? row.TrackNo; const customerOrderNo = row.customerOrderNo ?? row.clientNo ?? row.ClientNo; const orderStatus = row.orderStatus ?? row.status ?? row.Status ?? row.OrderStatus; return { orderId: orderId != null && String(orderId).trim() ? String(orderId).trim() : undefined, waybillNo: waybillNo != null && String(waybillNo).trim() ? String(waybillNo).trim() : undefined, trackingNo: trackingNo != null && String(trackingNo).trim() ? String(trackingNo).trim() : undefined, customerOrderNo: customerOrderNo != null && String(customerOrderNo).trim() ? String(customerOrderNo).trim() : undefined, from, orderStatus: orderStatus != null && String(orderStatus).trim() ? String(orderStatus).trim() : undefined, }; } export function parseWaybillContextFromQuery(query: LocationQuery): WaybillContext | null { const ctx: WaybillContext = { orderId: pickQueryValue(query.orderId), waybillNo: pickQueryValue(query.waybillNo), trackingNo: pickQueryValue(query.trackingNo), customerOrderNo: pickQueryValue(query.customerOrderNo), from: pickWaybillSource(query.from), orderStatus: pickQueryValue(query.orderStatus), }; if (!ctx.orderId && !ctx.waybillNo && !ctx.trackingNo && !ctx.customerOrderNo) { return null; } return ctx; } export function getWaybillContextKey(ctx: WaybillContext): string { return ctx.orderId || ctx.waybillNo || ctx.trackingNo || ctx.customerOrderNo || ''; } export function getWaybillDisplayNo(ctx: WaybillContext): string { return ctx.waybillNo || ctx.trackingNo || ctx.customerOrderNo || ctx.orderId || ''; } export function getWaybillPrefillText(ctx: WaybillContext): string { return ctx.trackingNo || ctx.waybillNo || ctx.customerOrderNo || ''; } export function getWaybillReturnPath(ctx?: WaybillContext | null): string { if (ctx?.from === 'waybillManage') return '/order/waybillManage'; if (ctx?.from === 'h5Orders') { return ctx.orderId ? `/h5/orders/${ctx.orderId}` : '/h5/orders'; } if (ctx?.from === 'myOrders') return '/order/myOrders'; return '/order/myOrders'; } export function buildAskAdvisorQuery(ctx: WaybillContext): Record { const query: Record = {}; if (ctx.orderId) query.orderId = ctx.orderId; if (ctx.waybillNo) query.waybillNo = ctx.waybillNo; if (ctx.trackingNo) query.trackingNo = ctx.trackingNo; if (ctx.customerOrderNo) query.customerOrderNo = ctx.customerOrderNo; if (ctx.from) query.from = ctx.from; if (ctx.orderStatus) query.orderStatus = ctx.orderStatus; return query; } export function openAskAdvisor(router: Router, ctx: WaybillContext) { const path = router.currentRoute.value.path || ''; // H5 仍用独立顾问页 if (path.startsWith('/h5')) { router.push({ path: '/h5/assistant', query: buildAskAdvisorQuery(ctx), }); return; } // PC:打开全局大面板,不离开当前业务页 useEdiAdvisorPanel().open({ waybill: ctx }); } export default function useAskAdvisor() { return { buildWaybillContextFromRow, parseWaybillContextFromQuery, getWaybillContextKey, getWaybillDisplayNo, getWaybillPrefillText, getWaybillReturnPath, buildAskAdvisorQuery, openAskAdvisor, }; }