更新记录

1.6(2022-05-07) 下载此版本

新增针对支持标签打印的商米设备的接口调用

1.5(2022-01-18) 下载此版本

调整部分打印机模块中的文本打印下划线参数设置顺序,由于设置下划线会导致汉字大小无法修改

1.4(2021-12-23) 下载此版本

1、打印图片支持Base64; 2、新增了打印分割线和切刀控制; 3、针对Android11的一些适配修改; 4、修复了打印文本内容中不使能直接输出不生效的问题; PS:把价格去掉了,之前设了1元是产品同学想通过价格判断下使用需求状态,现在去掉希望之前购买的小伙伴应该不会打我们 PSS:因为Dcloud插件开发增加了规则导致package.json中的定义使用不了,为了不影响以前的使用,用hooksClass类替代了之前的module注册,所以有什么使用问题请及时联系我!

查看更多

平台兼容性

Android Android CPU类型 iOS
适用版本区间:5.0 - 11.0 armeabi-v7a:未测试,arm64-v8a:未测试,x86:未测试 ×

原生插件通用使用流程:

  1. 购买插件,选择该插件绑定的项目。
  2. 在HBuilderX里找到项目,在manifest的app原生插件配置中勾选模块,如需要填写参数则参考插件作者的文档添加。
  3. 根据插件作者的提供的文档开发代码,在代码中引用插件,调用插件功能。
  4. 打包自定义基座,选择插件,得到自定义基座,然后运行时选择自定义基座,进行log输出测试。
  5. 开发完毕后正式云打包

付费原生插件目前不支持离线打包。
Android 离线打包原生插件另见文档 https://nativesupport.dcloud.net.cn/NativePlugin/offline_package/android
iOS 离线打包原生插件另见文档 https://nativesupport.dcloud.net.cn/NativePlugin/offline_package/ios

注意事项:使用HBuilderX2.7.14以下版本,如果同一插件且同一appid下购买并绑定了多个包名,提交云打包界面提示包名绑定不一致时,需要在HBuilderX项目中manifest.json->“App原生插件配置”->”云端插件“列表中删除该插件重新选择


商米打印插件

本插件适用于带打印机的所有商米机器及商米58热敏票据打印机
请注意插件更新了新的引入方式和新接口!!!

1、插件使用

新版本(1.2+)请根据开发使用的商米打印机选择不同的插件引入方式:

//连接商米一体机(如V1、V2、T1、T2等等),通过如下方式
const sunmi_print = uni.requireNativePlugin('Sunmi-Print-Inner')
//连接商米自助终端(如K1、H1等等),通过如下方式
const sunmi_print = uni.requireNativePlugin('Sunmi-Print-Self')
//连接商米58热敏票据打印机(小黑盒),通过如下方式
const sunmi_print = uni.requireNativePlugin('Sunmi-Print-Outter')
//连接商米云打印机,通过如下方式
const sunmi_print = uni.requireNativePlugin('Sunmi-Print-Ble')

以及新的初始化打印机方法和新的释放方法:

//使用前连接打印机
sunmi_print.connect(res => {
    if(res.connect == "hello"){
        console.log("打印机成功连接")
    }
})
//使用后断开打印机
sunmi_print.disconnect()

2、接口说明

  • 连接打印机 connect(res=>{res.connect})

    v1.2+)新增接口替换原init()接口

    首次启动app时调用此方法来连接商米打印机,当conect返回成功后才可以进行打印;
    注意:回调func是个异步长连接,会实时返回连接的状态结果,建议一直监控回调状态,这样当使用过程中如果打印机断开了也可以及时获取

    对于商米云打印机,即引用“Sunmi-Print-Ble”时,连接是通过蓝牙方式,所以需要先在手机或商米机器上将商米云打印机配对后才能通过此方法连接打印机,如果有多个已配对打印机请使用另一种方法连接云打印机见connectByMac

    res.connect回调参数说明

    回调参数结果 回调参数说明
    hello 打印机连接成功
    bye 打印机断开连接
    unknown 没有打印机
  • 连接商米云打印机 connectByMac(string, res=>{res.connect})

    通过指定蓝牙mac地址,以蓝牙方式连接对应的商米云打印机
    注意:本方法仅能在requireNativePlugin('Sunmi-Print-Ble')后使用,其他打印机调用此方法无效,如果指定的蓝牙mac地址打印机没有配对将不会连接成功

    参数 类型 说明
    string String 指定的商米云打印机地址
    res func 连接状态的回调函数

    string详细说明
    商米云打印机地址可以通过自己设置或通过getBlueToothDevice方法获取

  • 获取蓝牙设备地址 getBlueToothDevice(arrays=>{arrays})

    调用此方法将获取到已配对的商米云打印机设备地址列表,列表返回后选择其中需要的打印机进行connectByMac连接打印机后使用 注意:返回打mac地址仅有商米云打印机,并不能依靠本方法获取其他蓝牙设备mac地址

  • 断开打印机 disconnect()

    v1.2+)新增接口替换原destroy()接口

    当不需要打印功能时需要调用本方法断开打印机的连接释放资源

    连接和断开58热敏打印机示例,同样用于其他商米机器

    <script>
        const print = uni.requireNativePlugin('Sunmi-Print-Outter');
        var isConnect = false;
        export default {
            methods: {
                start() {
                    print.connect(res => {
                        isConnect = res.connect == "hello"
                    })          
                },
               print() {
                    if(isConnect){
                        //打印小票
                    }
               },
                over() {
                    print.disconnect()
                }
            }
        }
    </script>

    连接和断开商米云打印机示例(这里直接连接返回的第一个mac地址)

    <script>
        const print = uni.requireNativePlugin('Sunmi-Print-Ble');
        var isConnect = false;
        export default {
            methods: {
                scanAndStart() {
                    print.getBlueToothDevice(arrays=>{
                        if(arrays.length > 0){
                            print.connect(arrays[0], res =>{
                                console.log(res.connect)
                            })
                        }
                    })
                },
               print() {
                    if(isConnect){
                        //打印小票
                    }
               },
                over() {
                    print.disconnect()
                }
            }
        }
    </script>
  • 获取打印机的实时状态 getPrinterStatus(result => {result.status})

    result.status回调参数说明

    回调参数结果 回调参数说明
    offline 打印机未连接
    running 打印机正常
    fault 打印机故障
    outpaper 打印机缺纸
    hot 打印机过热
    open 打印机开盖(58打印机不支持)
    error 未知错误

    示例

    sunmi_print.getPrinterStatus(result => {
       console.log("打印机当前状态是:” + result.status)
    })
  • 打印机初始化 printInit()

    初始化打印机的缓存、样式恢复为默认(默认字体大小24、居左)

  • 打印自检页 printSelfCheck()

    可通过自检页查看打印效果、打印机信息
    商米自助打印机和58热敏打印机不支持打印自检页

  • 打印指令数据 printRawData(array)

    接收ESC指令内容并执行打印(参考ESC指令)
    初始化、自检页、指令数据打印示例

    //主动初始化打印机
    sunmi_print.printInit()
    //打印一张自检页
    sunmi_print.printSelfCheck()
    //设置加粗指令,之后的文本内容会加粗
    var cmd1 = [0x1b, 0x45, 0x01] 
    //设置行高指令,之后的文本内容行间距改变
    var cmd2 = [0x1b, 0x33, 0x00]
    //打印12345678
    var data = [0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x0a]
    sunmi_print.printRawData(cmd1);
    sunmi_print.printRawData(cmd2);
    sunmi_print.printRawData(data)
    sunmi_print.printRawData(data);
  • 打印富含样式的文本内容 printText(object)

    object参数说明

    参数 类型 默认值 说明
    text String 要打印的文本内容
    align Number 0 对齐方式:0居左 1居中 2居右
    size Number 24 打印文本的字体大小
    bold Boolean false 文本内容是否加粗
    underline Boolean false 文本内容是否下划线
    compact Boolean false 文本内容是否紧凑模式
    skip Boolean true 文本内容当不满一行时是否直接输出

    size说明
    商米内置打印机的字体大小支持12~60
    商米自助打印机和58打印机仅支持默认字体24的倍数大小

    underline说明
    商米K1自助打印机下划线无法在大字体下使用其他打印机无此问题

    compact说明
    紧凑模式指当启用紧凑模式时,本行内容将会挨着上一行内容,没有行间距

    示例

    print.printText({
            text:"商米打印测试",
            align:1,
            size:30,
            bold:true,
            underline:true,
            compact:true,
            skip:true
    })
    print.printText({
            text:"商米打印测试",
            align:2,
            size:12,
            compact:true,
            skip:true
    })
  • 按列打印文本内容 printColumnsText(object)

    本接口将按列打印文本内容,方便打印出特殊排版格式的文本,为了排版固定本接口只能打印默认字体大小的文本,不能改变字体大小

    object参数说明 参数 类型 默认值 说明
    texts Array 字符串数组表示每一列的文本内容
    lengths Array 整型数组表示每一列占的字符数量
    aligns Array 整型数组表示每一列的对齐方式
    bold Boolean false 文本内容是否加粗

    注意:
    1、各个数组的长度必须一致否则将打印失败
    2、字符数量lengths表示ascii码的数量,中文汉字占两个ascii码
    3、58mm的打印纸lengths和不超过30,80mm的lengths和不超过46
    示例

    //按列打印一行简单的内容,每列分别居左、居中、居右
    print.printColumnsText({
        texts:["一", "二二", "三三三"],
        lengths:[10, 10, 10],
        aligns:[0, 1, 2]
    })
    //左右两列打印商品列表
    print.printColumnsText({
        texts:["商品", "价格"],
        lengths:[15, 15],
        aligns:[0, 2],
        bold:true
    })
    print.printColumnsText({
    texts:["汉堡", "15元"],
    lengths:[15, 15],
    aligns:[0, 2],
    })
    print.printColumnsText({
        texts:["可乐", "8元"],
        lengths:[15, 15],
        aligns:[0, 2],
    })
  • 打印条码内容 printBarCode(object)

    object参数说明

    参数 类型 默认值 说明
    text String 要打印的条形码内容需搭配条形码类型
    symbology Number 8 条形码类型,如下
    height Number 128 条形码高度 1~255
    width Number 2 条形码宽度 2~6
    hri Number 0 HRI位置 0:不打印 1:上方 2:下方 3:上下
    align Number 0 对齐方式:0居左 1居中 2居右

    symbology说明

    取值 码类型
    0 UPC-A编码
    1 UPC-E编码
    2 EAN13编码
    3 EAN8编码
    4 CODE39编码
    5 ITF编码
    6 CODABAR编码
    7 CODE93编码
    8 CODE128编码

    注意Code128分三种类型: A类:包含⼤写字⺟、数字、标点等; B类:⼤⼩写字⺟,数字; C类:纯数字,复数字符,若为单数位,最后⼀个将忽略; 接⼝默认使⽤B类编码,若要使⽤A类、C类编码需在内容前⾯加“{A”、“{C”
    例如:
    默认B类 “1234567”
    使用A类 “{A2344A”
    使用C类 “{C123123”
    混合使用 ”{A1A{B13B{C12”

    示例

    print.printBarCode({
                    text:"1234567890",
                    symbolgy:8,
                    height:100,
                    width:3,
                    hri:0,
                    align:1
    })
  • 打印二维码码内容 printQrCode(object)

    object参数说明

    参数 类型 默认值 说明
    text String 二维码内容
    size Number 24 二维码块大小 4~16
    errorlevel Number 0 纠错等级 0:L 1:M 2:Q 3:H
    align Number 0 对齐方式:0居左 1居中 2居右

    示例

    print.printQrCode({
                text:"1234567890",
                size:5,
                errorlevel:0,
                align:1
    })
  • 打印图片 printBitmap(object)

    object参数说明

    参数 类型 默认值 说明
    path String 图片的存储路径
    url String 来自网络的图片url
    base64 String 传入的图片base64数据
    align Number 0 对齐方式:0居左 1居中 2居右

    path说明 路径必须是/static目录下的图片
    url说明 当同时有path参数时将会使用本地存储的图片,而不使用来自网络的图片,图片最好不要超过打印纸宽度,否则将不能打印

    示例

    //打印本地存储的图片logo
    print.printBitmap({
        path:"/static/logo.png",
        align:1
    })
    //打印网络端的图片logo
    print.printBitmap({
        url:"http://img.pconline.com.cn/images/upload/upc/tx/itbbs/1607/06/c3/23806812_1467754402821_1024x1024.jpg",
        align:1
    })
  • 打印分隔线 printDividingline(object)

    object参数说明 参数 类型 默认值 说明
    style Number 分隔线的样式,见下
    height Number 分割线的高度 1-60

    style说明

    取值 说明
    0 58mm实线
    1 80mm实线
    2 58mm虚线
    3 80mm虚线
    4 空白行

    需要根据打印机的纸张规格选择对应的分割线类型,错误的选择可能导致打印乱码
    可通过在分割线前后增加空白行创造理想的分割效果

    height参数说明
    分割线的高度设置,标准范围在1-60像素
    示例

    //空白行分割之前打印内容(类似走空白)
    print.printDividingline({
                    style:"4",
                    height:"10"
                }),
    //虚线分割线      
    print.printDividingline({
                    style:"3",
                    height:"3"
                }),
    //空白行分割之后的打印内容(类似走空白)
    print.printDividingline({
                    style:"4",
                    height:"10"
                }),
  • 标准切纸 cutPaper()

    只有支持切刀的设备调用有效
    标准切纸会自动进纸到切纸区域所以不需要在调用切刀前走纸来控制切纸位

3、标签(黑标)打印

使用商米手持机V2Pro、V2s等或使用商米台式机T2等设备时,可以在系统设置中切换打印机为标签(黑标)打印机,此时可以使用如下接口实现针对标签或黑标的打印;
注意:
此打印方法仅支持内置打印机即使用 uni.requireNativePlugin('Sunmi-Print-Inner') 导入有效

  • 获取打印机的类型 getPrinterType(result => {result.type})

    通过此接口可以获取当前的打印机类型,根据类型实现不同方式的打印
    result.type回调参数说明

    回调参数结果 回调参数说明
    ticket 小票打印机
    label 标签/黑标打印机

    示例

    sunmi_print.getPrinterType(result => {
       console.log("打印机是:” + result.type)
    })
  • 标签定位 labelLocate()

    在打印每张标签内容前使用此方法定位到标签起始位置,之后使用打印接口绘制标签内容

  • 标准切纸 labelOutput()

    在绘制完标签内容后如果不需要继续打印标签时使用此方法,将标签自动推送到切纸口

完整示例

 //仅打印一张标签:
 sunmi_print.labelLocate()
 sunmi_print.printText(...)
 sunmi_print.labelOutput()

 //连续打印标签
 //第一张
 sunmi_print.labelLocate()
 sunmi_print.printText(...)
 //第二张
 sunmi_print.labelLocate()
 sunmi_print.printText(...)
 //...第n张
 sunmi_print.labelLocate()
 sunmi_print.printText(...)
 //结束
 sunmi_print.labelOutput()

隐私、权限声明

1. 本插件需要申请的系统权限列表:

蓝牙权限 位置权限

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

许可协议

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/

TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION

  1. Definitions.

    "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.

    "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.

    "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.

    "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.

    "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.

    "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.

    "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).

    "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.

    "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."

    "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.

  2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.

  3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.

  4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:

    (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and

    (b) You must cause any modified files to carry prominent notices stating that You changed the files; and

    (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and

    (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.

    You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.

  5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.

  6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.

  7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.

  8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.

  9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.

    END OF TERMS AND CONDITIONS

    APPENDIX: How to apply the Apache License to your work.

    To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives.

    Copyright [yyyy] [name of copyright owner]

    Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

使用中有什么不明白的地方,就向插件作者提问吧~ 我要提问