index.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import Vue from 'vue'
  2. import VueRouter from 'vue-router'
  3. // 路由3+版本的异常处理
  4. const originalPush = VueRouter.prototype.push
  5. VueRouter.prototype.push = function push(location) {
  6. return originalPush.call(this, location).catch(err => err)
  7. }
  8. const originalReplace = VueRouter.prototype.replace;
  9. VueRouter.prototype.replace = function replace(location) {
  10. return originalReplace.call(this, location).catch(err => err);
  11. }
  12. // 公共页面
  13. import main from '@views/main'
  14. import error404 from '@views/404'
  15. // 子路由
  16. import yRouter from '@router/ylf';
  17. import lRouter from '@router/lzx';
  18. import zRouter from '@router/zm';
  19. Vue.use(VueRouter)
  20. const routes = [
  21. {
  22. path: '/',
  23. meta: {
  24. requireAuth: true, // 添加该字段,表示进入这个路由是需要登录的
  25. },
  26. component: main,
  27. children:[
  28. { // 首页
  29. path:'/',
  30. component: r => require.ensure([], () => r(require('@views/index/index')), 'indexM')
  31. },
  32. ...yRouter,
  33. ...lRouter,
  34. ...zRouter
  35. ]
  36. },
  37. {
  38. path: '/404', // 页面不存在的情况下会跳到404页面
  39. meta: {
  40. requireAuth: true, // 添加该字段,表示进入这个路由是需要登录的 /进路由勾子函数beforeEach
  41. },
  42. name: 'error404',
  43. component: error404,
  44. },
  45. // { // 健康评价报告 - 临时 /部署单页面
  46. // path:'/healthAssessmentDetailed2',
  47. // component: r => require.ensure([], () => r(require('@views/healthManagement/healthAssessmentDetailed')), 'healthManagement')
  48. // },
  49. ]
  50. const router = new VueRouter({
  51. mode: 'history',
  52. base: '/sis/', // 部署单页面 404 history 带井号解决(去掉history 放web-inf)
  53. routes
  54. })
  55. /**
  56. * 路由的钩子函数,处理是否登录的判断
  57. * **/
  58. router.beforeEach((to, from, next) => {
  59. // 路由地址不存在的处理办法
  60. if (to.matched.length === 0) { // 如果未匹配到路由
  61. // sessionStorage.removeItem("btrh_sxsd_locationHref");
  62. next('/404') // 如果上级也未匹配到路由则跳转登录页面,如果上级能匹配到则转上级路由
  63. }
  64. if (to.matched.some(r => r.meta.requireAuth)) {
  65. let userinfo = JSON.parse(sessionStorage.getItem("btrh_sxsd_userinfo"));
  66. userinfo = "";
  67. if(userinfo !== null){
  68. // if(to.path !== "/"){
  69. // // 判断当前菜单是否有权限打开,如果无权限,自动退出
  70. // routerCheck(userinfo.roleId, to.path, next);
  71. // }else{
  72. // next();
  73. // }
  74. next();
  75. return;
  76. }
  77. next("/login");
  78. } else {
  79. next();
  80. }
  81. })
  82. export default router