router.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. export default ({ app, context,req, store }) => {
  2. if (process.server) {
  3. let host = req.headers.host;
  4. const isKaifain = host.indexOf('kaifain') !== -1
  5. const isJishuin = host.indexOf('jishuin') !== -1
  6. const isJob = host.indexOf('job') !== -1
  7. return
  8. }
  9. if (process.client) {
  10. const { host } = 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 kaifainIndexNow = {
  21. name: 'kaifain_$',
  22. path: '/*',
  23. component: kaifainIndex.component
  24. }
  25. let kaifainDetailNow = {
  26. name: 'kaifainSeoDetails_$',
  27. path: '/s/:tid',
  28. component: kaifainDetail.component
  29. }
  30. let kaifainCaseDetailNow = {
  31. name: 'kaifainCaseSeoDetails_$',
  32. path: '/d/:tid',
  33. component: kaifainCaseDetail.component
  34. }
  35. try {
  36. app.router.options.routes.unshift(kaifainIndexNow)
  37. app.router.options.routes.unshift(kaifainDetailNow)
  38. app.router.options.routes.unshift(kaifainCaseDetailNow)
  39. app.router.matcher.addRoutes([ kaifainIndexNow, kaifainDetailNow, kaifainCaseDetailNow ])
  40. } catch ( e ) {
  41. console.log(e)
  42. }
  43. }
  44. if (isJishuin) {
  45. window.__NUXT__.routePath = app.context.route.path.replace(/^\/jishuin/, '')
  46. let jishuinCIndex = app.router.options.routes.filter(v => v.name === "jishuinCollectedC1")[ 0 ]
  47. let jishuinCType = app.router.options.routes.filter(v => v.name === "jishuinCollectedCID1")[ 0 ]
  48. let jishuinUIndex = app.router.options.routes.filter(v => v.name === "jishuinUserU1")[ 0 ]
  49. let jishuinUType = app.router.options.routes.filter(v => v.name === "jishuinUserUID1")[ 0 ]
  50. let jishuinU = {
  51. name: 'jishuinSeoU_$',
  52. path: '/u/:id',
  53. component: jishuinUIndex.component
  54. }
  55. let jishuinUT = {
  56. name: 'jishuinSeoUType_$',
  57. path: '/u/:id/:type',
  58. component: jishuinUType.component
  59. }
  60. let jishuinC = {
  61. name: 'jishuinSeoC_$',
  62. path: '/c/:id',
  63. component: jishuinCIndex.component
  64. }
  65. let jishuinCT = {
  66. name: 'jishuinSeoCT_$',
  67. path: '/c/:id/:type',
  68. component: jishuinCType.component
  69. }
  70. try {
  71. app.router.options.routes.unshift(jishuinU)
  72. app.router.options.routes.unshift(jishuinUT)
  73. app.router.options.routes.unshift(jishuinC)
  74. app.router.options.routes.unshift(jishuinCT)
  75. app.router.matcher.addRoutes([ jishuinU, jishuinUT, jishuinC, jishuinCT ])
  76. } catch ( e ) {
  77. console.log(e)
  78. }
  79. }
  80. if (isJob) {
  81. window.__NUXT__.routePath = app.context.route.path.replace(/^\/job/, '')
  82. let jobIndex = app.router.options.routes.filter(v => v.name === 'JobListSeoIndex')[ 0 ]
  83. let jobDetail = app.router.options.routes.filter(v => v.name === 'job-detail-id')[ 0 ]
  84. let jobIndexNow = {
  85. name: 'job_$',
  86. path: '/*',
  87. component: jobIndex.component
  88. }
  89. let jobDetailNow = {
  90. name: 'jobSeoDetails_$',
  91. path: '/d/:tid',
  92. component: jobDetail.component
  93. }
  94. try {
  95. app.router.options.routes.unshift(jobIndexNow)
  96. app.router.options.routes.unshift(jobDetailNow)
  97. app.router.matcher.addRoutes([ jobIndexNow, jobDetailNow ])
  98. } catch ( e ) {
  99. console.log(e)
  100. }
  101. }
  102. console.log('router', app.router)
  103. // app.router.beforeEach((to, from, next) => {
  104. // console.log("location.href", to, from)
  105. // next()
  106. // })
  107. }
  108. }
  109. function getMatchedComponents(route, matches = false, prop = 'components') {
  110. return Array.prototype.concat.apply([], route.matched.map((m, index) => {
  111. return Object.keys(m[prop]).map((key) => {
  112. matches && matches.push(index)
  113. return m[prop][key]
  114. })
  115. }))
  116. }