精神世界 發表於 2024-3-21 12:20:00

uni-app攻略:如何对接驰腾打印机

<p><img src="https://img2024.cnblogs.com/blog/2149129/202403/2149129-20240321115714871-974367264.png" alt="" width="968" height="413" loading="lazy"></p>
<h2>一.引言</h2>
<p>在当前的移动开发生态中,跨平台框架如uni-app因其高效、灵活的特点受到了开发者们的青睐。同时,随着物联网技术的飞速发展,智能打印设备已成为许多业务场景中不可或缺的一环。今天,我们就来探讨如何使用uni-app轻松对接驰腾品牌的智能打印机,实现无线打印功能。无论您是初学者还是有经验的开发者,本教程都将带您一步步实现这一目标。</p>
<h2>二.准备工作</h2>
<p>首先确保您的开发环境已就绪。这包括安装HBuilderX和uni-app框架。同时,您需要准备一台驰腾打印机,并熟悉其用户手册和API文档。了解打印机支持的通信协议(比如蓝牙或Wi-Fi)也至关重要。</p>
<h2>三.对接流程解析</h2>
<p>在进行代码编写之前,我们需要理解整个接口调用的流程。这通常包括建立与打印机的连接、发送打印指令以及处理返回结果。此外,我们还需要关注数据格式、编码要求以及安全机制。</p>
<h2>四.详细步骤与实施</h2>
<h3>1.设备连接与通讯建立</h3>
<p>蓝牙连接流程</p>
<ol>
<li>使用uni-app提供的蓝牙模块初始化并搜索打印机设备。</li>
<li>配对并连接到驰腾打印机。</li>
</ol>
<h3>2.发送打印指令</h3>
<ol>
<li>数据封装与传输
<ul>
<li>依据驰腾打印机的API文档,正确封装打印数据。</li>
<li>调用相关API发送打印任务。</li>
</ul>
</li>
<li>错误处理与反馈
<ul>
<li>监听打印状态变化,及时响应可能出现的错误。</li>
<li>向用户提供清晰的状态反馈信息。</li>
</ul>
</li>
</ol>
<h3>3.打印状态展示</h3>
<ul>
<li>实时显示当前打印任务的状态,包括成功、等待和失败等。</li>
</ul>
<h2>五.代码实例与详解</h2>
<h3>前期准备:</h3>
<h4>需要下载一个js文件支持包,请先点击下载</h4>
<h3><strong>点击下载js支持包</strong></h3>
<h3><strong>点击下载开发指南</strong></h3>
<p><strong>开发说明中有详细的指令文档,需要的大家可以自行翻阅</strong></p>
<p><img src="https://img2024.cnblogs.com/blog/2149129/202403/2149129-20240321120349900-439491393.png" alt="" width="544" height="580" loading="lazy"></p>
<h3>以下提供一个使用打印机进行二维码打印的经典案例</h3>
<h3>1.先将js包解压,并在项目中创建文件夹保存</h3>
<p><img src="https://img2024.cnblogs.com/blog/2149129/202403/2149129-20240321120648095-697306577.png" alt="" loading="lazy"></p>
<h3>&nbsp;2.现在需要两个页面,一个负责蓝牙搜索和连接,一个复制连接后的打印工作<br>测试蓝牙连接页面代码:</h3>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;">&lt;template&gt;
&lt;view class="container"&gt;
          &lt;view class="top-box"&gt;
                  &lt;view class="name"&gt;打印机搜索&lt;/view&gt;
                  &lt;view class="value" @click="onLoadFun" v-if="submitMain"&gt;
                          点击搜索
                  &lt;/view&gt;
               &lt;!-- &lt;view class="value" @click="rescan" v-else&gt;
                          重新搜索
                  &lt;/view&gt; --&gt;
          &lt;/view&gt;
    &lt;scroll-view scroll-y class="box"&gt;
      &lt;view
      class="item"
      v-for="(item, index) in blueDeviceList"
      :key="index"
      @click="connect(item, index)"
      :class="{ active: blueIndex === index }"
      &gt;
      &lt;view&gt;
          &lt;text&gt;{{ item.name }}&lt;/text&gt;
      &lt;/view&gt;
      &lt;view&gt;
          &lt;text&gt;{{ item.deviceId }}&lt;/text&gt;
      &lt;/view&gt;
      &lt;/view&gt;
       &lt;!-- &lt;view class="item"&gt;{{code}}&lt;/view&gt; --&gt;
    &lt;/scroll-view&gt;
&lt;/view&gt;
&lt;/template&gt;

&lt;script&gt;
import CTPL from "@/web-CTPL-SDK/ctpl.js";
export default {
data() {
    return {
      blueDeviceList: [], //蓝牙设备列表
      blueName: "", //蓝牙设备名称
      blueDeviceId: "", //蓝牙设备特征值
      blueIndex: -1,
      submitMain:true
    };
},
onUnload() {
        if(this.blueDeviceId){
                CTPL.disconnect();
        }
},
methods: {
        async onLoadFun(){
                await CTPL.init();
                this.submitMain = false;
                await this.discoveryList()
        },
    clickLeft() {
      uni.navigateBack();
    },
    async discoveryList() {
               
      uni.showLoading({
                  title:'搜索设备中'
          });
       CTPL.discovery().then((res)=&gt;{
                  uni.hideLoading();
                  this.blueDeviceList = res;
           }).catch((err)=&gt;{
                  uni.hideLoading();
                        uni.$u.toast(err)
           })
    },
    //重新扫描
    rescan() {
      this.blueDeviceList = [];
      this.discoveryList();
    },
    //开始连接蓝牙
    connect(data, index) {
                const that = this;
                uni.showModal({
                        title:'温馨提示',
                        content:'是否使用选中设备进行打印',
                        success(res) {
                                if(res.confirm){
                                        CTPL.connect(data.deviceId);
                                        that.blueIndex = index;
                                        that.blueDeviceId = data.deviceId;
                                        that.blueName = data.name;
                                        setTimeout(() =&gt; {
                                               
                                                uni.showLoading({
                                                        title:'配置设备中'
                                                })
                                           that.setCodeFun()
                                        }, 1000);
                                }
                        }
                })
    },
        setCodeFun(){
                const that = this;
                CTPL.setPaperType(0);
                setTimeout(()=&gt;{
                        CTPL.setMemoryPrint(0);
                        uni.hideLoading()
                        setTimeout(()=&gt;{
                                uni.navigateTo({
                                  url: `要进行打印的页面?id=${that.orderId}&amp;deviceId=${that.blueDeviceId}`,
                                });
                        },500)
                },500)
        },

},
};
&lt;/script&gt;

&lt;style lang="scss" scoped&gt;
.container {
width: 100%;
overflow: hidden;
min-height: 100vh;
}
.top-box{
        width: 100%;
        padding: 30rpx;
        background-color: white;
        color: #000000;
        line-height: 70rpx;
        font-size: 32rpx;
        overflow: hidden;
        .name{
                width: 50%;
                display: inline-block;
                vertical-align: top;
        }
        .value{
                width: 30%;
                float: right;
                display: inline-block;
                vertical-align: top;
                background:#009180;
                color: white;
                text-align: center;
                border-radius: 10rpx;
        }
}

$nav-height: 30px;

.box-bg {
background-color: #f5f5f5;
.nav {
    text {
      font-size: 28rpx !important;
    }
    .uni-nav-bar-right-text {
      color: #1aad19 !important;
    }
}
}

.city {
/* #ifndef APP-PLUS-NVUE */
display: flex;
/* #endif */
flex-direction: row;
align-items: center;
justify-content: flex-start;
// width: 160rpx;
margin-left: 4px;
}

.input-view {
/* #ifndef APP-PLUS-NVUE */
display: flex;
/* #endif */
flex-direction: row;
// width: 500rpx;
flex: 1;
background-color: #f8f8f8;
height: $nav-height;
border-radius: 15px;
padding: 0 15px;
flex-wrap: nowrap;
margin: 7px 0;
line-height: $nav-height;
}

.input-uni-icon {
line-height: $nav-height;
}

.nav-bar-input {
height: $nav-height;
line-height: $nav-height;
/* #ifdef APP-PLUS-NVUE */
width: 370rpx;
/* #endif */
padding: 0 5px;
font-size: 14px;
background-color: #f8f8f8;
}

.box {
height: calc(100vh - 100px);
overflow: hidden;
}

.item {
height: 120rpx;
border-bottom: 1px solid #e5e5e5;
background-color: white;
width: 700rpx;
margin: 26rpx auto 0 auto;
overflow: hidden;
font-size: 28rpx;
line-height: 120rpx;
padding: 0 20rpx;
border-radius: 10rpx;
}

.active {
background-color: #1aad19;
color: white;
}
&lt;/style&gt;    </pre>
</div>
<h4><span style="color: rgba(255, 0, 0, 1)">注意点:连接了设备后,除非断开并关闭小程序,不然不要重新连接,会直接卡死</span></h4>
<h3>测试打印页面代码(核心打印代码):</h3>
<p><strong>数据:</strong></p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;">      mainCodeArr:[],
                qrcodeObj: {
                        x: 100,
                        y: 70,
                        eccLevel: "H",
                        cellWidth: 6,
                        encodeMode: "A",
                        rotation: 0,
                        codeMode: "M1",
                        mask: "S7",
                        content: 1234567890,
                },
                textObj: {
                        x: "80",
                        y: "20",
                        rotation: "0",
                        xRatio: "1",
                        yRatio: "1",
                        textAlignment: "0",
                        text: "我的测试商品(1)"
                },
                code:''</pre>
</div>
<p><strong>调用方法:</strong></p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;"> async setCodeIndex(index){
                  uni.showLoading({
                          title:'打印中'
                  })
                  const item = this.mainCodeArr
                  CTPL.queryPrintMode(0);
                  CTPL.setSize(4,3);
                  CTPL.clearCache();
                  let code =item.code;
                  this.code = code;
                  setTimeout(()=&gt;{
                          CTPL.drawQRCode(
                                  this.qrcodeObj.x,
                                  this.qrcodeObj.y,
                                  this.qrcodeObj.eccLevel,
                                  this.qrcodeObj.cellWidth,
                                  this.qrcodeObj.encodeMode,
                                  this.qrcodeObj.rotation,
                                  this.qrcodeObj.codeMode,
                                  this.qrcodeObj.mask,
                                  code
                          );
                          setTimeout(()=&gt;{
                                  let left = 40;
                                  if(item.product_title.length &lt; 9){
                                          left += ((10 - item.product_title.length) * 10)
                                  }else{
                                          item.product_title = item.product_title.slice(0,9) +'...'
                                  }
                                  // 绘制条码
                                  CTPL.drawText(
                                          left,
                                          this.textObj.y,
                                          this.textObj.rotation,
                                          this.textObj.xRatio,
                                          this.textObj.yRatio,
                                          this.textObj.textAlignment,
                                          (item.product_title+'('+item.index+')')
                                  );
                                  setTimeout(()=&gt;{
                                          CTPL.setPrintCopies(1, 1);
                                          CTPL.execute();
                                          uni.hideLoading()
                                          if(this.mainCodeArr.length != index +1){
                                                  setTimeout(()=&gt;{
                                                          this.setCodeIndex(index +1)
                                                  },500)
                                                 
                                          }else{
                                                  uni.showModal({
                                                          title:'温馨提示',
                                                          content:'打印完成',
                                                          showCancel:false
                                                  })
                                          }
                                  },1000)
                          },500)
                  },1000)
          },
</pre>
</div>
<p><strong>&nbsp;调用代码:</strong></p>
<div class="cnblogs_Highlighter">
<pre class="brush:csharp;gutter:true;">this.setCodeIndex(0)
</pre>
</div>
<h2>总结</h2>
<h3>以上的一些步骤和代码案例,就是我对接驰腾打印机的全流程,核心主要在:1.设备连接与通讯建立,2.发送打印指令,3.打印状态显示和真机调试,希望我的一点经验能对你有用</h3>
<h3 id="tid-D8HBxE">如果对您有所帮助,欢迎您点个关注,我会定时更新技术文档,大家一起讨论学习,一起进步。</h3>
<p>&nbsp;<img src="https://img2020.cnblogs.com/blog/2149129/202107/2149129-20210719144042684-15122820.jpg" width="212" height="209"></p><br><br>
来源:https://www.cnblogs.com/smileZAZ/p/18087102
頁: [1]
查看完整版本: uni-app攻略:如何对接驰腾打印机