feat:回退操作

This commit is contained in:
许永平
2025-07-05 21:40:14 +08:00
parent 0385a4cfa9
commit 17ec8ecf25
13 changed files with 461 additions and 199 deletions

View File

@@ -1,52 +0,0 @@
import { NavigateFunction } from 'react-router-dom';
/**
* 智能返回上一页
* 如果没有上一页历史记录,则返回到主页
* @param navigate React Router的navigate函数
* @param fallbackPath 默认返回路径,默认为'/'
*/
export const smartGoBack = (navigate: NavigateFunction, fallbackPath: string = '/') => {
// 检查是否有历史记录
if (window.history.length > 1) {
// 尝试返回上一页
navigate(-1);
} else {
// 如果没有历史记录,返回到主页
navigate(fallbackPath);
}
};
/**
* 带确认的智能返回
* @param navigate React Router的navigate函数
* @param message 确认消息
* @param fallbackPath 默认返回路径
*/
export const smartGoBackWithConfirm = (
navigate: NavigateFunction,
message: string = '确定要返回吗?未保存的内容将丢失。',
fallbackPath: string = '/'
) => {
if (window.confirm(message)) {
smartGoBack(navigate, fallbackPath);
}
};
/**
* 检查是否可以返回上一页
* @returns boolean
*/
export const canGoBack = (): boolean => {
return window.history.length > 1;
};
/**
* 获取返回按钮文本
* @param hasHistory 是否有历史记录
* @returns 按钮文本
*/
export const getBackButtonText = (hasHistory?: boolean): string => {
const canBack = hasHistory !== undefined ? hasHistory : canGoBack();
return canBack ? '返回上一页' : '返回主页';
};