index.ts 4.3 KB
Newer Older
1
import Router from 'vue-router';
2
import { Environment } from '@ibiz-template/core';
3 4 5
import { AuthGuard } from '../guard';
import RouterShell from '@/components/router-shell/router-shell';
import appRedirectView from '@/views/app-redirect-view/app-redirect-view';
6 7 8 9
// import loginView from '../components/login-view/app-login-view/app-login-view';
import loginView from '../views/login-view/login-view';
import _404View from '../views/404-view/404-view';
import _403View from '../views/403-view/403-view';
10 11
import todoRedirect from '@/views/todo-redirect/todo-redirect';

12 13 14 15 16 17
const placeholder = Environment.routePlaceholder;
// 参数正则,占位符或者以等号相隔的参数键值对
const paramReg = `[^/]+=[^/]+|${placeholder}`;
// 视图正则,非=/的字符串
const viewReg = `[^=/]+`;

18 19 20 21 22 23 24 25 26 27
// 兼容ie
const originalPush = Router.prototype.push;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Router.prototype.push = function push(location: any) {
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  const result: any = originalPush.call(this, location);
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  return result.catch((err: any) => err);
};

28 29 30 31
const router = new Router({
  routes: [
    {
      path: '/',
32
      redirect: `/${placeholder}/index/${placeholder}`,
33 34 35 36
    },
    {
      path: '/login',
      name: 'loginView',
37
      component: loginView,
38 39 40
    },
    {
      path: '/404',
41
      name: '404View1',
42
      component: _404View,
43
    },
44 45 46
    {
      path: '/403',
      name: '403View1',
47
      component: _403View,
48
    },
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    {
      path: '/appredirectview',
      name: 'appRedirectView',
      beforeEnter: async (_to, _from, next) => {
        const authority = await AuthGuard();
        if (authority) {
          next();
        } else {
          next(false);
        }
      },
      component: appRedirectView,
    },
    {
      path: '/todoredirect',
      name: 'todoRedirect',
      beforeEnter: async (_to, _from, next) => {
        const authority = await AuthGuard();
        if (authority) {
          next();
        } else {
          next(false);
        }
      },
      component: todoRedirect,
    },
    {
76
      path: `/:appContext(${paramReg})/:view1(${viewReg})/:params1(${paramReg})`,
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
      props: {
        level: 1,
      },
      beforeEnter: async (_to, _from, next) => {
        const authority = await AuthGuard();
        if (authority) {
          next();
        } else {
          next(false);
        }
      },
      component: RouterShell,
      children: [
        {
          path: '404',
92
          name: '404View2',
93
          component: _404View,
94
        },
95 96 97
        {
          path: '403',
          name: '403View2',
98
          component: _403View,
99
        },
100
        {
101
          path: `:view2(${viewReg})/:params2(${paramReg})`,
102 103 104 105 106 107
          props: {
            level: 2,
          },
          component: RouterShell,
          children: [
            {
108 109
              path: '404',
              name: '404View3',
110
              component: _404View,
111
            },
112 113 114
            {
              path: '403',
              name: '403View3',
115
              component: _403View,
116
            },
117 118
            {
              path: `:view3(${viewReg})/:params3(${paramReg})`,
119 120 121 122
              props: {
                level: 3,
              },
              component: RouterShell,
123 124 125 126
              children: [
                {
                  path: '404',
                  name: '404View4',
127
                  component: _404View,
128
                },
129 130 131
                {
                  path: '403',
                  name: '403View4',
132
                  component: _403View,
133
                },
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
                {
                  path: `:view4(${viewReg})/:params4(${paramReg})`,
                  props: {
                    level: 4,
                  },
                  component: RouterShell,
                },
                {
                  path: '*',
                  redirect: { name: '404View4' },
                },
              ],
            },
            {
              path: '*',
              redirect: { name: '404View3' },
150 151 152
            },
          ],
        },
153 154 155 156
        {
          path: '*',
          redirect: { name: '404View2' },
        },
157 158 159
      ],
    },
    {
160 161
      path: '/*',
      redirect: { name: '404View1' },
162 163 164 165 166
    },
  ],
});

export default router;