0.1.12 • Published 2 years ago
print-brigde v0.1.12
print-brigde
与电商云打印组件通信,目前已支持菜鸟、拼多多、抖店。
安装
npm i print-brigde --save
import printer from 'print-brigde'
Demo
https://feizhaojun.com/web/print-brigde
可以通过 Console 查看返回信息。
API
打印模块提供如下API,所有API方法都可能是异步的,所以需要将回调函数作为参数。
获取打印组件状态
获取当前打印组件的链接状态
方法
getCmpts(callback)
Example
// 参数是一个 Function
printer.getCmpts(res => {
console.log(res); // res 是回参
});
// 或者 参数传一个包含键名为 callback 的对象
printer.getCmpts({
callback: (res) => {
console.log(res);
}
});
入参
回调函数
回参
返回 三大组件 是否已连接
{
"cainiao": true,
"pinduoduo": false,
"doudian": false
}
连接打印组件
print-brigde 在实例化的时候会自动连接各个打印组件,无需手动操作。但是如果出现需要重连的场景,可以通过 connect
方法手动重新连接某个打印组件。
方法
connect(cmpt: string)
Example
printer.connect('cainiao');
入参
String
打印组件代码,目前支持:
打印组件 | 代码 |
---|---|
菜鸟打印组件 | cainiao |
拼多多打印组件 | pinduoduo |
抖店打印组件 | doudian |
回参
如果该打印组件目前已经是已连接状态,返回:
{
"msg": "Already connected",
"status": "connected",
}
连接成功,返回 Event:
Event {
type: "open"
}
连接错误,返回 Event:
Event {
type: "error"
}
获取打印机列表
获取当前系统已连接的打印机
方法
getPrinters(callback)
Example
// 参数是一个 Function
printer.getPrinters(res => {
console.log(res);
});
// 或者 参数传一个包含键名为 callback 的对象
printer.getPrinters({
cmpt: 'cainiao', // 优先使用指定打印组件获取,尽量不要使用这个参数。
callback(res) {
console.log(res);
}
});
入参
回调函数
回参
!!#ff0000 注意:!! 如果任何打印组件都未连接,不会抛出错误,defaultPrinter 和 printers 均返回 null。
{
"defaultPrinter": "默认打印机的名称",
"printers": [
{
"name": "打印机名称1"
},
{
"name": "打印机名称2"
}
]
}
打印
打印动作。将需要打印的数据发给打印组件。
方法
print(params)
Example
printer.print({
// 打印参数,见下方入参详解
});
入参
{
// 必传参数,Array,需要打印的文档数据,格式请参照各平台协议
"documents": [],
// 必传参数,发送打印之后的回调函数(可能会执行多次!)
"callback": (res) => {
console.log(res);
},
// 可选参数,指定打印组件,不传会随机使用已连接的打印组件。如果是快递单打印,一定要传。
cmpt: "cainiao",
// 可选参数,指定打印机的名称,不传使用默认打印机
printer: "",
preview: false, // 可选,默认 false
previewType: 'pdf' // 可选,默认 'pdf'
}
回调函数会执行多次,目前情况下,我们只需要对第一次进行处理,但是为了避免多次执行,需要判断回参中 cmd === 'print'。
回参
回参会因打印组件、操作系统不同而有差异:
菜鸟打印组件:
// Windows
// 第 1 次回调,数据成功提交给打印组件。如果失败 status = failed
{ cmd: "print", status: "success" }
// 第 2 次回调,渲染染完。如果失败 taskStatus = failed
{ cmd: "notifyPrintResult", taskStatus: "rendered" }
// 第 3 次回调
{ cmd: "notifyPrintResult", taskStatus: "printed" }
// Mac
// 第 1 次回调,数据成功提交给打印组件。如果失败 status = failed
{ cmd: "print", status: "success" }
// 第 2 次回调
{ cmd: "notifyTaskResult", status: "initial" }
// 第 3 次回调
{ cmd: "notifyDocResult", status: "rendered" }
// 第 4 次回调
{ cmd: "notifyDocResult", status: "printed" }
// 第 5 次回调
{ cmd: "notifyPrintResult", taskStatus: "rendered" }
// 第 6 次回调
{ cmd: "notifyTaskResult", status: "completeSuccess" }
拼多多 & 抖店打印组件,表现一致
// Windows & Mac (拼多多暂无Mac版本)
// 第 1 次回调,数据成功提交给打印组件。如果失败 status = failed
{ cmd: "print", status: "success" }
// 第 2 次回调,渲染染完。如果失败 taskStatus = failed
{ cmd: "notifyPrintResult", taskStatus: "printed" }
其它
拼多多打印组件下载 Windows
拼多多打印组件文档 文档
抖店打印组件下载 Windows & Mac