调整扫码widget
This commit is contained in:
@@ -1,115 +1,41 @@
|
|||||||
/** @odoo-module **/
|
/** @odoo-module **/
|
||||||
import { registry } from '@web/core/registry';
|
import { registry } from '@web/core/registry';
|
||||||
import { Component } from '@odoo/owl';
|
import { Component, onWillUpdateProps } from '@odoo/owl';
|
||||||
|
|
||||||
class QRCodeWidget extends Component {
|
class QRCodeWidget extends Component {
|
||||||
// 初始化组件
|
|
||||||
setup() {
|
setup() {
|
||||||
console.log('QRCodeWidget setup');
|
super.setup();
|
||||||
this.qrCodeValue = ''; // 初始化为空字符串,用于存储条码
|
console.log('===================')
|
||||||
this.inputBuffer = ''; // 存储临时输入的字符
|
|
||||||
this.inputTimer = null; // 定时器
|
|
||||||
|
|
||||||
// 显式绑定上下文
|
// 记录初始值
|
||||||
this.onGlobalKeyDown = this.onGlobalKeyDown.bind(this);
|
this.lastValue = this.props.value;
|
||||||
window.addEventListener('keydown', this.onGlobalKeyDown);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 清理事件监听器,防止内存泄漏
|
// 使用 onWillUpdateProps 监听 props 变化
|
||||||
willUnmount() {
|
onWillUpdateProps((nextProps) => {
|
||||||
window.removeEventListener('keydown', this.onGlobalKeyDown);
|
if (nextProps.value && nextProps.value !== this.lastValue) {
|
||||||
if (this.inputTimer) {
|
this.lastValue = nextProps.value;
|
||||||
clearTimeout(this.inputTimer);
|
this.handleRfidUpdate();
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 全局键盘事件监听器
|
|
||||||
onGlobalKeyDown(event) {
|
|
||||||
|
|
||||||
// 如果是Tab键,表示扫码输入结束
|
|
||||||
if (event.key === 'Tab' || event.key === 'Enter') {
|
|
||||||
this.qrCodeValue = this.inputBuffer; // 完整条码赋值
|
|
||||||
console.log('完整条码:', this.qrCodeValue);
|
|
||||||
this.onQRCodeChange(this.qrCodeValue); // 调用父组件的 onQRCodeChange 方法
|
|
||||||
this.inputBuffer = ''; // 清空临时缓冲区
|
|
||||||
event.preventDefault(); // 阻止Tab键的默认行为
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 只处理可打印字符
|
|
||||||
if (event.key.length === 1) {
|
|
||||||
this.inputBuffer += event.key; // 添加到缓冲区
|
|
||||||
// console.log('当前缓冲区:', this.inputBuffer);
|
|
||||||
|
|
||||||
// 清除之前的定时器,重新开始计时
|
|
||||||
if (this.inputTimer) {
|
|
||||||
clearTimeout(this.inputTimer);
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
// 启动一个定时器,如果500ms内没有新的输入,则认为条码输入完成
|
|
||||||
this.inputTimer = setTimeout(() => {
|
|
||||||
this.qrCodeValue = this.inputBuffer;
|
|
||||||
// console.log('定时器触发,完整条码:', this.qrCodeValue);
|
|
||||||
this.inputBuffer = ''; // 清空缓冲区
|
|
||||||
}, 500); // 可以根据需要调整时间
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理二维码输入变更
|
async handleRfidUpdate() {
|
||||||
async onQRCodeChange(qrCodeValue) {
|
const routingTypeField = document.querySelector('[name="routing_type"]');
|
||||||
console.log('onQRCodeChange二维码输入变更', qrCodeValue); // 检查二维码的输入是否被捕获
|
if (routingTypeField) {
|
||||||
|
let fieldValue = routingTypeField.querySelector('span').getAttribute('raw-value');
|
||||||
|
fieldValue = fieldValue ? fieldValue.replace(/["]+/g, '') : null;
|
||||||
|
|
||||||
if (qrCodeValue) {
|
if (fieldValue === '装夹预调') {
|
||||||
// console.log('二维码输入变更');
|
console.log('woshiddddddddddddddddd')
|
||||||
try {
|
const startButton = document.querySelector('[name="button_start"]');
|
||||||
// 发起 RPC 请求
|
if (startButton) {
|
||||||
const result = await this.env.services.rpc('/web/dataset/call_kw', {
|
startButton.click();
|
||||||
model: 'mrp.workorder',
|
|
||||||
method: 'search_read',
|
|
||||||
args: [
|
|
||||||
[['rfid_code', '=', qrCodeValue]], // 查询条件
|
|
||||||
['id'] // 返回的字段
|
|
||||||
],
|
|
||||||
kwargs: {}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (result.length > 0) {
|
|
||||||
console.log('该二维码对应的工单存在!');
|
|
||||||
} else {
|
|
||||||
console.log('未找到对应的工单。');
|
|
||||||
|
|
||||||
const routingTypeField = document.querySelector('[name="routing_type"]');
|
|
||||||
if (routingTypeField) {
|
|
||||||
let fieldValue = routingTypeField.querySelector('span').getAttribute('raw-value');
|
|
||||||
console.log('Routing Type Value:', fieldValue);
|
|
||||||
// 清理多余的引号
|
|
||||||
fieldValue = fieldValue ? fieldValue.replace(/["]+/g, '') : null;
|
|
||||||
console.log(fieldValue);
|
|
||||||
|
|
||||||
if (fieldValue && fieldValue === '装夹预调') {
|
|
||||||
// console.log('routing_type 为装夹预调');
|
|
||||||
|
|
||||||
// 检查 RFID 值
|
|
||||||
if (!qrCodeValue || qrCodeValue.length <= 3) return;
|
|
||||||
|
|
||||||
// 查找 name="button_start" 按钮并触发点击事件
|
|
||||||
const startButton = document.querySelector('[name="button_start"]');
|
|
||||||
if (startButton) {
|
|
||||||
startButton.click();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
|
||||||
console.error('查询工单时出错:', error);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 返回模板名称
|
|
||||||
static template = 'sf_manufacturing.QRCodeWidgetTemplate';
|
static template = 'sf_manufacturing.QRCodeWidgetTemplate';
|
||||||
}
|
}
|
||||||
|
|
||||||
// 将自定义字段注册到字段注册表
|
registry.category('fields').add('qrcode_widget', QRCodeWidget);
|
||||||
registry.category('fields').add('qrcode_widget', QRCodeWidget);
|
|
||||||
@@ -126,6 +126,9 @@
|
|||||||
<field name="model">mrp.workorder</field>
|
<field name="model">mrp.workorder</field>
|
||||||
<field name="inherit_id" ref="mrp.mrp_production_workorder_form_view_inherit"/>
|
<field name="inherit_id" ref="mrp.mrp_production_workorder_form_view_inherit"/>
|
||||||
<field name="arch" type="xml">
|
<field name="arch" type="xml">
|
||||||
|
<form position="inside">
|
||||||
|
<field name="_barcode_scanned" widget="barcode_handler" invisible="1"/>
|
||||||
|
</form>
|
||||||
<!-- <xpath expr="//form" position="inside"> -->
|
<!-- <xpath expr="//form" position="inside"> -->
|
||||||
<!-- <script src="sf_manufacturing/static/src/js/customRFID.js"/> -->
|
<!-- <script src="sf_manufacturing/static/src/js/customRFID.js"/> -->
|
||||||
<!-- </xpath> -->
|
<!-- </xpath> -->
|
||||||
@@ -308,7 +311,7 @@
|
|||||||
<xpath expr="//page[1]" position="before">
|
<xpath expr="//page[1]" position="before">
|
||||||
<page string="工件装夹" attrs='{"invisible": [("routing_type","!=","装夹预调")]}'>
|
<page string="工件装夹" attrs='{"invisible": [("routing_type","!=","装夹预调")]}'>
|
||||||
<group>
|
<group>
|
||||||
<field name="_barcode_scanned" widget="barcode_handler"/>
|
<!-- <field name="_barcode_scanned" widget="barcode_handler"/> -->
|
||||||
<group string="托盘">
|
<group string="托盘">
|
||||||
<field name="tray_serial_number" readonly="1" string="序列号"/>
|
<field name="tray_serial_number" readonly="1" string="序列号"/>
|
||||||
</group>
|
</group>
|
||||||
|
|||||||
Reference in New Issue
Block a user