备份:大改造前的完整版本 - 修复合同细节/付款节点/文件上传/施工管理/项目保存等BUG

This commit is contained in:
root
2026-05-15 12:02:24 +08:00
parent 1cad1e438d
commit fad28741bd
6157 changed files with 5147 additions and 877912 deletions
+52
View File
@@ -0,0 +1,52 @@
import { useEffect } from 'react';
import { FormInstance } from 'antd';
interface UseFormGuardOptions {
form: FormInstance;
enabled?: boolean;
onSaveDraft?: () => void;
}
/**
* 表单离开拦截 Hook
* - 浏览器刷新/关闭拦截 (beforeunload)
* - 移动端切后台时保存草稿 (visibilitychange + pagehide)
*
* 注意:路由跳转拦截需要在使用组件中手动处理(因为 BrowserRouter 不支持 useBlocker
*/
function useFormGuard(options: UseFormGuardOptions) {
const { form, enabled = true, onSaveDraft } = options;
// 浏览器刷新/关闭拦截
useEffect(() => {
if (!enabled) return;
const handler = (e: BeforeUnloadEvent) => {
if (form.isFieldsTouched()) {
e.preventDefault();
}
};
window.addEventListener('beforeunload', handler);
return () => window.removeEventListener('beforeunload', handler);
}, [form, enabled]);
// 移动端:页面切到后台时保存草稿
useEffect(() => {
if (!enabled || !onSaveDraft) return;
const handler = () => {
if (document.visibilityState === 'hidden' && form.isFieldsTouched()) {
onSaveDraft();
}
};
document.addEventListener('visibilitychange', handler);
const pageHideHandler = () => {
if (form.isFieldsTouched()) onSaveDraft();
};
window.addEventListener('pagehide', pageHideHandler);
return () => {
document.removeEventListener('visibilitychange', handler);
window.removeEventListener('pagehide', pageHideHandler);
};
}, [form, enabled, onSaveDraft]);
}
export default useFormGuard;