优化
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
import { reactive } from 'vue'
|
||||
|
||||
export const receiving = reactive({
|
||||
candidates: [], selected: null, current: null, photoSlots: [null, null, null],
|
||||
selectedInboundCode: '', resolvedCarton: null, resolvedSku: null, resolvedPallet: null,
|
||||
activeScanTarget: 'container', lastReceipt: null
|
||||
})
|
||||
|
||||
const idempotencyKeys = new Map()
|
||||
|
||||
export function receivingPhase(session = receiving.current) {
|
||||
if (!session) return 'candidates'
|
||||
if (session.status === 'WAIT_SIGN') return 'sign'
|
||||
if (session.status === 'SIGNED' || session.status === 'RECEIVING') return 'receive'
|
||||
if (session.status === 'COMPLETED') return 'completed'
|
||||
return 'candidates'
|
||||
}
|
||||
|
||||
export function receivingProgress(session = receiving.current) {
|
||||
const total = Math.max(0, Number(session?.totalPackages || 0))
|
||||
const received = Math.max(0, Number(session?.receivedPackages || 0))
|
||||
return { total, received, pending: Math.max(0, Number(session?.pendingPackages ?? total - received)), percent: total ? Math.min(100, Math.round(received / total * 100)) : 0 }
|
||||
}
|
||||
|
||||
export function selectedInbound(session = receiving.current) {
|
||||
return (session?.inbounds || []).find((item) => item.inboundCode === receiving.selectedInboundCode) || null
|
||||
}
|
||||
|
||||
export function setCurrentReceiving(value) {
|
||||
receiving.current = value || null
|
||||
receiving.selected = value || null
|
||||
const chosen = selectedInbound(value)
|
||||
if (!chosen || Number(chosen.pendingPackages || 0) <= 0) {
|
||||
receiving.selectedInboundCode = (value?.inbounds || []).find((item) => Number(item.pendingPackages || 0) > 0)?.inboundCode || value?.inbounds?.[0]?.inboundCode || ''
|
||||
}
|
||||
if (value?.photos?.length) {
|
||||
const roles = ['LICENSE_PLATE', 'CONTAINER_SEAL', 'DOOR_OPEN']
|
||||
receiving.photoSlots = roles.map((role) => {
|
||||
const photo = value.photos.find((item) => item.role === role)
|
||||
return photo ? { ...photo, fileId: String(photo.fileId), role, uploaded: true } : null
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export function resetReceivingDraft({ keepCurrent = false } = {}) {
|
||||
receiving.selected = keepCurrent ? receiving.current : null
|
||||
if (!keepCurrent) receiving.current = null
|
||||
receiving.photoSlots = [null, null, null]
|
||||
receiving.selectedInboundCode = ''
|
||||
receiving.resolvedCarton = null
|
||||
receiving.resolvedSku = null
|
||||
receiving.resolvedPallet = null
|
||||
receiving.activeScanTarget = keepCurrent ? 'pallet' : 'container'
|
||||
receiving.lastReceipt = null
|
||||
idempotencyKeys.clear()
|
||||
}
|
||||
|
||||
export function idempotencyKey(action, payload = {}) {
|
||||
const signature = `${action}:${JSON.stringify(payload)}`
|
||||
if (!idempotencyKeys.has(signature)) idempotencyKeys.set(signature, `pda-${Date.now()}-${Math.random().toString(16).slice(2)}`)
|
||||
return { signature, value: idempotencyKeys.get(signature) }
|
||||
}
|
||||
|
||||
export function releaseIdempotencyKey(signature) { idempotencyKeys.delete(signature) }
|
||||
Reference in New Issue
Block a user