index.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { createRouter,createWebHistory } from 'vue-router';
  2. //all routes
  3. const appAllRoutes = [];
  4. const importAllRoutes = (r) => {
  5. for(let key in r) {
  6. appAllRoutes.push(...r[key].default)
  7. }
  8. }
  9. importAllRoutes(import.meta.glob('./modules/*.js',{ eager: true }))
  10. const routes = [
  11. ...appAllRoutes,
  12. {
  13. name: 'login',
  14. path: '/login',
  15. component: () => import("@/views/Login.vue"),
  16. meta: {
  17. title: "登录"
  18. },
  19. },
  20. {
  21. path: '/temppage',
  22. component: ()=>import('@/views/transferPage.vue'),
  23. name: 'transferPage',
  24. hidden: true
  25. },
  26. {
  27. name: '404',
  28. path: '/404',
  29. component: () => import("@/views/404.vue"),
  30. meta: {
  31. title: "404"
  32. },
  33. },
  34. {
  35. path: '',
  36. redirect: {
  37. path: '/dashboard'
  38. },
  39. },
  40. {
  41. path: '/:pathMatch(.*)',
  42. redirect: {
  43. path: '/404'
  44. },
  45. },
  46. ]
  47. export const router = createRouter({
  48. history: createWebHistory(import.meta.env.VITE_APP_BASE_URL),
  49. routes
  50. });
  51. router.beforeEach(async(to, from, next) => {
  52. let auth = localStorage.getItem("auth") || false;
  53. if (to.path != "/login" && to.path!='/temppage' &&to.path!='/fogetpassword' && !auth) {
  54. console.log(1111111)
  55. window.location.href = window.location.origin + '/login';
  56. return false;
  57. }
  58. if (to.path) {
  59. //百度统计
  60. if (window._hmt) {
  61. window._hmt.push(["_trackPageview", "/#" + to.fullPath]);
  62. }
  63. next();
  64. } else {
  65. next({ path: "/404" });
  66. }
  67. });
  68. router.afterEach((to, from, next) => {
  69. // 页面标题
  70. document.title = to.matched[to.matched.length-1].meta.title ?
  71. `弘则-${to.matched[to.matched.length-1].meta.title}`:'弘则管理后台'
  72. window.scrollTo(0, 0);
  73. });
  74. export function initRouter(app) {
  75. app.use(router)
  76. }