From 6154a8a3c4f94fd8ead478822983c85b1eed3707 Mon Sep 17 00:00:00 2001 From: chenxt <10125295+chen-xintao97@user.noreply.gitee.com> Date: Tue, 20 Jan 2026 13:33:10 +0800 Subject: [PATCH] 左侧扫码 --- src/mes/common/commonOperationBarComponent/index.js | 146 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------- src/mes/indexMes/index.js | 2 +- 2 files changed, 140 insertions(+), 8 deletions(-) diff --git a/src/mes/common/commonOperationBarComponent/index.js b/src/mes/common/commonOperationBarComponent/index.js index 0b50939..4230acf 100644 --- a/src/mes/common/commonOperationBarComponent/index.js +++ b/src/mes/common/commonOperationBarComponent/index.js @@ -908,6 +908,7 @@ const useCommonOperationBarComponentEvent = props => { }; const CommonOperationBarComponent = basProps => { + const [value, setValue] = useState(""); if (basProps.belongsToForm && basProps.costomFormOperationBar) return basProps.costomFormOperationBar; @@ -1005,6 +1006,7 @@ const CommonOperationBarComponent = basProps => { const { app, form, sModelsId, searchValue } = props; let showTypeProps = {}; + // let leftShowTypeProps = {} if (commonUtils.isNotEmptyObject(searchField)) { if (sModelsId === "12710101117203990977610") { const { gdsconfigformslave } = config; @@ -1050,9 +1052,12 @@ const CommonOperationBarComponent = basProps => { } let searchProductProps = {}; + const searchProductConfig = gdsconfigformslave.filter( item => item.sName === "sSearchProductName" ); + const letfSearchField = btnLeftConfig.find(x => x.sControlName.includes('ScanLoadMaterial') && x.sButtonParam) + if (commonUtils.isNotEmptyArr(searchProductConfig)) { searchProductConfig[0].placeholder = searchProductConfig[0].showName; searchProductProps = { @@ -1194,7 +1199,7 @@ const CommonOperationBarComponent = basProps => { } const btnSearch = commonFunc.showLocalMessage(props, 'BtnSearch', '查询'); - const letfSearchField = btnLeftConfig.find(x => x.sControlName.includes('ScanLoadMaterial') && x.sButtonParam) + let leftShowTypeProps = { app, record: {}, @@ -1203,7 +1208,7 @@ const CommonOperationBarComponent = basProps => { form, formId: sModelsId, getFloatNum: props.getFloatNum, - onChange: props.onSearchValueChange, + showConfig: { ...letfSearchField, placeholder: '请输入' @@ -1221,9 +1226,116 @@ const CommonOperationBarComponent = basProps => { getDateFormat: props.getDateFormat, bNoMesBlue: true, record: props.slaveData?.[0], - onDropDownBlur: props.onDropDownBlur + onDropDownBlur: props.onDropDownBlur, } const isShow = props.sModelType?.includes('/indexPage/commonList') && props.sModelsId?.includes("172117190122117670007462620") + const scanEventTime = useRef(-1); + const userInfo = commonUtils.getAppData("userinfo") + // 执行扫码枪指令 + const handleScanCodeInstructs = (value, bCostom) => { + scanEventTime.current = -1; + // this.handleBtnClick() + props.onProcedureCall({ + btnConfig: letfSearchField, + sValue: { + sproName: value, + inMap: userInfo?.sMachineGuid + }, + onSuccess: () => { + props.onRefresh && props.onRefresh(); + // setValue('') + }, + onConfirm: () => { }, + onError: () => { } + }); + }; + // 判断是否扫码枪输入 + const isScannerInput = event => { + const thresholdMs = 50; // 扫码枪输入间隔时间阈值(毫秒) + const lastInputTime = event.timeStamp; + + if (!event.repeat) { + if (!("lastInputTime" in window)) { + window.lastInputTime = lastInputTime; + scanEventTime.current = 0; + return false; + } + + const timeDiff = lastInputTime - window.lastInputTime; + window.lastInputTime = lastInputTime; + + if (timeDiff <= thresholdMs) { + scanEventTime.current += 1; + return true; + } + } + + scanEventTime.current = 0; + return false; + }; + const timer = useRef(null); + // const timerNew = useRef(null); + useEffect( + () => { + if (value === "") return; + + // 10s后清空数据 + clearTimeout(timer.current); + timer.current = setTimeout(() => { + setValue(""); + }, 10000); + }, + [value] + ); + + const scannedData = useRef(""); + const scannedDataTimer = useRef(null); + + useEffect(() => { + const keypressEvent = event => { + if ( + ["textarea", "input"].includes( + document.activeElement.tagName.toLowerCase() + ) + ) + return; + + if (document.activeElement.tagName.toLowerCase() === "button") { + document.querySelectorAll("button").forEach(btn => { + btn.blur(); + }); + } + + // 获取按键的字符值 + const keyValue = event.key; + + // 检查是否为可打印字符 + if (keyValue.length === 1) { + // 添加到已扫描的数据字符串 + scannedData.current += keyValue; + } else if (event.key === "Enter") { + // 如果扫码枪以回车键结束,则处理已扫描的数据 + setValue(scannedData.current); + handleScanCodeInstructs(scannedData.current, false); + + // 清空已扫描的数据 + scannedData.current = ""; + } + + clearTimeout(scannedDataTimer.current); + scannedDataTimer.current = setTimeout(() => { + scannedData.current = ""; + }, 200); + }; + + // 监听 keypress 事件 + document.body.addEventListener("keypress", keypressEvent); + + return () => { + // 在组件卸载时移除事件监听 + document.body.removeEventListener("keypress", keypressEvent); + }; + }, []); return (