diff --git a/src/components/Jodetail/JobPickExecutionsecondscan.tsx b/src/components/Jodetail/JobPickExecutionsecondscan.tsx index deb22ce..d43a661 100644 --- a/src/components/Jodetail/JobPickExecutionsecondscan.tsx +++ b/src/components/Jodetail/JobPickExecutionsecondscan.tsx @@ -549,52 +549,17 @@ const JobPickExecution: React.FC = ({ filterArgs, onBack }) => { useEffect(() => { - unassignExecutedRef.current = false; - - const executeUnassign = () => { - if (unassignExecutedRef.current || !currentPickOrderId || !currentUserId) { - return; - } - unassignExecutedRef.current = true; - console.log("🔄 Executing unassign:", currentPickOrderId); - handleUnassignSync(currentPickOrderId); - }; - - const handleVisibilityChange = () => { - if (document.visibilityState === 'hidden') { - // 页面隐藏时(可能是刷新或登出),执行 unassign - executeUnassign(); - } - }; - - const handleBeforeUnload = () => { - // beforeunload 中无法执行异步操作,使用同步版本 - if (currentPickOrderId && currentUserId) { - console.log("🚪 Page unloading, attempting to unassign pick order:", currentPickOrderId); - executeUnassign(); - } - }; - - document.addEventListener('visibilitychange', handleVisibilityChange); - window.addEventListener('beforeunload', handleBeforeUnload); - + return () => { - document.removeEventListener('visibilitychange', handleVisibilityChange); - window.removeEventListener('beforeunload', handleBeforeUnload); - - // 组件卸载时执行 unassign(但只在未执行过的情况下) - if (!unassignExecutedRef.current && currentPickOrderId && currentUserId) { - executeUnassign(); - } - // Cleanup QR scanner if (isManualScanning) { console.log("🧹 Second scan component unmounting, stopping QR scanner..."); stopScan(); resetScan(); } + }; - }, [currentPickOrderId, currentUserId, isManualScanning, handleUnassignSync, stopScan, resetScan]); + }, [isManualScanning, stopScan, resetScan]); // 添加:处理返回按钮 const handleBack = useCallback(async () => { diff --git a/src/components/ProductionProcess/ProductionProcessPage.tsx b/src/components/ProductionProcess/ProductionProcessPage.tsx index eafda87..033f903 100644 --- a/src/components/ProductionProcess/ProductionProcessPage.tsx +++ b/src/components/ProductionProcess/ProductionProcessPage.tsx @@ -25,6 +25,7 @@ type PrinterCombo = { interface ProductionProcessPageProps { printerCombo: PrinterCombo[]; } +const STORAGE_KEY = 'productionProcess_selectedMatchingStock'; const ProductionProcessPage: React.FC = ({ printerCombo }) => { const [selectedProcessId, setSelectedProcessId] = useState(null); @@ -35,11 +36,51 @@ const ProductionProcessPage: React.FC = ({ printerCo const { data: session } = useSession() as { data: SessionWithTokens | null }; const currentUserId = session?.id ? parseInt(session.id) : undefined; + // 从 sessionStorage 恢复状态(仅在客户端) + useEffect(() => { + if (typeof window !== 'undefined') { + try { + const saved = sessionStorage.getItem(STORAGE_KEY); + if (saved) { + const parsed = JSON.parse(saved); + // 验证数据有效性 + if (parsed && typeof parsed.jobOrderId === 'number' && typeof parsed.productProcessId === 'number') { + setSelectedMatchingStock(parsed); + console.log("📦 Restored selectedMatchingStock from sessionStorage:", parsed); + } + } + } catch (error) { + console.error("Error restoring selectedMatchingStock:", error); + sessionStorage.removeItem(STORAGE_KEY); + } + } + }, []); + + // 保存状态到 sessionStorage + useEffect(() => { + if (typeof window !== 'undefined') { + if (selectedMatchingStock) { + sessionStorage.setItem(STORAGE_KEY, JSON.stringify(selectedMatchingStock)); + console.log("💾 Saved selectedMatchingStock to sessionStorage:", selectedMatchingStock); + } else { + sessionStorage.removeItem(STORAGE_KEY); + } + } + }, [selectedMatchingStock]); + + // 处理返回列表时清除存储 + const handleBackFromSecondScan = useCallback(() => { + setSelectedMatchingStock(null); + if (typeof window !== 'undefined') { + sessionStorage.removeItem(STORAGE_KEY); + } + }, []); + if (selectedMatchingStock) { return ( setSelectedMatchingStock(null)} + onBack={handleBackFromSecondScan} /> ); }