hookehuyr

refactor(router): 将异步路由守卫改为Promise链式调用

避免使用async/await语法,改用Promise.then/catch处理异步逻辑
添加错误处理流程,在周期选择检查失败时继续正常路由流程
......@@ -79,7 +79,7 @@ const checkCycleSelection = async (to) => {
}
};
router.beforeEach(async (to, from, next) => {
router.beforeEach((to, from, next) => {
// 使用404为中转页面,避免动态路由没有渲染出来,控制台报警告问题
if (to.path == '/404' && to.redirectedFrom != undefined) {
// 模拟异步操作
......@@ -93,8 +93,8 @@ router.beforeEach(async (to, from, next) => {
next({ ...to.redirectedFrom, replace: true });
}, 1000);
} else {
// 检查是否需要周期选择
const needsCycleSelection = await checkCycleSelection(to);
// 检查是否需要周期选择 - 使用Promise.then避免async/await
checkCycleSelection(to).then(needsCycleSelection => {
if (needsCycleSelection) {
// 保存目标路由到sessionStorage
sessionStorage.setItem('cycle_target_route', JSON.stringify({
......@@ -110,6 +110,7 @@ router.beforeEach(async (to, from, next) => {
return;
}
// 继续原有的表单检查逻辑
if (to.query.page_type === 'add' || to.query.page_type === undefined) { // 表单为新增状态, 检查是否有未完成的表单信息
const existingCookie = Cookies.get(to.query.code);
if (existingCookie && to.query.force_back !== '1') {
......@@ -134,6 +135,34 @@ router.beforeEach(async (to, from, next) => {
} else {
next()
}
}).catch(error => {
console.error('周期选择检查失败:', error);
// 出错时继续正常流程
if (to.query.page_type === 'add' || to.query.page_type === undefined) { // 表单为新增状态, 检查是否有未完成的表单信息
const existingCookie = Cookies.get(to.query.code);
if (existingCookie && to.query.force_back !== '1') {
// TAG: 只能在进入路由之前判断,不然很多组件数据不渲染
showConfirmDialog({
title: '温馨提示',
message: '您还未完成的表单,是否继续?',
confirmButtonColor: styleColor.baseColor,
cancelButtonText: '删除',
closeOnPopstate: false,
})
.then(() => { // 通过后把数据绑定上去
next();
})
.catch(() => { // 删除cookie
Cookies.remove(to.query.code);
next();
});
} else {
next();
}
} else {
next()
}
});
}
})
......