Files
pda-uniapp/stores/dropship-palletizing.js

37 lines
1.5 KiB
JavaScript

const STORAGE_PREFIX = 'pda-dropship-palletizing-draft'
function clone(value) {
return JSON.parse(JSON.stringify(value))
}
function text(value) {
return value == null ? '' : String(value)
}
export function getDropshipPalletizingDraftKey(identity = {}) {
const parts = [text(identity.operatorId), text(identity.taskId), text(identity.warehouseId)]
return parts.every(Boolean) ? `${STORAGE_PREFIX}:${parts.map(encodeURIComponent).join(':')}` : ''
}
export function saveDropshipPalletizingDraft(identity, state) {
const key = getDropshipPalletizingDraftKey(identity)
if (!key || typeof uni === 'undefined' || typeof uni.setStorageSync !== 'function') return false
uni.setStorageSync(key, { identity: { ...identity }, state: clone(state), updatedAt: new Date().toISOString() })
return true
}
export function restoreDropshipPalletizingDraft(identity) {
const key = getDropshipPalletizingDraftKey(identity)
if (!key || typeof uni === 'undefined' || typeof uni.getStorageSync !== 'function') return null
const saved = uni.getStorageSync(key)
if (!saved?.state || text(saved.identity?.operatorId) !== text(identity.operatorId) || text(saved.identity?.taskId) !== text(identity.taskId) || text(saved.identity?.warehouseId) !== text(identity.warehouseId)) return null
return clone(saved.state)
}
export function clearDropshipPalletizingDraft(identity) {
const key = getDropshipPalletizingDraftKey(identity)
if (!key || typeof uni === 'undefined' || typeof uni.removeStorageSync !== 'function') return false
uni.removeStorageSync(key)
return true
}