/*
 * @Date: 2023-06-19 13:38:07
 * @LastEditors: zhubj
 * @LastEditTime: 2023-06-19 17:01:56
 * @Description: 头部注释
 * @FilePath: \own-vue3-vuecli-template\src\permission.js
 */
import NProgress from "nprogress";
import 'nprogress/nprogress.css'
import router from "./router";
import store from "./store";
import { ElMessage } from "element-plus";
import { getToken } from "./utils/auth";
import { isRelogin } from "./utils/request";

NProgress.configure({ showSpinner: false }) // 关闭加载微调器

const whiteList = ['/login','/404','/layout', '/home'] // 设置白名单，用于任何人可访问

// 钩子函数
router.beforeEach((to, from, next) => {
  NProgress.start()
  let token = getToken() 
  if (token) {
        if(to.path==='/login'){
            return next()
        }else{
            if(!token){
                ElMessage.error('登录已失效，请重新登录');
                return next('/login')
            } else if (to.path==='/') {
                return next('/login')
            } else{
                next()
            }
        }
  } else {
    // 没有token
    if (whiteList.indexOf(to.path) !== -1) {
      // 在免登录白名单，直接进入
      next()
    } else {
      next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  NProgress.done()
})