feat: add phase 5 overseas PDA fulfillment
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
import { reactive } from 'vue'
|
||||
|
||||
const STORAGE_KEY = 'overseas-pda-fulfillment-draft-v1'
|
||||
|
||||
const blankState = () => ({
|
||||
scopeKey: '',
|
||||
dispatch: { currentNo: '', palletCode: '', packages: 1, pendingKeys: {} },
|
||||
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)
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import { reactive } from 'vue'
|
||||
import { OVERSEAS_API_ROOT } from '../config/runtime.js'
|
||||
import { resetReceivingSession } from './receiving.js'
|
||||
import { resetFulfillmentSession } from './fulfillment.js'
|
||||
|
||||
const SESSION_KEY = 'overseas-pda-auth-session'
|
||||
export const PDA_ROLE = 'OVERSEAS_WAREHOUSE_PDA_OPERATOR'
|
||||
@@ -179,6 +180,7 @@ export function signOut() {
|
||||
session.apiBase = OVERSEAS_API_ROOT
|
||||
if (typeof uni !== 'undefined' && typeof uni.removeStorageSync === 'function') uni.removeStorageSync(SESSION_KEY)
|
||||
resetReceivingSession()
|
||||
resetFulfillmentSession()
|
||||
}
|
||||
|
||||
export function requireSession() {
|
||||
|
||||
Reference in New Issue
Block a user