13404089107
2026-04-20 03ea6417827d2dab4f7dd33626d084ff28a3de19
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
27
28
29
30
31
32
33
34
35
36
37
38
/*
 * @Author: weisheng
 * @Date: 2025-06-23 22:23:05
 * @LastEditTime: 2025-06-24 19:03:21
 * @LastEditors: weisheng
 * @Description:
 * @FilePath: /wot-starter/src/store/persist.ts
 * 记得注释
 */
import type { PiniaPluginContext } from 'pinia'
 
function persist({ store }: PiniaPluginContext, excludedIds: string[]) {
  // 检查当前store的id是否在排除列表中
  const isExcluded = excludedIds.includes(store.$id)
 
  // 如果当前store的id在排除列表中,则不进行持久化
  if (isExcluded) {
    return
  }
 
  // 暂存State
  let persistState = CommonUtil.deepClone(store.$state)
  // 从缓存中读取
  const storageState = uni.getStorageSync(store.$id)
  if (storageState) {
    persistState = storageState
  }
  store.$state = persistState
  store.$subscribe(() => {
    // 在存储变化的时候将store缓存
    uni.setStorageSync(store.$id, CommonUtil.deepClone(store.$state))
  })
}
 
export function persistPlugin(context: PiniaPluginContext) {
  // 调用persist函数,并传入排除列表
  persist(context, ['temp'])
}