index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import Vue from 'vue';
  2. import VueRouter from 'vue-router';
  3. import store from '@/store/index';
  4. const jwt = require('jsonwebtoken');
  5. Vue.use(VueRouter);
  6. const admin = [
  7. {
  8. path: '/admin/index',
  9. meta: { title: '统计报表', isleftarrow: false },
  10. component: () => import('../views/adminCenter/index.vue'),
  11. },
  12. {
  13. path: '/adminRefute',
  14. meta: { title: '文章辟谣', isleftarrow: false },
  15. component: () => import('../views/adminCenter/adminRefute.vue'),
  16. },
  17. {
  18. path: '/adminRefute/edit',
  19. meta: { title: '文章辟谣', isleftarrow: false },
  20. component: () => import('../views/adminCenter/refute/edit.vue'),
  21. },
  22. {
  23. path: '/adminCommunity',
  24. meta: { title: '社区话题', isleftarrow: false },
  25. component: () => import('../views/adminCenter/adminCommunity.vue'),
  26. },
  27. {
  28. path: '/adminServe',
  29. meta: { title: '咨询服务', isleftarrow: false },
  30. component: () => import('../views/adminCenter/adminServe.vue'),
  31. },
  32. ];
  33. const routes = [
  34. {
  35. path: '/',
  36. meta: { title: '系统首页', isleftarrow: false },
  37. component: () => import('../views/index.vue'),
  38. },
  39. {
  40. path: '/login',
  41. meta: { title: '管理登陆', isleftarrow: false },
  42. component: () => import('../views/login.vue'),
  43. },
  44. // 文章辟谣
  45. {
  46. path: '/refute/index',
  47. meta: { title: '文章辟谣', isleftarrow: false },
  48. component: () => import('../views/refute/index.vue'),
  49. },
  50. {
  51. path: '/refute/detail',
  52. meta: { title: '文章正文', isleftarrow: false },
  53. component: () => import('../views/refute/detail.vue'),
  54. },
  55. // 社区话题
  56. {
  57. path: '/community/index',
  58. meta: { title: '社区话题', isleftarrow: false },
  59. component: () => import('../views/community/index.vue'),
  60. },
  61. {
  62. path: '/community/detail',
  63. meta: { title: '话题正文', isleftarrow: false },
  64. component: () => import('../views/community/detail.vue'),
  65. },
  66. // 咨询服务
  67. {
  68. path: '/service/index',
  69. meta: { title: '咨询服务', isleftarrow: false },
  70. component: () => import('../views/service/index.vue'),
  71. },
  72. {
  73. path: '/service/detail',
  74. meta: { title: '文章正文', isleftarrow: false },
  75. component: () => import('../views/service/detail.vue'),
  76. },
  77. // 引用管理员
  78. ...admin,
  79. ];
  80. const router = new VueRouter({
  81. mode: 'history',
  82. base: process.env.NODE_ENV === 'development' ? '' : process.env.VUE_APP_ROUTER,
  83. routes,
  84. });
  85. router.beforeEach(async (to, form, next) => {
  86. if (to.name == 'account_user') {
  87. let res = await store.dispatch('login/toGetUser');
  88. if (res && res.uid) {
  89. next();
  90. } else {
  91. let key = sessionStorage.getItem('token');
  92. let user = jwt.decode(key);
  93. if (user && user.uid) {
  94. store.commit('setUser', user, { root: true });
  95. next();
  96. } else {
  97. next({ name: 'login' });
  98. }
  99. }
  100. } else {
  101. let res = await store.dispatch('login/toGetUser');
  102. next();
  103. }
  104. });
  105. const originalPush = VueRouter.prototype.push;
  106. VueRouter.prototype.push = function push(location, onResolve, onReject) {
  107. if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject);
  108. return originalPush.call(this, location).catch(err => err);
  109. };
  110. export default router;