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

This commit is contained in:
XiaoJia Chen 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: [], writableCharacteristics: [],
// 存储所有服务及其特征值 // 存储所有服务及其特征值
allServicesAndCharacteristics: {} allServicesAndCharacteristics: {},
tspl: {}
}, },
props: { props: {
printdata: { printdata: {
@ -84,7 +85,243 @@ Component({
} }
}, },
methods: { 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() { 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; const selectedDevice = this.data.selectedDevice;
console.log('selectedDevice', selectedDevice); console.log('selectedDevice', selectedDevice);
const isConnected = this.data.isConnected; const isConnected = this.data.isConnected;
@ -176,8 +413,6 @@ Component({
}); });
} }
const totalCodes = allValuesToPrint.length; const totalCodes = allValuesToPrint.length;
let sentCodes = 0; let sentCodes = 0;
let totalChunks = 0; let totalChunks = 0;
@ -185,21 +420,12 @@ Component({
// 计算总的数据块数量 // 计算总的数据块数量
allValuesToPrint.forEach((code) => { allValuesToPrint.forEach((code) => {
const command = ` // 使用 tspl 模板,并替换其中的值
SIZE 75 mm,60 mm let command = tspl.code;
GAP 2,0 command = command.replace('{{name}}', code.name)
CLS .replace('{{color}}', code.color)
QRCODE 20,150,H,5,A,0,"${code.name} ${code.color} ${code.code} 020-89061189" .replace('{{code}}', code.code);
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 commandBuffer = iconv.encode(command, 'GB18030');
const hexValue = Array.from(commandBuffer) const hexValue = Array.from(commandBuffer)
.map(byte => byte.toString(16).padStart(2, '0')) .map(byte => byte.toString(16).padStart(2, '0'))
@ -222,22 +448,12 @@ Component({
} }
const currentCode = allValuesToPrint[index]; const currentCode = allValuesToPrint[index];
const command = ` // 使用 tspl 模板,并替换其中的值
SIZE 75 mm,60 mm let command = tspl.code;
GAP 2,0 command = command.replace('{{name}}', currentCode.name)
CLS .replace('{{color}}', currentCode.color)
QRCODE 20,150,H,5,A,0,"${currentCode.name} ${currentCode.color} ${currentCode.code} 020-89061189" .replace('{{code}}', currentCode.code);
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) console.log("command",command)
// const commandBuffer = Buffer.from(command, 'utf8');
const commandBuffer = iconv.encode(command, 'GB18030'); const commandBuffer = iconv.encode(command, 'GB18030');
// 将二进制数据转换为 hex 编码 // 将二进制数据转换为 hex 编码
const hexValue = Array.from(commandBuffer) const hexValue = Array.from(commandBuffer)
@ -297,7 +513,6 @@ Component({
sendPrintCode(0); sendPrintCode(0);
}, },
getBluetoothAdapterState() { getBluetoothAdapterState() {
// 调用 dd.getBluetoothAdapterState 方法获取本机蓝牙模块的状态 // 调用 dd.getBluetoothAdapterState 方法获取本机蓝牙模块的状态
dd.getBluetoothAdapterState({ dd.getBluetoothAdapterState({

View File

@ -1,6 +1,6 @@
<view class="page-section"> <view class="page-section">
<view class="page-section-demo"> <view class="page-section-demo">
<text> v1.0.11 </text> <text> v1.0.12 </text>
</view> </view>
</view> </view>
<view class="nav-container"> <view class="nav-container">

View File

@ -24,3 +24,12 @@
border-radius: 5px; border-radius: 5px;
} }
/* 让 rich-text 内的文字字体变小 */
rich-text {
font-size: 12px;
}
/* 让 textarea 内的文字字体变小 */
textarea {
font-size: 12px;
}

View File

@ -2,7 +2,18 @@
<view> <view>
<rich-text nodes="{{nodes}}" onTap="tap"></rich-text> <rich-text nodes="{{nodes}}" onTap="tap"></rich-text>
</view> </view>
<view class="page-section">
<view class="page-section-title">TSPL指令集</view>
<view class="page-section-demo">
<textarea onBlur="bindTextAreaBlur" auto-height placeholder="Please input something" value="{{tspl.code}}" />
</view>
</view>
<view class="page-section">
<view class="page-section-title">TSPL指令集</view>
<view class="page-section-demo">
<textarea onBlur="bindTextAreaBlur" auto-height placeholder="Please input something" value="{{tspl.currentCode}}" />
</view>
</view>
<view class="cart-bottom"> <view class="cart-bottom">
<view class="cart-info"> <view class="cart-info">
</view> </view>

View File

@ -1,5 +1,6 @@
import jsonrpcFunc from '../../../../../../ulti/jsonrpc'
Page({ Page({
data: { data: {
nodes: [{ nodes: [{
@ -13,9 +14,24 @@ Page({
text: 'Hello&nbsp;World! 拖拉设计打印模板待开发', text: 'Hello&nbsp;World! 拖拉设计打印模板待开发',
}], }],
}], }],
tspl:{},
loginInfo: '', //权限验证信息
}, },
onLoad(query) { onLoad(query) {
// 页面加载 // 页面加载
const loginInfo = dd.getStorageSync({
key: 'loginInfo'
}).data;
if (loginInfo && loginInfo.apiurl && loginInfo.databaseName && loginInfo.username && loginInfo.password && loginInfo.uid > 0) {
this.setData({
loginInfo
});
// console.log('销售页得到的本地信息', loginInfo);
//执行获取销售订单第一页数据分页第一页默认
this.fetchData(1);
} else {
// console.log('本地缓存中未获取到登录信息');
}
}, },
onReady() { onReady() {
// 页面加载完成 // 页面加载完成
@ -51,4 +67,46 @@ Page({
console.log('tap'); console.log('tap');
}, },
// //
fetchData(page) {
const {
loginInfo,
} = this.data;
// 调用 search_count 方法获取总记录数
const tspl = {
"jsonrpc": "2.0",
"method": "call",
"id": 2,
"params": {
"service": "object",
"method": "execute_kw",
"args": [
loginInfo.databaseName,
loginInfo.uid,
loginInfo.password,
'sale.order',
"tspl",
[
[]
],
{}
]
}
};
jsonrpcFunc(loginInfo, tspl)
.then((response) => {
// 保存数据到本地缓存
dd.setStorageSync({
key: 'tspl',
data: response
});
this.setData({
tspl: response
});
console.log("response", response)
})
.catch((error) => {
console.log("获取打印模板失败")
// console.error('请求数据时出错:', error);
});
},
}); });

View File

@ -48,7 +48,7 @@
</form> </form>
<view class="page-section"> <view class="page-section">
<view class="page-section-demo"> <view class="page-section-demo">
<text> v1.0.11 </text> <text> v1.0.12 </text>
</view> </view>
</view> </view>
</view> </view>

View File

@ -1,3 +1,4 @@
import jsonrpcFunc from '../ulti/jsonrpc'
Page({ Page({
data: { data: {
apiurl: '', apiurl: '',
@ -134,6 +135,7 @@ Page({
} }
}; };
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
dd.httpRequest({ dd.httpRequest({
url: `${apiurl}/jsonrpc`, url: `${apiurl}/jsonrpc`,
@ -162,6 +164,38 @@ Page({
password, password,
databaseName databaseName
}; };
// const session_id = {
// "jsonrpc": "2.0",
// "method": "call",
// "id": 2,
// "params": {
// "service": "object",
// "method": "execute_kw",
// "args": [
// databaseName,
// uid,
// password,
// "ir.http",
// "session_info",
// [],
// {}
// ]
// }
// };
// jsonrpcFunc(loginInfo, session_id)
// .then((response) => {
// // 保存数据到本地缓存
// dd.setStorageSync({
// key: 'session_id',
// data: response
// });
// console.log("response", response)
// })
// .catch((error) => {
// console.log("获取session_id失败")
// // console.error('请求数据时出错:', error);
// });
//
dd.setStorageSync({ dd.setStorageSync({
key: 'loginInfo', key: 'loginInfo',
data: loginInfo data: loginInfo
@ -170,6 +204,7 @@ Page({
reject(new Error(`保存登录信息到本地缓存失败: ${e.message}`)); reject(new Error(`保存登录信息到本地缓存失败: ${e.message}`));
return; return;
} }
if (uid) { if (uid) {
dd.switchTab({ dd.switchTab({
url: '/pages/deal/deal', url: '/pages/deal/deal',