App.vue 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <template>
  2. <div id="app">
  3. <!-- <template v-if="isLogin">
  4. <router-view />
  5. </template>
  6. <template v-else>
  7. <Login />
  8. </template> -->
  9. <router-view />
  10. </div>
  11. </template>
  12. <script>
  13. import { mapState } from 'vuex'
  14. import { removeToken } from '@/utils/auth'
  15. // import Login from '@/views/login/index.vue'
  16. export default {
  17. name: 'App',
  18. components: {
  19. // Login
  20. },
  21. data() {
  22. return {
  23. beforeUnloadTime: '',
  24. gapTime: ''
  25. }
  26. },
  27. computed: {
  28. ...mapState('user', {
  29. token: state => state.token
  30. }),
  31. isLogin() {
  32. return Boolean(this.token)
  33. }
  34. },
  35. mounted() {
  36. window.addEventListener('beforeunload', e => {
  37. const { path } = this.$route
  38. if (!path.includes('/perview')) {
  39. this.beforeUnloadTime = new Date().getTime()
  40. }
  41. })
  42. window.addEventListener('unload', async() => {
  43. const { path } = this.$route
  44. if (!path.includes('/perview')) {
  45. this.gapTime = new Date().getTime() - this.beforeUnloadTime
  46. if (this.gapTime <= 5) {
  47. // 关闭
  48. // 清除 cookie 或者 localStorage 的方法
  49. removeToken()
  50. } else {
  51. // 刷新
  52. }
  53. }
  54. })
  55. },
  56. destroyed() {
  57. window.removeEventListener('beforeunload')
  58. window.removeEventListener('unload')
  59. }
  60. }
  61. </script>