Files
pda-uniapp/stores/picking.js
T
2026-08-27 14:53:44 +08:00

120 lines
4.1 KiB
JavaScript

import { createBoxes } from '@/data/demo-data.js'
import { createItemStates } from '@/utils/picking-state.js'
import { stableTaskIdentity, stableTaskIdentityKey } from '@/utils/task-identity.js'
function clone(value) {
return JSON.parse(JSON.stringify(value))
}
function createInitialState() {
const boxes = createBoxes()
return {
boxes,
currentBoxIndex: 0,
itemStates: createItemStates(boxes[0].items),
partialQuantities: {}, scannedBoxCodes: {}, boxDrafts: {}, completedBoxes: {}, scanValue: '', scanHistory: []
}
}
let state = createInitialState()
const STORAGE_PREFIX = 'pda-picking-draft'
const COMPLETION_PREFIX = 'pda-loading-completion'
function storageAvailable() {
return typeof uni !== 'undefined' && typeof uni.getStorageSync === 'function' && typeof uni.setStorageSync === 'function'
}
export function getPickingDraftKey(identity = {}) {
const identityKey = stableTaskIdentityKey(identity)
return identityKey ? `${STORAGE_PREFIX}:${identityKey}` : ''
}
function draftPayload(nextState, identity) {
const value = stableTaskIdentity(identity)
return {
operatorId: value.operatorId,
taskId: value.taskId,
loadingPlanId: value.loadingPlanId,
containerBookingId: value.containerBookingId,
warehouseId: identity.warehouseId ?? null,
currentBoxIndex: nextState.currentBoxIndex,
itemStates: nextState.itemStates,
partialQuantities: nextState.partialQuantities,
scannedBoxCodes: nextState.scannedBoxCodes,
boxDrafts: nextState.boxDrafts,
completedBoxes: nextState.completedBoxes,
scanHistory: nextState.scanHistory,
updatedAt: new Date().toISOString()
}
}
export function getPickingSession() {
return clone(state)
}
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)
const expected = stableTaskIdentity(identity)
if (!saved || saved.operatorId !== expected.operatorId || saved.taskId !== expected.taskId || saved.loadingPlanId !== expected.loadingPlanId || saved.containerBookingId !== expected.containerBookingId) return null
const restored = { ...state, ...clone(saved) }
delete restored.operatorId
delete restored.taskId
delete restored.loadingPlanId
delete restored.containerBookingId
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)
}
function completionKey(identity = {}) {
const identityKey = stableTaskIdentityKey(identity)
return identityKey ? `${COMPLETION_PREFIX}:${identityKey}` : ''
}
export function saveLoadingCompletionAttempt(identity = {}, attempt = {}) {
const key = completionKey(identity)
if (!key || !storageAvailable() || !attempt.operationId) return
uni.setStorageSync(key, {
operationId: String(attempt.operationId),
expectedTaskVersion: Number(attempt.expectedTaskVersion),
remark: attempt.remark || '',
updatedAt: new Date().toISOString()
})
}
export function restoreLoadingCompletionAttempt(identity = {}) {
const key = completionKey(identity)
if (!key || !storageAvailable()) return null
const saved = uni.getStorageSync(key)
return saved?.operationId && Number.isInteger(Number(saved.expectedTaskVersion)) ? clone(saved) : null
}
export function clearLoadingCompletionAttempt(identity = {}) {
const key = completionKey(identity)
if (key && typeof uni !== 'undefined' && typeof uni.removeStorageSync === 'function') uni.removeStorageSync(key)
}
export function resetPickingSession() {
state = createInitialState()
}
export function getPickingOverview() {
const completedCount = Object.values(state.completedBoxes).filter(Boolean).length
return { boxes: clone(state.boxes), completedBoxes: { ...state.completedBoxes }, completedCount, pendingBoxCount: state.boxes.length - completedCount }
}