index.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. import Vue from 'vue'
  2. import VueRouter from 'vue-router'
  3. import Store from '../store'
  4. import Cookies from 'js-cookie'
  5. Vue.use(VueRouter)
  6. const originalPush = VueRouter.prototype.push
  7. VueRouter.prototype.push = function push(location) {
  8. return originalPush.call(this, location).catch(err => err)
  9. }
  10. const routes = [
  11. {
  12. path: "/",
  13. component: () => import('@/views/index'),
  14. },
  15. {
  16. path: '/register',
  17. name: 'Register',
  18. meta: {
  19. title: '注册'
  20. },
  21. component: () => import("@/views/register/index.vue")
  22. },
  23. {
  24. path: "",
  25. component: () => import('@/views/layout/index.vue'),
  26. children: [
  27. {
  28. path: '/home',
  29. name: 'Home',
  30. meta: {
  31. // title: '首页',
  32. sign: 'home',
  33. belong: 'home'
  34. },
  35. component: () => import('@/views/home/index.vue'),
  36. },
  37. ]
  38. }
  39. ]
  40. /* 后台管理系统Layout */
  41. import administrator_layout from '@/views/backStageManage/layout/Layout.vue'
  42. export const backStageManageRouterItem = [
  43. {
  44. path: '/administrator',
  45. component: administrator_layout,
  46. redirect: '/administrator/home',
  47. meta: { title: '首页', icon: 'home' },
  48. children: [
  49. {
  50. path: 'home',
  51. name: 'Administrator_home',
  52. component: () => import('@/views/backStageManage/home/index.vue'),
  53. meta: { title: '首页', icon: 'home' }
  54. },
  55. ]
  56. },
  57. {
  58. path: '/administrator',
  59. name: 'Administrator_organization',
  60. component: administrator_layout,
  61. redirect: '/administrator/personnel',
  62. meta: { title: '人员组织管理', icon: 'application' },
  63. children: [
  64. {
  65. path: 'personnel',
  66. name: 'Administrator_personnel',
  67. component: () => import('@/views/backStageManage/organization/personnel/index.vue'),
  68. meta: {title: '人员清单'},
  69. },
  70. {
  71. path: 'personnel2',
  72. name: 'Administrator_personnel2',
  73. component: () => import('@/views/backStageManage/organization/personnel/index.vue'),
  74. meta: {title: '人员清单2'},
  75. },
  76. ]
  77. },
  78. {
  79. path: '/administrator',
  80. name: 'Administrator_application',
  81. component: administrator_layout,
  82. redirect: '/administrator/apply',
  83. meta: { title: '应用管理', icon: 'application' },
  84. children: [
  85. {
  86. path: 'addApply',
  87. name: 'Administrator_addApply',
  88. component: () => import('@/views/backStageManage/application/apply/components/addApply.vue'),
  89. meta: {title: '添加应用功能', icon: 'apply', keepAlive:true},
  90. hidden:true
  91. },
  92. {
  93. path: 'apply',
  94. name: 'Administrator_apply',
  95. component: () => import('@/views/backStageManage/application/apply/index.vue'),
  96. meta: { title: '应用功能清单', icon: 'apply' }
  97. },
  98. {
  99. path: 'vision',
  100. name: 'Administrator_vision',
  101. component: () => import('@/views/backStageManage/application/vision/index.vue'),
  102. meta: { title: '应用版本清单', icon: 'vision' }
  103. },
  104. {
  105. path: 'addVersion',
  106. name: 'Administrator_addVersion',
  107. component: () => import('@/views/backStageManage/application/vision/addVersion.vue'),
  108. meta: {title: '添加应用版本', icon: 'addVision'},
  109. hidden:true
  110. },
  111. {
  112. path: 'editVersion',
  113. name: 'Administrator_editVersion',
  114. component: () => import('@/views/backStageManage/application/vision/editVersion.vue'),
  115. meta: {title: '编辑应用版本', icon: 'editVision'},
  116. hidden:true
  117. },
  118. {
  119. path: 'vip',
  120. name: 'Administrator_vip',
  121. component: () => import('@/views/backStageManage/application/vip/index.vue'),
  122. meta: {title: '应用会员清单', icon: 'vip'}
  123. },
  124. {
  125. path: 'addVip',
  126. name: 'Administrator_addVip',
  127. component: () => import('@/views/backStageManage/application/vip/components/addVipForm.vue'),
  128. meta: {title: '添加应用会员', icon: 'addVip'},
  129. hidden:true
  130. },
  131. {
  132. path: 'editVip',
  133. name: 'Administrator_editVip',
  134. component: () => import('@/views/backStageManage/application/vip/components/editVipForm.vue'),
  135. meta: {title: '编辑应用会员', icon: 'editVip'},
  136. hidden:true
  137. },
  138. ]
  139. },
  140. ]
  141. export const backStageManage = [
  142. {
  143. path: '/administrator',
  144. name: 'Administrator',
  145. component: { render(c) { return c('router-view') } },
  146. redirect: '/administrator',
  147. children: [
  148. {
  149. path: '/',
  150. name: 'Administrator_Index',
  151. meta: {
  152. title: '后台管理',
  153. },
  154. component: () => import('@/views/backStageManage/index/index.vue'),
  155. },
  156. {
  157. path: 'login',
  158. name: 'Administrator_login',
  159. meta: {
  160. title: '后台管理-登录',
  161. },
  162. component: () => import('@/views/backStageManage/login/index.vue'),
  163. },
  164. ...backStageManageRouterItem
  165. ]
  166. }
  167. ]
  168. const router = new VueRouter({
  169. mode: 'history',
  170. base: '/',
  171. routes: process.env.NODE_ENV !== 'production' ? [...routes, ...backStageManage] : routes
  172. })
  173. router.beforeEach((to, from, next) => {
  174. if (to.meta.title) {
  175. document.title = to.meta.title;
  176. }
  177. if (to.path == '/administrator/login'|| routeExists(routes,to.path) || to.meta.notLogin) {
  178. Cookies.remove("token")
  179. localStorage.removeItem('vuex')
  180. next()
  181. }
  182. else {
  183. const tokenStr = Cookies.get('token')
  184. switch (tokenStr) {
  185. case undefined:
  186. if (to.meta.title) {
  187. document.title = to.meta.title;
  188. }
  189. next({ name: 'Administrator_login' })
  190. break;
  191. default:
  192. if (to.meta.title) {
  193. document.title = to.meta.title;
  194. }
  195. next()
  196. break;
  197. }
  198. }
  199. })
  200. function routeExists(array, path, parentPath) {
  201. for (var i = 0; i < array.length; i++) {
  202. var item = array[i]
  203. var nowPath = item.path
  204. if (item.path.indexOf('/') != 0) {
  205. nowPath = parentPath + '/' + nowPath
  206. }
  207. if (!item.children || item.children.length == 0) {
  208. if (path == nowPath) {
  209. return true
  210. }
  211. } else {
  212. var data = item.children
  213. var sign = routeExists(data, path, nowPath)
  214. if (sign) {
  215. return true
  216. }
  217. }
  218. }
  219. return false
  220. }
  221. // 在你的路由配置文件中
  222. router.afterEach((to, from) => {
  223. var sign = routeExists(backStageManageRouterItem, to.path)
  224. if (!sign) {
  225. return
  226. }
  227. var obj = {
  228. name: to.meta.title,
  229. path: to.path,
  230. route: {
  231. name: to.name,
  232. path: to.path,
  233. query: to.query,
  234. params: to.params
  235. }
  236. }
  237. Store.commit('addHistory', obj);
  238. Store.commit('addHistoryPath', to.path);
  239. });
  240. export default router