import { isCtrlPress } from '@/utils/keyboard'

export default ({ app, store, redirect, $cookies }) => {
  app.router.beforeEach((to, from, next) => {
    // 在每次路由跳转的时候进行判断
    if (isCtrlPress()) {
      // 若是 ctrl 按键被按下，则以新窗口打开目标页面
      window.open(to.fullPath)
      // 并阻止当前页面的跳转
      return next(false)
    }

    let token = ''
    let userInfo = ''
    // console.log($cookies.get('can-use-cookie'), 'can use cookie')
    // console.log($cookies.get('token'), 'token')
    if ($cookies.get('can-use-cookie')) {
      token = $cookies.get('token')
      userInfo = $cookies.get('user-info')
    }
    if (token) {
      // console.log('set status')
      if (userInfo) {
        store.commit('setUserInfo', userInfo)
      } else {
        store.dispatch('getUserInfo')
      }
    } else {
      // console.log('remove status')
      store.commit('clearUserInfo')
    }
    next()
  })
}
