index.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. // 科普教育
  11. const education = [
  12. {
  13. path: '/education/videos',
  14. name: 'education_videos',
  15. meta: { title: '科普微视频' },
  16. component: () => import('../views/education/videos.vue'),
  17. },
  18. {
  19. path: '/education/videoDetail',
  20. name: 'education_videoDetail',
  21. meta: { title: '科普微视频' },
  22. component: () => import('../views/education/videoDetail.vue'),
  23. },
  24. {
  25. path: '/education/refute',
  26. name: 'education_refute',
  27. meta: { title: '科学辟谣' },
  28. component: () => import('../views/education/refute.vue'),
  29. },
  30. {
  31. path: '/education/refuteDetail',
  32. name: 'education_refuteDetail',
  33. meta: { title: '科学辟谣' },
  34. component: () => import('../views/education/refuteDetail.vue'),
  35. },
  36. {
  37. path: '/education/expert',
  38. name: 'education_expert',
  39. meta: { title: '专家观点' },
  40. component: () => import('../views/education/expert.vue'),
  41. },
  42. {
  43. path: '/education/expertDetail',
  44. name: 'education_expertDetail',
  45. meta: { title: '专家观点' },
  46. component: () => import('../views/education/expertDetail.vue'),
  47. },
  48. ];
  49. // 科教服务
  50. const service = [
  51. {
  52. path: '/service/zhongke',
  53. name: 'service_zhongke',
  54. meta: { title: '中科在线' },
  55. component: () => import('../views/service/zhongke.vue'),
  56. },
  57. {
  58. path: '/service/zhongkeDetail',
  59. name: 'service_zhongkeDetail',
  60. meta: { title: '中科在线' },
  61. component: () => import('../views/service/zhongkeDetail.vue'),
  62. },
  63. {
  64. path: '/service/jike',
  65. name: 'service_jike',
  66. meta: { title: '吉科在线' },
  67. component: () => import('../views/service/jike.vue'),
  68. },
  69. {
  70. path: '/service/jikeDetail',
  71. name: 'service_jikeDetail',
  72. meta: { title: '吉科在线' },
  73. component: () => import('../views/service/jikeDetail.vue'),
  74. },
  75. {
  76. path: '/service/make',
  77. name: 'service_make',
  78. meta: { title: '预约服务' },
  79. component: () => import('../views/service/make.vue'),
  80. },
  81. ];
  82. // 研发杂记
  83. const research = [
  84. {
  85. path: '/research/remember',
  86. name: 'research_remember',
  87. meta: { title: '研发杂记' },
  88. component: () => import('../views/research/remember.vue'),
  89. },
  90. ];
  91. // 用户中心
  92. const userCenter = [
  93. {
  94. path: '/userCenter/index',
  95. name: 'userCenter_index',
  96. meta: { title: '个人中心' },
  97. component: () => import('../views/userCenter/index.vue'),
  98. },
  99. // 科普教育
  100. {
  101. path: '/userCenter/education/videos',
  102. name: 'userCenter_education_videos',
  103. meta: { title: '科普微视频' },
  104. component: () => import('../views/userCenter/education/videos.vue'),
  105. },
  106. {
  107. path: '/userCenter/education/videosDetail',
  108. name: 'userCenter_education_videosDetail',
  109. meta: { title: '科普微视频' },
  110. component: () => import('../views/userCenter/education/videosDetail.vue'),
  111. },
  112. {
  113. path: '/userCenter/education/refute',
  114. name: 'userCenter_education_refute',
  115. meta: { title: '科学辟谣' },
  116. component: () => import('../views/userCenter/education/refute.vue'),
  117. },
  118. {
  119. path: '/userCenter/education/refuteDetail',
  120. name: 'userCenter_education_refuteDetail',
  121. meta: { title: '科学辟谣' },
  122. component: () => import('../views/userCenter/education/refuteDetail.vue'),
  123. },
  124. {
  125. path: '/userCenter/education/expert',
  126. name: 'userCenter_education_expert',
  127. meta: { title: '专家观点' },
  128. component: () => import('../views/userCenter/education/expert.vue'),
  129. },
  130. {
  131. path: '/userCenter/education/expertDetail',
  132. name: 'userCenter_education_expertDetail',
  133. meta: { title: '专家观点' },
  134. component: () => import('../views/userCenter/education/expertDetail.vue'),
  135. },
  136. // 科教服务
  137. {
  138. path: '/userCenter/service/zhongke',
  139. name: 'userCenter_service_zhongke',
  140. meta: { title: '中科在线' },
  141. component: () => import('../views/userCenter/service/zhongke.vue'),
  142. },
  143. {
  144. path: '/userCenter/service/zhongkeDetail',
  145. name: 'userCenter_service_zhongkeDetail',
  146. meta: { title: '中科在线' },
  147. component: () => import('../views/userCenter/service/zhongkeDetail.vue'),
  148. },
  149. {
  150. path: '/userCenter/service/jike',
  151. name: 'userCenter_service_jike',
  152. meta: { title: '吉科在线' },
  153. component: () => import('../views/userCenter/service/jike.vue'),
  154. },
  155. {
  156. path: '/userCenter/service/jikeDetail',
  157. name: 'userCenter_service_jikeDetail',
  158. meta: { title: '吉科在线' },
  159. component: () => import('../views/userCenter/service/jikeDetail.vue'),
  160. },
  161. {
  162. path: '/userCenter/service/project',
  163. name: 'userCenter_service_project',
  164. meta: { title: '服务项目' },
  165. component: () => import('../views/userCenter/service/project.vue'),
  166. },
  167. {
  168. path: '/userCenter/service/projectDetail',
  169. name: 'userCenter_service_projectDetail',
  170. meta: { title: '服务项目' },
  171. component: () => import('../views/userCenter/service/projectDetail.vue'),
  172. },
  173. {
  174. path: '/userCenter/service/equipment',
  175. name: 'userCenter_service_equipment',
  176. meta: { title: '设备共享' },
  177. component: () => import('../views/userCenter/service/equipment.vue'),
  178. },
  179. {
  180. path: '/userCenter/service/equipmentDetail',
  181. name: 'userCenter_service_equipmentDetail',
  182. meta: { title: '设备共享' },
  183. component: () => import('../views/userCenter/service/equipmentDetail.vue'),
  184. },
  185. ];
  186. const mechCenter = [
  187. {
  188. path: '/mechCenter/index',
  189. name: 'mechCenter_index',
  190. meta: { title: '个人中心' },
  191. component: () => import('../views/mechCenter/index.vue'),
  192. },
  193. {
  194. path: '/mechCenter/service/index',
  195. name: 'mechCenter_service_index',
  196. meta: { title: '预约服务' },
  197. component: () => import('../views/mechCenter/service/index.vue'),
  198. },
  199. ];
  200. const perCenter = [
  201. {
  202. path: '/perCenter/index',
  203. name: 'perCenter_index',
  204. meta: { title: '个人中心' },
  205. component: () => import('../views/perCenter/index.vue'),
  206. },
  207. {
  208. path: '/perCenter/service/index',
  209. name: 'perCenter_service_index',
  210. meta: { title: '预约服务' },
  211. component: () => import('../views/perCenter/service/index.vue'),
  212. },
  213. ];
  214. const web = [
  215. {
  216. path: '/',
  217. name: 'index',
  218. meta: { title: '网站首页' },
  219. component: () => import('../views/index.vue'),
  220. },
  221. {
  222. path: '/login',
  223. name: 'login',
  224. meta: { title: '登录' },
  225. component: () => import('../views/login.vue'),
  226. },
  227. ...education,
  228. ...service,
  229. ...research,
  230. ...userCenter,
  231. ...mechCenter,
  232. ...perCenter,
  233. ];
  234. const routes = [...web];
  235. const router = new VueRouter({
  236. mode: 'history',
  237. base: process.env.VUE_APP_ROUTER,
  238. routes,
  239. });
  240. router.beforeEach((to, from, next) => {
  241. document.title = `${to.meta.title} `;
  242. const token = localStorage.getItem('token');
  243. // if (to.name == 'mechCenter_index') {
  244. // if (!token) {
  245. // next(`/login?path=${to.path}`);
  246. // } else {
  247. // let user = jwt.decode(token);
  248. // store.commit('setUser', user, { root: true });
  249. // next();
  250. // }
  251. // } else if (to.name == 'login') {
  252. // next(`/login?path=${to.path}`);
  253. // } else {
  254. // let user = jwt.decode(token);
  255. // store.commit('setUser', user, { root: true });
  256. // next();
  257. // }
  258. if (to.name == 'mechCenter_index') {
  259. if (!token) {
  260. next(`/login?path=${to.path}`);
  261. } else {
  262. let user = jwt.decode(token);
  263. store.commit('setUser', user, { root: true });
  264. next();
  265. }
  266. } else {
  267. let user = jwt.decode(token);
  268. store.commit('setUser', user, { root: true });
  269. next();
  270. }
  271. });
  272. export default router;