2026-09-01 14:23:55 +08:00
|
|
|
import { reactive } from 'vue'
|
|
|
|
|
|
|
|
|
|
const STORAGE_KEY = 'overseas-pda-fulfillment-draft-v1'
|
|
|
|
|
|
|
|
|
|
const blankState = () => ({
|
|
|
|
|
scopeKey: '',
|
2026-10-08 15:34:47 +08:00
|
|
|
dispatch: { currentNo: '', palletCode: '', dispatchItemId: '', cartonCode: '', packages: 1, pendingKeys: {} },
|
2026-09-01 14:23:55 +08:00
|
|
|
outbound: { currentTaskNo: '', scanCode: '', packages: 1, pendingKeys: {} }
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
export const fulfillment = reactive(blankState())
|
|
|
|
|
|
|
|
|
|
function storageAvailable() {
|
|
|
|
|
return typeof uni !== 'undefined' && typeof uni.setStorageSync === 'function' && typeof uni.getStorageSync === 'function'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function safeDomain(value, fallback) {
|
|
|
|
|
return {
|
|
|
|
|
...fallback,
|
|
|
|
|
...(value || {}),
|
|
|
|
|
pendingKeys: { ...(value?.pendingKeys || {}) }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function persistFulfillmentDraft() {
|
|
|
|
|
if (!storageAvailable() || !fulfillment.scopeKey) return
|
|
|
|
|
uni.setStorageSync(STORAGE_KEY, JSON.parse(JSON.stringify(fulfillment)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function restoreFulfillmentDraft(scopeKey) {
|
|
|
|
|
const expected = String(scopeKey || '')
|
|
|
|
|
if (!expected) return false
|
|
|
|
|
const saved = storageAvailable() ? uni.getStorageSync(STORAGE_KEY) : null
|
|
|
|
|
if (!saved || saved.scopeKey !== expected) {
|
|
|
|
|
Object.assign(fulfillment, blankState(), { scopeKey: expected })
|
|
|
|
|
if (storageAvailable()) uni.removeStorageSync(STORAGE_KEY)
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
const fresh = blankState()
|
|
|
|
|
Object.assign(fulfillment, fresh, {
|
|
|
|
|
scopeKey: expected,
|
|
|
|
|
dispatch: safeDomain(saved.dispatch, fresh.dispatch),
|
|
|
|
|
outbound: safeDomain(saved.outbound, fresh.outbound)
|
|
|
|
|
})
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function updateFulfillmentDraft(domain, patch) {
|
|
|
|
|
if (!fulfillment[domain]) throw new Error('未知海外 PDA 履约草稿')
|
|
|
|
|
Object.assign(fulfillment[domain], patch)
|
|
|
|
|
persistFulfillmentDraft()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 履约类型')
|
|
|
|
|
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
|
|
|
|
|
delete fulfillment[domain].pendingKeys[signature]
|
|
|
|
|
persistFulfillmentDraft()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function resetFulfillmentDomain(domain) {
|
|
|
|
|
if (!fulfillment[domain]) return
|
|
|
|
|
fulfillment[domain] = blankState()[domain]
|
|
|
|
|
persistFulfillmentDraft()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function resetFulfillmentSession() {
|
|
|
|
|
Object.assign(fulfillment, blankState())
|
|
|
|
|
if (typeof uni !== 'undefined' && typeof uni.removeStorageSync === 'function') uni.removeStorageSync(STORAGE_KEY)
|
|
|
|
|
}
|