wlzboy
4 天以前 06a17c236d4cb9b8da75fce43af938cb7ea510bf
app/App.vue
@@ -14,6 +14,7 @@
      }
    },
    onLaunch: function(options) {
      console.log('App onLaunch, options:', options);
      this.lastToken = getToken()
      this.initApp(options)
      
@@ -37,7 +38,11 @@
        this.stopMessagePolling()
        this.lastToken = null
        // 清除消息徽标
        try {
        uni.removeTabBarBadge({ index: 3 })
        } catch (e) {
          console.error('清除消息徽标失败:', e)
        }
      })
    },
    onShow: function() {
@@ -74,10 +79,14 @@
    methods: {
      // 初始化应用
      initApp(options) {
        try {
        // 初始化应用配置
        this.initConfig()
        // 检查用户登录状态并自动跳转到合适的登录页面
        this.checkLoginAndRedirect(options)
        } catch (e) {
          console.error('初始化应用失败:', e)
        }
        
        // 注意:不在应用启动时自动启动轮询
        // 只有在用户主动登录成功后才启动(通过 user-login 事件触发)
@@ -87,14 +96,28 @@
      },
      // 检查登录状态并自动跳转到合适的登录页面
      checkLoginAndRedirect(options) {
        try {
        if (!getToken()) {
            console.log('用户未登录,准备跳转到登录页面')
          // 使用工具类根据环境自动跳转到合适的登录页面
          redirectToLoginByEnvironment(options, this.$tab);
          } else {
            console.log('用户已登录,无需跳转')
          }
        } catch (e) {
          console.error('检查登录状态并跳转失败:', e)
          // fallback到普通登录页面
          try {
            this.$tab.reLaunch('/pages/login');
          } catch (fallbackError) {
            console.error('fallback跳转也失败了:', fallbackError);
          }
        }
      },
      
      // 判断当前是否在登录页面
      isLoginPage() {
        try {
        const pages = getCurrentPages()
        if (pages.length === 0) {
          return false
@@ -103,6 +126,10 @@
        const route = currentPage.route || ''
        // 判断是否为登录相关页面
        return route.includes('login') || route.includes('register')
        } catch (e) {
          console.error('判断是否为登录页面失败:', e)
          return false
        }
      },
      
      // 更新未读消息徽标
@@ -119,15 +146,23 @@
          
          if (count > 0) {
            // 设置徽标
            try {
            uni.setTabBarBadge({
              index: 3, // 消息页面在tabBar中的索引位置(0开始)
              text: count > 99 ? '99+' : count.toString()
            })
            } catch (e) {
              console.error('设置消息徽标失败:', e)
            }
          } else {
            // 移除徽标
            try {
            uni.removeTabBarBadge({
              index: 3
            })
            } catch (e) {
              console.error('移除消息徽标失败:', e)
            }
          }
        }).catch(error => {
          console.error('获取未读消息数量失败:', error)
@@ -136,6 +171,7 @@
      
      // 启动消息轮询
      startMessagePolling() {
        try {
        // 每30秒轮询一次
        this.messagePollingTimer = setInterval(() => {
          if (getToken()) {
@@ -145,14 +181,21 @@
            this.stopMessagePolling()
          }
        }, 30000) // 30秒
        } catch (e) {
          console.error('启动消息轮询失败:', e)
        }
      },
      
      // 停止消息轮询
      stopMessagePolling() {
        try {
        if (this.messagePollingTimer) {
          clearInterval(this.messagePollingTimer)
          this.messagePollingTimer = null
        }
        } catch (e) {
          console.error('停止消息轮询失败:', e)
        }
      },
      
      // 检查存储空间