zhou zhou
4 小时以前 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { router } from '@/router'
import { useUserStore } from '@/store/modules/user'
import { StorageConfig } from '@/utils/storage/storage-config'
class StorageCompatibilityManager {
  /**
   * 获取系统版本号
   */
  getSystemVersion() {
    return localStorage.getItem(StorageConfig.VERSION_KEY)
  }
  /**
   * 获取系统存储数据(兼容旧格式)
   */
  getSystemStorage() {
    const version = this.getSystemVersion() || StorageConfig.CURRENT_VERSION
    const legacyKey = StorageConfig.generateLegacyKey(version)
    const data = localStorage.getItem(legacyKey)
    return data ? JSON.parse(data) : null
  }
  /**
   * 检查当前版本是否有存储数据
   */
  hasCurrentVersionStorage() {
    const storageKeys = Object.keys(localStorage)
    const currentVersionPattern = StorageConfig.createCurrentVersionPattern()
    return storageKeys.some(
      (key) => currentVersionPattern.test(key) && localStorage.getItem(key) !== null
    )
  }
  /**
   * 检查是否存在任何版本的存储数据
   */
  hasAnyVersionStorage() {
    const storageKeys = Object.keys(localStorage)
    const versionPattern = StorageConfig.createVersionPattern()
    return storageKeys.some((key) => versionPattern.test(key) && localStorage.getItem(key) !== null)
  }
  /**
   * 获取旧格式的本地存储数据
   */
  getLegacyStorageData() {
    try {
      const systemStorage = this.getSystemStorage()
      return systemStorage || {}
    } catch (error) {
      console.warn('[Storage] 解析旧格式存储数据失败:', error)
      return {}
    }
  }
  /**
   * 显示存储错误消息
   */
  showStorageError() {
    ElMessage({
      type: 'error',
      offset: 40,
      duration: 5e3,
      message: '系统检测到本地数据异常,请重新登录系统恢复使用!'
    })
  }
  /**
   * 执行系统登出
   */
  performSystemLogout() {
    setTimeout(() => {
      try {
        localStorage.clear()
        useUserStore().logOut()
        router.push({ name: 'Login' })
        console.info('[Storage] 已执行系统登出')
      } catch (error) {
        console.error('[Storage] 系统登出失败:', error)
      }
    }, StorageConfig.LOGOUT_DELAY)
  }
  /**
   * 处理存储异常
   */
  handleStorageError() {
    this.showStorageError()
    this.performSystemLogout()
  }
  /**
   * 验证存储数据完整性
   * @param requireAuth 是否需要验证登录状态(默认 false)
   */
  validateStorageData(requireAuth = false) {
    try {
      if (this.hasCurrentVersionStorage()) {
        return true
      }
      if (this.hasAnyVersionStorage()) {
        return true
      }
      const legacyData = this.getLegacyStorageData()
      if (Object.keys(legacyData).length === 0) {
        if (requireAuth) {
          console.warn('[Storage] 未发现任何存储数据,需要重新登录')
          this.performSystemLogout()
          return false
        }
        return true
      }
      console.debug('[Storage] 发现旧版本存储数据')
      return true
    } catch (error) {
      console.error('[Storage] 存储数据验证失败:', error)
      if (requireAuth) {
        this.handleStorageError()
        return false
      }
      return true
    }
  }
  /**
   * 检查存储是否为空
   */
  isStorageEmpty() {
    if (this.hasCurrentVersionStorage()) {
      return false
    }
    if (this.hasAnyVersionStorage()) {
      return false
    }
    const legacyData = this.getLegacyStorageData()
    return Object.keys(legacyData).length === 0
  }
  /**
   * 检查存储兼容性
   * @param requireAuth 是否需要验证登录状态(默认 false)
   */
  checkCompatibility(requireAuth = false) {
    try {
      const isValid = this.validateStorageData(requireAuth)
      const isEmpty = this.isStorageEmpty()
      if (isValid || isEmpty) {
        return true
      }
      console.warn('[Storage] 存储兼容性检查失败')
      return false
    } catch (error) {
      console.error('[Storage] 兼容性检查异常:', error)
      return false
    }
  }
}
const storageManager = new StorageCompatibilityManager()
function getSystemStorage() {
  return storageManager.getSystemStorage()
}
function getSysVersion() {
  return storageManager.getSystemVersion()
}
function validateStorageData(requireAuth = false) {
  return storageManager.validateStorageData(requireAuth)
}
function checkStorageCompatibility(requireAuth = false) {
  return storageManager.checkCompatibility(requireAuth)
}
export { checkStorageCompatibility, getSysVersion, getSystemStorage, validateStorageData }