gaoluyang
3 天以前 0333d66e4b397c161c6a44ce1e2a121c2cc41082
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
export function loadList(storageKey, defaultRows = []) {
  try {
    const raw = uni.getStorageSync(storageKey);
    if (!raw) {
      return defaultRows.map(row => ({ ...row }));
    }
    const parsed = typeof raw === "string" ? JSON.parse(raw) : raw;
    return Array.isArray(parsed)
      ? parsed.map(row => ({ ...row }))
      : defaultRows.map(row => ({ ...row }));
  } catch {
    return defaultRows.map(row => ({ ...row }));
  }
}
 
export function saveList(storageKey, rows) {
  uni.setStorageSync(storageKey, JSON.stringify(rows));
}
 
export function ensureList(storageKey, defaultRows) {
  const list = loadList(storageKey, defaultRows);
  if (!uni.getStorageSync(storageKey)) {
    saveList(storageKey, list);
  }
  return list;
}