import VueRouter from 'vue-router'
import router from './router'
//加载进度
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

import { auth } from '@/utils/auth'

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function (location) {
  return (originalPush.call(this, location)).catch((err) => err)
}

NProgress.configure({
    showSpinner: false
})
router.beforeEach(async (to, from, next) => {
    NProgress.start()
    //登录拦截
    const info = auth.initUserInfo();
    if (info) {
        // auth.initUserInfo()
        if (to.path === '/login') {
            next({path: '/'})
        }
        else {
            next()
        }
    }
    else {
        if (to.path === '/login') {
            next()
        }
        else {
            next({
                path: `/login` // ?redirect=${to.path}
            })
        }
    }
})
router.afterEach(() => {
    NProgress.done()
})