hookehuyr

feat(路由): 重构未完成表单弹框逻辑并优化导航行为

将重复的未完成表单确认弹框逻辑提取到独立工具模块
使用location.replace替代href避免浏览器历史记录问题
优化取消操作后的跳转行为,删除cookie后跳转至首页
......@@ -9,9 +9,9 @@ import { createRouter, createWebHashHistory } from 'vue-router';
import RootRoute from './route.js';
import asyncRoutesArr from "./mock/routes"
import generateRoutes from './utils/generateRoute'
import { showConfirmDialog, Loading } from "vant";
import { Loading } from "vant";
import Cookies from 'js-cookie';
import { styleColor } from "@/constant.js";
import { showUnfinishedFormDialog } from '@/utils/dialogControl.js';
import { getCycleListAPI } from '@/api/cycle';
// TAG: 路由配置表
......@@ -130,20 +130,17 @@ router.beforeEach((to, from, next) => {
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') {
showConfirmDialog({
title: '温馨提示',
message: '您还未完成的表单,是否继续?',
confirmButtonColor: styleColor.baseColor,
cancelButtonText: '删除',
closeOnPopstate: false,
})
.then(() => {
showUnfinishedFormDialog(
() => {
// 用户选择继续,跳转到目标页面
next();
})
.catch(() => {
},
() => {
// 用户选择删除,删除cookie并跳转
Cookies.remove(to.query.code);
next();
});
}
);
} else {
next();
}
......@@ -181,21 +178,17 @@ router.beforeEach((to, from, next) => {
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(() => { // 通过后把数据绑定上去
showUnfinishedFormDialog(
() => {
// 用户选择继续,跳转到目标页面
next();
})
.catch(() => { // 删除cookie
},
() => {
// 用户选择删除,删除cookie并跳转
Cookies.remove(to.query.code);
next();
});
}
);
} else {
next();
}
......
import { showConfirmDialog } from 'vant';
import { styleColor } from '@/constant.js';
// 弹框显示状态管理
const DIALOG_SHOWN_KEY = 'unfinished_form_dialog_shown';
/**
* 检查弹框是否已经显示过
* @returns {boolean} 是否已显示过
*/
export function hasDialogShown() {
return sessionStorage.getItem(DIALOG_SHOWN_KEY) === 'true';
}
/**
* 标记弹框已显示
*/
export function markDialogShown() {
sessionStorage.setItem(DIALOG_SHOWN_KEY, 'true');
}
/**
* 重置弹框状态(用于新的会话)
*/
export function resetDialogState() {
sessionStorage.removeItem(DIALOG_SHOWN_KEY);
}
/**
* 显示未完成表单确认弹框(带全局控制)
* @param {Function} onConfirm - 确认回调
* @param {Function} onCancel - 取消回调
* @returns {Promise|null} 如果已显示过则返回null,否则返回弹框Promise
*/
export function showUnfinishedFormDialog(onConfirm, onCancel) {
// 如果已经显示过,直接执行确认回调
if (hasDialogShown()) {
if (onConfirm) {
onConfirm();
}
return null;
}
// 标记为已显示
markDialogShown();
// 显示弹框
return showConfirmDialog({
title: '温馨提示',
message: '您还未完成的表单,是否继续?',
confirmButtonColor: styleColor.baseColor,
cancelButtonText: '删除',
closeOnPopstate: false,
})
.then(() => {
if (onConfirm) {
onConfirm();
}
})
.catch(() => {
if (onCancel) {
onCancel();
}
});
}
\ No newline at end of file
......@@ -25,8 +25,10 @@ onMounted(() => {
let raw_url = encodeURIComponent(location.origin + location.pathname + $route.query.href); // 未授权的地址
// TAG: 开发环境测试数据
const short_url = `/srv/?f=custom_form&a=openid&res=${raw_url}&form_code=${$route.query.code}`;
location.href = import.meta.env.DEV
// 使用 replace 方法替代 href,避免在浏览器历史中留下记录
// 这样用户点击后退按钮时不会回到授权页面
window.location.replace(import.meta.env.DEV
? `${short_url}&openid=${import.meta.env.VITE_OPENID}`
: `${short_url}`;
: `${short_url}`);
})
</script>
......
......@@ -39,7 +39,7 @@ import { ref, onMounted, nextTick, onUnmounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { getCycleListAPI } from '@/api/cycle';
import { styleColor } from '@/constant.js';
import { showConfirmDialog } from 'vant';
import { showUnfinishedFormDialog } from '@/utils/dialogControl.js';
import Cookies from 'js-cookie';
const $router = useRouter();
......@@ -216,22 +216,17 @@ const checkUnfinishedForm = (route) => {
const existingCookie = Cookies.get(route.query.code);
if (existingCookie && route.query.force_back !== '1') {
// 显示确认对话框
showConfirmDialog({
title: '温馨提示',
message: '您还未完成的表单,是否继续?',
confirmButtonColor: styleColor.baseColor,
cancelButtonText: '删除',
closeOnPopstate: false,
})
.then(() => {
showUnfinishedFormDialog(
() => {
// 用户选择继续,跳转到目标页面
$router.replace(route);
})
.catch(() => {
// 用户选择删除,清除cookie后跳转
},
() => {
// 用户选择删除,删除cookie并跳转到首页
Cookies.remove(route.query.code);
$router.replace(route);
});
$router.replace({ path: '/', query: { x_cycle: selectedCycle.value, cycle_selected: '1' } });
}
);
} else {
// 没有未完成的表单,直接跳转
$router.replace(route);
......