zhou zhou
15 小时以前 fec285d150b377d004e47f0973d298b92fe4c711
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { StorageConfig } from '@/utils/storage'
class StorageKeyManager {
  /**
   * 获取当前版本的存储键名
   */
  getCurrentVersionKey(storeId) {
    return StorageConfig.generateStorageKey(storeId)
  }
  /**
   * 检查当前版本的数据是否存在
   */
  hasCurrentVersionData(key) {
    return localStorage.getItem(key) !== null
  }
  /**
   * 查找其他版本的同名存储键
   */
  findExistingKey(storeId) {
    const storageKeys = Object.keys(localStorage)
    const pattern = StorageConfig.createKeyPattern(storeId)
    return storageKeys.find((key) => pattern.test(key) && localStorage.getItem(key)) || null
  }
  /**
   * 将数据从旧版本迁移到当前版本
   */
  migrateData(fromKey, toKey) {
    try {
      const existingData = localStorage.getItem(fromKey)
      if (existingData) {
        localStorage.setItem(toKey, existingData)
        console.info(`[Storage] 已迁移数据: ${fromKey} → ${toKey}`)
      }
    } catch (error) {
      console.warn(`[Storage] 数据迁移失败: ${fromKey}`, error)
    }
  }
  /**
   * 获取持久化存储的键名(支持自动数据迁移)
   */
  getStorageKey(storeId) {
    const currentKey = this.getCurrentVersionKey(storeId)
    if (this.hasCurrentVersionData(currentKey)) {
      return currentKey
    }
    const existingKey = this.findExistingKey(storeId)
    if (existingKey) {
      this.migrateData(existingKey, currentKey)
    }
    return currentKey
  }
}
export { StorageKeyManager }