import { CommonData, CommonValue } from '~/common' import { Query } from '@/api/webApi' import { ElMessage } from 'element-plus' const waybillInfoItemsMap = { internal: [ { label: '运单', key: 'stockCode', }, { label: '货代公司', key: 'cargoCompany', }, { label: '品名', key: 'typeCode', }, { label: '特货信息', key: 'speCargoInfo', }, { label: '始发机场', key: 'departureAirport', }, { label: '目的机场', key: 'arriveAirport', }, { label: '货物数量', key: 'luggageCount', }, { label: '货物总重', key: 'weight', }, ], international: [ { label: '运单', key: 'stockCode,', }, { label: '运单类型', key: 'stockType,', }, { label: '货代公司', key: 'cargoCompany,', }, { label: '品名', key: 'typeCode,', }, { label: '特货信息', key: 'speCargoInfo,', }, { label: '始发机场', key: 'departureAirport,', }, { label: '目的机场', key: 'arriveAirport,', }, ], } const simulateWaybillInfoMap = { internal: { C0: 'FA56888829', C1: '深圳市联运通货有限公司', C3: '电路板、手机外壳', C4: '特', C5: 'SZX', C6: 'NKG', C7: '7件', C8: '82KG', }, international: { C0: 'FA56888829', C1: '国际普货', C2: '深圳市联运通货有限公司', C3: '电路板、手机外壳', C4: '特', C5: 'SZX', C6: 'NKG', }, } export function useWaybillInfo(name: string, dataContent: CommonValue[]) { const waybillInfoItems = ref< { label: string; key?: string; getter?: (info: any) => string }[] >([]) const getWaybillInfoItems = () => { waybillInfoItems.value = waybillInfoItemsMap[ name.includes('International') ? 'international' : 'internal' ] } getWaybillInfoItems() const waybillInfo = reactive({}) const getWaybillInfo = async () => { try { const { code, returnData: { listValues }, message, } = await Query({ id: DATACONTENT_ID.waybillInfo, dataContent, }) if (Number(code) !== 0) { throw new Error(message || '失败') } if (!listValues.length) { ElMessage.info('未查询到运单信息') return } Object.assign(waybillInfo, listValues[0]) } catch (error) { console.error(error) } } const computedWaybillInfo = computed(() => item => { if (item.getter) { return item.getter(waybillInfo) } else if (item.key) { return waybillInfo[item.key] } else { return '' } }) const getSimulateWaybillInfo = () => { const simulateWaybillInfo = simulateWaybillInfoMap[ name.includes('International') ? 'international' : 'internal' ] Object.assign(waybillInfo, simulateWaybillInfo) } onMounted(() => { getWaybillInfo() // getSimulateWaybillInfo() }) return { waybillInfoItems, waybillInfo, computedWaybillInfo, } }