打印细码成功 1.0.9版本

This commit is contained in:
QunSheng Lin 2025-04-15 12:29:44 +08:00
parent e4114251fb
commit 9116015e19
6 changed files with 323 additions and 154 deletions

View File

@ -7,8 +7,8 @@
<view class="item space-between"> <view class="item space-between">
<icon type="{{item}}" aria-label="{{item}}" size="20" /> <icon type="{{item}}" aria-label="{{item}}" size="20" />
<text> {{selectedDevice.name}}</text> <text> {{selectedDevice.name}}</text>
<text> {{discovering}}</text> <!-- <text> {{discovering}}</text>
<text> {{available}}</text> <text> {{available}}</text> -->
</view> </view>
</block> </block>
</view> </view>
@ -40,6 +40,7 @@
</view> </view>
</view> </view>
</view> </view>
<progress percent="{{percent}}" active/>
<view class="page-section-demo switch-list"> <view class="page-section-demo switch-list">
<view class="switch-item"> <view class="switch-item">
<!-- checked:是否被选中初始状态默认开关 onChangechecked 改变时触发event.detail={value:checked}。 --> <!-- checked:是否被选中初始状态默认开关 onChangechecked 改变时触发event.detail={value:checked}。 -->

View File

@ -2,7 +2,8 @@ Component({
mixins: [], mixins: [],
data: { data: {
printdata: {}, printdata: {},
array: ['TSPL', 'ZPL', 'CPCL', 'ESC/POS'], percent: '0',
array: ['TSPL', 'ZPL 待开发', 'CPCL 待开发', 'ESC/POS 待开发'],
index: 0, index: 0,
// 初始化蓝牙的开关状态false 表示关闭true 表示打开 // 初始化蓝牙的开关状态false 表示关闭true 表示打开
switchswitchopenBluetoothAdapter: false, switchswitchopenBluetoothAdapter: false,
@ -81,6 +82,197 @@ Component({
} }
}, },
methods: { methods: {
onSubmit() {
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) => {
// 拼接要打印的内容
value = `name ${item.name[1]}.color ${item.color_id_2[1]}.code ${value}`;
// 去除中文和[]符号
value = value.replace(/[\u4e00-\u9fa5\[\]]/g, '');
allValuesToPrint.push(value);
});
}
});
}
const totalCodes = allValuesToPrint.length;
let sentCodes = 0;
let totalChunks = 0;
let sentChunks = 0;
// 计算总的数据块数量
allValuesToPrint.forEach((code) => {
const command = `
SIZE 7,5
GAP 2,0
CLS
QRCODE 20,150,H,5,A,0,"${code}"
TEXT 20,350,"2",0,1,1,"${code}"
PRINT 1,1
`;
const encoder = new TextEncoder('utf-8');
const commandBuffer = encoder.encode(command);
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 7,5
GAP 2,0
CLS
QRCODE 20,150,H,5,A,0,"${currentCode}"
TEXT 20,350,"2",0,1,1,"${currentCode}"
PRINT 1,1
`;
const encoder = new TextEncoder('utf-8');
const commandBuffer = encoder.encode(command);
// 将二进制数据转换为 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);
},
getBluetoothAdapterState() { getBluetoothAdapterState() {
// 调用 dd.getBluetoothAdapterState 方法获取本机蓝牙模块的状态 // 调用 dd.getBluetoothAdapterState 方法获取本机蓝牙模块的状态
dd.getBluetoothAdapterState({ dd.getBluetoothAdapterState({
@ -639,138 +831,7 @@ Component({
}); });
this.reconnectBluetoothDevice(); this.reconnectBluetoothDevice();
} }
},
onSubmit() {
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;
}
// 假设 printcode 是一个变量,需要在代码里定义
const printcode = [1, 2, 3, 4];
const sendPrintCode = (index) => {
if (index >= printcode.length) {
console.log('所有 printcode 数据发送完成');
// const title = '所有 printcode 数据发送完成';
// if (title) {
// dd.showToast({
// // title,
// icon: 'success'
// });
// }
return;
}
const currentCode = printcode[index];
const command = `
SIZE 7,5
GAP 2,0
CLS
QRCODE 20,150,H,5,A,0,"${currentCode}"
TEXT 20,350,"2",0,1,1,"${currentCode}"
PRINT 1,1
`;
const encoder = new TextEncoder('utf-8');
const commandBuffer = encoder.encode(command);
// 将二进制数据转换为 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} 的所有数据分段发送完成`);
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);
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);
} }
}, },
}); });

View File

@ -0,0 +1,88 @@
.list-header {
font-size: 18px;
font-weight: bold;
padding: 10px;
background-color: #f0f0f0;
}
.order-item {
/* 卡片边框颜色 */
border: 1px solid #161515;
padding: 10px;
margin: 10px;
cursor: pointer;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
background-color: #ffffff;
}
.order-title {
font-size: 16px;
font-weight: bold;
color: #dbe45a;
}
.order-brief {
font-size: 14px;
color: #666;
}
.order-extra {
font-size: 14px;
color: #666;
float: right;
}
.order-details {
font-size: 14px;
margin-top: 5px;
color: #444444;
}
.order-line-item {
border: 1px solid #eee;
padding: 5px;
margin-top: 5px;
border-radius: 3px;
/* 销售订单行背景色 */
background-color: #f9f9f9;
}
.pagination {
display: flex;
justify-content: center;
align-items: center;
margin-top: 20px;
}
.pagination-button {
padding: 8px 16px;
margin: 0 10px;
border: none;
border-radius: 5px;
background-color: #007aff;
color: #ffffff;
cursor: pointer;
transition: background-color 0.3s ease;
}
.pagination-button:hover {
background-color: #0056b3;
}
.disabled-button {
background-color: #cccccc;
cursor: not-allowed;
}
.pagination-text {
font-size: 14px;
color: #333333;
margin: 0 10px;
}
.image {
width: 50px;
height: 50px;
}

View File

@ -1,3 +1,9 @@
<view> <view>
<bluetooth-component printdata="{{printdata}}"/> <bluetooth-component printdata="{{printdata}}" />
<view a:for="{{fineCode3Names}}" a:key="*this" class="order-item" data-info="{{item.fineCode}}">
<image class="image" src="../../../../../../../static/images/胜佳纺织.png" />
<view class="order-details">
name:{{item.name || '无货号'}}. color: {{item.color_id_2 || '无色号'}} . code: {{item.fineCode || '无细码'}}
</view>
</view>
</view> </view>

View File

@ -1,22 +1,35 @@
Page({ Page({
data: { data: {
printdata: {}, printdata: {},
// printFormat: 'TSPL', selectedItems: [],
// maxAllowedLength: 1024, fineCode3Names: []
// delayTime: 5000, // 默认延迟 5 秒,单位:毫秒
// paperSize: {
// width: 80, // 默认宽度 80mm
// height: 60 // 默认高度 60mm
// },
// index: 0 //指令集默认选择
}, },
onLoad() { onLoad() {
const selectedSaleOrderLines = dd.getStorageSync({ const selectedSaleOrderLines = dd.getStorageSync({
key: 'selectedSaleOrderLines' key: 'selectedSaleOrderLines'
}).data; }).data;
const printdata = selectedSaleOrderLines || {}; const printdata = selectedSaleOrderLines || {};
this.setData({ const selectedItems = printdata.selectedItems || [];
printdata const fineCode3Names = [];
});
}, selectedItems.forEach(item => {
if (item.fineCode3Names) {
item.fineCode3Names.forEach(fineCode => {
fineCode3Names.push({
name: item.name || '无货号',
color_id_2: item.color_id_2[1] || '无色号',
fineCode
});
});
}
});
this.setData({
printdata,
selectedItems,
fineCode3Names
});
console.log('selectedItems', selectedItems);
console.log('fineCode3Names', fineCode3Names);
}
}); });

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB