index.coffee 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # app/router/index.coffee
  2. config = require 'app/config'
  3. # Импорт компонентов страниц
  4. HomePage = require 'app/pages/Home/index.coffee'
  5. NotFoundPage = require 'app/pages/NotFound/index.coffee'
  6. CatalogPage = require 'app/pages/Catalog/index.coffee'
  7. ProductPage = require 'app/pages/Product/index.coffee'
  8. CartPage = require 'app/pages/Cart/index.coffee'
  9. AdminPage = require 'app/pages/Admin/index.coffee'
  10. # Импорт дочерних страниц админ-панели
  11. AdminDashboard = require 'app/pages/Admin/Dashboard/index.coffee'
  12. AdminProducts = require 'app/pages/Admin/Products/index.coffee'
  13. AdminCategories = require 'app/pages/Admin/Categories/index.coffee'
  14. AdminImport = require 'app/pages/Admin/Import/index.coffee'
  15. AdminMedia = require 'app/pages/Admin/Media/index.coffee'
  16. # Middleware для проверки прав доступа
  17. authGuard = (to, from, next) ->
  18. log 'Проверка прав доступа для route:', to.path
  19. # Здесь будет логика проверки пользователя из глобального состояния
  20. if to.matched.some (record) -> record.meta.requiresAuth
  21. # Проверка авторизации
  22. userData = localStorage.getItem('user')
  23. if userData
  24. try
  25. user = JSON.parse(userData)
  26. user.role = 'admin'
  27. if user.role == 'admin'
  28. next()
  29. else
  30. log '🚫 Доступ запрещен: недостаточно прав'
  31. next('/')
  32. catch
  33. log '🚫 Ошибка проверки пользователя'
  34. next('/')
  35. else
  36. log '🚫 Пользователь не авторизован'
  37. next()
  38. else
  39. next()
  40. domainMiddleware = (to, from, next) ->
  41. log 'Обработка динамического домена для route'
  42. # Логика обработки домена будет интегрирована позже
  43. next()
  44. router = VueRouter.createRouter({
  45. history: VueRouter.createWebHistory()
  46. routes: [
  47. {
  48. path: '/'
  49. name: 'Home'
  50. component: HomePage
  51. beforeEnter: [domainMiddleware]
  52. }
  53. {
  54. path: '/catalog'
  55. name: 'Catalog'
  56. component: CatalogPage
  57. beforeEnter: [domainMiddleware]
  58. }
  59. {
  60. path: '/catalog/:category?'
  61. name: 'CatalogCategory'
  62. component: CatalogPage
  63. beforeEnter: [domainMiddleware]
  64. }
  65. {
  66. path: '/product/:id'
  67. name: 'Product'
  68. component: ProductPage
  69. beforeEnter: [domainMiddleware]
  70. }
  71. {
  72. path: '/cart'
  73. name: 'Cart'
  74. component: CartPage
  75. beforeEnter: [domainMiddleware]
  76. }
  77. {
  78. path: '/admin'
  79. name: 'Admin'
  80. component: AdminPage
  81. meta: { requiresAuth: true }
  82. beforeEnter: [domainMiddleware, authGuard]
  83. redirect: '/admin/dashboard'
  84. children: [
  85. {
  86. path: 'dashboard'
  87. name: 'AdminDashboard'
  88. component: AdminDashboard
  89. meta: {
  90. title: 'Дашборд',
  91. breadcrumb: 'Дашборд'
  92. }
  93. }
  94. {
  95. path: 'products'
  96. name: 'AdminProducts'
  97. component: AdminProducts
  98. meta: {
  99. title: 'Управление товарами',
  100. breadcrumb: 'Товары'
  101. }
  102. }
  103. {
  104. path: 'categories'
  105. name: 'AdminCategories'
  106. component: AdminCategories
  107. meta: {
  108. title: 'Управление категориями',
  109. breadcrumb: 'Категории'
  110. }
  111. }
  112. {
  113. path: 'import'
  114. name: 'AdminImport'
  115. component: AdminImport
  116. meta: {
  117. title: 'Импорт товаров',
  118. breadcrumb: 'Импорт'
  119. }
  120. }
  121. {
  122. path: 'media'
  123. name: 'AdminMedia'
  124. component: AdminMedia
  125. meta: {
  126. title: 'Медиа-менеджер',
  127. breadcrumb: 'Медиа'
  128. }
  129. }
  130. {
  131. path: 'import/:step?'
  132. name: 'AdminImportStep'
  133. component: AdminImport
  134. meta: {
  135. title: 'Импорт товаров',
  136. breadcrumb: 'Импорт'
  137. }
  138. }
  139. ]
  140. }
  141. {
  142. path: '/:pathMatch(.*)*'
  143. name: 'NotFound'
  144. component: NotFoundPage
  145. beforeEnter: [domainMiddleware]
  146. }
  147. ]
  148. })
  149. # Глобальные обработчики роутера
  150. router.beforeEach (to, from, next) ->
  151. #log "Переход с \"#{from.path}\" на \"#{to.path}\""
  152. # Установка заголовка страницы
  153. if to.meta?.title
  154. document.title = "#{to.meta.title} - Браер-Колор"
  155. else if to.name
  156. document.title = "#{to.name} - Браер-Колор"
  157. next()
  158. router.afterEach (to, from) ->
  159. #log "Навигация завершена на \"#{to.path}\""
  160. # Глобальный обработчик ошибок навигации
  161. router.onError (error) ->
  162. log '💥 Ошибка навигации:', error
  163. console.error('Ошибка навигации:', error)
  164. module.exports = router