first commit
This commit is contained in:
9
resources/admin/router/constantRoutes.ts
Normal file
9
resources/admin/router/constantRoutes.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { RouteRecordRaw } from 'vue-router'
|
||||
// @ts-ignore
|
||||
const modules = import.meta.glob('@/module/**/views/router.ts', { eager: true })
|
||||
let moduleRoutes: RouteRecordRaw[] = []
|
||||
|
||||
Object.keys(modules).forEach(routePath => {
|
||||
moduleRoutes = moduleRoutes.concat(modules[routePath].default)
|
||||
})
|
||||
export default moduleRoutes
|
68
resources/admin/router/guard/index.ts
Normal file
68
resources/admin/router/guard/index.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
import { useUserStore } from '/admin/stores/modules/user'
|
||||
import { getAuthToken, removeAuthToken, setPageTitle } from '/admin/support/Helper'
|
||||
import progress from '/admin/support/progress'
|
||||
import { WhiteListPage } from '/admin/enum/app'
|
||||
import { Router, RouteRecordRaw } from 'vue-router'
|
||||
import { usePermissionsStore } from '/admin/stores/modules/user/permissions'
|
||||
import { Menu } from '/admin/types/Menu'
|
||||
|
||||
const guard = (router: Router) => {
|
||||
// white list
|
||||
const whiteList: string[] = [WhiteListPage.LOGIN_PATH, WhiteListPage.NOT_FOUND_PATH]
|
||||
|
||||
router.beforeEach(async (to, from, next) => {
|
||||
// set page title
|
||||
setPageTitle(to.meta.title as unknown as string)
|
||||
// page start
|
||||
progress.start()
|
||||
// 获取用户的 token
|
||||
const authToken = getAuthToken()
|
||||
// 如果 token 存在
|
||||
if (authToken) {
|
||||
// 如果进入 /login 页面,重定向到首页
|
||||
if (to.path === WhiteListPage.LOGIN_PATH) {
|
||||
next({ path: '/' })
|
||||
} else {
|
||||
const userStore = useUserStore()
|
||||
// 获取用户ID
|
||||
if (userStore.getId) {
|
||||
next()
|
||||
} else {
|
||||
try {
|
||||
// 阻塞获取用户信息
|
||||
await userStore.getUserInfo()
|
||||
// 如果后端没有返回 permissions,前台则只使用静态路由
|
||||
if (userStore.getPermissions !== undefined) {
|
||||
// 挂载路由(实际是从后端获取用户的权限)
|
||||
const permissionStore = usePermissionsStore()
|
||||
// 动态路由挂载
|
||||
const asyncRoutes = permissionStore.getAsyncMenusFrom(userStore.getPermissions)
|
||||
asyncRoutes.forEach((route: Menu) => {
|
||||
router.addRoute(route as unknown as RouteRecordRaw)
|
||||
})
|
||||
}
|
||||
next({ ...to, replace: true })
|
||||
} catch (e) {
|
||||
removeAuthToken()
|
||||
next({ path: `${WhiteListPage.LOGIN_PATH}?redirect=${to.path}` })
|
||||
}
|
||||
}
|
||||
}
|
||||
progress.done()
|
||||
} else {
|
||||
// 如果不在白名单
|
||||
if (whiteList.indexOf(to.path) !== -1) {
|
||||
next()
|
||||
} else {
|
||||
next({ path: WhiteListPage.LOGIN_PATH })
|
||||
}
|
||||
progress.done()
|
||||
}
|
||||
})
|
||||
|
||||
router.afterEach(() => {
|
||||
progress.done()
|
||||
})
|
||||
}
|
||||
|
||||
export default guard
|
65
resources/admin/router/index.ts
Normal file
65
resources/admin/router/index.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
|
||||
import type { App } from 'vue'
|
||||
// module routers
|
||||
import moduleRoutes from './constantRoutes'
|
||||
|
||||
export const constantRoutes: RouteRecordRaw[] = [
|
||||
{
|
||||
path: '/dashboard',
|
||||
component: () => import('/admin/layout/index.vue'),
|
||||
children: [
|
||||
{
|
||||
path: '',
|
||||
name: 'Dashboard',
|
||||
meta: { title: 'Dashboard', icon: 'home', hidden: false },
|
||||
component: () => import('/admin/views/dashboard/index.vue'),
|
||||
},
|
||||
],
|
||||
},
|
||||
]
|
||||
// @ts-ignore
|
||||
.concat(moduleRoutes)
|
||||
|
||||
// default routes, it will not change to menus
|
||||
const defaultRoutes: RouteRecordRaw[] = [
|
||||
{
|
||||
path: '/',
|
||||
name: '/',
|
||||
component: () => import('/admin/layout/index.vue'),
|
||||
redirect: '/dashboard',
|
||||
children: [
|
||||
{
|
||||
path: '/404',
|
||||
name: '404',
|
||||
meta: { title: '404' },
|
||||
component: () => import('/admin/components/404/index.vue'),
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
path: '/login',
|
||||
name: 'login',
|
||||
component: () => import('/admin/views/login/index.vue'),
|
||||
},
|
||||
// 未定义路有重定向到 404
|
||||
{
|
||||
path: '/:pathMatch(.*)*',
|
||||
redirect: '/404',
|
||||
},
|
||||
]
|
||||
|
||||
const routes = constantRoutes.concat(defaultRoutes)
|
||||
const router = createRouter({
|
||||
history: createWebHashHistory(),
|
||||
routes,
|
||||
// 路由滚动
|
||||
scrollBehavior(to, from, savedPosition) {
|
||||
return savedPosition || { top: 0, behavior: 'smooth' }
|
||||
},
|
||||
})
|
||||
|
||||
export function bootstrapRouter(app: App) {
|
||||
app.use(router)
|
||||
}
|
||||
|
||||
export default router
|
Reference in New Issue
Block a user