This commit is contained in:
2026-10-22 11:56:54 +08:00
parent 090431b409
commit 3e77c3976e
40 changed files with 976 additions and 241 deletions
@@ -33,6 +33,7 @@ describe('OceanBookingPrintDialog', () => {
await flushPromises()
expect(downloadOceanBookingPrint).toHaveBeenCalledWith('123', 'SHIPPING_INSTRUCTION')
expect(wrapper.emitted('previewed')).toEqual([[{ bookingId: '123', type: 'SHIPPING_INSTRUCTION' }]])
expect(document.body.querySelector('iframe')?.getAttribute('src')).toBe('blob:booking-print')
expect(document.body.querySelector('.el-dialog__title')?.textContent).toBe('出口货物托运单 · OB-123')
document.querySelector<HTMLButtonElement>('[aria-label="展开全屏"]')!.click()
@@ -144,4 +145,24 @@ describe('OceanBookingPrintDialog identity and resource lifecycle', () => {
wrapper.unmount()
expect(URL.revokeObjectURL).toHaveBeenCalledExactlyOnceWith('blob:current-print')
})
it('opens the selected document directly and reloads the other type after closing', async () => {
vi.stubGlobal('URL', { createObjectURL: vi.fn(() => 'blob:booking-print'), revokeObjectURL: vi.fn() })
vi.mocked(downloadOceanBookingPrint).mockResolvedValue(new Blob(['pdf'], { type: 'application/pdf' }))
const wrapper = mount(OceanBookingPrintDialog, {
props: { modelValue: true, bookingId: '123', bookingNo: 'OB-123', allowed: true, initialType: 'SHIPPING_INSTRUCTION' },
global: { plugins: [ElementPlus] }, attachTo: document.body,
})
await flushPromises()
expect(downloadOceanBookingPrint).toHaveBeenLastCalledWith('123', 'SHIPPING_INSTRUCTION')
expect(document.body.querySelector('iframe')).not.toBeNull()
await wrapper.setProps({ modelValue: false })
await flushPromises()
expect(URL.revokeObjectURL).toHaveBeenCalledWith('blob:booking-print')
await wrapper.setProps({ initialType: 'BILL_OF_LADING_CONFIRMATION', modelValue: true })
await flushPromises()
expect(downloadOceanBookingPrint).toHaveBeenLastCalledWith('123', 'BILL_OF_LADING_CONFIRMATION')
expect(document.body.querySelector('.el-dialog__title')?.textContent).toBe('提单确认件 · OB-123')
wrapper.unmount()
})
})
@@ -10,7 +10,8 @@ import { getApiErrorDetails, type ApiErrorDetails } from '@/shared/api/http'
import { downloadOceanBookingPrint } from '../api/platformOceanBookings'
import type { OceanBookingPrintType } from '../types'
const props = defineProps<{ bookingId: string; bookingNo?: string | null; allowed: boolean }>()
const props = defineProps<{ bookingId: string; bookingNo?: string | null; allowed: boolean; initialType?: OceanBookingPrintType }>()
const emit = defineEmits<{ previewed: [event: { bookingId: string; type: OceanBookingPrintType }] }>()
const open = defineModel<boolean>({ default: false })
const selectedType = ref<OceanBookingPrintType | ''>('')
const loading = ref(false)
@@ -42,15 +43,18 @@ function reset() {
}
async function preview() {
if (!props.allowed || !selectedType.value || loading.value) return
const bookingId = props.bookingId
const type = selectedType.value
const request = ++previewRequest
loading.value = true
error.value = null
releasePreview()
try {
const blob = await downloadOceanBookingPrint(props.bookingId, selectedType.value)
const blob = await downloadOceanBookingPrint(bookingId, type)
if (request !== previewRequest || !open.value) return
printBlob.value = blob
previewUrl.value = URL.createObjectURL(blob)
emit('previewed', { bookingId, type })
} catch (cause) {
if (request === previewRequest) error.value = getApiErrorDetails(cause, '打印单据加载失败,请稍后重试')
} finally {
@@ -71,7 +75,13 @@ function download() {
URL.revokeObjectURL(url)
}
watch(open, (value) => { if (!value) reset() })
watch(open, (value) => {
if (!value) reset()
else if (props.initialType) {
selectedType.value = props.initialType
void preview()
}
}, { immediate: true })
onBeforeUnmount(() => { previewRequest += 1; releasePreview() })
</script>