index.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { getKeyToken } from "@/auth/auth";
  2. import { isNil } from "lodash-es";
  3. import Vue from "vue";
  4. import Router from "vue-router";
  5. import Home from "../views/Home.vue";
  6. Vue.use(Router);
  7. // function propsValidator(route, component) {
  8. // const props = { ...route.params };
  9. // Object.entries(props).map(([key, prop]) => {
  10. // console.log(prop, key);
  11. // if (!(prop instanceof component.props[key].type)) {
  12. // props[key] = component.props[key].type(prop);
  13. // }
  14. // });
  15. // return props;
  16. // }
  17. let router = new Router({
  18. mode: "history",
  19. base: process.env.BASE_URL,
  20. routes: [
  21. {
  22. path: "/",
  23. name: "home",
  24. component: Home,
  25. },
  26. {
  27. path: "/about",
  28. name: "about",
  29. // route level code-splitting
  30. // this generates a separate chunk (about.[hash].js) for this route
  31. // which is lazy-loaded when the route is visited.
  32. component: () =>
  33. import(/* webpackChunkName: "about" */ "../views/About.vue"),
  34. },
  35. // {
  36. // path: "/xxx/:id",
  37. // component: XXX,
  38. // props(route) {
  39. // return propsValidator(route, XXX);
  40. // }
  41. // }
  42. ],
  43. });
  44. router.beforeEach((to, from, next) => {
  45. const [uid, token] = getKeyToken();
  46. if ((isNil(uid) || isNil(token)) && to.path.includes("/login") === false) {
  47. if (
  48. window.___lastInvalidDate === undefined ||
  49. window.___lastInvalidDate < Date.now() - 300
  50. ) {
  51. window.___lastInvalidDate = Date.now();
  52. }
  53. router.push("/login");
  54. next(false);
  55. } else {
  56. next();
  57. }
  58. });
  59. export default router;