仰望星辰工作室

better-staridc-MNBT

Z zfhsh first commit 2 个月前
1import { createRouter, createWebHashHistory } from 'vue-router'
2import authState, { initAuth } from '@/home/store/auth'
3
4const HomeLayout = () => import('@/home/layouts/HomeLayout.vue')
5const LandingView = () => import('@/home/views/LandingView.vue')
6const LoginView = () => import('@/home/views/auth/LoginView.vue')
7const RegisterView = () => import('@/home/views/auth/RegisterView.vue')
8const ShopView = () => import('@/home/views/shop/ShopView.vue')
9// Docker 售卖(docker_shop 插件)
10const DockerShopView = () => import('@/home/views/docker/DockerShopView.vue')
11// 官网内容(official_site 插件)
12const AboutView = () => import('@/home/views/site/AboutView.vue')
13const SiteProductsView = () => import('@/home/views/site/products/ProductsView.vue')
14const SiteProductDetailView = () => import('@/home/views/site/products/ProductDetailView.vue')
15const SiteNewsView = () => import('@/home/views/site/news/NewsView.vue')
16const SiteNewsDetailView = () => import('@/home/views/site/news/NewsDetailView.vue')
17const ContactView = () => import('@/home/views/site/ContactView.vue')
18
19const routes = [
20 {
21 path: '/',
22 component: HomeLayout,
23 children: [
24 { path: '', name: 'home-landing', component: LandingView, meta: { title: '' } },
25 { path: 'login', name: 'home-login', component: LoginView, meta: { title: '登录', guest: true, cap: 'user' } },
26 { path: 'register', name: 'home-register', component: RegisterView, meta: { title: '注册', guest: true, cap: 'user' } },
27 { path: 'shop', name: 'home-shop', component: ShopView, meta: { title: '主机套餐', cap: 'shop' } },
28 // Docker 售卖(docker_shop 插件)
29 { path: 'docker-shop', name: 'home-docker-shop', component: DockerShopView, meta: { title: 'Docker 套餐', cap: 'docker' } },
30 // 官网内容(official_site 插件)
31 { path: 'about', name: 'home-about', component: AboutView, meta: { title: '关于我们', cap: 'site' } },
32 { path: 'site/products', name: 'home-site-products', component: SiteProductsView, meta: { title: '产品中心', cap: 'site' } },
33 { path: 'site/products/:id', name: 'home-site-product-detail', component: SiteProductDetailView, meta: { title: '产品详情', cap: 'site' } },
34 { path: 'site/news', name: 'home-site-news', component: SiteNewsView, meta: { title: '新闻资讯', cap: 'site' } },
35 { path: 'site/news/:id', name: 'home-site-news-detail', component: SiteNewsDetailView, meta: { title: '新闻详情', cap: 'site' } },
36 { path: 'site/contact', name: 'home-site-contact', component: ContactView, meta: { title: '联系我们', cap: 'site' } },
37 ],
38 },
39 { path: '/:pathMatch(.*)*', redirect: '/' },
40]
41
42const router = createRouter({
43 history: createWebHashHistory(),
44 routes,
45 scrollBehavior() {
46 return { top: 0 }
47 },
48})
49
50router.beforeEach(async (to, _from, next) => {
51 const boot = window.__TD_BOOT__ || {}
52 document.title = to.meta.title ? `${to.meta.title} · ${boot.siteTitle || 'MNBT'}` : boot.siteTitle || 'MNBT'
53
54 if (!authState.initialized) {
55 await initAuth()
56 }
57 if (to.meta.guest && authState.loggedIn) {
58 next('/')
59 return
60 }
61 if (to.meta.auth && !authState.loggedIn) {
62 next('/login')
63 return
64 }
65 // 插件能力校验:对应插件未启用时自动禁用(hasXxx 仅在明确 false 时拦截,兼容旧注入缺字段)
66 const cap = to.meta.cap
67 if (cap === 'shop' && boot.hasShop === false) {
68 next('/')
69 return
70 }
71 if (cap === 'docker' && boot.hasDocker === false) {
72 next('/')
73 return
74 }
75 if (cap === 'user' && boot.hasUser === false) {
76 next('/')
77 return
78 }
79 if (cap === 'site' && boot.hasSite === false) {
80 next('/')
81 return
82 }
83 next()
84})
85
86export default router