27 lines
1.9 KiB
JavaScript
27 lines
1.9 KiB
JavaScript
import { overseasRequest, overseasUpload, overseasDownload } from './overseas.js'
|
|||
|
|
|
||
|
|
export const loadingPhotoSlots = ['LOADING_SCENE', 'LOADING_LIST', 'PRE_DEPARTURE_CHECK', 'DRIVER_LICENSE', 'RECEIPT']
|
||
|
|
const path = no => {
|
||
|
|
if (!no || typeof no !== 'string') throw new Error('请先打开排车任务')
|
||
|
|
return `/pda/v1/last-leg/dispatches/${encodeURIComponent(no)}/loading-documents`
|
||
|
|
}
|
||
|
|
function slotPath(no, slot) {
|
||
|
|
if (!loadingPhotoSlots.includes(slot)) throw new Error('装车照片框无效')
|
||
|
|
return `${path(no)}/${slot}`
|
||
|
|
}
|
||
|
|
export function normalizeLoadingDocuments(value, no) {
|
||
|
|
if (value?.dispatchNo !== no || !/^\d+$/.test(String(value.recordVersion ?? '')) || !Array.isArray(value.photos)
|
||
|
|
|| !['AFTER_LOADING', 'DELIVERING', 'DELIVERED'].includes(value.stage)) throw new Error('装车资料与当前排车事实不一致')
|
||
|
|
const slots = new Set()
|
||
|
|
for (const photo of value.photos) {
|
||
|
|
if (!loadingPhotoSlots.includes(photo.slot) || !photo.fileId || slots.has(photo.slot)) throw new Error('装车照片引用不完整或重复')
|
||
|
|
slots.add(photo.slot)
|
||
|
|
}
|
||
|
|
return { ...value, editable: value.editable === true, complete: value.complete === true }
|
||
|
|
}
|
||
|
|
export async function getLoadingDocuments(no) { return normalizeLoadingDocuments(await overseasRequest('GET', path(no)), no) }
|
||
|
|
export function uploadLoadingPhoto(no, slot, filePath, key) { return overseasUpload(`${slotPath(no, slot)}/uploads?idempotencyKey=${encodeURIComponent(key)}`, filePath) }
|
||
|
|
export function recoverLoadingPhoto(no, key) { return overseasRequest('GET', `${path(no)}/uploads/${encodeURIComponent(key)}`) }
|
||
|
|
export async function bindLoadingPhoto(no, slot, body) { return normalizeLoadingDocuments(await overseasRequest('PUT', slotPath(no, slot), body), no) }
|
||
|
|
export function readLoadingPhoto(no, photo) { return overseasDownload(`${slotPath(no, photo.slot)}/files/${encodeURIComponent(photo.fileId)}/content`, { expectedContentTypePrefix: 'image/' }) }
|