75 lines
2.9 KiB
JavaScript
75 lines
2.9 KiB
JavaScript
import { reactive } from 'vue'
|
|||
|
|
|
||
|
|
const STORAGE_KEY = 'overseas-pda-warehouse-work-v1'
|
||
|
|
const blankState = () => ({
|
||
|
|
scopeKey: '',
|
||
|
|
stocktake: { taskNo: '', scopeType: 'LOCATION', scopeCode: '', reason: '', pendingKeys: {} },
|
||
|
|
move: { sourcePalletCode: '', targetLocationCode: '', reason: '', pendingKeys: {} },
|
||
|
|
relabel: { currentNo: '', oldLabel: '', newLabel: '', action: 'RELABEL', reason: '', remark: '', photoFileIds: [], photoNames: [], pendingKeys: {} },
|
||
|
|
query: { inventoryCode: '', locationCode: '', palletCode: '' }
|
||
|
|
})
|
||
|
|
|
||
|
|
export const warehouseWork = reactive(blankState())
|
||
|
|
|
||
|
|
function storageAvailable() {
|
||
|
|
return typeof uni !== 'undefined' && typeof uni.setStorageSync === 'function' && typeof uni.getStorageSync === 'function'
|
||
|
|
}
|
||
|
|
|
||
|
|
function domain(value, fallback) { return { ...fallback, ...(value || {}), pendingKeys: { ...(value?.pendingKeys || {}) } } }
|
||
|
|
|
||
|
|
export function persistWarehouseWork() {
|
||
|
|
if (!storageAvailable() || !warehouseWork.scopeKey) return
|
||
|
|
uni.setStorageSync(STORAGE_KEY, JSON.parse(JSON.stringify(warehouseWork)))
|
||
|
|
}
|
||
|
|
|
||
|
|
export function restoreWarehouseWork(scopeKey) {
|
||
|
|
const expected = String(scopeKey || '')
|
||
|
|
if (!expected) return false
|
||
|
|
const saved = storageAvailable() ? uni.getStorageSync(STORAGE_KEY) : null
|
||
|
|
const fresh = blankState()
|
||
|
|
if (!saved || saved.scopeKey !== expected) {
|
||
|
|
Object.assign(warehouseWork, fresh, { scopeKey: expected })
|
||
|
|
if (storageAvailable()) uni.removeStorageSync(STORAGE_KEY)
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
Object.assign(warehouseWork, fresh, {
|
||
|
|
scopeKey: expected,
|
||
|
|
stocktake: domain(saved.stocktake, fresh.stocktake), move: domain(saved.move, fresh.move),
|
||
|
|
relabel: domain(saved.relabel, fresh.relabel), query: { ...fresh.query, ...(saved.query || {}) }
|
||
|
|
})
|
||
|
|
return true
|
||
|
|
}
|
||
|
|
|
||
|
|
export function updateWarehouseWork(name, patch) {
|
||
|
|
if (!warehouseWork[name]) throw new Error('未知海外仓现场作业')
|
||
|
|
Object.assign(warehouseWork[name], patch)
|
||
|
|
persistWarehouseWork()
|
||
|
|
}
|
||
|
|
|
||
|
|
export function warehouseIdempotencyKey(name, action, payload = {}) {
|
||
|
|
const target = warehouseWork[name]
|
||
|
|
if (!target?.pendingKeys) throw new Error('当前作业不支持写操作')
|
||
|
|
const signature = `${action}:${JSON.stringify(payload)}`
|
||
|
|
if (!target.pendingKeys[signature]) target.pendingKeys[signature] = `pda-${name}-${action}-${Date.now()}-${Math.random().toString(36).slice(2, 10)}`
|
||
|
|
persistWarehouseWork()
|
||
|
|
return { signature, value: target.pendingKeys[signature] }
|
||
|
|
}
|
||
|
|
|
||
|
|
export function releaseWarehouseKey(name, signature) {
|
||
|
|
if (!warehouseWork[name]?.pendingKeys) return
|
||
|
|
delete warehouseWork[name].pendingKeys[signature]
|
||
|
|
persistWarehouseWork()
|
||
|
|
}
|
||
|
|
|
||
|
|
export function resetWarehouseDomain(name) {
|
||
|
|
const fresh = blankState()
|
||
|
|
if (!fresh[name]) return
|
||
|
|
warehouseWork[name] = fresh[name]
|
||
|
|
persistWarehouseWork()
|
||
|
|
}
|
||
|
|
|
||
|
|
export function resetWarehouseWork() {
|
||
|
|
Object.assign(warehouseWork, blankState())
|
||
|
|
if (typeof uni !== 'undefined' && typeof uni.removeStorageSync === 'function') uni.removeStorageSync(STORAGE_KEY)
|
||
|
|
}
|