import NProgress from "nprogress";
import router from "./index";
// import 'nprogress/nprogress.css'; // nprogress样式文件
import "@/router/nprogress.less";

router.beforeEach((to, from, next) => {
  const token = window.$cookies.get("token");
  const userinfo = window.$cookies.get("userinfo");
  // 路由发生变化修改页面title
  if (to.meta.title) {
    document.title = to.meta.title;
  }
  // 开启进度条
  NProgress.start();
  if (to.meta?.auth) {
    if (!!token && !!userinfo) {
      next();
    } else {
      next({
        path: "/login",
        query: {
          redirect: to.fullPath,
        },
      });
    }
  } else {
    next();
  }
});

//当路由跳转结束后
router.afterEach(() => {
  // 关闭进度条
  NProgress.done();
});

export default router;
