summaryrefslogtreecommitdiff
path: root/src/App.vue
blob: 9aa39e75396101b63aadd18bab65dc65029086c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<template>
  <div id="app">
    <router-view/>
  </div>
</template>
<script>
import { storageKey } from '@/utils/constants'
import router from '@/router'
import axios from 'axios'
import { api } from '@/utils/api'
const dayjs = require('dayjs')
const utc = require('dayjs/plugin/utc')
const timezone = require('dayjs/plugin/timezone')
const advancedFormat = require('dayjs/plugin/advancedFormat')
const weekday = require('dayjs/plugin/weekday')
dayjs.extend(utc)
dayjs.extend(timezone)
dayjs.extend(advancedFormat)
dayjs.extend(weekday)
window.$dayJs = dayjs

export default {
  name: 'App',
  setup () {
    // 处理刷新后  $dayJs的时区变为默认的问题
    const timezone = localStorage.getItem(storageKey.sysTimezone) || ''
    if (timezone) {
      window.$dayJs.tz.setDefault(timezone)
    } else {
      window.$dayJs.tz.setDefault()
    }
  },
  mounted () {
    this.loginAlready()
  },
  methods: {
    /**
     * 已经登录判断
     * 如果已经登录,新打开页面按url打开,否则进入首页
     */
    loginAlready () {
      // 登录判断在router/index.js下操作更合适,但该文件内引入post等方法会导致路由报错,
      // 目前不知道其原因,后续解决该问题后,将登录操作移入router
      const url = window.location.href
      const currentPath = url.match(/#(\S*)/)[1]

      if (currentPath === '/' || currentPath === '/login') {
        if (localStorage.getItem(storageKey.token) !== null) {
          // 刚进入会请求失败,故采用延时,请求成功清除延时器,避免内存泄漏
          const timer = setTimeout(() => {
            axios.post(api.permissions, { token: localStorage.getItem(storageKey.token) }).then(res => {
              router.push({
                path: '/panel/networkOverview'
              })
              clearTimeout(timer)
            })
          }, 10)
        }
      }
    }
  }
}
</script>