Files
lexgo/admin/src/router/index.js
T

27 lines
1.2 KiB
JavaScript

import { createRouter, createWebHashHistory } from 'vue-router'
import { session } from '../store'
import Layout from '../layout/index.vue'
import Login from '../views/Login.vue'
import Accounts from '../views/Accounts.vue'
import AuditLogs from '../views/AuditLogs.vue'
import Dictionaries from '../views/Dictionaries.vue'
const router = createRouter({ history: createWebHashHistory(), routes: [
{ path: '/login', component: Login },
{ path: '/', component: Layout, children: [
{ path: '', redirect: '/accounts' },
{ path: 'accounts', component: Accounts, meta: { title: '账号管理' } },
{ path: 'dictionaries', component: Dictionaries, meta: { title: '英语词典' } },
{ path: 'login-logs', component: AuditLogs, props: { kind: 'login' }, meta: { title: '登录日志' } },
{ path: 'operation-logs', component: AuditLogs, props: { kind: 'operation' }, meta: { title: '操作日志' } }
] },
{ path: '/:pathMatch(.*)*', redirect: '/accounts' }
] })
router.beforeEach(async to => {
if (to.path === '/login') return true
try {
if (!session.state.user && !await session.restore()) return '/login'
if (session.state.user?.role !== 'admin') return '/login'
} catch { return '/login' }
})
export default router