364 lines
10 KiB
JavaScript
364 lines
10 KiB
JavaScript
import { createRouter, createWebHistory,createWebHashHistory } from 'vue-router'
|
|
import i18n from '@/locales/i18n'
|
|
import { watch } from 'vue'
|
|
import About from '@/views/About.vue'
|
|
import NotFound from '@/views/NotFound.vue'
|
|
import AdminLogin from '@/views/AdminLogin/AdminLogin.vue'
|
|
|
|
// 管理员布局组件(懒加载)
|
|
const AdminLayout = () => import('@/components/admin/AdminLayout.vue')
|
|
const AdminDashboard = () => import('@/views/admin/AdminDashboard.vue')
|
|
const AdminContent = () => import('@/views/admin/AdminContent.vue')
|
|
const AdminOrders = () => import('@/views/admin/AdminOrders/AdminOrders.vue')
|
|
const AdminUsers = () => import('@/views/admin/AdminUsers/AdminUsers.vue')
|
|
const AdminUserInvites = () => import('@/views/admin/AdminUsers/AdminUserInvites.vue')
|
|
const AdminContentReview = () => import('@/views/admin/AdminContentReview.vue')
|
|
const AdminDisassemblyOrders = () => import('@/views/admin/AdminDisassemblyOrders.vue')
|
|
const AdminDisassemblyDetail = () => import('@/views/admin/AdminDisassemblyDetail/AdminDisassemblyDetail.vue')
|
|
const AdminRoleManagement = () => import('@/views/admin/AdminRoleManagement/AdminRoleManagement.vue')
|
|
const AdminRoleDetail = () => import('@/views/admin/AdminRoleManagement/AdminRoleDetail.vue')
|
|
const AdminPermissionManagement = () => import('@/views/admin/AdminPermissionManagement/AdminPermissionManagement.vue')
|
|
const AdminPermissionDetail = () => import('@/views/admin/AdminPermissionManagement/AdminPermissionDetail.vue')
|
|
const AdminUserList = () => import('@/views/admin/AdminRoleManagement/AdminUserList.vue')
|
|
const AdminPointsManagement = () => import('@/views/admin/AdminPointsManagement/AdminPointsManagement.vue')
|
|
const AdminCommissionManagement = () => import('@/views/admin/AdminCommissionManagement/AdminCommissionManagement.vue')
|
|
const AdminPromptManagement = () => import('@/views/admin/AdminPromptManagement/AdminPromptManagement.vue')
|
|
const AdminProductManagement = () => import('@/views/admin/ProductManagement/ProductManagement.vue')
|
|
//权限路由映射表
|
|
export const permissionRoutes = [
|
|
{
|
|
path: 'admindashboard',
|
|
name: 'AdminDashboard',
|
|
component: AdminDashboard,
|
|
meta: {
|
|
title: 'admin.layout.dashboard',
|
|
icon: 'DataAnalysis',
|
|
requiresAuth: true,
|
|
menuOrder: 1
|
|
}
|
|
},
|
|
{
|
|
path: 'product-management',
|
|
name: 'AdminProductManagement',
|
|
component: AdminProductManagement,
|
|
meta: {
|
|
title: 'admin.layout.productManagement',
|
|
icon: 'ShoppingCartFull',
|
|
menuOrder: 2,
|
|
requiresAuth: true
|
|
}
|
|
},
|
|
{
|
|
path: 'orders',
|
|
name: 'AdminOrders',
|
|
meta: {
|
|
title: 'admin.layout.orders',
|
|
icon: 'ShoppingCart',
|
|
requiresAuth: true,
|
|
menuOrder: 2,
|
|
isParent: true
|
|
},
|
|
children: [
|
|
{
|
|
path: 'content-review',
|
|
name: 'AdminContentReview',
|
|
component: AdminContentReview,
|
|
meta: {
|
|
title: 'admin.layout.contentReview',
|
|
icon: 'Document',
|
|
menuOrder: 1,
|
|
requiresAuth: true,
|
|
},
|
|
},
|
|
|
|
{
|
|
path: 'order-list',
|
|
name: 'AdminOrdersList',
|
|
component: AdminOrders,
|
|
meta: {
|
|
title: 'admin.layout.orders',
|
|
icon: 'ShoppingCart',
|
|
menuOrder: 3,
|
|
requiresAuth: true,
|
|
}
|
|
}
|
|
]
|
|
},
|
|
{
|
|
path: 'users',
|
|
name: 'AdminUsers',
|
|
component: AdminUsers,
|
|
meta: {
|
|
title: 'admin.layout.users',
|
|
icon: 'UserFilled',
|
|
menuOrder: 3,
|
|
requiresAuth: true
|
|
}
|
|
},
|
|
{
|
|
path: 'points-management',
|
|
name: 'AdminPointsManagement',
|
|
component: AdminPointsManagement,
|
|
meta: {
|
|
title: 'admin.layout.pointsManagement',
|
|
icon: 'Coin',
|
|
menuOrder: 4,
|
|
requiresAuth: true
|
|
}
|
|
},
|
|
{
|
|
path: 'commission-management',
|
|
name: 'AdminCommissionManagement',
|
|
component: AdminCommissionManagement,
|
|
meta: {
|
|
title: 'admin.layout.commissionManagement',
|
|
icon: 'Coin',
|
|
menuOrder: 5,
|
|
requiresAuth: true
|
|
}
|
|
},
|
|
{
|
|
path: 'prompt-management',
|
|
name: 'AdminPromptManagement',
|
|
component: AdminPromptManagement,
|
|
meta: {
|
|
title: 'admin.layout.promptManagement',
|
|
icon: 'Document',
|
|
menuOrder: 6,
|
|
requiresAuth: true
|
|
}
|
|
},
|
|
|
|
{
|
|
path: 'permission',
|
|
name: 'AdminPermission',
|
|
meta: {
|
|
title: 'admin.layout.permission',
|
|
icon: 'Lock',
|
|
menuOrder: 7,
|
|
isParent: true,
|
|
requiresAuth: true
|
|
},
|
|
children: [
|
|
{
|
|
path: 'role-management',
|
|
name: 'AdminRoleManagement',
|
|
component: AdminRoleManagement,
|
|
meta: {
|
|
title: 'admin.layout.roleManagement',
|
|
icon: 'Key',
|
|
menuOrder: 1,
|
|
requiresAuth: true
|
|
}
|
|
},
|
|
{
|
|
path: 'permission-management',
|
|
name: 'AdminPermissionManagement',
|
|
component: AdminPermissionManagement,
|
|
meta: {
|
|
title: 'admin.layout.permissionManagement',
|
|
icon: 'Document',
|
|
menuOrder: 2,
|
|
requiresAuth: true
|
|
}
|
|
},
|
|
{
|
|
path: 'user-list',
|
|
name: 'AdminUserList',
|
|
component: AdminUserList,
|
|
meta: {
|
|
title: 'admin.layout.userList',
|
|
icon: 'List',
|
|
menuOrder: 3,
|
|
requiresAuth: true
|
|
}
|
|
}
|
|
]
|
|
},
|
|
// {
|
|
// path: 'content',
|
|
// name: 'AdminContent',
|
|
// component: AdminContent,
|
|
// meta: {
|
|
// title: '内容管理',
|
|
// icon: 'Document',
|
|
// menuOrder: 8,
|
|
// hideInMenu: true,
|
|
// requiresAuth: true
|
|
// }
|
|
// },
|
|
|
|
|
|
|
|
|
|
]
|
|
const routes = [
|
|
{
|
|
path: '/',
|
|
name: 'Home',
|
|
meta: {
|
|
title: '首页重定向'
|
|
}
|
|
},
|
|
{
|
|
path: '/about',
|
|
name: 'About',
|
|
component: About,
|
|
meta: {
|
|
title: '关于页面'
|
|
}
|
|
},
|
|
{
|
|
path: '/login',
|
|
name: 'Login',
|
|
component: AdminLogin,
|
|
meta: {
|
|
title: '登录',
|
|
requiresAuth: false
|
|
}
|
|
},
|
|
{
|
|
path: '/admin',
|
|
name: 'Admin',
|
|
component: AdminLayout,
|
|
meta: {
|
|
requiresAuth: true
|
|
},
|
|
children: [
|
|
{
|
|
path: 'disassembly-orders',
|
|
name: 'AdminDisassemblyOrders',
|
|
component: AdminDisassemblyOrders,
|
|
meta: {
|
|
title: 'admin.layout.disassemblyOrders',
|
|
icon: 'EditPen',
|
|
menuOrder: 2,
|
|
requiresAuth: true,
|
|
}
|
|
},
|
|
{
|
|
path: 'disassembly-orders/:id',
|
|
name: 'AdminDisassemblyDetail',
|
|
component: AdminDisassemblyDetail,
|
|
meta: {
|
|
title: '拆件详情',
|
|
icon: 'EditPen',
|
|
menuOrder: 10,
|
|
hideInMenu: true,
|
|
requiresAuth: true
|
|
}
|
|
},
|
|
{
|
|
path: 'role-management/:roleId',
|
|
name: 'AdminRoleDetail',
|
|
component: AdminRoleDetail,
|
|
meta: {
|
|
title: '角色详情',
|
|
icon: 'Key',
|
|
menuOrder: 11,
|
|
hideInMenu: true,
|
|
requiresAuth: true
|
|
}
|
|
},
|
|
{
|
|
path: 'permission-management/:permissionId',
|
|
name: 'AdminPermissionDetail',
|
|
component: AdminPermissionDetail,
|
|
meta: {
|
|
title: '权限详情',
|
|
icon: 'Document',
|
|
menuOrder: 12,
|
|
hideInMenu: true,
|
|
requiresAuth: true
|
|
}
|
|
},{
|
|
path: 'users/:id/invites',
|
|
name: 'AdminUserInvites',
|
|
component: AdminUserInvites,
|
|
meta: {
|
|
title: '邀请列表',
|
|
icon: 'UserFilled',
|
|
menuOrder: 9,
|
|
hideInMenu: true,
|
|
requiresAuth: true
|
|
}
|
|
},
|
|
...(window.location.hostname.indexOf('local') === -1 ? [] : permissionRoutes)
|
|
]//[]//
|
|
},
|
|
{
|
|
path: '/:pathMatch(.*)*',
|
|
name: 'NotFound',
|
|
component: NotFound,
|
|
meta: {
|
|
title: '页面不存在'
|
|
}
|
|
}
|
|
]
|
|
|
|
|
|
|
|
|
|
const router = createRouter({
|
|
// history: createWebHistory(),
|
|
history: createWebHashHistory(),
|
|
routes,
|
|
scrollBehavior(to, from, savedPosition) {
|
|
if (savedPosition) {
|
|
return savedPosition
|
|
} else {
|
|
return { top: 0 }
|
|
}
|
|
}
|
|
})
|
|
window.Redirectlogin = () => {
|
|
localStorage.removeItem('token')
|
|
router.push('/login')
|
|
}
|
|
// 路由守卫 - 认证检查
|
|
router.beforeEach(async (to, from, next) => {
|
|
// 设置页面标题(支持中英文切换)
|
|
if (to.meta?.title) {
|
|
const title = typeof to.meta.title === 'string'
|
|
? i18n.global.t(to.meta.title)
|
|
: to.meta.title
|
|
document.title = title
|
|
}
|
|
// 检查是否需要认证
|
|
if (to.meta?.requiresAuth||to.path === '/') {
|
|
const token = localStorage.getItem('token')
|
|
if (!token) {
|
|
next('/login')
|
|
return
|
|
}
|
|
}
|
|
if (to.path === '/login'||to.path === '/') {
|
|
const token = localStorage.getItem('token')
|
|
if (token) {
|
|
// 获取第一个 admin 子路由
|
|
const adminRoutes = router.getRoutes().filter(r => r.path.startsWith('/admin/') && r.path !== '/admin')
|
|
if (adminRoutes.length > 0) {
|
|
// 按 menuOrder 排序,取第一个
|
|
const sortedRoutes = adminRoutes.sort((a, b) => (a.meta?.menuOrder || 999) - (b.meta?.menuOrder || 999))
|
|
next(sortedRoutes[0].path)
|
|
} else {
|
|
next('/admin')
|
|
}
|
|
return
|
|
}
|
|
}
|
|
next()
|
|
})
|
|
|
|
// 监听语言变化,实时更新页面标题
|
|
watch(() => i18n.global.locale.value, (newLocale) => {
|
|
const currentRoute = router.currentRoute.value
|
|
if (currentRoute.meta?.title) {
|
|
const title = typeof currentRoute.meta.title === 'string'
|
|
? i18n.global.t(currentRoute.meta.title)
|
|
: currentRoute.meta.title
|
|
document.title = title
|
|
}
|
|
})
|
|
|
|
export default router |