index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import Vue from 'vue';
  2. import VueRouter from 'vue-router';
  3. import store from '@/store/index';
  4. const jwt = require('jsonwebtoken');
  5. const originalPush = VueRouter.prototype.push;
  6. VueRouter.prototype.push = function push(location) {
  7. return originalPush.call(this, location).catch((err) => err);
  8. };
  9. Vue.use(VueRouter);
  10. const web = [
  11. {
  12. path: '/',
  13. name: 'index',
  14. meta: { title: '网站首页' },
  15. component: () => import('../views/index.vue'),
  16. },
  17. {
  18. path: '/website',
  19. name: 'website',
  20. component: () => import('../views/website.vue'),
  21. children: [
  22. {
  23. path: '/news/index',
  24. name: 'news_index',
  25. meta: { title: '新闻咨询' },
  26. component: () => import('../views/news/index.vue'),
  27. },
  28. {
  29. path: '/service/index',
  30. name: 'service_index',
  31. meta: { title: '科技服务' },
  32. component: () => import('../views/service/index.vue'),
  33. },
  34. {
  35. path: '/market/index',
  36. name: 'market_index',
  37. meta: { title: '技术超市' },
  38. component: () => import('../views/market/index.vue'),
  39. },
  40. {
  41. path: '/train/index',
  42. name: 'train_index',
  43. meta: { title: '宣传培训' },
  44. component: () => import('../views/train/index.vue'),
  45. },
  46. {
  47. path: '/dynamic/index',
  48. name: 'dynamic_index',
  49. meta: { title: '数据动态' },
  50. component: () => import('../views/dynamic/index.vue'),
  51. },
  52. ],
  53. },
  54. // 管理登录
  55. {
  56. path: '/login',
  57. name: 'login',
  58. meta: { title: '管理登录' },
  59. component: () => import('../views/login.vue'),
  60. },
  61. {
  62. path: '/adminCenter/homeIndex',
  63. name: 'adminCenter',
  64. component: () => import('../views/adminCenter/index.vue'),
  65. children: [
  66. {
  67. path: '/adminCenter/homeIndex',
  68. name: 'admin_homeIndex',
  69. meta: { title: '首页' },
  70. component: () => import('../views/adminCenter/homeIndex/index.vue'),
  71. },
  72. {
  73. path: '/adminCenter/test',
  74. name: 'admin_test',
  75. meta: { title: '测试页面' },
  76. component: () => import('../views/adminCenter/test/index.vue'),
  77. },
  78. {
  79. path: '/adminCenter/news',
  80. name: 'admin_news',
  81. meta: { title: '新闻管理' },
  82. component: () => import('../views/adminCenter/news/index.vue'),
  83. },
  84. {
  85. path: '/adminCenter/news/detail',
  86. name: 'admin_news_detail',
  87. meta: { title: '新闻管理' },
  88. component: () => import('../views/adminCenter/news/detail.vue'),
  89. },
  90. {
  91. path: '/adminCenter/product',
  92. name: 'admin_product',
  93. meta: { title: '科技成果' },
  94. component: () => import('../views/adminCenter/product/index.vue'),
  95. },
  96. {
  97. path: '/adminCenter/product/detail',
  98. name: 'admin_product_detail',
  99. meta: { title: '科技成果' },
  100. component: () => import('../views/adminCenter/product/detail.vue'),
  101. },
  102. ],
  103. },
  104. ];
  105. const routes = [...web];
  106. const router = new VueRouter({
  107. mode: 'history',
  108. base: process.env.VUE_APP_ROUTER,
  109. routes,
  110. });
  111. router.beforeEach((to, from, next) => {
  112. document.title = `${to.meta.title} `;
  113. const token = localStorage.getItem('token');
  114. if (to.path == '/adminCenter/homeIndex') {
  115. if (!token) {
  116. next('/login');
  117. } else {
  118. let user = jwt.decode(token);
  119. store.commit('setUser', user, { root: true });
  120. next();
  121. }
  122. } else {
  123. let user = jwt.decode(token);
  124. store.commit('setUser', user, { root: true });
  125. next();
  126. }
  127. });
  128. export default router;