index.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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, // 添加该字段,表示进入这个路由是需要登录的
  41. },
  42. name: 'error404',
  43. component: error404,
  44. }
  45. ]
  46. const router = new VueRouter({
  47. mode: 'history',
  48. base: process.env.BASE_URL,
  49. routes
  50. })
  51. /**
  52. * 路由的钩子函数,处理是否登录的判断
  53. * **/
  54. router.beforeEach((to, from, next) => {
  55. // 路由地址不存在的处理办法
  56. if (to.matched.length === 0) { // 如果未匹配到路由
  57. // sessionStorage.removeItem("btrh_sxsd_locationHref");
  58. next('/404') // 如果上级也未匹配到路由则跳转登录页面,如果上级能匹配到则转上级路由
  59. }
  60. if (to.matched.some(r => r.meta.requireAuth)) {
  61. let userinfo = JSON.parse(sessionStorage.getItem("btrh_sxsd_userinfo"));
  62. userinfo = "";
  63. if(userinfo !== null){
  64. // if(to.path !== "/"){
  65. // // 判断当前菜单是否有权限打开,如果无权限,自动退出
  66. // routerCheck(userinfo.roleId, to.path, next);
  67. // }else{
  68. // next();
  69. // }
  70. next();
  71. return;
  72. }
  73. next("/login");
  74. } else {
  75. next();
  76. }
  77. })
  78. export default router