fix: complete October 22 workflow feedback and request failure recovery
This commit is contained in:
@@ -4,8 +4,8 @@ const STORAGE_KEY = 'overseas-pda-fulfillment-draft-v2'
|
||||
|
||||
const blankState = () => ({
|
||||
scopeKey: '',
|
||||
dispatch: { currentNo: '', palletCode: '', dispatchItemId: '', sourceKey: '', cartonCode: '', packages: 1, pendingCommand: null },
|
||||
outbound: { currentTaskNo: '', scanCode: '', packages: 1, pendingKeys: {} }
|
||||
dispatch: { currentNo: '', palletCode: '', dispatchItemId: '', sourceKey: '', cartonCode: '', packages: 1 },
|
||||
outbound: { currentTaskNo: '', scanCode: '', packages: 1 }
|
||||
})
|
||||
|
||||
export const fulfillment = reactive(blankState())
|
||||
@@ -14,13 +14,7 @@ function storageAvailable() {
|
||||
return typeof uni !== 'undefined' && typeof uni.setStorageSync === 'function' && typeof uni.getStorageSync === 'function'
|
||||
}
|
||||
|
||||
function safeDomain(value, fallback) {
|
||||
return {
|
||||
...fallback,
|
||||
...(value || {}),
|
||||
...(Object.hasOwn(fallback, 'pendingKeys') ? { pendingKeys: { ...(value?.pendingKeys || {}) } } : {})
|
||||
}
|
||||
}
|
||||
function safeDomain(value, fallback) { return Object.fromEntries(Object.keys(fallback).map(key => [key, value?.[key] ?? fallback[key]])) }
|
||||
|
||||
export function persistFulfillmentDraft() {
|
||||
if (!storageAvailable() || !fulfillment.scopeKey) return
|
||||
@@ -55,49 +49,9 @@ function randomKey(domain, action) {
|
||||
return `pda-${domain}-${action}-${Date.now()}-${Math.random().toString(36).slice(2, 10)}`
|
||||
}
|
||||
|
||||
export function fulfillmentIdempotencyKey(domain, action, payload = {}) {
|
||||
if (!fulfillment[domain]) throw new Error('未知海外 PDA 履约类型')
|
||||
if (domain === 'dispatch') throw new Error('排车必须保存完整原命令,不能只生成请求键')
|
||||
const signature = `${action}:${JSON.stringify(payload)}`
|
||||
const pendingKeys = fulfillment[domain].pendingKeys
|
||||
if (!pendingKeys[signature]) pendingKeys[signature] = randomKey(domain, action)
|
||||
persistFulfillmentDraft()
|
||||
return { signature, value: pendingKeys[signature] }
|
||||
}
|
||||
|
||||
export function releaseFulfillmentKey(domain, signature) {
|
||||
if (!fulfillment[domain]) return
|
||||
if (domain === 'dispatch') throw new Error('排车原命令只能在服务端事实核对后清除')
|
||||
delete fulfillment[domain].pendingKeys[signature]
|
||||
persistFulfillmentDraft()
|
||||
}
|
||||
|
||||
export function beginDispatchCommand(dispatchNo, action, body) {
|
||||
if (!fulfillment.scopeKey) throw new Error('当前作业身份未恢复')
|
||||
if (fulfillment.dispatch.pendingCommand) throw new Error('请先核对原排车操作结果')
|
||||
const idempotencyKey = randomKey('dispatch', action)
|
||||
fulfillment.dispatch.pendingCommand = { dispatchNo, action, body: JSON.parse(JSON.stringify({ ...body, idempotencyKey })), attempted: false, rejected: false, result: null }
|
||||
persistFulfillmentDraft()
|
||||
return fulfillment.dispatch.pendingCommand
|
||||
}
|
||||
|
||||
export function patchDispatchCommand(patch) {
|
||||
if (!fulfillment.dispatch.pendingCommand) throw new Error('原排车请求不存在')
|
||||
Object.assign(fulfillment.dispatch.pendingCommand, patch)
|
||||
persistFulfillmentDraft()
|
||||
}
|
||||
|
||||
export function finishDispatchCommand(key) {
|
||||
if (fulfillment.dispatch.pendingCommand?.body.idempotencyKey !== key) throw new Error('核对结果与原请求键不一致')
|
||||
fulfillment.dispatch.pendingCommand = null
|
||||
persistFulfillmentDraft()
|
||||
}
|
||||
|
||||
export function discardRejectedDispatchCommand() {
|
||||
if (fulfillment.dispatch.pendingCommand?.rejected === true) {
|
||||
fulfillment.dispatch.pendingCommand = null
|
||||
persistFulfillmentDraft()
|
||||
}
|
||||
export function fulfillmentIdempotencyKey(domain, action, _payload = {}) {
|
||||
if (!fulfillment[domain]) throw new Error('未知海外 PDA 履约类型')
|
||||
return { value: randomKey(domain, action) }
|
||||
}
|
||||
|
||||
export function resetFulfillmentDomain(domain) {
|
||||
|
||||
Reference in New Issue
Block a user