import Vue from "vue";
import VueRouter from "vue-router";
// import axios from "axios";

Vue.use(VueRouter);

const createRouter = () =>
  new VueRouter({
    scrollBehavior: () => ({ y: 0 }),
  });

const router = createRouter();

router.beforeEach((to, from, next) => {
  // console.log(to,next);
  const token = localStorage.getItem("token");
  // 目标路由不是登录页，并且还需要token验证，还没有token，那就直接给返回到登录页
  if (to.name !== "Login" && !token) {
    next({ name: "Login" });
  } else {
    // 目标路由是登录页-自然不需要token验证
    // 或目标路由不需要身份验证
    // 又或目标路由非登录页，需要token验证，但是有token
    // next放行
    next();
  }
});

export default router;
