import { NativeModules } from 'react-native'; import { IPrintOptions } from './interface/iPrintOptions'; const { BluetoothEscposPrinter: OC } = NativeModules; export class BluetoothEscposPrinter { /** * 初始化打印机 */ public static async printerInit() { return await OC.printerInit(); } /** * printer the buffer data and feed (feed lines). */ public static async printAndFeed(feed: number) { return await OC.printAndFeed(feed); } /** * 设置打印机左边距 * @param sp */ public static async printerLeftSpace(sp: number) { return await OC.printerLeftSpace(sp); } /** * 设置行间距 * @param sp */ public static async printerLineSpace(sp: number) { return await OC.printerLineSpace(sp); } /** * 设置文本的下线 * @param sp 0-关,1-开,2-deeper */ public static async printerUnderLine(sp: 'OFF' | 'ON' | 'DEEPER' = 'OFF') { const hash = { OFF: 0, ON: 1, DEEPER: 2 } return await OC.printerUnderLine(hash[sp]); } /** * 设置打印机对齐方式 * @param align */ public static async printerAlign(align: 'LEFT' | 'CENTER' | 'RIGHT' = 'LEFT') { const hash = { LEFT: 0, CENTER: 1, RIGHT: 2, }; await OC.printerAlign(hash[align]); } /** * 打印文字 * @param text * @param options */ public static async printText(text: string, options?: IPrintOptions) { return await OC.printText(text, options); } /** * 按列打印文本 * @param columnWidths 各列宽度 * @param columnAligns 各列对齐方式 * @param columnTexts 各列内容 * @param options */ public static async printColumn( columnWidths: number[], columnAligns: ('LEFT' | 'CENTER' | 'RIGHT')[], columnTexts: string[], options?: IPrintOptions ) { return await OC.printColumn( columnWidths, columnAligns, columnTexts, options ); } /** * 设置打印机宽度 * @param width */ public static async setWidth(width: number) { return await OC.setWidth(width); } /** * 打印图片 * @param base64encodeStr * @param options */ public static async printPic( base64encodeStr: string, options: { width: number; left: number } ) { return await OC.printPic(base64encodeStr, options); } /** * prints the selft test. */ public static async setfTest() { return await OC.setfTest(); } /** * 设置线的旋转。 */ public static async rotate(state: 'ON' | 'OFF' = 'OFF') { const hash = { ON: 1, OFF: 0 }; return await OC.rotate(hash[state]); } /** * 设置行的斑点 */ public static async setBlob(weight: number) { return await OC.setBlob(weight); } /** * 打印二维码 * @param content * @param size * @param correctionLevel */ public static async printQRCode( content: string, size: number, correctionLevel: 'M' | 'L' | 'H' | 'Q' = 'M' ) { const hash = { M: 0, L: 1, H: 2, Q: 3, }; return await OC.printQRCode(content, size, hash[correctionLevel]); } /** * 打印条形码 * @param content * @param nType * @param nWidth * @param nHeight * @param nHriFontType * @param nHriFontPosition */ public static async printBarCode( content: string, nType: | 'UPC_A' | 'UPC_E' | 'JAN13' | 'JAN8' | 'CODE39' | 'ITF' | 'CODABAR' | 'CODE93' | 'CODE128', nWidth: number, nHeight: number, nHriFontType: number, nHriFontPosition: number ) { const hash = { UPC_A: 65, //11<=n<=12 UPC_E: 66, //11<=n<=12 JAN13: 67, //12<=n<=12 JAN8: 68, //7<=n<=8 CODE39: 69, //1<=n<=255 ITF: 70, //1<=n<=255(even numbers) CODABAR: 71, //1<=n<=255 CODE93: 72, //1<=n<=255 CODE128: 73, //2<=n<=255 }; return await OC.printBarCode( content, hash[nType], nWidth, nHeight, nHriFontType, nHriFontPosition ); } }