feat(pda): persist and batch cabinet picking
This commit is contained in:
+59
-1
@@ -17,12 +17,70 @@ function createInitialState() {
|
||||
|
||||
let state = createInitialState()
|
||||
|
||||
const STORAGE_PREFIX = 'pda-picking-draft'
|
||||
|
||||
function storageAvailable() {
|
||||
return typeof uni !== 'undefined' && typeof uni.getStorageSync === 'function' && typeof uni.setStorageSync === 'function'
|
||||
}
|
||||
|
||||
function identityValue(identity = {}) {
|
||||
const operatorId = identity.operatorId ?? identity.accountId ?? identity.userId
|
||||
const taskId = identity.taskId ?? identity.inboundId
|
||||
const containerNo = identity.containerNo ?? identity.boxNo
|
||||
return { operatorId: operatorId == null ? '' : String(operatorId), taskId: taskId == null ? '' : String(taskId), containerNo: containerNo == null ? '' : String(containerNo) }
|
||||
}
|
||||
|
||||
export function getPickingDraftKey(identity = {}) {
|
||||
const value = identityValue(identity)
|
||||
if (!value.operatorId || !value.taskId || !value.containerNo) return ''
|
||||
return `${STORAGE_PREFIX}:${encodeURIComponent(value.operatorId)}:${encodeURIComponent(value.taskId)}:${encodeURIComponent(value.containerNo)}`
|
||||
}
|
||||
|
||||
function draftPayload(nextState, identity) {
|
||||
const value = identityValue(identity)
|
||||
return {
|
||||
operatorId: value.operatorId,
|
||||
taskId: value.taskId,
|
||||
containerNo: value.containerNo,
|
||||
warehouseId: identity.warehouseId ?? null,
|
||||
currentBoxIndex: nextState.currentBoxIndex,
|
||||
itemStates: nextState.itemStates,
|
||||
partialQuantities: nextState.partialQuantities,
|
||||
boxDrafts: nextState.boxDrafts,
|
||||
completedBoxes: nextState.completedBoxes,
|
||||
scanHistory: nextState.scanHistory,
|
||||
updatedAt: new Date().toISOString()
|
||||
}
|
||||
}
|
||||
|
||||
export function getPickingSession() {
|
||||
return clone(state)
|
||||
}
|
||||
|
||||
export function savePickingSession(nextState) {
|
||||
export function savePickingSession(nextState, identity = {}) {
|
||||
state = clone(nextState)
|
||||
const key = getPickingDraftKey(identity)
|
||||
if (key && storageAvailable()) uni.setStorageSync(key, draftPayload(state, identity))
|
||||
}
|
||||
|
||||
export function restorePickingSession(identity = {}) {
|
||||
const key = getPickingDraftKey(identity)
|
||||
if (!key || !storageAvailable()) return null
|
||||
const saved = uni.getStorageSync(key)
|
||||
if (!saved || saved.operatorId !== identityValue(identity).operatorId || saved.taskId !== identityValue(identity).taskId || saved.containerNo !== identityValue(identity).containerNo) return null
|
||||
const restored = { ...state, ...clone(saved) }
|
||||
delete restored.operatorId
|
||||
delete restored.taskId
|
||||
delete restored.containerNo
|
||||
delete restored.warehouseId
|
||||
delete restored.updatedAt
|
||||
state = restored
|
||||
return clone(state)
|
||||
}
|
||||
|
||||
export function clearPickingDraft(identity = {}) {
|
||||
const key = getPickingDraftKey(identity)
|
||||
if (key && typeof uni !== 'undefined' && typeof uni.removeStorageSync === 'function') uni.removeStorageSync(key)
|
||||
}
|
||||
|
||||
export function resetPickingSession() {
|
||||
|
||||
Reference in New Issue
Block a user