181 lines
3.4 KiB
JavaScript
181 lines
3.4 KiB
JavaScript
import { createRouter, createWebHistory } from 'vue-router'
|
|
import { useUserStore } from '@/stores/user'
|
|
import NProgress from 'nprogress'
|
|
|
|
const routes = [
|
|
{
|
|
path: '/',
|
|
name: 'Home',
|
|
redirect: '/transfers',
|
|
meta: {
|
|
title: '首页'
|
|
}
|
|
},
|
|
{
|
|
path: '/home',
|
|
name: 'HomePage',
|
|
component: () => import('@/views/Home.vue'),
|
|
meta: {
|
|
title: '首页'
|
|
}
|
|
},
|
|
{
|
|
path: '/shop',
|
|
name: 'Shop',
|
|
component: () => import('@/views/Shop.vue'),
|
|
meta: {
|
|
title: '积分商城'
|
|
}
|
|
},
|
|
{
|
|
path: '/product/:id',
|
|
name: 'ProductDetail',
|
|
component: () => import('@/views/ProductDetail.vue'),
|
|
meta: {
|
|
title: '商品详情'
|
|
}
|
|
},
|
|
{
|
|
path: '/login',
|
|
name: 'Login',
|
|
component: () => import('@/views/Login.vue'),
|
|
meta: {
|
|
title: '登录',
|
|
hideForAuth: true
|
|
}
|
|
},
|
|
{
|
|
path: '/register',
|
|
name: 'Register',
|
|
component: () => import('@/views/Register.vue'),
|
|
meta: {
|
|
title: '注册',
|
|
hideForAuth: true
|
|
}
|
|
},
|
|
{
|
|
path: '/profile',
|
|
name: 'Profile',
|
|
component: () => import('@/views/Profile.vue'),
|
|
meta: {
|
|
title: '个人中心',
|
|
requiresAuth: true
|
|
}
|
|
},
|
|
{
|
|
path: '/orders',
|
|
name: 'Orders',
|
|
component: () => import('@/views/Orders.vue'),
|
|
meta: {
|
|
title: '我的订单',
|
|
requiresAuth: true
|
|
}
|
|
},
|
|
{
|
|
path: '/points-history',
|
|
name: 'PointsHistory',
|
|
component: () => import('@/views/PointsHistory.vue'),
|
|
meta: {
|
|
title: '积分记录',
|
|
requiresAuth: true
|
|
}
|
|
},
|
|
|
|
{
|
|
path: '/task-center',
|
|
name: 'TaskCenter',
|
|
component: () => import('@/views/TaskCenter.vue'),
|
|
meta: {
|
|
title: '任务中心',
|
|
requiresAuth: true
|
|
}
|
|
},
|
|
{
|
|
path: '/transfers',
|
|
name: 'Transfers',
|
|
component: () => import('@/views/Transfers.vue'),
|
|
meta: {
|
|
title: '转账',
|
|
requiresAuth: true
|
|
}
|
|
},
|
|
{
|
|
path: '/matching',
|
|
name: 'Matching',
|
|
component: () => import('@/views/Matching.vue'),
|
|
meta: {
|
|
title: '资金匹配',
|
|
requiresAuth: true
|
|
}
|
|
},
|
|
{
|
|
path: '/about',
|
|
name: 'About',
|
|
component: () => import('@/views/About.vue'),
|
|
meta: {
|
|
title: '关于我们'
|
|
}
|
|
},
|
|
{
|
|
path: '/:pathMatch(.*)*',
|
|
name: 'NotFound',
|
|
component: () => import('@/views/NotFound.vue'),
|
|
meta: {
|
|
title: '页面不存在'
|
|
}
|
|
}
|
|
]
|
|
|
|
const router = createRouter({
|
|
history: createWebHistory('/frontend/'),
|
|
routes,
|
|
scrollBehavior(to, from, savedPosition) {
|
|
if (savedPosition) {
|
|
return savedPosition
|
|
} else {
|
|
return { top: 0 }
|
|
}
|
|
}
|
|
})
|
|
|
|
// 路由守卫
|
|
router.beforeEach(async (to, from, next) => {
|
|
NProgress.start()
|
|
|
|
const userStore = useUserStore()
|
|
|
|
// 设置页面标题
|
|
if (to.meta.title) {
|
|
document.title = `${to.meta.title} - 易融宝`
|
|
}
|
|
|
|
// 检查是否需要认证
|
|
if (to.meta.requiresAuth) {
|
|
if (!userStore.isAuthenticated) {
|
|
// 尝试从本地存储恢复登录状态
|
|
await userStore.checkAuth()
|
|
|
|
if (!userStore.isAuthenticated) {
|
|
next({
|
|
name: 'Login',
|
|
query: { redirect: to.fullPath }
|
|
})
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
// 如果已登录用户访问登录/注册页面,重定向到转账管理
|
|
if (to.meta.hideForAuth && userStore.isAuthenticated) {
|
|
next({ name: 'Transfers' })
|
|
return
|
|
}
|
|
|
|
next()
|
|
})
|
|
|
|
router.afterEach(() => {
|
|
NProgress.done()
|
|
})
|
|
|
|
export default router |