js连接抖音打印组件实现打印小票
安装抖音打印组件
抖音打印组件文档:
https://bytedance.larkoffice.com/docs/doccn2vbOOdd3KWrCd6Z93nIlvg
跟着文档案例一步步配基本上没问题,
打印的时候需要设置下打印机名称
export class DouyinPrint {constructor(socketUrl = "ws://127.0.0.1:13888") {this.url = socketUrl;this.doConnect();this.isReady = false;}doConnect() {this.socket = new WebSocket(this.url);}/*** 获取打印机列表*/doGetPrinters() {var request = {requestID: this.getUUID(),version: "1.0",cmd: "getPrinters"};this.socket.send(JSON.stringify(request));}/*** 获取打印机配置*/doPrinterConfig() {var request = {requestID: this.getUUID(),version: "1.0",cmd: "printerConfig"};this.socket.send(JSON.stringify(request));}/*** 设置打印机* @param {*} data */doSetPrinterConfig(data) {var request = {requestID: this.getUUID(),version: "1.0",cmd: "setPrinterConfig",printer: data};this.socket.send(JSON.stringify(request));}/*** 通过taskID查询打印状态*/doGetTaskStatus() {var request = {requestID: "12345678901234567890",version: "1.0",cmd: "getTaskStatus",taskID: ["1", "2"]};this.socket.send(JSON.stringify(request));}// 通过documentId查询打印状态doGetDocumentStatus() {var request = {requestID: "12345678901234567890",version: "1.0",cmd: "getDocumentStatus",documentIDs: ["9890000076011"]};this.socket.send(JSON.stringify(request));}/*** 打印* @param {*} printer_name * @param {*} data * @param {*} order_id */doPrint(printer_name, data,order_id) {let request ={"cmd": "print","requestID": data.pack_id,"version": "1.0","task": {"taskID": `${order_id}_${this.getUUID()}`,"preview": false,"printer": printer_name,"documents": [{"documentID":data.warehouse_danhao,"contents": [{"templateURL": data.templateURL,"params": data.params,"signature": data.signature,"encryptedData": data.encryptedData,"addData": data.addData},// {// "data": {},// "templateURL": "自定义模板URL,保存在ISV服务端"// }]}]}}this.socket.send(JSON.stringify(request));}getUUID(len, radix) {var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".split("");var uuid = [],i;radix = radix || chars.length;if (len) {for (i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * radix)];} else {var r;uuid[8] = uuid[13] = uuid[18] = uuid[23] = "-";uuid[14] = "4";for (i = 0; i < 36; i++) {if (!uuid[i]) {r = 0 | (Math.random() * 16);uuid[i] = chars[i == 19 ? (r & 0x3) | 0x8 : r];}}}return uuid.join("");}
}