1
0
mirror of https://github.com/zclzone/vue-naive-admin.git synced 2026-03-10 21:50:22 +08:00
This commit is contained in:
zclzone
2023-12-07 21:55:23 +08:00
commit cfeb813b62
401 changed files with 11125 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
/**********************************
* @Author: Ronnie Zhang
* @LastEditor: Ronnie Zhang
* @LastEditTime: 2023/12/05 21:24:46
* @Email: zclzone@outlook.com
* Copyright © 2023 Ronnie Zhang(大脸怪) | https://isme.top
**********************************/
import { createPageLoadingGuard } from './page-loading-guard'
import { createPageTitleGuard } from './page-title-guard'
import { createPermissionGuard } from './permission-guard'
import { createTabGuard } from './tab-guard'
export function setupRouterGuards(router) {
createPageLoadingGuard(router)
createPermissionGuard(router)
createPageTitleGuard(router)
createTabGuard(router)
}

View File

@@ -0,0 +1,23 @@
/**********************************
* @Author: Ronnie Zhang
* @LastEditor: Ronnie Zhang
* @LastEditTime: 2023/12/05 21:24:53
* @Email: zclzone@outlook.com
* Copyright © 2023 Ronnie Zhang(大脸怪) | https://isme.top
**********************************/
export function createPageLoadingGuard(router) {
router.beforeEach(() => {
$loadingBar.start()
})
router.afterEach(() => {
setTimeout(() => {
$loadingBar.finish()
}, 200)
})
router.onError(() => {
$loadingBar.error()
})
}

View File

@@ -0,0 +1,20 @@
/**********************************
* @Author: Ronnie Zhang
* @LastEditor: Ronnie Zhang
* @LastEditTime: 2023/12/05 21:25:00
* @Email: zclzone@outlook.com
* Copyright © 2023 Ronnie Zhang(大脸怪) | https://isme.top
**********************************/
const baseTitle = import.meta.env.VITE_TITLE
export function createPageTitleGuard(router) {
router.afterEach((to) => {
const pageTitle = to.meta?.title
if (pageTitle) {
document.title = `${pageTitle} | ${baseTitle}`
} else {
document.title = baseTitle
}
})
}

View File

@@ -0,0 +1,37 @@
/**********************************
* @Author: Ronnie Zhang
* @LastEditor: Ronnie Zhang
* @LastEditTime: 2023/12/05 21:25:07
* @Email: zclzone@outlook.com
* Copyright © 2023 Ronnie Zhang(大脸怪) | https://isme.top
**********************************/
import { useAuthStore } from '@/store'
import api from '@/api'
const WHITE_LIST = ['/login', '/404', '/role-select']
export function createPermissionGuard(router) {
router.beforeEach(async (to) => {
const authStore = useAuthStore()
const token = authStore.accessToken
/** 没有token */
if (!token) {
if (WHITE_LIST.includes(to.path)) return true
return { path: 'login', query: { ...to.query, redirect: to.path } }
}
// 有token的情况
if (to.path === '/login') return { path: '/' }
if (WHITE_LIST.includes(to.path)) return true
const routes = router.getRoutes()
if (routes.find((route) => route.name === to.name)) return true
// 判断是无权限还是404
const { data: hasMenu } = await api.validateMenuPath(to.path)
return hasMenu
? { name: '403', query: { path: to.fullPath }, state: { from: 'permission-guard' } }
: { name: '404', query: { path: to.fullPath } }
})
}

View File

@@ -0,0 +1,23 @@
/**********************************
* @Author: Ronnie Zhang
* @LastEditor: Ronnie Zhang
* @LastEditTime: 2023/12/05 21:25:17
* @Email: zclzone@outlook.com
* Copyright © 2023 Ronnie Zhang(大脸怪) | https://isme.top
**********************************/
import { useTabStore } from '@/store'
export const EXCLUDE_TAB = ['/404', '/403', '/login', '/role-select']
export function createTabGuard(router) {
router.afterEach((to) => {
if (EXCLUDE_TAB.includes(to.path)) return
const tabStore = useTabStore()
const { name, fullPath: path } = to
const title = to.meta?.title
const icon = to.meta?.icon
const keepAlive = to.meta?.keepAlive
tabStore.addTab({ name, path, title, icon, keepAlive })
})
}