import { http, unwrapApiResponse, type ApiEnvelope } from '@/shared/api/http' export type FuelSlot = 'FUEL_START' | 'FUEL_END' export interface VehiclePhoto { slot: FuelSlot; fileId: string; fileName: string; contentType: string; savedAt: string | null } export interface VehicleDocuments { dispatchNo: string; recordVersion: string; editable: boolean; photos: VehiclePhoto[] } export interface BindVehiclePhoto { recordVersion: string; idempotencyKey: string; fileId: string } const path = (no: string) => `/platform/v1/last-leg/dispatches/${encodeURIComponent(no)}/vehicle-documents` export async function getVehicleDocuments(no: string) { return unwrapApiResponse(await http.get, ApiEnvelope>(path(no))) } export async function uploadVehiclePhoto(no: string, slot: FuelSlot, file: File, key: string) { const form = new FormData(); form.append('file', file); form.append('idempotencyKey', key) return unwrapApiResponse(await http.post, ApiEnvelope>(`${path(no)}/${slot}/uploads`, form)) } export async function recoverVehiclePhoto(no: string, key: string) { return unwrapApiResponse(await http.get, ApiEnvelope>(`${path(no)}/uploads/${encodeURIComponent(key)}`)) } export async function bindVehiclePhoto(no: string, slot: FuelSlot, body: BindVehiclePhoto) { return unwrapApiResponse(await http.put, ApiEnvelope>(`${path(no)}/${slot}`, body)) } export async function readVehiclePhoto(no: string, photo: VehiclePhoto) { 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) || !blob.size) throw new Error('图片读取失败,请重新核对') return blob }