<template>
  <div id="app">
    <!-- <template v-if="isLogin">
      <router-view />
    </template>
    <template v-else>
      <Login />
    </template> -->
    <router-view />
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { removeToken } from '@/utils/auth'
// import Login from '@/views/login/index.vue'
export default {
  name: 'App',
  components: {
    // Login
  },
  data() {
    return {
      beforeUnloadTime: '',
      gapTime: ''
    }
  },
  computed: {
    ...mapState('user', {
      token: state => state.token
    }),
    isLogin() {
      return Boolean(this.token)
    }
  },
  mounted() {
    window.addEventListener('beforeunload', e => {
      const { path } = this.$route
      if (!path.includes('/perview')) {
        this.beforeUnloadTime = new Date().getTime()
      }
    })
    window.addEventListener('unload', async() => {
      const { path } = this.$route
      if (!path.includes('/perview')) {
        this.gapTime = new Date().getTime() - this.beforeUnloadTime
        if (this.gapTime <= 5) {
        // 关闭
        // 清除 cookie 或者 localStorage 的方法
          removeToken()
        } else {
        // 刷新
        }
      }
    })
  },
  destroyed() {
    window.removeEventListener('beforeunload')
    window.removeEventListener('unload')
  }
}
</script>
