Files
opencloud/src/router/index.ts
T

154 lines
4.7 KiB
TypeScript

import { createRouter, createWebHistory } from 'vue-router'
import { getCloudTypeName } from '@/lib/cloudTypes'
import { applyRouteSeo } from '@/lib/seo'
import { useAuthStore } from '@/stores/auth'
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes: [
{
path: '/',
name: 'map',
component: () => import('@/views/map/MapView.vue'),
meta: {
title: '实时云图地图',
description: '在 OpenCloud 地图上浏览社区拍摄的云图,按位置查看天空观测记录。',
},
},
{
path: '/login',
name: 'login',
component: () => import('@/views/auth/LoginView.vue'),
meta: { title: '登录', noindex: true },
},
{
path: '/register',
name: 'register',
component: () => import('@/views/auth/RegisterView.vue'),
meta: { title: '注册', noindex: true },
},
{
path: '/forgot-password',
name: 'forgot-password',
component: () => import('@/views/auth/ForgotPasswordView.vue'),
meta: { title: '忘记密码', noindex: true },
},
{
path: '/auth/confirm',
name: 'auth-confirm',
component: () => import('@/views/auth/AuthConfirmView.vue'),
meta: { title: '确认邮箱', noindex: true },
},
{
path: '/auth/reset-password',
name: 'auth-reset-password',
component: () => import('@/views/auth/ResetPasswordView.vue'),
meta: { title: '重置密码', noindex: true },
},
{
path: '/upload',
name: 'upload',
component: () => import('@/views/upload/UploadView.vue'),
meta: { requiresAuth: true, title: '上传云图', noindex: true },
},
{
path: '/encyclopedia',
name: 'encyclopedia',
component: () => import('@/views/encyclopedia/EncyclopediaView.vue'),
meta: {
title: '云朵图鉴',
description: '浏览 10 种基础云属,了解云型特征,并通过拍摄云图点亮个人图鉴。',
},
},
{
path: '/encyclopedia/:id',
name: 'cloud-type',
component: () => import('@/views/encyclopedia/CloudTypeView.vue'),
meta: {
title: route => getCloudTypeName(route.params.id),
description: route => `查看 ${getCloudTypeName(route.params.id)} 的识别要点、公开云图和社区观测记录。`,
},
},
{
path: '/gallery',
name: 'gallery',
component: () => import('@/views/gallery/GalleryView.vue'),
meta: {
title: '云图画廊',
description: '按时间浏览 OpenCloud 社区公开分享的云图照片和观测记录。',
},
},
{
path: '/community',
name: 'community',
component: () => import('@/views/community/CommunityView.vue'),
meta: {
title: '社区',
description: '查看 OpenCloud 社区动态,发现更多天空观测和云图收藏。',
},
},
{
path: '/profile',
name: 'profile',
component: () => import('@/views/profile/ProfileView.vue'),
meta: { requiresAuth: true, title: '个人主页', noindex: true },
},
{
path: '/profile/settings',
name: 'profile-settings',
component: () => import('@/views/profile/ProfileSettingsView.vue'),
meta: { requiresAuth: true, title: '账号设置', noindex: true },
},
{
path: '/profile/:id',
name: 'user-profile',
component: () => import('@/views/profile/ProfileView.vue'),
meta: {
title: '用户云图档案',
description: '查看用户公开分享的云图记录、图鉴进度和拍摄时间线。',
},
},
{
path: '/admin',
name: 'admin',
component: () => import('@/views/admin/AdminView.vue'),
meta: { requiresAuth: true, requiresAdmin: true, title: '管理后台', noindex: true },
},
{
path: '/403',
name: 'forbidden',
component: () => import('@/views/system/ForbiddenView.vue'),
meta: { title: '无权访问', noindex: true },
},
{
path: '/:pathMatch(.*)*',
name: 'not-found',
component: () => import('@/views/system/NotFoundView.vue'),
meta: { title: '页面不存在', noindex: true },
},
],
scrollBehavior() {
return { top: 0 }
},
})
router.beforeEach((to, _from, next) => {
const authStore = useAuthStore()
if (to.meta.requiresAuth && !authStore.isLoggedIn) {
next({ name: 'login', query: { redirect: to.fullPath } })
} else if (to.meta.requiresAdmin && !authStore.isAdmin) {
next({ name: 'forbidden' })
} else if ((to.name === 'login' || to.name === 'register') && authStore.isLoggedIn) {
next({ name: 'map' })
} else {
next()
}
})
router.afterEach(to => {
applyRouteSeo(to)
})
export default router