import { http, unwrapApiResponse, type ApiEnvelope } from '@/shared/api/http' import type { DispatchStage } from '../dispatchContracts' export const loadingPhotoSlots = ['LOADING_SCENE', 'LOADING_LIST', 'PRE_DEPARTURE_CHECK', 'DRIVER_LICENSE', 'RECEIPT'] as const export type LoadingPhotoSlot = typeof loadingPhotoSlots[number] export const loadingPhotoLabels: Record = { LOADING_SCENE: '司机驾驶证', LOADING_LIST: '车牌照片', PRE_DEPARTURE_CHECK: '装车完成', DRIVER_LICENSE: '封车照片', RECEIPT: 'BOL司机签字' } export interface LoadingPhoto { slot: LoadingPhotoSlot; fileId: string; fileName: string; contentType: string; savedAt: string | null } export interface LoadingDocuments { dispatchNo: string; recordVersion: string; dispatchRecordVersion: string; reviewRound: string; stage: DispatchStage; editable: boolean; complete: boolean; photos: LoadingPhoto[] } export interface BindLoadingPhoto { recordVersion: string; idempotencyKey: string; fileId: string; uploadKey: string; replaceFileId?: string } const path = (no: string) => `/platform/v1/last-leg/dispatches/${encodeURIComponent(no)}/loading-documents` export async function getLoadingDocuments(no: string) { return unwrapApiResponse(await http.get, ApiEnvelope>(path(no))) } export async function uploadLoadingPhoto(no: string, slot: LoadingPhotoSlot, file: File, key: string, reviewRound: string) { const form = new FormData(); form.append('file', file); form.append('idempotencyKey', key); form.append('reviewRound', reviewRound) return unwrapApiResponse(await http.post, ApiEnvelope>(`${path(no)}/${slot}/uploads`, form)) } export async function recoverLoadingPhoto(no: string, key: string) { return unwrapApiResponse(await http.get, ApiEnvelope>(`${path(no)}/uploads/${encodeURIComponent(key)}`)) } export async function bindLoadingPhoto(no: string, slot: LoadingPhotoSlot, body: BindLoadingPhoto) { return unwrapApiResponse(await http.put, ApiEnvelope>(`${path(no)}/${slot}`, body)) } export async function readLoadingPhoto(no: string, photo: LoadingPhoto) { const blob = await http.get(`${path(no)}/${photo.slot}/files/${encodeURIComponent(photo.fileId)}/content`, { responseType: 'blob' }) if (!(blob instanceof Blob) || !(['image/jpeg', 'image/png'].includes(blob.type) || photo.slot === 'LOADING_SCENE' && blob.type === 'application/pdf') || blob.type !== photo.contentType || !blob.size) throw new Error('资料读取失败,请重新核对') return blob }