wlzboy
4 天以前 06a17c236d4cb9b8da75fce43af938cb7ea510bf
app/utils/storage.js
@@ -26,6 +26,58 @@
  },
  clean: function() {
    uni.removeStorageSync(storageKey)
  },
  // 新增:获取存储信息
  getStorageInfo: function() {
    try {
      const info = uni.getStorageInfoSync()
      console.log('💾 存储信息:', {
        keys: info.keys,
        currentSize: info.currentSize,
        limitSize: info.limitSize,
        usage: ((info.currentSize / info.limitSize) * 100).toFixed(2) + '%'
      })
      return info
    } catch (e) {
      console.error('获取存储信息失败:', e)
      return null
    }
  },
  // 新增:清理所有存储(慎用)
  clearAll: function() {
    try {
      uni.clearStorageSync()
      console.log('✅ 清理所有存储成功')
    } catch (e) {
      console.error('清理存储失败:', e)
    }
  },
  // 新增:检查并清理过期数据
  checkAndClean: function() {
    try {
      const info = uni.getStorageInfoSync()
      const usagePercent = (info.currentSize / info.limitSize) * 100
      // 如果使用超过 80%,提示用户
      if (usagePercent > 80) {
        console.warn('⚠️ 存储空间使用超过 80%:', usagePercent.toFixed(2) + '%')
        // 自动清理非必要的缓存
        const keysToKeep = [storageKey, 'token', 'uni-id-pages-userInfo']
        info.keys.forEach(key => {
          if (!keysToKeep.includes(key)) {
            try {
              uni.removeStorageSync(key)
              console.log('🗑️ 清理缓存:', key)
            } catch (e) {
              console.error('清理失败:', key, e)
            }
          }
        })
      }
    } catch (e) {
      console.error('检查存储失败:', e)
    }
  }
}