wlzboy
5 天以前 3bbd80a63ac7728ac01b641a48a26befcb171a0f
app/App.vue
@@ -3,6 +3,8 @@
  import store from '@/store'
  import { getToken } from '@/utils/auth'
  import { getUnreadCount } from '@/api/message'
  import storage from '@/utils/storage'
  import { redirectToLoginByEnvironment } from '@/utils/wechat'
  export default {
    data() {
@@ -11,9 +13,12 @@
        lastToken: null // 用于检测 token 变化
      }
    },
    onLaunch: function() {
    onLaunch: function(options) {
      this.lastToken = getToken()
      this.initApp()
      this.initApp(options)
      // 检查并清理存储空间
      this.checkStorage()
      
      // 监听用户登录事件
      uni.$on('user-login', () => {
@@ -68,13 +73,11 @@
    },
    methods: {
      // 初始化应用
      initApp() {
      initApp(options) {
        // 初始化应用配置
        this.initConfig()
        // 检查用户登录状态
        //#ifdef H5
        this.checkLogin()
        //#endif
        // 检查用户登录状态并自动跳转到合适的登录页面
        this.checkLoginAndRedirect(options)
        
        // 注意:不在应用启动时自动启动轮询
        // 只有在用户主动登录成功后才启动(通过 user-login 事件触发)
@@ -82,9 +85,11 @@
      initConfig() {
        this.globalData.config = config
      },
      checkLogin() {
      // 检查登录状态并自动跳转到合适的登录页面
      checkLoginAndRedirect(options) {
        if (!getToken()) {
          this.$tab.reLaunch('/pages/login')
          // 使用工具类根据环境自动跳转到合适的登录页面
          redirectToLoginByEnvironment(options, this.$tab);
        }
      },
      
@@ -148,6 +153,34 @@
          clearInterval(this.messagePollingTimer)
          this.messagePollingTimer = null
        }
      },
      // 检查存储空间
      checkStorage() {
        try {
          // 获取存储信息
          const info = storage.getStorageInfo()
          if (info) {
            const usagePercent = (info.currentSize / info.limitSize) * 100
            console.log(`💾 存储使用情况: ${usagePercent.toFixed(2)}% (${info.currentSize}KB / ${info.limitSize}KB)`)
            // 如果使用超过 80%,自动清理
            if (usagePercent > 80) {
              console.warn('⚠️ 存储空间使用超过 80%,开始自动清理...')
              storage.checkAndClean()
              // 再次检查
              const newInfo = storage.getStorageInfo()
              if (newInfo) {
                const newUsagePercent = (newInfo.currentSize / newInfo.limitSize) * 100
                console.log(`✅ 清理后使用情况: ${newUsagePercent.toFixed(2)}%`)
              }
            }
          }
        } catch (e) {
          console.error('检查存储失败:', e)
        }
      }
    }
  }
@@ -155,4 +188,4 @@
<style lang="scss">
  @import '@/static/scss/index.scss'
</style>
</style>