-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
439 lines (371 loc) · 14.9 KB
/
main.js
File metadata and controls
439 lines (371 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/**
* ScannerKeyboardInput - 键盘与扫码枪输入处理核心逻辑 (IE6 Compatible)
*
* 功能:
* 1. 自动识别键盘手动输入与扫码枪输入
* 2. 扫码枪输入缓冲与解析 (支持自定义起止符)
* 3. 键盘输入缓冲与长度限制
* 4. 防抖与重复提交限制
* 5. 提供丰富的回调钩子用于UI交互
* 6. IE6 完美兼容 (分离 keydown/keypress 处理)
*
* @author Open Source Contributor
*/
(function (global) {
'use strict';
// 检查 JSON 支持,若无则提示
if (typeof JSON === 'undefined') {
if (typeof console !== 'undefined' && console.warn) {
console.warn('Current browser does not support JSON. Please include json2.js for IE6/7 support.');
}
// 尝试简单的 Polyfill 以防止立即崩溃 (虽然 parse 很难 polyfill, 至少留个空对象)
// 强烈建议用户引入 json2.js
if (typeof window.JSON === 'undefined') {
window.JSON = {
parse: function () { throw new Error("JSON.parse not implemented. Please load json2.js"); },
stringify: function () { throw new Error("JSON.stringify not implemented. Please load json2.js"); }
};
}
}
// 输入模式常量
var MODE = {
INIT: 0, // 初始状态
KEYBOARD: 1, // 键盘输入模式
SCAN: 2 // 扫码模式
};
/**
* @class ScannerKeyboardInput
* @param {Object} options 配置项
*/
function ScannerKeyboardInput(options) {
var opts = options || {};
// === 配置项 ===
// 最大输入码长度 (键盘模式)
this.maxCodeLength = opts.maxCodeLength || 8;
// 扫码状态超时时间 (毫秒)
this.scanTimeout = opts.scanTimeout || 2000;
// 短时间内同码重复提交拦截 (毫秒)
this.throttleInterval = opts.throttleInterval || 3000;
// 扫码起始键码 (默认 '{' 的keyCode. 注意: 这是一个 heuristic,
// 在 keydown 中用于快速切模式, 但实际字符在 keypress 中获取)
// Shift + [ = { (keyCode 219 on standard US layout keydown)
this.scanStartKeyCode = opts.scanStartKeyCode || 219;
// 扫码结束键码 (默认 '}' 的keyCode 221)
this.scanEndKeyCode = opts.scanEndKeyCode || 221;
// 重置键码列表 (默认 '*' 的keyCode 106, '/' 111 等)
this.resetKeyCodes = opts.resetKeyCodes || [106, 111, 107, 110, 13];
// === 回调函数 ===
this.onScanStart = opts.onScanStart || function () { };
this.onScanInput = opts.onScanInput || function (val) { };
this.onScanComplete = opts.onScanComplete || function (code) { };
this.onKeyboardInput = opts.onKeyboardInput || function (char, currentLoc, fullCode) { };
this.onKeyboardBackspace = opts.onKeyboardBackspace || function (currentLoc) { };
this.onKeyboardComplete = opts.onKeyboardComplete || function (code) { };
this.onStatusChange = opts.onStatusChange || function (statusName, statusVal) { }; // 'KEYBOARD', 'SCAN', 'INIT'
this.onReset = opts.onReset || function () { };
this.onError = opts.onError || function (msg) { };
this.shouldSubmit = opts.shouldSubmit || null; // 自定义提交校验
// === 内部状态 ===
this.currentMode = MODE.INIT;
this.isJudgedInputType = false;
// 键盘模式状态
this.currentInputLoc = 0;
this.keyboardBuffer = new Array(this.maxCodeLength); // 用于存储每位输入的字符
// 扫码模式状态
this.currentScanValue = '';
this.scanTimerId = null;
// 提交节流状态
this.lastSubmitCode = '';
this.lastSubmitTime = 0;
// 绑定上下文
var self = this;
// 仅处理功能键 / 模式切换触发器
this.handleKeyDown = function (event) {
return self._handleKeyDown(event);
};
// 处理实际字符输入
this.handleKeyPress = function (event) {
return self._handleKeyPress(event);
};
}
/**
* 初始化:自动绑定事件
* @param {HTMLElement} element 要绑定的元素,默认为 document
*/
ScannerKeyboardInput.prototype.init = function (element) {
var el = element || document;
this.addEvent(el, 'keydown', this.handleKeyDown);
this.addEvent(el, 'keypress', this.handleKeyPress);
};
/**
* 销毁:解绑事件
* @param {HTMLElement} element
*/
ScannerKeyboardInput.prototype.destroy = function (element) {
var el = element || document;
this.removeEvent(el, 'keydown', this.handleKeyDown);
this.removeEvent(el, 'keypress', this.handleKeyPress);
};
/**
* 通用事件绑定 (兼容 IE6 attachEvent)
*/
ScannerKeyboardInput.prototype.addEvent = function (element, type, handler) {
if (element.addEventListener) {
element.addEventListener(type, handler, false);
} else if (element.attachEvent) {
element.attachEvent('on' + type, handler);
} else {
element['on' + type] = handler;
}
};
ScannerKeyboardInput.prototype.removeEvent = function (element, type, handler) {
if (element.removeEventListener) {
element.removeEventListener(type, handler, false);
} else if (element.detachEvent) {
element.detachEvent('on' + type, handler);
} else {
element['on' + type] = null;
}
};
/**
* KeyDownHandler: 负责模式识别、功能键处理 (Backspace, 扫码枪特殊起止键)
*/
ScannerKeyboardInput.prototype._handleKeyDown = function (event) {
var evt = event || window.event;
var code = evt.keyCode;
// 1. 预处理:判断是否是重置键
if (this.isResetKey(code)) {
this.forceReset();
this.preventDefault(evt);
return false;
}
// 2. 模式判断 (如果是初始状态)
// 注意:只根据 keydown 判断模式及其初始化。
// 如果是数字键,在 keydown 阶段我们就可以知道用户想输入数字了,
// 但我们在 keypress 阶段才真正收集该数字。
if (!this.isJudgedInputType) {
// 键盘数字区 (字母区 48-57, 小键盘 96-105)
if ((code >= 48 && code <= 57) || (code >= 96 && code <= 105)) {
this.switchMode(MODE.KEYBOARD);
}
// 扫码起始符
else if (code === this.scanStartKeyCode) {
this.switchMode(MODE.SCAN);
this.startScanTimer();
this.onScanStart();
}
}
// 3. 处理不需要字符码的功能键 (Backspace)
if (this.currentMode === MODE.KEYBOARD) {
if (code === 8) { // Backspace
this.handleKeyboardBackspace();
this.preventDefault(evt); // 防止浏览器回退
return false;
}
}
// 扫码模式下,如果遇到结束符 KeyDown,也要处理 (防止有些结束符不触发 keypress)
if (this.currentMode === MODE.SCAN) {
if (code === this.scanEndKeyCode) {
this.finishScan();
this.preventDefault(evt);
}
}
// 对于可打印字符,我们留给 keypress 处理
};
/**
* KeyPressHandler: 负责捕获准确的字符 (区分大小写, 特殊符号)
*/
ScannerKeyboardInput.prototype._handleKeyPress = function (event) {
var evt = event || window.event;
// IE6/7/8: keyCode 是字符码
// 现代: charCode 或 which 是字符码
var charCode = evt.which || evt.keyCode;
// 过滤非可打印字符 (如控制符)
if (charCode < 32 && charCode !== 13) return; // 13是回车,有时候扫码枪带回车
var symbol = String.fromCharCode(charCode);
if (this.currentMode === MODE.KEYBOARD) {
this.handleKeyboardInput(symbol);
this.preventDefault(evt); // 接管输入,不让其上屏到可能存在的 input 框
} else if (this.currentMode === MODE.SCAN) {
this.handleScanInput(symbol, evt);
this.preventDefault(evt);
}
};
/**
* 处理键盘输入 (字符)
*/
ScannerKeyboardInput.prototype.handleKeyboardInput = function (symbol) {
// 只允许数字输入 (根据原逻辑 inferred, 原逻辑 symbol >= '0' && symbol <= '9')
if (symbol >= '0' && symbol <= '9') {
this.keyboardBuffer[this.currentInputLoc] = symbol;
this.onKeyboardInput(symbol, this.currentInputLoc, this.getKeyboardCode());
this.currentInputLoc++;
// 达到最大长度,完成输入
if (this.currentInputLoc >= this.maxCodeLength) {
var fullCode = this.getKeyboardCode();
// 节流与自定义校验
if (this._internalShouldSubmit(fullCode)) {
this.onKeyboardComplete(fullCode);
}
this.forceReset();
}
}
};
/**
* 处理键盘回退 (Backspace)
*/
ScannerKeyboardInput.prototype.handleKeyboardBackspace = function () {
if (this.currentInputLoc > 0) {
this.currentInputLoc--;
this.keyboardBuffer[this.currentInputLoc] = ''; // 清除缓冲
this.onKeyboardBackspace(this.currentInputLoc);
// 如果退格到0位,重置
if (this.currentInputLoc <= 0) {
this.forceReset();
}
} else {
this.forceReset();
}
};
/**
* 处理扫码输入 (字符)
*/
ScannerKeyboardInput.prototype.handleScanInput = function (symbol, evt) {
// 每次敲击重置超时定时器
this.startScanTimer();
// 累积字符
// 注意:这里我们累积所有可打印字符,包括 '{' '}' '"' 等
// 因为我们依赖 keypress,这是准确的字符
this.currentScanValue += symbol;
this.onScanInput(this.currentScanValue);
// 我们已经在 KeyDown 处理了 ScanEndKeyCode (如果是特定功能键)
// 但如果 ScanEndKeyCode 是一个普通字符 (如 '}'), keypress 也会触发
// 不过通常我们建议在 KeyDown 里处理控制逻辑。
// 为防万一,如果 keydown 没捕获到结束符 (例如某些情况下 keydown 和 keypress 并不完全对应)
// 我们这里不做结束符检查,因为 keydown 用 keyCode 检查更靠谱 (对应物理按键)。
// 扫码枪的结束符通常是一个具体的物理按键。
};
/**
* 结束扫码并尝试解析
*/
ScannerKeyboardInput.prototype.finishScan = function () {
this.clearScanTimer();
try {
var payloadStr = this.currentScanValue;
// 容错:去除首尾空白
payloadStr = payloadStr.replace(/^\s+|\s+$/g, '');
// JSON 解析
// 假设扫码枪输出类似 {"code":"..."}
// 容错逻辑:检查是否存在花括号,不存在则尝试补全 (兼容原逻辑)
// 如果 input 为空,直接忽略
if (!payloadStr) {
this.forceReset();
return;
}
if (payloadStr.indexOf('{') === -1) {
payloadStr = '{' + payloadStr;
}
if (payloadStr.indexOf('}') === -1) {
payloadStr = payloadStr + '}';
}
// 只有当看起来像 JSON 才 parse
// 简单检查是否包含 "
if (payloadStr.indexOf('"') === -1) {
// 如果完全没有引号,可能是纯数字或 raw text?
// 原逻辑是强制 parse。抛出异常会被 catch。
}
var scanObj = JSON.parse(payloadStr);
var code = scanObj.code || scanObj.take_code;
if (code && this._internalShouldSubmit(code)) {
this.onScanComplete(code);
}
} catch (err) {
this.onError('扫码数据解析失败: ' + err.message + ' Raw: ' + this.currentScanValue);
if (typeof console !== 'undefined' && console.error) {
console.error(err);
}
}
this.forceReset();
};
/**
* 切换模式
*/
ScannerKeyboardInput.prototype.switchMode = function (mode) {
this.currentMode = mode;
this.isJudgedInputType = true;
var modeName = 'INIT';
if (mode === MODE.KEYBOARD) modeName = 'KEYBOARD';
if (mode === MODE.SCAN) modeName = 'SCAN';
this.onStatusChange(modeName, mode);
};
/**
* 启动/重置扫码超时定时器
*/
ScannerKeyboardInput.prototype.startScanTimer = function () {
this.clearScanTimer();
var self = this;
this.scanTimerId = setTimeout(function () {
self.forceReset();
}, this.scanTimeout);
};
/**
* 清除扫码定时器
*/
ScannerKeyboardInput.prototype.clearScanTimer = function () {
if (this.scanTimerId) {
clearTimeout(this.scanTimerId);
this.scanTimerId = null;
}
};
/**
* 强制重置所有状态
*/
ScannerKeyboardInput.prototype.forceReset = function () {
this.clearScanTimer();
this.currentMode = MODE.INIT;
this.isJudgedInputType = false;
// Reset buffers
this.currentInputLoc = 0;
this.keyboardBuffer = new Array(this.maxCodeLength);
this.currentScanValue = '';
this.onReset();
};
/**
* 获取当前完整的键盘输入码
*/
ScannerKeyboardInput.prototype.getKeyboardCode = function () {
return this.keyboardBuffer.join('');
};
/**
* 内部节流检查
*/
ScannerKeyboardInput.prototype._internalShouldSubmit = function (code) {
var now = (new Date()).getTime();
// 1. 优先调用用户自定义校验
if (typeof this.shouldSubmit === 'function') {
if (!this.shouldSubmit(code)) return false;
}
// 2. 默认节流逻辑
if (code === this.lastSubmitCode && (now - this.lastSubmitTime) < this.throttleInterval) {
return false;
}
this.lastSubmitCode = code;
this.lastSubmitTime = now;
return true;
};
ScannerKeyboardInput.prototype.isResetKey = function (keyCode) {
for (var i = 0; i < this.resetKeyCodes.length; i++) {
if (this.resetKeyCodes[i] === keyCode) return true;
}
return false;
};
ScannerKeyboardInput.prototype.preventDefault = function (evt) {
if (evt.preventDefault) {
evt.preventDefault();
} else {
evt.returnValue = false;
}
};
// 暴露给全局
global.ScannerKeyboardInput = ScannerKeyboardInput;
})(window);