'admin-21.02.25:处理后端控制路由刷新界面404空白问题'

This commit is contained in:
lyt 2021-02-25 18:45:27 +08:00
parent 7ce8e26f0a
commit 1009a1a1c6
5 changed files with 65 additions and 63 deletions

View File

@ -439,7 +439,7 @@ const staticRoutes: Array<RouteRecordRaw> = [
}, },
{ {
path: '/404', path: '/404',
name: '404', name: 'notFound',
component: () => import('/@/views/error/404.vue'), component: () => import('/@/views/error/404.vue'),
meta: { meta: {
title: '找不到此页面' title: '找不到此页面'
@ -447,22 +447,18 @@ const staticRoutes: Array<RouteRecordRaw> = [
}, },
{ {
path: '/401', path: '/401',
name: '401', name: 'noPower',
component: () => import('/@/views/error/401.vue'), component: () => import('/@/views/error/401.vue'),
meta: { meta: {
title: '没有权限' title: '没有权限'
} }
} },
] ]
// 定义404界面 // 定义404界面
const pathMatch = { const pathMatch = {
path: '/:pathMatch(.*)', path: '/:path(.*)*',
name: 'pathMatch', redirect: '/404'
component: () => import('/@/views/error/404.vue'),
meta: {
title: '页面找不到重定向'
}
} }
// 获取目录下的 .vue 全部文件,参考 viteimport.meta.glob // 获取目录下的 .vue 全部文件,参考 viteimport.meta.glob
@ -475,35 +471,41 @@ const router = createRouter({
}) })
// 后端控制路由isRequestRoutes 为 true则开启后端控制路由 // 后端控制路由isRequestRoutes 为 true则开启后端控制路由
// store.dispatch('setBackEndControlRoutes', 'admin')
export function getBackEndControlRoutes() { export function getBackEndControlRoutes() {
const token = getSession('token')
if (!token) return false
store.dispatch('setUserInfos') store.dispatch('setUserInfos')
NProgress.remove()
const auth = store.state.userInfos.authPageList[0] // 模拟 admin 与 test const auth = store.state.userInfos.authPageList[0] // 模拟 admin 与 test
if (auth !== 'admin') { if (auth !== 'admin') {
getMenuAdmin().then((res: any) => { return new Promise((resolve, reject) => {
setBackEndControlRoutesFun(res) getMenuAdmin().then((res: any) => {
setBackEndControlRoutesFun(res)
if (res.data) resolve(res)
else reject('请求错误')
})
}) })
} else { } else {
getMenuTest().then((res: any) => { return new Promise((resolve, reject) => {
setTimeout(() => { getMenuTest().then((res: any) => {
setBackEndControlRoutesFun(res) setBackEndControlRoutesFun(res)
}, 500) if (res.data) resolve(res)
else reject('请求错误')
})
}) })
} }
} }
// 后端控制路由,模拟执行路由初始化 // 后端控制路由,模拟执行路由数据初始化
export function setBackEndControlRoutesFun(res: any) { export function setBackEndControlRoutesFun(res: any) {
const oldRoutes = JSON.parse(JSON.stringify(res.data)) const oldRoutes = JSON.parse(JSON.stringify(res.data))
store.dispatch('setBackEndControlRoutes', oldRoutes) store.dispatch('setBackEndControlRoutes', oldRoutes)
dynamicRoutes[0].children = backEndRouter(res.data) dynamicRoutes[0].children = backEndRouter(res.data)
setAddRoute() // 添加动态路由
router.addRoute(pathMatch) // 添加404界面 router.addRoute(pathMatch) // 添加404界面
setAddRoute() // 添加动态路由
setFilterMenu() // 过滤权限菜单 setFilterMenu() // 过滤权限菜单
setCacheTagsViewRoutes() // 添加 keepAlive 缓存 setCacheTagsViewRoutes() // 添加 keepAlive 缓存
// window.location.href = window.location.href // 页面刷新时防止404未找到方法临时使用控制台报黄 console.log(dynamicRoutes)
console.log(store) console.log(router.getRoutes())
} }
// 后端控制路由,后端路由 component 转换 // 后端控制路由,后端路由 component 转换
@ -625,7 +627,7 @@ export function setFilterRouteEnd() {
// 添加动态路由 // 添加动态路由
export function setAddRoute() { export function setAddRoute() {
setFilterRouteEnd().map((route: any) => { setFilterRouteEnd().map((route: any) => {
router.addRoute(route as RouteRecordRaw) router.addRoute((route as unknown) as RouteRecordRaw)
}) })
} }
@ -639,6 +641,8 @@ export function resetRoute() {
// 初始化方法,防止刷新时丢失 // 初始化方法,防止刷新时丢失
export function initAllFun() { export function initAllFun() {
const token = getSession('token')
if (!token) return false
store.dispatch('setUserInfos') // 触发初始化用户信息 store.dispatch('setUserInfos') // 触发初始化用户信息
setAddRoute() // 添加动态路由 setAddRoute() // 添加动态路由
router.addRoute(pathMatch) // 添加404界面 router.addRoute(pathMatch) // 添加404界面
@ -647,13 +651,13 @@ export function initAllFun() {
} }
// 初始化方法执行 // 初始化方法执行
if (!store.state.themeConfig.isRequestRoutes && getSession('token')) initAllFun() if (!store.state.themeConfig.isRequestRoutes) initAllFun()
// 后端控制路由isRequestRoutes 为 true则开启后端控制路由 // 后端控制路由isRequestRoutes 为 true则开启后端控制路由
if (store.state.themeConfig.isRequestRoutes && getSession('token')) getBackEndControlRoutes() if (store.state.themeConfig.isRequestRoutes) getBackEndControlRoutes()
// 路由加载前 // 路由加载前
router.beforeEach((to, from, next) => { router.beforeEach((to, from, next) => {
document.title = `${to.meta.title} - vue-admin-wonderful-next` || 'vue-admin-wonderful-next' document.title = `${to.meta.title} - vue-admin-wonderful-next` || `vue-admin-wonderful-next`
NProgress.configure({ showSpinner: false }) NProgress.configure({ showSpinner: false })
NProgress.start() NProgress.start()
const token = getSession('token') const token = getSession('token')

View File

@ -62,3 +62,24 @@
transform: scale(1); transform: scale(1);
} }
} }
/* 404401 过渡动画
------------------------------- */
@keyframes error-num {
0% {
transform: translateY(60px);
opacity: 0;
}
100% {
transform: translateY(0);
opacity: 1;
}
}
@keyframes error-img {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}

View File

@ -91,23 +91,5 @@ export default {
} }
} }
} }
@keyframes error-num {
0% {
transform: translateY(60px);
opacity: 0;
}
100% {
transform: translateY(0);
opacity: 1;
}
}
@keyframes error-img {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
} }
</style> </style>

View File

@ -89,23 +89,5 @@ export default {
} }
} }
} }
@keyframes error-num {
0% {
transform: translateY(60px);
opacity: 0;
}
100% {
transform: translateY(0);
opacity: 1;
}
}
@keyframes error-img {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
} }
</style> </style>

View File

@ -44,6 +44,8 @@ import {
import { useStore } from "/@/store/index.ts"; import { useStore } from "/@/store/index.ts";
import { setSession } from "/@/utils/storage.ts"; import { setSession } from "/@/utils/storage.ts";
import { formatAxis } from "/@/utils/formatTime"; import { formatAxis } from "/@/utils/formatTime";
import themeConfig from "/@/utils/themeConfig.ts";
export default defineComponent({ export default defineComponent({
name: "login", name: "login",
setup() { setup() {
@ -68,7 +70,6 @@ export default defineComponent({
}; };
// //
const onSignIn = () => { const onSignIn = () => {
let currentTimeInfo = currentTime.value;
let defaultAuthPageList: Array<string> = []; let defaultAuthPageList: Array<string> = [];
let defaultAuthBtnList: Array<string> = []; let defaultAuthBtnList: Array<string> = [];
// admin meta.auth // admin meta.auth
@ -108,9 +109,21 @@ export default defineComponent({
// 1(vuex) // 1(vuex)
store.dispatch("setUserInfos", userInfos); store.dispatch("setUserInfos", userInfos);
// 2 // 2
if (store.state.themeConfig.isRequestRoutes) initAllFun(); if (!store.state.themeConfig.isRequestRoutes) {
initAllFun();
signInSuccess();
}
// isRequestRoutes true // isRequestRoutes true
else getBackEndControlRoutes(); else {
getBackEndControlRoutes().then((res) => {
signInSuccess();
});
}
};
//
const signInSuccess = () => {
//
let currentTimeInfo = currentTime.value;
// //
router.push("/"); router.push("/");
// //