index.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";
  2. import UserLogin from "@/features/UserLogin/UserLogin.vue";
  3. import MainLayout from "@/components/MainLayout/MainLayout.vue";
  4. import ChangePassword from "@/features/ChangePassword/ChangePassword.vue";
  5. import { resetStore, store } from "@/store/store";
  6. const routes: RouteRecordRaw[] = [
  7. { path: "/", component: UserLogin },
  8. { path: "/login", component: UserLogin, name: "UserLogin" },
  9. {
  10. path: "/",
  11. component: MainLayout,
  12. children: [
  13. {
  14. path: "password",
  15. component: ChangePassword,
  16. },
  17. ],
  18. },
  19. {
  20. path: "/:pathMatch(.*)*",
  21. name: "NotFound",
  22. component: () => import("@/components/PageError404.vue"),
  23. },
  24. ];
  25. // 3. Create the router instance and pass the `routes` option
  26. // You can pass in additional options here, but let's
  27. // keep it simple for now.
  28. const router = createRouter({
  29. // 4. Provide the history implementation to use. We are using the hash history for simplicity here.
  30. history: createWebHistory("oe-web"),
  31. routes, // short for `routes: routes`
  32. });
  33. router.beforeEach((to, from, next) => {
  34. const loginPath = "/login/";
  35. if (to.path) {
  36. _hmt.push(["_trackPageview", "/oe-web" + to.fullPath]);
  37. }
  38. if (to.path.match(/^\/?/) || to.path.match(/^\/login\/.?$/)) {
  39. resetStore();
  40. next();
  41. } else {
  42. if (!localStorage.getItem("domain")) {
  43. alert("地址出错,找不到机构!请关闭应用后重试!");
  44. }
  45. if (!store.user.token) {
  46. next({ path: loginPath });
  47. } else {
  48. next();
  49. }
  50. }
  51. });
  52. export default router;