kyy
2025-08-16 51f8d59bbd079e41f0bb1f8ef334c5eeed154a22
PadClient2/utils/printSupport.js
@@ -24,70 +24,115 @@
 */
// 样式调整demo
// export const queue = async (arr, ip, port) => {
//    let jsons = ``;
//    console.log(arr[0]);
//    console.log(arr[1]);
//    console.log(ip);
//    console.log(port);
//    const printArr = arr;
//    console.log(printArr);
//    jsons = "! 0 200 200 200 1\r\n" +
//       "PAGE - WIDTH 700 \r\n" +
//       "GAP-SENSE \r\n" +
//       // "BOX 0 0 700 180 0 \r\n" +
//       "T 16 0 10 10 物料编码:" + "\r\n" +
//       "T 55 3 120 10 " + "{itemNo}" + "\r\n" +
//       "T 16 0 300 10 " + "{itemName1111111111111111}" + "\r\n" +
//       "T 55 2 10 42 物料规格:" + "\r\n" +
//       "T 55 2 110 42 " + "{itemModel11111111111111111111111111111}" + "\r\n" +
//       "T 16 0 10 78 数量:" + "\r\n" +
//       "T 0 5 90 78 " + "200.102000" + "\r\n" +
//       "BARCODE 128 1 25 35 350 130 " + 'TM12131ASFASF123456' + "\r\n" +
//       "T 0 1 350 130 " + "TM12131ASFASF123456" + "\r\n" +
//       "T 55 0 10 125 创建时间:" + "\r\n" +
//       "T 55 0 120 125 " + "{2025-05-07 15:55:59}" + "\r\n" +
//       "T 55 0 10 150 打印时间:" + "\r\n" +
//       "T 55 0 120 150 " + "{2025-05-07 15:55:59}" + "\r\n" +
//       //"B QR 500 10 M 1 U 9 \r\n" +
//       //"MA," + '001' + "\r\n" +
//       "B QR 580 12 M 2 U 4" + "\r\n" +
//       "MA," + "TM12345620154" + "\r\n" +
//       "ENDQR\r\n" +
//       "FORM\r\n" +
//       "PRINT\r\n";
//    await printHandle(jsons, ip, port);
// };
//正式代码
export const queue = async (arr, ip, port) => {
   try {
      const printArr = arr; // 接收传入的打印指令数组
      console.log('当前打印参数:', {
         ip,
         port
      });
   let jsons = ``;
      // 循环处理每一条打印指令
      for (const [index, jsons] of printArr.entries()) {
         console.log(`正在处理第 ${index + 1} 条打印指令`);
         console.debug('打印指令内容:', jsons);
   console.log(arr[0]);
   console.log(arr[1]);
   console.log(ip);
   console.log(port);
   const printArr = arr;
   console.log(printArr);
         // 执行异步打印操作
         await printHandle(
            jsons, // 动态替换文本
            ip,
            port
         );
      }
   } catch (error) {
      console.error('打印队列执行出错:', error);
      throw error; // 向上抛出错误供调用方处理
   }
jsons =
 "! 0 195 200 472 1\r\n" +  // 基础偏移量左移5像素
 "PAGE-WIDTH 545\r\n" +     // 页面宽度减少5像素
 "GAP-SENSE\r\n" +
 // 外框 - 整体左移5像素
 "BOX -3 2 543 470 2\r\n" +
 // ==================== 物料条码(第一行)====================
 "LINE -3 62 425 62 1\r\n" +
 "T 55 0 8 27 物料条码:\r\n" +
 // ==================== 物料编码(第二行)====================
 "LINE -3 124 543 124 1\r\n" +
 "T 55 0 8 89 编码:\r\n" +
 // ==================== 供应商(第三行)====================
 "LINE -3 186 543 186 1\r\n" +     // 第3条分隔线(间隔62)
 "T 55 0 8 151 供应商:\r\n" +
 // ==================== 物料名称(第四行)====================
 "LINE -3 248 543 248 1\r\n" +     // 第4条分隔线(间隔62)
 "T 55 0 8 213 名称:\r\n" +
 // ==================== 规格型号(第五行)====================
 "LINE -3 310 543 310 1\r\n" +     // 第5条分隔线(间隔62)
 "T 55 0 8 275 规格:\r\n" +
 // ==================== 仓库(第六行)====================
 "LINE -3 360 543 360 1\r\n" +     // 第6条分隔线(移至竖线顶端Y=360)
 "T 55 0 8 335 仓库:\r\n" +
 // ==================== 数量和仓管员(第七行)====================
 "LINE -3 430 543 430 1\r\n" +     // 底部横线(与竖线底端对齐)
 "T 55 0 8 380 数量:\r\n" +        // 文本位置保持
 "T 55 0 275 380 仓管员:\r\n" +    // 文本位置保持
 // 中间竖线分隔(范围Y=360-430,与上下横线衔接)
 "LINE 265 430 265 360 1\r\n" +    // 竖线顶端对齐仓库行分隔线
 // ==================== 底部时间信息 ====================
 "T 55 0 395 440 打印时间\r\n" +  // 位置保持
 "T 55 0 8 440 " + new Date().toLocaleString('zh-CN', {  // 时间格式改为年月日时分秒
   year: 'numeric',
   month: '2-digit',
   day: '2-digit',
   hour: '2-digit',
   minute: '2-digit',
   second: '2-digit'
 }) + "\r\n" +
 // ==================== 二维码区域(左移5像素)====================
 "BOX 425 3 535 113 1\r\n" +       // 位置保持
 "B QR 430 8 M 2 U 5\r\n" +        // 位置保持
 "MA,{zb.物料条码}\r\n" +
 "ENDQR\r\n" +
 "FORM\r\n" +
 "PRINT\r\n";
await printHandle(jsons, ip, port);
};
// //正式代码
// export const queue = async (arr, ip, port) => {
//    try {
//       const printArr = arr; // 接收传入的打印指令数组
//       console.log('当前打印参数:', {
//          ip,
//          port
//       });
//       // 循环处理每一条打印指令
//       for (const [index, jsons] of printArr.entries()) {
//          console.log(`正在处理第 ${index + 1} 条打印指令`);
//          console.debug('打印指令内容:', jsons);
//          // 执行异步打印操作
//          await printHandle(
//             jsons, // 动态替换文本
//             ip,
//             port
//          );
//       }
//    } catch (error) {
//       console.error('打印队列执行出错:', error);
//       throw error; // 向上抛出错误供调用方处理
//    }
// };
export const queue111 = async (arr, ip, port) => {
   let jsons = ``;
@@ -260,7 +305,8 @@
 * @param {string} ip  ip地址
 * @param {Number} port //端口号
 */
export const printHandle = async (jsons, ip, port) => {
/* export const printHandle = async (jsons, ip, port) => {
   return new Promise((resolve, reject) => {
      try {
         var Socket = plus.android.importClass('java.net.Socket');
@@ -304,4 +350,54 @@
         }
      }
   });
}; */
export const printHandle = async (jsons, ip, port) => {
         var mac_address =  uni.getStorageSync('printMac');
         console.log('打印开始:'+mac_address)
         var device = null,
            BAdapter = null,
            BluetoothAdapter = null,
            uuid = null,
            main = null,
            bluetoothSocket = null;
         if (!mac_address) {
            this.$showMessage('请选择蓝牙打印机');
            return false;
         }
         main = plus.android.runtimeMainActivity();
         BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");
         let UUID = plus.android.importClass("java.util.UUID");
         uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
         BAdapter = BluetoothAdapter.getDefaultAdapter();
         device = BAdapter.getRemoteDevice(mac_address);
         plus.android.importClass(device);
         bluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(uuid);
         plus.android.importClass(bluetoothSocket);
         if (!bluetoothSocket.isConnected()) {
            console.log('检测到设备未连接,尝试连接....');
            bluetoothSocket.connect();
         }
         console.log('设备已连接');
         if (bluetoothSocket.isConnected()) {
            var outputStream = bluetoothSocket.getOutputStream();
            plus.android.importClass(outputStream);
            let printStr =jsons;
            var bytes = plus.android.invoke(printStr, 'getBytes', 'gbk');
            outputStream.write(bytes);
            outputStream.flush();
            device = null //这里关键
            bluetoothSocket.close(); //必须关闭蓝牙连接否则意外断开的话打印错误
            //this.$showMessage('OK');
            console.log("打印机连接状态:111");
            return true;
         } else {
            //this.$showMessage('NG');
            console.log("打印机连接状态:222");
            return false;
         }
};