等待增加打印模板后端渲染

This commit is contained in:
2025-04-20 01:24:05 +08:00
parent 81515b035d
commit 0a2e1cc47b
7 changed files with 389 additions and 61 deletions

View File

@@ -38,7 +38,8 @@ Component({
// 存储获取到的可写特征值列表
writableCharacteristics: [],
// 存储所有服务及其特征值
allServicesAndCharacteristics: {}
allServicesAndCharacteristics: {},
tspl: {}
},
props: {
printdata: {
@@ -84,7 +85,243 @@ Component({
}
},
methods: {
// onSubmit() {
// const tspl = dd.getStorageSync({
// key: 'tspl'
// }).data;
// if (tspl) {
// this.setData({
// tspl
// });
// console.log('tspl', tspl);
// } else {
// // console.log('本地缓存中未获取到登录信息');
// }
// const selectedDevice = this.data.selectedDevice;
// console.log('selectedDevice', selectedDevice);
// const isConnected = this.data.isConnected;
// // 检查蓝牙设备是否已连接
// if (!selectedDevice || !isConnected) {
// console.log('请先连接蓝牙设备');
// const title = '请先连接蓝牙设备';
// if (title) {
// dd.showToast({
// title,
// icon: 'none'
// });
// }
// return;
// }
// // 从可写特征值列表中获取第一个可写特征值(这里假设只有一个可写特征值,你可以根据实际情况调整)
// const writableCharacteristic = this.data.writableCharacteristics[0];
// console.log('writableCharacteristics', this.data.writableCharacteristics);
// if (!writableCharacteristic) {
// console.log('未找到可写特征值');
// const title = '未找到可写特征值';
// if (title) {
// dd.showToast({
// title,
// icon: 'none'
// });
// }
// return;
// }
// const serviceId = writableCharacteristic.serviceId;
// const characteristicId = writableCharacteristic.characteristicId;
// const deviceId = selectedDevice.deviceId;
// // 检查参数完整性
// if (!deviceId || !serviceId || !characteristicId) {
// console.error('缺少必要参数:', {
// deviceId,
// serviceId,
// characteristicId
// });
// const title = '缺少必要参数,请检查连接';
// if (title) {
// dd.showToast({
// title,
// icon: 'none'
// });
// }
// return;
// }
// //打印数据处理
// const selectedSaleOrderLines = dd.getStorageSync({
// key: 'selectedSaleOrderLines'
// }).data;
// const printdata = selectedSaleOrderLines || {};
// this.setData({
// printdata
// });
// const allValuesToPrint = [];
// // 检查打印数据是否存在,并且 selectedItems 是一个数组
// if (printdata && printdata.selectedItems && Array.isArray(printdata.selectedItems)) {
// // 遍历 selectedItems 数组
// printdata.selectedItems.forEach((item) => {
// // 输出当前遍历的 item
// console.log('每个订单行数据item:', item);
// // 检查 item 的 fineCode3Names 是否存在,并且是一个数组
// if (item.fineCode3Names && Array.isArray(item.fineCode3Names)) {
// // 遍历 fineCode3Names 数组
// item.fineCode3Names.forEach((value) => {
// // 将可能的浮点数转换为文本
// const nameValue = typeof item.name === 'number'? item.name.toString() : item.name;
// const colorValue = typeof item.color_id_2[1] === 'number'? item.color_id_2[1].toString() : item.color_id_2[1];
// const codeValue = typeof value === 'number'? value.toString() : value;
// // 去除中文和[]符号
// const nameValuereplace = nameValue.replace(/[\u4e00-\u9fa5\[\]]/g, '');
// // 拼接要打印的内容
// value = `货号: ${nameValuereplace}.色号: ${colorValue}.细码: ${codeValue}`;
// // allValuesToPrint.push(value);
// allValuesToPrint.push({
// name: `货号: ${nameValuereplace}`,
// color: `色号: ${colorValue}`,
// code: `细码: ${codeValue}`
// });
// });
// }
// });
// }
// const totalCodes = allValuesToPrint.length;
// let sentCodes = 0;
// let totalChunks = 0;
// let sentChunks = 0;
// // 计算总的数据块数量
// allValuesToPrint.forEach((code) => {
// const command = `
// SIZE 75 mm,60 mm
// GAP 2,0
// CLS
// QRCODE 20,150,H,5,A,0,"${code.name} ${code.color} ${code.code} 020-89061189"
// TEXT 200,100,"TSS32.BF2",0,1,1,"${code.name}"
// TEXT 200,200,"TSS32.BF2",0,1,1,"${code.color}"
// TEXT 200,300,"TSS32.BF2",0,1,1,"${code.code}"
// TEXT 20,400,"TSS24.BF2",0,1,1,"备注:如经裁剪或加工后本布行概不负责"
// PRINT 1,1
// `;
// // const encoder = new TextEncoder('utf-8');
// // const commandBuffer = encoder.encode(command);
// // const commandBuffer = Buffer.from(command, 'utf8');
// const commandBuffer = iconv.encode(command, 'GB18030');
// const hexValue = Array.from(commandBuffer)
// .map(byte => byte.toString(16).padStart(2, '0'))
// .join('');
// const chunkSize = 40;
// const chunks = [];
// for (let i = 0; i < hexValue.length; i += chunkSize) {
// chunks.push(hexValue.slice(i, i + chunkSize));
// }
// totalChunks += chunks.length;
// });
// const sendPrintCode = (index) => {
// if (index >= allValuesToPrint.length) {
// console.log('所有 printcode 数据发送完成');
// this.setData({
// percent: '100',
// });
// return;
// }
// const currentCode = allValuesToPrint[index];
// const command = `
// SIZE 75 mm,60 mm
// GAP 2,0
// CLS
// QRCODE 20,150,H,5,A,0,"${currentCode.name} ${currentCode.color} ${currentCode.code} 020-89061189"
// TEXT 200,100,"TSS32.BF2",0,1,1,"${currentCode.name}"
// TEXT 200,200,"TSS32.BF2",0,1,1,"${currentCode.color}"
// TEXT 200,300,"TSS32.BF2",0,1,1,"${currentCode.code}"
// TEXT 20,400,"TSS24.BF2",0,1,1,"备注:如经裁剪或加工后本布行概不负责"
// PRINT 1,1
// `;
// // const encoder = new TextEncoder('utf-8');
// // const commandBuffer = encoder.encode(command);
// console.log("command",command)
// // const commandBuffer = Buffer.from(command, 'utf8');
// const commandBuffer = iconv.encode(command, 'GB18030');
// // 将二进制数据转换为 hex 编码
// const hexValue = Array.from(commandBuffer)
// .map(byte => byte.toString(16).padStart(2, '0'))
// .join('');
// // 分段发送数据
// const chunkSize = 40; // 每个分段的长度20 字节 = 40 个十六进制字符
// const chunks = [];
// for (let i = 0; i < hexValue.length; i += chunkSize) {
// chunks.push(hexValue.slice(i, i + chunkSize));
// }
// const sendNextChunk = (chunkIndex) => {
// if (chunkIndex >= chunks.length) {
// console.log(`printcode ${currentCode} 的所有数据分段发送完成`);
// sentCodes++;
// sendPrintCode(index + 1);
// return;
// }
// const currentChunk = chunks[chunkIndex];
// console.log(`准备发送 printcode ${currentCode} 的第 ${chunkIndex + 1} 个数据分段,长度: ${currentChunk.length}`, currentChunk);
// dd.writeBLECharacteristicValue({
// deviceId,
// serviceId,
// characteristicId,
// value: currentChunk,
// success: (res) => {
// console.log(`printcode ${currentCode} 的第 ${chunkIndex + 1} 个数据分段发送成功`, res);
// sentChunks++;
// const percent = ((sentChunks / totalChunks) * 100).toFixed(0);
// this.setData({
// percent: percent,
// });
// sendNextChunk(chunkIndex + 1);
// },
// fail: (res) => {
// console.error(`printcode ${currentCode} 的第 ${chunkIndex + 1} 个数据分段发送失败,错误码:`, res.errorCode);
// const title = `printcode ${currentCode} 的第 ${chunkIndex + 1} 个数据分段发送失败,错误码: ${res.errorCode}`;
// if (title) {
// dd.showToast({
// title: title,
// icon: 'none'
// });
// }
// },
// complete: (res) => {
// console.log(`printcode ${currentCode} 的第 ${chunkIndex + 1} 个数据分段发送调用结束`, res);
// }
// });
// };
// sendNextChunk(0);
// };
// sendPrintCode(0);
// },
onSubmit() {
const tspl = dd.getStorageSync({
key: 'tspl'
}).data;
if (tspl) {
this.setData({
tspl
});
console.log('tspl', tspl);
} else {
// console.log('本地缓存中未获取到登录信息');
return;
}
const selectedDevice = this.data.selectedDevice;
console.log('selectedDevice', selectedDevice);
const isConnected = this.data.isConnected;
@@ -136,7 +373,7 @@ Component({
}
return;
}
//打印数据处理
// 打印数据处理
const selectedSaleOrderLines = dd.getStorageSync({
key: 'selectedSaleOrderLines'
}).data;
@@ -150,33 +387,31 @@ Component({
if (printdata && printdata.selectedItems && Array.isArray(printdata.selectedItems)) {
// 遍历 selectedItems 数组
printdata.selectedItems.forEach((item) => {
// 输出当前遍历的 item
console.log('每个订单行数据item:', item);
// 检查 item 的 fineCode3Names 是否存在,并且是一个数组
if (item.fineCode3Names && Array.isArray(item.fineCode3Names)) {
// 遍历 fineCode3Names 数组
item.fineCode3Names.forEach((value) => {
// 将可能的浮点数转换为文本
const nameValue = typeof item.name === 'number'? item.name.toString() : item.name;
const colorValue = typeof item.color_id_2[1] === 'number'? item.color_id_2[1].toString() : item.color_id_2[1];
const codeValue = typeof value === 'number'? value.toString() : value;
// 去除中文和[]符号
const nameValuereplace = nameValue.replace(/[\u4e00-\u9fa5\[\]]/g, '');
// 拼接要打印的内容
value = `货号: ${nameValuereplace}.色号: ${colorValue}.细码: ${codeValue}`;
// 输出当前遍历的 item
console.log('每个订单行数据item:', item);
// 检查 item 的 fineCode3Names 是否存在,并且是一个数组
if (item.fineCode3Names && Array.isArray(item.fineCode3Names)) {
// 遍历 fineCode3Names 数组
item.fineCode3Names.forEach((value) => {
// 将可能的浮点数转换为文本
const nameValue = typeof item.name === 'number' ? item.name.toString() : item.name;
const colorValue = typeof item.color_id_2[1] === 'number' ? item.color_id_2[1].toString() : item.color_id_2[1];
const codeValue = typeof value === 'number' ? value.toString() : value;
// 去除中文和[]符号
const nameValuereplace = nameValue.replace(/[\u4e00-\u9fa5\[\]]/g, '');
// 拼接要打印的内容
value = `货号: ${nameValuereplace}.色号: ${colorValue}.细码: ${codeValue}`;
// allValuesToPrint.push(value);
allValuesToPrint.push({
name: `货号: ${nameValuereplace}`,
color: `色号: ${colorValue}`,
code: `细码: ${codeValue}`
});
});
}
// allValuesToPrint.push(value);
allValuesToPrint.push({
name: `货号: ${nameValuereplace}`,
color: `色号: ${colorValue}`,
code: `细码: ${codeValue}`
});
});
}
});
}
}
const totalCodes = allValuesToPrint.length;
let sentCodes = 0;
@@ -185,21 +420,12 @@ Component({
// 计算总的数据块数量
allValuesToPrint.forEach((code) => {
const command = `
SIZE 75 mm,60 mm
GAP 2,0
CLS
QRCODE 20,150,H,5,A,0,"${code.name} ${code.color} ${code.code} 020-89061189"
TEXT 200,100,"TSS32.BF2",0,1,1,"${code.name}"
TEXT 200,200,"TSS32.BF2",0,1,1,"${code.color}"
TEXT 200,300,"TSS32.BF2",0,1,1,"${code.code}"
TEXT 20,400,"TSS24.BF2",0,1,1,"备注:如经裁剪或加工后本布行概不负责"
PRINT 1,1
`;
// 使用 tspl 模板,并替换其中的值
let command = tspl.code;
command = command.replace('{{name}}', code.name)
.replace('{{color}}', code.color)
.replace('{{code}}', code.code);
// const encoder = new TextEncoder('utf-8');
// const commandBuffer = encoder.encode(command);
// const commandBuffer = Buffer.from(command, 'utf8');
const commandBuffer = iconv.encode(command, 'GB18030');
const hexValue = Array.from(commandBuffer)
.map(byte => byte.toString(16).padStart(2, '0'))
@@ -222,22 +448,12 @@ Component({
}
const currentCode = allValuesToPrint[index];
const command = `
SIZE 75 mm,60 mm
GAP 2,0
CLS
QRCODE 20,150,H,5,A,0,"${currentCode.name} ${currentCode.color} ${currentCode.code} 020-89061189"
TEXT 200,100,"TSS32.BF2",0,1,1,"${currentCode.name}"
TEXT 200,200,"TSS32.BF2",0,1,1,"${currentCode.color}"
TEXT 200,300,"TSS32.BF2",0,1,1,"${currentCode.code}"
TEXT 20,400,"TSS24.BF2",0,1,1,"备注:如经裁剪或加工后本布行概不负责"
PRINT 1,1
`;
// const encoder = new TextEncoder('utf-8');
// const commandBuffer = encoder.encode(command);
// 使用 tspl 模板,并替换其中的值
let command = tspl.code;
command = command.replace('{{name}}', currentCode.name)
.replace('{{color}}', currentCode.color)
.replace('{{code}}', currentCode.code);
console.log("command",command)
// const commandBuffer = Buffer.from(command, 'utf8');
const commandBuffer = iconv.encode(command, 'GB18030');
// 将二进制数据转换为 hex 编码
const hexValue = Array.from(commandBuffer)
@@ -297,7 +513,6 @@ Component({
sendPrintCode(0);
},
getBluetoothAdapterState() {
// 调用 dd.getBluetoothAdapterState 方法获取本机蓝牙模块的状态
dd.getBluetoothAdapterState({