index.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { createRouter, createWebHistory, type Router, type RouteRecordRaw } from 'vue-router'
  2. import HomeView from '../view/HomeView.vue'
  3. import PostDetailView from '../view/PostDetailView.vue'
  4. import OrdersView from '../view/OrdersView.vue'
  5. import NotificationsView from '../view/NotificationsView.vue'
  6. import ProfileView from '../view/ProfileView.vue'
  7. import WalletView from '../view/WalletView.vue'
  8. import EditProfileView from '../view/EditProfileView.vue'
  9. import StaticPageView from '../view/StaticPageView.vue'
  10. import LoginView from '../view/LoginView.vue'
  11. import UserLayout from '../layout/UserLayout.vue'
  12. import AdminLayout from "../layout/AdminLayout.vue"
  13. import UserView from "../view/UserView.vue"
  14. import SiteSettingsView from "../view/SiteSettingsView.vue"
  15. import {
  16. HomeFilled,
  17. Menu,
  18. User,
  19. ShoppingCart,
  20. Bell,
  21. Setting,
  22. } from '@element-plus/icons-vue'
  23. import { useLoginUserStore } from "../store"
  24. import { ElMessage } from "element-plus"
  25. export const userRoutes: RouteRecordRaw[] = [
  26. {
  27. path: '/',
  28. name: 'commonLayout',
  29. component: UserLayout,
  30. children: [
  31. {
  32. path: '/',
  33. name: 'home',
  34. component: HomeView,
  35. meta: { title: '首页', icon: HomeFilled, tab: 'home' }
  36. },
  37. {
  38. path: '/orders',
  39. name: 'orders',
  40. component: OrdersView,
  41. meta: { title: '我的订单', icon: ShoppingCart, tab: 'orders' }
  42. },
  43. {
  44. path: '/notifications',
  45. name: 'notifications',
  46. component: NotificationsView,
  47. meta: { title: '我的信息', icon: Bell, tab: 'notifications' }
  48. },
  49. {
  50. path: '/profile',
  51. name: 'profile',
  52. component: ProfileView,
  53. meta: { title: '我的账户', icon: User, tab: 'profile' }
  54. },
  55. {
  56. path: '/post/:id',
  57. name: 'postDetail',
  58. component: PostDetailView,
  59. meta: { title: '帖子详情' }
  60. },
  61. {
  62. path: '/wallet',
  63. name: 'wallet',
  64. component: WalletView,
  65. meta: { title: '钱包' }
  66. },
  67. {
  68. path: '/edit-profile',
  69. name: 'editProfile',
  70. component: EditProfileView,
  71. meta: { title: '修改个人信息' }
  72. },
  73. {
  74. path: '/page/:type',
  75. name: 'staticPage',
  76. component: StaticPageView,
  77. meta: { title: '' }
  78. },
  79. ],
  80. },
  81. ]
  82. export const adminRoutes: RouteRecordRaw[] = [
  83. {
  84. path: '/admin',
  85. name: 'adminLayout',
  86. component: AdminLayout,
  87. children: [
  88. {
  89. path: '/admin/users',
  90. name: 'users',
  91. component: UserView,
  92. meta: { title: '用户管理', icon: User, requiresAdmin: true }
  93. },
  94. {
  95. path: '/admin/other',
  96. name: 'other',
  97. component: AdminLayout,
  98. meta: { title: '其他', icon: Menu, requiresAdmin: true }
  99. },
  100. {
  101. path: '/admin/settings',
  102. name: 'settings',
  103. component: SiteSettingsView,
  104. meta: { title: '网站设置', icon: Setting, requiresAdmin: true }
  105. }
  106. ],
  107. meta: { requiresAdmin: true }
  108. }
  109. ]
  110. const router: Router = createRouter({
  111. history: createWebHistory(),
  112. routes: [
  113. { path: '/login', name: 'login', component: LoginView },
  114. ...userRoutes,
  115. ...adminRoutes
  116. ],
  117. })
  118. router.beforeEach((to, _from, next) => {
  119. if (to.matched.some(record => record.meta.requiresAdmin)) {
  120. const loginUserStore = useLoginUserStore()
  121. if (!loginUserStore.loginUser.isLogin) {
  122. ElMessage.warning('请先登录')
  123. next({ path: '/login', query: { redirect: to.fullPath } })
  124. } else if (loginUserStore.loginUser.user?.role !== 'admin') {
  125. ElMessage.error('无权访问')
  126. next('/')
  127. } else {
  128. next()
  129. }
  130. } else {
  131. next()
  132. }
  133. })
  134. export default router