wlzboy
5 天以前 7de1396e315896dbc72a9d54e44f77434ea90f18
app/App.vue
@@ -12,9 +12,9 @@
        lastToken: null // 用于检测 token 变化
      }
    },
    onLaunch: function() {
    onLaunch: function(options) {
      this.lastToken = getToken()
      this.initApp()
      this.initApp(options)
      
      // 检查并清理存储空间
      this.checkStorage()
@@ -72,13 +72,11 @@
    },
    methods: {
      // 初始化应用
      initApp() {
      initApp(options) {
        // 初始化应用配置
        this.initConfig()
        // 检查用户登录状态
        //#ifdef H5
        this.checkLogin()
        //#endif
        // 检查用户登录状态并自动跳转到合适的登录页面
        this.checkLoginAndRedirect(options)
        
        // 注意:不在应用启动时自动启动轮询
        // 只有在用户主动登录成功后才启动(通过 user-login 事件触发)
@@ -86,9 +84,62 @@
      initConfig() {
        this.globalData.config = config
      },
      checkLogin() {
      // 检查登录状态并自动跳转到合适的登录页面
      checkLoginAndRedirect(options) {
        if (!getToken()) {
          this.$tab.reLaunch('/pages/login')
          // 检查运行环境并跳转到对应的登录页面
          // #ifdef MP-WEIXIN
          // 在微信小程序环境中
          try {
            // 获取系统信息
            const systemInfo = uni.getSystemInfoSync()
            console.log('系统信息:', systemInfo)
            // 检查environment字段是否为wxwork
            if (systemInfo.environment === 'wxwork') {
              console.log('检测到企业微信环境,跳转到企业微信免登页面')
              // 构造带参数的URL
              let url = '/pages/qylogin'
              if (options && options.query) {
                const queryParams = Object.keys(options.query).map(key => `${key}=${encodeURIComponent(options.query[key])}`).join('&')
                if (queryParams) {
                  url += '?' + queryParams
                }
              }
              this.$tab.reLaunch(url)
              return
            } else {
              console.log('检测到普通微信环境,跳转到微信登录页面')
              // 构造带参数的URL
              let url = '/pages/login'
              if (options && options.query) {
                const queryParams = Object.keys(options.query).map(key => `${key}=${encodeURIComponent(options.query[key])}`).join('&')
                if (queryParams) {
                  url += '?' + queryParams
                }
              }
              this.$tab.reLaunch(url)
              return
            }
          } catch (e) {
            console.error('获取系统信息失败:', e)
            // 默认跳转到普通登录页面
            this.$tab.reLaunch('/pages/login')
          }
          // #endif
          // #ifndef MP-WEIXIN
          // 非微信小程序环境,跳转到普通登录页面
          console.log('非微信小程序环境,跳转到普通登录页面')
          let url = '/pages/login'
          if (options && options.query) {
            const queryParams = Object.keys(options.query).map(key => `${key}=${encodeURIComponent(options.query[key])}`).join('&')
            if (queryParams) {
              url += '?' + queryParams
            }
          }
          this.$tab.reLaunch(url)
          // #endif
        }
      },
      
@@ -187,4 +238,4 @@
<style lang="scss">
  @import '@/static/scss/index.scss'
</style>
</style>