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 }