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 interface LoadingPhoto { slot: LoadingPhotoSlot; fileId: string; fileName: string; contentType: string; savedAt: string | null } export interface LoadingDocuments { dispatchNo: string; recordVersion: string; dispatchRecordVersion: string; stage: DispatchStage; editable: boolean; complete: boolean; photos: LoadingPhoto[] } export interface BindLoadingPhoto { recordVersion: string; idempotencyKey: string; fileId: 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) { 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 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) || !blob.size) throw new Error('图片读取失败,请重新核对') return blob }