48 lines
1.0 KiB
JavaScript
48 lines
1.0 KiB
JavaScript
const STORAGE_KEY = 'zy_orders_v1'
|
|
|
|
function readAll() {
|
|
return uni.getStorageSync(STORAGE_KEY) || []
|
|
}
|
|
|
|
function writeAll(list) {
|
|
uni.setStorageSync(STORAGE_KEY, list)
|
|
}
|
|
|
|
export function listOrders() {
|
|
return readAll().sort((a, b) => (b.createdAt || 0) - (a.createdAt || 0))
|
|
}
|
|
|
|
export function getOrderById(id) {
|
|
return readAll().find((x) => x.id === id) || null
|
|
}
|
|
|
|
export function upsertOrder(order) {
|
|
const list = readAll()
|
|
const idx = list.findIndex((x) => x.id === order.id)
|
|
if (idx >= 0) list[idx] = order
|
|
else list.unshift(order)
|
|
writeAll(list)
|
|
return order
|
|
}
|
|
|
|
export function patchOrder(id, patch) {
|
|
const list = readAll()
|
|
const idx = list.findIndex((x) => x.id === id)
|
|
if (idx < 0) return null
|
|
list[idx] = { ...list[idx], ...patch }
|
|
writeAll(list)
|
|
return list[idx]
|
|
}
|
|
|
|
export function ensureSeedOrders() {
|
|
const list = readAll()
|
|
if (list.length) return
|
|
writeAll([])
|
|
}
|
|
|
|
export function createId(prefix) {
|
|
const s = `${Date.now()}${Math.floor(Math.random() * 1000)}`
|
|
return `${prefix}_${s}`
|
|
}
|
|
|