From 940966019f1d6bee6ea97af4c0373dbb25992098 Mon Sep 17 00:00:00 2001
From: zhou zhou <3272660260@qq.com>
Date: 星期四, 18 十二月 2025 11:43:21 +0800
Subject: [PATCH] #添加了一个页面,在home中添加了进入这个页面的按钮,pages.json中的pages数组中注册了该页面

---
 pages/sockroom/sockroom.vue |  445 ++++++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 304 insertions(+), 141 deletions(-)

diff --git a/pages/sockroom/sockroom.vue b/pages/sockroom/sockroom.vue
index f5199ee..8751742 100644
--- a/pages/sockroom/sockroom.vue
+++ b/pages/sockroom/sockroom.vue
@@ -1,18 +1,24 @@
 <template>
 	<view class="sockpage">
 		<view class="column">
-			<!-- 鎵樼洏缂栫爜鏍忥紙瀵瑰簲鍚庣barcode锛� -->
 			<view class="form-card">
 				<view class="form-row">
 					<view class="label">
 						<text class="required">*</text>鎵樼洏缂栫爜锛�
 					</view>
-					<view class="picker-wrap" @click="scan" hover-class="picker-hover">
+					<view class="picker-wrap" hover-class="picker-hover">
 						<view class="input-box">
-							<text class="input-placeholder" v-if="!barcode">鐐瑰嚮鎵爜褰曞叆</text>
-							<text class="input-value" v-else>{{barcode}}</text>
+							<input 
+							id="pdacode"
+							type="text"
+							ref="barcodeInput"
+							v-model="barcode"
+							placeholder="璇锋壂鐮�(12浣嶄互涓嬩负鎵樼洏鐮�)"
+							:focus="focusState"
+							@focus="focusFn"
+							@blur="focusState = false"
+							/>
 						</view>
-						<view class="scan-icon">馃摲</view>
 					</view>
 				</view>
 			</view>
@@ -44,8 +50,9 @@
 							class="material-input" 
 							type="text" 
 							v-model="matList.model"
-							placeholder="璇疯緭鍏ュ嵎鍙�"
+							placeholder="璇锋壂鐮佹垨杈撳叆鍗峰彿"
 							placeholder-class="input-placeholder"
+							ref="modelInput"
 						/>
 					</view>
 					<view class="material-item">
@@ -54,8 +61,9 @@
 							class="material-input" 
 							type="text" 
 							v-model="matList.batch"
-							placeholder="璇疯緭鍏ョ鍙�"
+							placeholder="璇锋壂鐮佹垨杈撳叆绠卞彿"
 							placeholder-class="input-placeholder"
+							ref="batchInput"
 						/>
 					</view>
 					<view class="material-item">
@@ -74,7 +82,7 @@
 							class="material-input" 
 							type="number" 
 							v-model="matList.rollExtent"
-							placeholder="璇疯緭鍏ラ暱搴�(m)"
+							placeholder="璇疯緭鍏ラ暱搴�"
 							placeholder-class="input-placeholder"
 						/>
 					</view>
@@ -84,7 +92,7 @@
 							class="material-input" 
 							type="number" 
 							v-model="matList.weight"
-							placeholder="璇疯緭鍏ュ噣閲�(kg)"
+							placeholder="璇疯緭鍏ュ噣閲�"
 							placeholder-class="input-placeholder"
 						/>
 					</view>
@@ -94,7 +102,7 @@
 							class="material-input" 
 							type="number" 
 							v-model="matList.roughWeight"
-							placeholder="璇疯緭鍏ユ瘺閲�(kg)"
+							placeholder="璇疯緭鍏ユ瘺閲�"
 							placeholder-class="input-placeholder"
 						/>
 					</view>
@@ -113,7 +121,7 @@
 			
 			<!-- 鎿嶄綔鎸夐挳缁� -->
 			<view class="btn-group">
-				<button class="operate-btn scan-btn" @click="scantwo">鎵揣鐗╃爜</button>
+				<button class="operate-btn scan-btn" @click="startScan">寮�濮嬫壂鐮�</button>
 				<button class="operate-btn submit-btn" @click="submit">鎴愬搧鍏ュ簱</button>
 			</view>
 		</view>
@@ -131,8 +139,18 @@
 		// 椤甸潰鍔犺浇鏃� mode 涓虹┖锛岃皟鐢ㄦ帴鍙e悗鎵嶅~鍏呮暟鎹�
 		this.POSTinfo(); 
 	},
+
+	mounted() {
+		this.setupScanListener();
+	},
+
+	beforeDestroy() {
+		this.removeScanListener();
+	},
+
 	data() {
 		return {
+			focusState: false,
 			baseUrl: '',
 			barcodeNum:'',
 			barcode: '', // 鎵樼洏鐮�
@@ -149,16 +167,21 @@
 				anfme: 1.0, // 榛樿鍊�1.0
 				rollExtent: '', // 鍗烽暱搴�
 				joint: 0, // 鎺ュご
-				cutting: 2, // 榛樿鈥滃惁鈥�
-				qualified: 0, // 榛樿鈥滃惁鈥�
+				cutting: 2,
+				qualified: 0,
 				modelFront: '', // 鍒嗗垏鍓嶇缂栧彿
 				batchFront: '' ,// 鍒嗗垏鍓嶅嵎鍙�
 			},
-			// 鍏抽敭1锛氬垵濮嬪寲 mode 涓虹┖鏁扮粍 鈫� 椤甸潰鍔犺浇鏃剁鍨嬫棤鏁版嵁
+			// 鍒濆鍖� mode 涓虹┖鏁扮粍 鈫� 椤甸潰鍔犺浇鏃剁鍨嬫棤鏁版嵁
 			mode: [], 
 			index_mode: 0,
 			index_num: 0,
-			submitData: {} // 鏈�缁堟彁浜ょ粰鍚庣鐨勬暟鎹�
+			submitData: {} ,// 鏈�缁堟彁浜ょ粰鍚庣鐨勬暟鎹�
+			scanBuffer: '', // 娣诲姞鎵爜缂撳啿鍖�
+			scanTimeout: null, // 鎵爜瓒呮椂瀹氭椂鍣�
+			isScanning: false, // 鎵爜鐘舵�佹爣蹇�
+			scanMode: 'tray', // 鎵爜妯″紡: tray-鎵樼洏鐮�, material-鐗╂枡鐮�
+			lastFillTarget: null // 璁板綍涓婃濉厖鐨勭洰鏍�
 		}
 	},
 	methods: {
@@ -179,14 +202,218 @@
 				}
 			})
 		},
+		
+		// 璁剧疆鎵爜鐩戝惉
+		setupScanListener() {
+			// 绉婚櫎涔嬪墠鐨勭洃鍚�
+			this.removeScanListener();
+			
+			// 鐩戝惉閿洏杈撳叆浜嬩欢
+			document.addEventListener('keydown', this.handleKeyDown);
+		},
+		
+		// 绉婚櫎鐩戝惉
+		removeScanListener() {
+			document.removeEventListener('keydown', this.handleKeyDown);
+			if (this.scanTimeout) {
+				clearTimeout(this.scanTimeout);
+			}
+		},
+		
+		// 閿洏浜嬩欢澶勭悊
+		handleKeyDown(e) {
+			// 鎺掗櫎鍔熻兘閿紙F1-F12绛夛級
+			if (e.key.length > 1 && !['Enter', 'Tab'].includes(e.key)) {
+				return;
+			}
+			
+			// PDA鎵爜閫氬父浠ュ洖杞︾粨鏉�
+			if (e.key === 'Enter') {
+				e.preventDefault(); // 闃绘榛樿琛屼负
+				this.processScanCode();
+			} else {
+				// 绱Н鎵爜瀛楃锛堟帓闄hift绛変慨楗伴敭锛�
+				if (!e.ctrlKey && !e.altKey && !e.metaKey && e.key.length === 1) {
+					this.scanBuffer += e.key;
+					
+					// 閲嶇疆瓒呮椂瀹氭椂鍣�
+					if (this.scanTimeout) {
+						clearTimeout(this.scanTimeout);
+					}
+					
+					// 璁剧疆瓒呮椂锛堝亣璁炬壂鐮侀棿闅旇秴杩�200ms琛ㄧず涓�娆℃壂鐮佸畬鎴愶級
+					this.scanTimeout = setTimeout(() => {
+						this.processScanCode();
+					}, 200);
+				}
+			}
+		},
+		
+		// 澶勭悊鎵爜缁撴灉
+		processScanCode() {
+			if (!this.scanBuffer) return;
+			
+			const scanResult = this.scanBuffer.trim();
+			this.scanBuffer = '';
+			
+			// 鍒ゆ柇鎵爜闀垮害鍐冲畾濉厖浣嶇疆
+			if (scanResult.length <= 12) {
+				// 12浣嶄互涓嬪~鍏呭埌鎵樼洏缂栫爜
+				this.barcode = scanResult;
+				this.lastFillTarget = 'tray';
+				uni.showToast({
+					title: '鎵樼洏鐮佸凡濉厖',
+					icon: 'success',
+					duration: 1000
+				});
+			} else {
+				// 12浣嶄互涓婂~鍏呭埌鐗╂枡淇℃伅
+				this.autoFillMaterialInfo(scanResult);
+				this.lastFillTarget = 'material';
+				uni.showToast({
+					title: '鐗╂枡鐮佸凡濉厖',
+					icon: 'success',
+					duration: 1000
+				});
+			}
+			
+			// 娓呴櫎瀹氭椂鍣�
+			if (this.scanTimeout) {
+				clearTimeout(this.scanTimeout);
+				this.scanTimeout = null;
+			}
+			
+			// 鑷姩澶辩劍
+			setTimeout(() => {
+				this.focusState = false;
+			}, 100);
+		},
+		
+		// 鑷姩濉厖鐗╂枡淇℃伅
+		autoFillMaterialInfo(code) {
+			// 灏濊瘯瑙f瀽浜岀淮鐮佸唴瀹�
+			if (code.includes('鍗峰彿锛�') && code.includes('绠卞彿锛�')) {
+				// 濡傛灉鏄甫鏍煎紡鐨勪簩缁寸爜
+				const parseResult = this.parseQrCodeText(code);
+				this.matList = { ...this.matList, ...parseResult };
+				console.log('瑙f瀽鍚庣殑鐗╂枡淇℃伅:', parseResult);
+			} else {
+				// 濡傛灉鏄函鏁板瓧/鏉$爜锛屾櫤鑳藉~鍏�
+				this.smartFillMaterialFields(code);
+			}
+		},
+		
+		// 鏅鸿兘濉厖鐗╂枡瀛楁
+		smartFillMaterialFields(code) {
+			// 鏍规嵁涓氬姟閫昏緫鍒ゆ柇锛岃繖閲屽亣璁鹃暱鏉$爜鍙兘鏄嵎鍙锋垨绠卞彿
+			// 濡傛灉鍗峰彿涓虹┖锛屼紭鍏堝~鍏呭嵎鍙�
+			if (!this.matList.model) {
+				this.matList.model = code;
+			} 
+			// 濡傛灉鍗峰彿宸叉湁鍊间絾绠卞彿涓虹┖锛屽~鍏呯鍙�
+			else if (!this.matList.batch) {
+				this.matList.batch = code;
+			}
+			// 濡傛灉涓よ�呴兘鏈夊�硷紝鎻愮ず鐢ㄦ埛
+			else {
+				uni.showModal({
+					title: '鎻愮ず',
+					content: `鎵弿鍒伴暱鏉$爜: ${code}\n璇烽�夋嫨濉厖浣嶇疆锛歚,
+					showCancel: true,
+					cancelText: '鍗峰彿',
+					confirmText: '绠卞彿',
+					success: (res) => {
+						if (res.confirm) {
+							this.matList.batch = code;
+						} else if (res.cancel) {
+							this.matList.model = code;
+						}
+					}
+				});
+			}
+		},
+		
+		// 瑙f瀽浜岀淮鐮佹枃鏈紙鐢ㄤ簬鎵揣鐗╃爜鍔熻兘锛�
+		parseQrCodeText(qrText) {
+			const result = {};
+			const lines = qrText.split('\n');
+			
+			lines.forEach(line => {
+				if (line.includes('鍗峰彿锛�')) {
+					result.model = line.split('鍗峰彿锛�')[1]?.trim() || '';
+				}
+				if (line.includes('绠卞彿锛�')) {
+					result.batch = line.split('绠卞彿锛�')[1]?.trim() || '';
+				}
+				if (line.includes('瑙勬牸锛�')) {
+					result.matnr = line.split('瑙勬牸锛�')[1]?.trim() || '';
+				}
+				// 鍙互鏍规嵁闇�瑕佹坊鍔犳洿澶氬瓧娈电殑瑙f瀽
+			});
+			
+			return result;
+		},
+		
+		// 鎵嬪姩寮�濮嬫壂鐮�
+		startScan() {
+			// 娓呯┖褰撳墠鍊煎苟鑱氱劍
+			this.scanBuffer = '';
+			
+			this.$nextTick(() => {
+				this.focusState = true;
+				
+				// 寤惰繜纭繚input宸茶仛鐒�
+				setTimeout(() => {
+					const input = document.getElementById('pdacode');
+					if (input) {
+						input.focus();
+						input.select();
+					}
+				}, 100);
+			});
+		},
+		
+		// 淇敼focusFn
+		focusFn() {
+			this.focusState = true;
+			this.scanBuffer = ''; // 鑱氱劍鏃舵竻绌虹紦鍐插尯
+		},
+		
+		// 淇濈暀鍘熸湁鐨勬壂璐х墿鐮佸姛鑳�
+		scantwo() {
+			uni.scanCode({
+				autoDecodeCharSet: true,
+				scanType: ['qrCode', 'barCode'],
+				success: (res) => {
+					const qrResult = res.result.trim();
+					this.autoFillMaterialInfo(qrResult);
+					
+					uni.showToast({
+						title: '鐗╂枡淇℃伅鎵爜鎴愬姛',
+						icon: 'success',
+						duration: 2000
+					});
+				},
+				fail: (err) => {
+					console.log('鎵爜澶辫触:', err);
+					uni.showToast({
+						title: '鎵爜澶辫触鎴栧彇娑�',
+						icon: 'none'
+					});
+				}
+			});
+		},
+		
 		modePickerChange(e) {
 			this.index_mode = e.detail.value;
 			this.boxType = this.mode[this.index_mode]; // 鍚屾鍒癰oxType
 		},
+		
 		numPickerChange(e) {
-			this.index_num = e.detail.value
+			this.index_num = e.detail.value;
 		},
-		// 鍏抽敭2锛氫慨鏀归噸缃柟娉� 鈫� 娓呯┖ mode 鍙婄浉鍏崇姸鎬�
+		
+		// 淇敼閲嶇疆鏂规硶
 		resetForm() {
 			this.barcodeNum = '';
 			this.barcode = ''; 
@@ -194,9 +421,11 @@
 			this.index_mode = 0; 
 			this.index_num = 0; 
 			this.submitData = {}; 
-			// 娓呯┖ mode 鏁扮粍 鈫� 绠卞瀷閫夋嫨鍣ㄦ仮澶嶆棤鏁版嵁鐘舵��
 			this.mode = [];
-			// 閲嶇疆鐗╂枡淇℃伅锛堜繚鐣欓粯璁ゅ�硷級
+			this.lastFillTarget = null;
+			this.scanBuffer = '';
+			
+			// 閲嶇疆鐗╂枡淇℃伅
 			this.matList = {
 				matnr: '',
 				maktx: '',
@@ -213,129 +442,68 @@
 				modelFront: '',
 				batchFront: ''
 			};
-			// 鍙�夛細閲嶇疆鍚庨噸鏂拌姹傜鍨嬫暟鎹紙鏍规嵁闇�姹傞�夋嫨锛�
-			// this.POSTinfo();
 		},
-		submit(){
-		    // 鏍¢獙蹇呭~椤�
-		    if(!this.barcode) return uni.showToast({title: '璇锋壂鎻忔墭鐩樼紪鐮�', icon: 'none'})
-		    if(!this.boxType) return uni.showToast({title: '璇烽�夋嫨绠卞瀷', icon: 'none'})
-		    if(!this.matList.model || !this.matList.batch) return uni.showToast({title: '璇峰~鍐欏嵎鍙�/绠卞彿', icon: 'none'})
-		    
-		    // 鏁版嵁绫诲瀷杞崲
-		    const matItem = {
-		        ...this.matList,
-		        weight: this.matList.weight ? Number(this.matList.weight) : null,
-		        roughWeight: this.matList.roughWeight ? Number(this.matList.roughWeight) : null,
-		        rollExtent: this.matList.rollExtent ? Number(this.matList.rollExtent) : null,
-		        joint: this.matList.joint ? Number(this.matList.joint) : 0,
-		        anfme: 1.0,
-		        cutting: 2,
-		        qualified: 0
-		    }
-		    
-		    // 缁勮鎻愪氦鏁版嵁
-		    this.submitData = {
-		        barcode: this.barcode,
-		        palletizingNo: this.palletizingNo,
-		        boxType: this.boxType,
-		        matList: [matItem]
-		    }
-		    console.log('淇绫诲瀷鍚庣殑鎻愪氦鏁版嵁锛�', this.submitData)
-		    
-		    // 鎻愪氦璇锋眰
-		    uni.request({
-		        url: this.baseUrl + '/mobile/truss/comd/auth/v2',
-		        method: 'POST',
-		        header: {'Content-Type': 'application/json'},
-		        data: this.submitData,
-		        success: (res) => {
-		            console.log('鍚庣杩斿洖锛�', res)
-		            uni.showToast({title:'鎻愪氦鎴愬姛',icon: 'success'})
-		            // 鎻愪氦鎴愬姛鍚庨噸缃墍鏈夋暟鎹�
-		            this.resetForm();
-		            // 鍙�夛細閲嶇疆鍚庨噸鏂版媺鍙栫鍨嬫暟鎹紝鏂逛究涓嬫鎿嶄綔
-		            // setTimeout(() => {
-		            //     this.POSTinfo();
-		            // }, 300);
-		        },
-		        fail: (err) => {
-		            console.error('璇锋眰澶辫触璇︽儏锛�', err)
-		            uni.showToast({title:'鎻愪氦澶辫触',icon: 'none'})
-		        },
-		        complete: (res) => {
-		            console.log('璇锋眰瀹屾垚鐘舵�侊細', res)
-		        }
-		    })
-		},
-		scan(){
-			uni.scanCode({
-				autoDecodeCharSet: true,
-				scanType: ['barCode'],
-				onlyFromCamera: true,
-				continuousScan: false,
-				camera: 'back',
-				timeout: 5000,
+		
+		submit() {
+			// 鏍¢獙蹇呭~椤�
+			if (!this.barcode) {
+				uni.showToast({title: '璇锋壂鎻忔墭鐩樼紪鐮�', icon: 'none'});
+				return;
+			}
+			
+			if (!this.boxType) {
+				uni.showToast({title: '璇烽�夋嫨绠卞瀷', icon: 'none'});
+				return;
+			}
+			
+			if (!this.matList.model || !this.matList.batch) {
+				uni.showToast({title: '璇峰~鍐欏嵎鍙峰拰绠卞彿', icon: 'none'});
+				return;
+			}
+			
+			// 鏁版嵁绫诲瀷杞崲
+			const matItem = {
+				...this.matList,
+				weight: this.matList.weight ? Number(this.matList.weight) : null,
+				roughWeight: this.matList.roughWeight ? Number(this.matList.roughWeight) : null,
+				rollExtent: this.matList.rollExtent ? Number(this.matList.rollExtent) : null,
+				joint: this.matList.joint ? Number(this.matList.joint) : 0,
+				anfme: 1.0,
+				cutting: 2,
+				qualified: 0
+			};
+			
+			// 缁勮鎻愪氦鏁版嵁
+			this.submitData = {
+				barcode: this.barcode,
+				palletizingNo: this.palletizingNo,
+				boxType: this.boxType,
+				matList: [matItem]
+			};
+			
+			console.log('鎻愪氦鏁版嵁锛�', this.submitData);
+			
+			// 鎻愪氦璇锋眰
+			uni.request({
+				url: this.baseUrl + '/mobile/truss/comd/auth/v2',
+				method: 'POST',
+				header: {'Content-Type': 'application/json'},
+				data: this.submitData,
 				success: (res) => {
-					this.barcodeNum = res.result
-					this.barcode = res.result
-					uni.showToast({title: `鎵樼洏缂栫爜鎵爜鎴愬姛`,icon: 'success'})
+					console.log('鍚庣杩斿洖锛�', res);
+					
+					if (res.data && res.data.code === 200) {
+						uni.showToast({title: '鎻愪氦鎴愬姛', icon: 'success'});
+						this.resetForm();
+					} else {
+						uni.showToast({title: res.data.message || '鎻愪氦澶辫触', icon: 'none'});
+					}
 				},
 				fail: (err) => {
-					uni.showToast({title: '鎵爜澶辫触鎴栧彇娑�',icon: 'none'})
-				}
-			})
-		},
-		parseQrCodeText(text) {
-			const result = {};
-			const regMap = {
-				model: /鍗峰彿锛�(.+)/,
-				batch: /绠卞彿锛�(.+)/,
-				matnr: /瑙勬牸锛�(.+)/,
-				rollExtent: /闀垮害锛�(.+?)\s*m/,
-				weight: /鍑�閲嶏細(.+?)\s*kg/,
-				roughWeight: /姣涢噸锛�(.+?)\s*kg/,
-				joint: /鎺ュご锛�(.+?)\s*涓�/
-			};
-			Object.keys(regMap).forEach(key => {
-				const match = text.match(regMap[key]);
-				if (match && match[1]) {
-					result[key] = match[1].trim();
+					console.error('璇锋眰澶辫触璇︽儏锛�', err);
+					uni.showToast({title: '缃戠粶璇锋眰澶辫触', icon: 'none'});
 				}
 			});
-			return result;
-		},
-		scantwo(){
-			uni.scanCode({
-				autoDecodeCharSet:true,
-				scanType: ['qrCode'], 
-				success: (res) => {
-					const qrResult = res.result.trim();
-					this.barcodeNum = qrResult;
-					let parseResult = {};
-					
-					try {
-						parseResult = JSON.parse(qrResult);
-						parseResult = {
-							model: parseResult.鍗峰彿 || '',
-							batch: parseResult.绠卞彿 || '',
-							matnr: parseResult.瑙勬牸 || '',
-							rollExtent: (parseResult.闀垮害 || '').replace(/\s*m/g, '') || '',
-							weight: (parseResult.鍑�閲� || '').replace(/\s*kg/g, '') || '',
-							roughWeight: (parseResult.姣涢噸 || '').replace(/\s*kg/g, '') || '',
-							joint: (parseResult.鎺ュご || '').replace(/\s*涓�/g, '') || ''
-						};
-					} catch (e) {
-						parseResult = this.parseQrCodeText(qrResult);
-					}
-					
-					this.matList = { ...this.matList, ...parseResult };
-					uni.showToast({title: `鐗╂枡淇℃伅鎵爜鎴愬姛`,icon: 'success'});
-				},
-				fail: (err) => {
-					uni.showToast({title: '鎵爜澶辫触鎴栧彇娑�',icon: 'none'})
-				}
-			})
 		}
 	}
 }
@@ -426,11 +594,6 @@
 
 .input-box:focus-within {
 	border-color: #f97316;
-}
-
-/* 鍗犱綅绗︽牱寮� */
-.input-placeholder {
-	color: #9ca3af;
 }
 
 /* 杈撳叆鍊兼牱寮� */

--
Gitblit v1.9.1