router.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. export default ({
  2. app,
  3. context,
  4. req,
  5. store
  6. }) => {
  7. if (process.client) {
  8. const {
  9. host
  10. } = location || {}
  11. const isKaifain = host.indexOf('kaifain') !== -1
  12. const isJishuin = host.indexOf('jishuin') !== -1
  13. const isJob = host.indexOf('job') !== -1
  14. console.log("app.router.options.routes", app.router.options.routes)
  15. if (isKaifain) {
  16. window.__NUXT__.routePath = app.context.route.path.replace(/^\/kaifain/, '')
  17. let kaifainIndex = app.router.options.routes.filter(v => v.name === 'kaifain')[0]
  18. let kaifainDetail = app.router.options.routes.filter(v => v.name === 'kaifain-detail-tid')[0]
  19. let kaifainCaseDetail = app.router.options.routes.filter(v => v.name === 'kaifain-case-tid')[0]
  20. let kaifainAdd = app.router.options.routes.filter(v => v.name === 'kaifain-add')[0]
  21. let kaifainPreview = app.router.options.routes.filter(v => v.name === 'kaifain-preview')[0]
  22. let kaifainPreviewCase = app.router.options.routes.filter(v => v.name === 'kaifain-previewCase')[0]
  23. let kaifainIndexNow = {
  24. name: 'kaifain_$',
  25. path: '/*',
  26. component: kaifainIndex.component
  27. }
  28. let kaifainDetailNow = {
  29. name: 'kaifainSeoDetails_$',
  30. path: '/s/:tid',
  31. component: kaifainDetail.component
  32. }
  33. let kaifainCaseDetailNow = {
  34. name: 'kaifainCaseSeoDetails_$',
  35. path: '/d/:tid',
  36. component: kaifainCaseDetail.component
  37. }
  38. let kaifainAddNow = {
  39. name: 'kaifainAdd_$',
  40. path: '/add',
  41. component: kaifainAdd.component
  42. }
  43. let kaifainPreviewNow = {
  44. name: 'kaifainPreview_$',
  45. path: '/preview',
  46. component: kaifainPreview.component
  47. }
  48. let kaifainPreviewCaseNow = {
  49. name: 'kaifainPreviewCase_$',
  50. path: '/previewCase',
  51. component: kaifainPreviewCase.component
  52. }
  53. try {
  54. app.router.options.routes.unshift(
  55. kaifainPreviewCaseNow,
  56. kaifainPreviewNow,
  57. kaifainAddNow,
  58. kaifainCaseDetailNow,
  59. kaifainDetailNow,
  60. kaifainIndexNow)
  61. app.router.matcher.addRoutes([
  62. kaifainIndexNow, kaifainDetailNow, kaifainCaseDetailNow,
  63. kaifainAddNow, kaifainPreviewNow, kaifainPreviewCaseNow
  64. ])
  65. } catch (e) {
  66. console.log(e)
  67. }
  68. }
  69. if (isJishuin) {
  70. window.__NUXT__.routePath = app.context.route.path.replace(/^\/jishuin/, '')
  71. let jishuinCIndex = app.router.options.routes.filter(v => v.name === "user-collect_article-id-type")[0]
  72. let jishuinCType = app.router.options.routes.filter(v => v.name === "user-collect_article-id-type")[0]
  73. let jishuinUIndex = app.router.options.routes.filter(v => v.name === "user-id-type")[0]
  74. let jishuinUType = app.router.options.routes.filter(v => v.name === "user-id-type")[0]
  75. let jishuinU = {
  76. name: 'jishuinSeoU_$',
  77. path: '/u/:id',
  78. component: jishuinUIndex.component
  79. }
  80. let jishuinUT = {
  81. name: 'jishuinSeoUType_$',
  82. path: '/u/:id/:type',
  83. component: jishuinUType.component
  84. }
  85. let jishuinC = {
  86. name: 'jishuinSeoC_$',
  87. path: '/c/:id',
  88. component: jishuinCIndex.component
  89. }
  90. let jishuinCT = {
  91. name: 'jishuinSeoCT_$',
  92. path: '/c/:id/:type',
  93. component: jishuinCType.component
  94. }
  95. try {
  96. app.router.options.routes.unshift(jishuinU)
  97. app.router.options.routes.unshift(jishuinUT)
  98. app.router.options.routes.unshift(jishuinC)
  99. app.router.options.routes.unshift(jishuinCT)
  100. app.router.matcher.addRoutes([jishuinU, jishuinUT, jishuinC, jishuinCT])
  101. } catch (e) {
  102. console.log(e)
  103. }
  104. }
  105. if (isJob) {
  106. window.__NUXT__.routePath = app.context.route.path.replace(/^\/job/, '')
  107. let jobIndex = app.router.options.routes.filter(v => v.name === 'job')[0]
  108. let jobDetail = app.router.options.routes.filter(v => v.name === 'job-detail-id')[0]
  109. let jobIndexNow = {
  110. name: 'job_$',
  111. path: '/*',
  112. component: jobIndex.component
  113. }
  114. let jobDetailNow = {
  115. name: 'jobSeoDetails_$',
  116. path: '/d/:tid',
  117. component: jobDetail.component
  118. }
  119. try {
  120. app.router.options.routes.unshift(jobIndexNow)
  121. app.router.options.routes.unshift(jobDetailNow)
  122. app.router.matcher.addRoutes([jobIndexNow, jobDetailNow])
  123. } catch (e) {
  124. console.log(e)
  125. }
  126. }
  127. console.log('router', app.router)
  128. app.router.beforeEach((to, from, next) => {
  129. console.log("beforeEach", to, from)
  130. next()
  131. })
  132. }
  133. }
  134. function getMatchedComponents(route, matches = false, prop = 'components') {
  135. return Array.prototype.concat.apply([], route.matched.map((m, index) => {
  136. return Object.keys(m[prop]).map((key) => {
  137. matches && matches.push(index)
  138. return m[prop][key]
  139. })
  140. }))
  141. }