taro-bluetooth-print 2.3.0 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/CHANGELOG.md +73 -195
  2. package/README.md +134 -386
  3. package/dist/index.cjs.js +1 -1
  4. package/dist/index.es.js +1 -1
  5. package/dist/index.umd.js +1 -1
  6. package/dist/types/config/PrinterConfigManager.d.ts +206 -0
  7. package/dist/types/config/index.d.ts +8 -0
  8. package/dist/types/core/BluetoothPrinter.d.ts +1 -1
  9. package/dist/types/core/EventEmitter.d.ts +6 -26
  10. package/dist/types/core/index.d.ts +6 -0
  11. package/dist/types/device/MultiPrinterManager.d.ts +164 -0
  12. package/dist/types/device/index.d.ts +2 -0
  13. package/dist/types/drivers/CpclDriver.d.ts +304 -0
  14. package/dist/types/drivers/GPrinterDriver.d.ts +63 -0
  15. package/dist/types/drivers/ZplDriver.d.ts +325 -0
  16. package/dist/types/drivers/index.d.ts +9 -0
  17. package/dist/types/encoding/gbk-lite.d.ts +8 -0
  18. package/dist/types/encoding/gbk-table.d.ts +8 -30
  19. package/dist/types/index.d.ts +12 -8
  20. package/dist/types/services/BatchPrintManager.d.ts +205 -0
  21. package/dist/types/services/ConnectionManager.d.ts +1 -1
  22. package/dist/types/services/PrintHistory.d.ts +142 -0
  23. package/dist/types/services/PrintJobManager.d.ts +28 -4
  24. package/dist/types/services/PrinterStatus.d.ts +97 -0
  25. package/dist/types/services/index.d.ts +11 -0
  26. package/package.json +25 -7
  27. package/src/adapters/AlipayAdapter.ts +1 -0
  28. package/src/adapters/BaiduAdapter.ts +1 -0
  29. package/src/adapters/BaseAdapter.ts +6 -8
  30. package/src/adapters/ByteDanceAdapter.ts +1 -0
  31. package/src/adapters/TaroAdapter.ts +1 -0
  32. package/src/adapters/WebBluetoothAdapter.ts +1 -1
  33. package/src/config/PrinterConfigManager.ts +519 -0
  34. package/src/config/index.ts +15 -0
  35. package/src/core/BluetoothPrinter.ts +15 -15
  36. package/src/core/EventEmitter.ts +15 -15
  37. package/src/core/index.ts +7 -0
  38. package/src/device/MultiPrinterManager.ts +470 -0
  39. package/src/device/index.ts +8 -0
  40. package/src/drivers/CpclDriver.ts +549 -0
  41. package/src/drivers/GPrinterDriver.ts +115 -0
  42. package/src/drivers/TsplDriver.ts +9 -21
  43. package/src/drivers/ZplDriver.ts +543 -0
  44. package/src/drivers/index.ts +37 -0
  45. package/src/encoding/gbk-lite.ts +113 -0
  46. package/src/encoding/gbk-table.ts +80 -58
  47. package/src/index.ts +40 -35
  48. package/src/plugins/PluginManager.ts +3 -1
  49. package/src/plugins/builtin/LoggingPlugin.ts +4 -2
  50. package/src/plugins/builtin/RetryPlugin.ts +8 -14
  51. package/src/services/BatchPrintManager.ts +500 -0
  52. package/src/services/ConnectionManager.ts +25 -22
  53. package/src/services/PrintHistory.ts +336 -0
  54. package/src/services/PrintJobManager.ts +69 -9
  55. package/src/services/PrinterStatus.ts +267 -0
  56. package/src/services/index.ts +22 -0
  57. package/src/template/TemplateEngine.ts +4 -1
package/README.md CHANGED
@@ -4,12 +4,14 @@
4
4
  <img src="https://img.shields.io/npm/v/taro-bluetooth-print?style=flat-square&color=00d8ff" alt="npm version">
5
5
  <img src="https://img.shields.io/npm/dm/taro-bluetooth-print?style=flat-square&color=00d8ff" alt="downloads">
6
6
  <img src="https://img.shields.io/npm/l/taro-bluetooth-print?style=flat-square&color=00d8ff" alt="license">
7
- <img src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square" alt="PRs Welcome">
7
+ <img src="https://img.shields.io/github/stars/agions/taro-bluetooth-print?style=flat-square" alt="stars">
8
+ <img src="https://img.shields.io/github/forks/agions/taro-bluetooth-print?style=flat-square" alt="forks">
9
+ <img src="https://img.shields.io/bundlephobia/minzip/taro-bluetooth-print?style=flat-square" alt="bundle size">
8
10
  </p>
9
11
 
10
12
  <p align="center">
11
13
  <strong>轻量级、高性能的 Taro 蓝牙打印库</strong><br>
12
- 支持图片、二维码、断点续传与弱网适配
14
+ 支持热敏打印机、标签打印机,多平台适配
13
15
  </p>
14
16
 
15
17
  ---
@@ -17,421 +19,176 @@
17
19
  ## ✨ 特性
18
20
 
19
21
  - 🚀 **高性能** - 直接字节缓冲区操作,服务缓存优化
20
- - 📱 **跨平台** - 基于 Taro 4.x 蓝牙 API,支持微信小程序、H5、React Native 等
21
- - 🎨 **简洁 API** - 链式调用,易于使用
22
- - 🖼️ **图片打印** - 内置 Floyd-Steinberg 抖动算法,高质量图片转换
23
- - 📲 **二维码支持** - 原生 ESC/POS 二维码指令
24
- - 📊 **条码支持** - 支持 Code128、Code39、EAN-13、EAN-8、UPC-A 格式
25
- - 🔄 **断点续传** - 支持打印任务暂停/恢复/取消
26
- - 📶 **弱网适配** - 智能分片与重试机制,自适应传输参数
22
+ - 📱 **多平台** - 微信小程序、H5、鸿蒙、支付宝、百度、字节跳动
23
+ - 🎨 **多驱动** - ESC/POS (热敏)、TSPL/ZPL/CPCL (标签)
24
+ - 🖼️ **图片打印** - 内置 Floyd-Steinberg 抖动算法
25
+ - 📲 **二维码/条码** - 原生指令支持,多种格式,格式校验
26
+ - 🔄 **断点续传** - 暂停/恢复/取消打印任务
27
+ - 📶 **弱网适配** - 智能分片与重试机制
27
28
  - 📊 **进度追踪** - 实时打印进度事件
28
- - 💾 **离线缓存** - 断网时自动缓存,重连后自动同步
29
+ - 💾 **离线缓存** - 断网自动缓存,联网自动同步
29
30
  - 📋 **打印队列** - 优先级排序,失败自动重试
30
31
  - 📝 **模板引擎** - 内置收据和标签模板
31
- - 🔍 **打印预览** - ESC/POS 命令渲染为图像预览
32
- - 🌐 **Web Bluetooth** - 支持 H5 环境的 Web Bluetooth API
33
- - 💓 **连接稳定** - 心跳检测和自动重连机制
34
- - 🏷️ **标签打印** - TSPL 协议支持,适用于 TSC 等标签打印机 (v2.3+)
35
- - 🔌 **插件系统** - 可扩展架构,支持自定义钩子 (v2.3+)
36
- - 🛠️ **TypeScript** - 完整的类型定义和 JSDoc 文档
37
- - 🧪 **高测试覆盖** - Vitest 驱动,69+ 测试用例
32
+ - 🔍 **打印预览** - ESC/POS 命令渲染为图像
33
+ - 🔌 **插件系统** - 可扩展架构,支持自定义钩子
34
+ - 🛠️ **TypeScript** - 完整的类型定义和 JSDoc
35
+ - 📡 **多打印机管理** - MultiPrinterManager 支持多设备并发
36
+ - 💰 **配置持久化** - PrinterConfigManager 保存打印机配置
37
+ - 📦 **批量打印优化** - BatchPrintManager 合并小任务减少开销
38
+ - 📜 **打印历史** - PrintHistory 追踪打印记录和统计
39
+ - 🖨️ **状态查询** - PrinterStatus 检测纸张/电量状态
38
40
 
39
41
  ## 📦 安装
40
42
 
41
43
  ```bash
44
+ # npm
42
45
  npm install taro-bluetooth-print
43
- # 或
46
+
47
+ # yarn
44
48
  yarn add taro-bluetooth-print
45
- # 或
49
+
50
+ # pnpm
46
51
  pnpm add taro-bluetooth-print
47
52
  ```
48
53
 
49
- ## 🚀 快速开始
50
-
51
- ### 基础示例
54
+ ## 📊 性能
52
55
 
53
- ```typescript
54
- import { BluetoothPrinter } from 'taro-bluetooth-print';
56
+ | 指标 | 值 |
57
+ |------|-----|
58
+ | Bundle 大小 | **26 KB** (gzip) |
59
+ | Tree-shaking | ✅ 支持 |
60
+ | 按需加载 | ✅ 编码表懒加载 |
55
61
 
56
- const printer = new BluetoothPrinter();
62
+ ## 🚀 快速开始
57
63
 
58
- async function printReceipt(deviceId: string) {
59
- try {
60
- // 连接设备
61
- await printer.connect(deviceId);
62
-
63
- // 构建打印内容
64
- await printer
65
- .text('=== 欢迎光临 ===', 'GBK')
66
- .feed()
67
- .text('商品A x1 ¥10.00')
68
- .text('商品B x2 ¥20.00')
69
- .feed()
70
- .text('------------------------')
71
- .text('合计: ¥30.00')
72
- .feed(2)
73
- .qr('https://example.com', { size: 8 })
74
- .feed(2)
75
- .cut()
76
- .print(); // 发送到打印机
77
-
78
- console.log('打印成功!');
79
- } catch (error) {
80
- console.error('打印失败:', error);
81
- } finally {
82
- await printer.disconnect();
64
+ ```typescript
65
+ import { BluetoothPrinter, DeviceManager } from 'taro-bluetooth-print';
66
+
67
+ async function print() {
68
+ // 1. 扫描设备
69
+ const manager = new DeviceManager();
70
+ await manager.startScan({ timeout: 10000 });
71
+ const devices = manager.getDiscoveredDevices();
72
+
73
+ if (devices.length === 0) {
74
+ console.log('未发现设备');
75
+ return;
83
76
  }
77
+
78
+ // 2. 连接打印机
79
+ const printer = new BluetoothPrinter();
80
+ await printer.connect(devices[0].deviceId);
81
+
82
+ // 3. 打印
83
+ await printer
84
+ .text('=== 欢迎光临 ===', 'GBK')
85
+ .feed()
86
+ .text('商品A x1 ¥10.00', 'GBK')
87
+ .text('商品B x2 ¥20.00', 'GBK')
88
+ .feed()
89
+ .text('------------------------')
90
+ .text('合计: ¥30.00', 'GBK')
91
+ .feed(2)
92
+ .qr('https://example.com')
93
+ .feed(2)
94
+ .cut()
95
+ .print();
96
+
97
+ // 4. 断开
98
+ await printer.disconnect();
99
+
100
+ console.log('打印完成!');
84
101
  }
85
102
  ```
86
103
 
87
- ### 监听打印进度
104
+ ## 🖥️ 支持的平台
88
105
 
89
- ```typescript
90
- import { BluetoothPrinter, LogLevel, Logger } from 'taro-bluetooth-print';
106
+ | 平台 | 适配器 | 状态 |
107
+ |------|--------|------|
108
+ | 微信小程序 | `TaroAdapter` | ✅ |
109
+ | H5 (Web Bluetooth) | `WebBluetoothAdapter` | ✅ |
110
+ | 支付宝小程序 | `AlipayAdapter` | ✅ |
111
+ | 百度小程序 | `BaiduAdapter` | ✅ |
112
+ | 字节跳动小程序 | `ByteDanceAdapter` | ✅ |
113
+ | 鸿蒙 HarmonyOS | `HarmonyOSAdapter` | ✅ |
114
+ | React Native | `TaroAdapter` | ✅ |
91
115
 
92
- // 启用调试日志(可选)
93
- Logger.setLevel(LogLevel.DEBUG);
116
+ ## 🖨️ 支持的驱动
94
117
 
95
- const printer = new BluetoothPrinter();
118
+ | 驱动 | 协议 | 适用打印机 |
119
+ |------|------|-----------|
120
+ | `EscPos` | ESC/POS | 热敏票据打印机 (58/80mm) |
121
+ | `TsplDriver` | TSPL | TSC 标签打印机 |
122
+ | `ZplDriver` | ZPL | Zebra 斑马标签打印机 |
123
+ | `CpclDriver` | CPCL | HP/霍尼韦尔移动打印机 |
96
124
 
97
- // 监听进度
98
- printer.on('progress', ({ sent, total }) => {
99
- const percent = ((sent / total) * 100).toFixed(1);
100
- console.log(`打印进度: ${percent}%`);
101
- });
102
-
103
- // 监听错误
104
- printer.on('error', error => {
105
- console.error('打印错误:', error.code, error.message);
106
- });
107
-
108
- // 监听完成
109
- printer.on('print-complete', () => {
110
- console.log('打印完成!');
111
- });
125
+ ### 标签打印示例 (TSPL)
112
126
 
113
- await printer.connect(deviceId);
114
- await printer.text('Hello').feed().print();
115
- ```
127
+ ```typescript
128
+ import { BluetoothPrinter, TsplDriver } from 'taro-bluetooth-print';
116
129
 
117
- ### 断点续传示例
130
+ const driver = new TsplDriver();
131
+ const printer = new BluetoothPrinter(undefined, driver);
118
132
 
119
- ```typescript
120
- const printer = new BluetoothPrinter();
133
+ driver
134
+ .size(60, 40) // 60x40mm 标签
135
+ .gap(3) // 间隙 3mm
136
+ .clear()
137
+ .text('商品名称', { x: 20, y: 20, font: 3 })
138
+ .text('¥99.00', { x: 20, y: 60, font: 4 })
139
+ .barcode('6901234567890', { x: 20, y: 100, type: 'EAN13' })
140
+ .qrcode('https://example.com', { x: 250, y: 20 })
141
+ .print(1);
121
142
 
122
143
  await printer.connect(deviceId);
144
+ await printer.print();
145
+ ```
123
146
 
124
- // 构建大量打印内容
125
- printer.text('第1页内容...').feed(10).text('第2页内容...').feed(10).text('第3页内容...');
147
+ ## 📚 示例项目
126
148
 
127
- // 开始打印(异步)
128
- const printPromise = printer.print();
149
+ 完整的示例项目,帮助快速上手:
129
150
 
130
- // 5秒后暂停
131
- setTimeout(() => {
132
- printer.pause();
133
- console.log('已暂停,剩余:', printer.remaining(), '字节');
134
- }, 5000);
151
+ - [微信小程序示例](examples/weapp/) - 完整的打印页面
152
+ - [H5 示例](examples/h5/) - Web Bluetooth 网页打印
153
+ - [鸿蒙示例](examples/harmonyos/) - HarmonyOS 原生打印服务
154
+ - [React Native 示例](examples/react-native/) - RN 打印组件
135
155
 
136
- // 再过5秒恢复
137
- setTimeout(async () => {
138
- await printer.resume();
139
- console.log('已恢复打印');
140
- }, 10000);
156
+ ## 📖 文档
141
157
 
142
- await printPromise;
143
- ```
158
+ - [快速开始](https://agions.github.io/taro-bluetooth-print/guide/getting-started) - 5 分钟入门
159
+ - [功能特性](https://agions.github.io/taro-bluetooth-print/guide/features) - 全部功能介绍
160
+ - [驱动支持](https://agions.github.io/taro-bluetooth-print/guide/drivers) - ESC/POS, TSPL, ZPL, CPCL
161
+ - [核心概念](https://agions.github.io/taro-bluetooth-print/guide/core-concepts) - 架构设计与原理
162
+ - [API 参考](https://agions.github.io/taro-bluetooth-print/api) - 完整的 API 文档
163
+ - [故障排除](https://agions.github.io/taro-bluetooth-print/guide/troubleshooting) - 常见问题解决
144
164
 
145
- ### 图片打印
165
+ ## 🔧 配置
146
166
 
147
167
  ```typescript
148
- import Taro from '@tarojs/taro';
149
-
150
- // 从 Canvas 获取图片数据
151
- const canvas = Taro.createCanvasContext('myCanvas');
152
- // ... 绘制内容 ...
153
-
154
- Taro.canvasGetImageData({
155
- canvasId: 'myCanvas',
156
- x: 0,
157
- y: 0,
158
- width: 200,
159
- height: 100,
160
- success: res => {
161
- const imageData = new Uint8Array(res.data);
162
-
163
- printer.image(imageData, res.width, res.height).feed(2).print();
164
- },
165
- });
166
- ```
167
-
168
- ### 弱网适配
168
+ const printer = new BluetoothPrinter();
169
169
 
170
- ```typescript
171
- // 配置重试和分片参数
170
+ // 适配器参数
172
171
  printer.setOptions({
173
- chunkSize: 20, // 每次发送20字节
174
- delay: 30, // 分片间延迟30ms
175
- retries: 5, // 失败重试5次
172
+ chunkSize: 20, // 分片大小 (默认 20)
173
+ delay: 20, // 分片间隔 ms (默认 20)
174
+ retries: 3, // 重试次数 (默认 3)
176
175
  });
177
176
 
178
- await printer.text('测试内容').print();
179
- ```
180
-
181
- ### 文本格式化 (v2.2+)
182
-
183
- ```typescript
184
- await printer
185
- .align('center')
186
- .setSize(2, 2)
187
- .setBold(true)
188
- .text('大标题')
189
- .resetStyle()
190
- .feed()
191
- .align('left')
192
- .text('正常文本')
193
- .print();
194
- ```
195
-
196
- ### 条码打印 (v2.2+)
197
-
198
- ```typescript
199
- await printer.text('商品条码:').barcode('6901234567890', 'EAN13', { height: 80 }).feed(2).print();
200
- ```
201
-
202
- ### 设备管理 (v2.2+)
203
-
204
- ```typescript
205
- import { DeviceManager } from 'taro-bluetooth-print';
206
-
207
- const deviceManager = new DeviceManager();
208
-
209
- // 监听设备发现
210
- deviceManager.on('device-found', device => {
211
- console.log('发现设备:', device.name, device.deviceId);
177
+ // 事件监听
178
+ printer.on('progress', ({ sent, total }) => {
179
+ console.log(`进度: ${(sent / total * 100).toFixed(1)}%`);
212
180
  });
213
181
 
214
- // 开始扫描
215
- await deviceManager.startScan({ timeout: 10000 });
216
-
217
- // 获取已发现的设备
218
- const devices = deviceManager.getDiscoveredDevices();
219
- ```
220
-
221
- ### 打印队列 (v2.2+)
222
-
223
- ```typescript
224
- import { PrintQueue } from 'taro-bluetooth-print';
225
-
226
- const queue = new PrintQueue({ maxSize: 100 });
227
-
228
- // 添加高优先级任务
229
- queue.add(printData, { priority: 'HIGH' });
230
-
231
- // 监听完成事件
232
- queue.on('job-completed', job => {
233
- console.log('任务完成:', job.id);
182
+ printer.on('error', (error) => {
183
+ console.error('错误:', error.code, error.message);
234
184
  });
235
- ```
236
-
237
- ### 离线缓存 (v2.2+)
238
-
239
- ```typescript
240
- import { OfflineCache } from 'taro-bluetooth-print';
241
-
242
- const cache = new OfflineCache();
243
-
244
- // 断网时自动缓存
245
- await cache.save({ id: 'job-1', data: printData });
246
-
247
- // 重连后同步
248
- await cache.sync();
249
- ```
250
-
251
- ### TSPL 标签打印 (v2.3+)
252
185
 
253
- ```typescript
254
- import { TsplDriver } from 'taro-bluetooth-print';
255
-
256
- const tspl = new TsplDriver();
257
-
258
- // 生成标签打印指令
259
- const buffer = tspl
260
- .size(60, 40) // 标签尺寸 60x40mm
261
- .gap(3) // 标签间隙 3mm
262
- .clear() // 清除缓冲区
263
- .text('商品名称', { x: 50, y: 30, font: 3 })
264
- .text('¥99.00', { x: 50, y: 80, font: 4, xMultiplier: 2 })
265
- .barcode('6901234567890', {
266
- x: 50,
267
- y: 120,
268
- type: 'EAN13',
269
- height: 60
270
- })
271
- .qrcode('https://example.com', {
272
- x: 300,
273
- y: 30,
274
- cellWidth: 4
275
- })
276
- .print(1) // 打印1份
277
- .getBuffer();
278
-
279
- // 发送到打印机
280
- await printer.connect(deviceId);
281
- await printer.setOptions({ chunkSize: 100 });
282
- // 直接发送 TSPL 指令需要自定义适配器
283
- ```
284
-
285
- ### 插件系统 (v2.3+)
286
-
287
- ```typescript
288
- import {
289
- PluginManager,
290
- createLoggingPlugin,
291
- createRetryPlugin
292
- } from 'taro-bluetooth-print';
293
-
294
- // 创建插件管理器
295
- const plugins = new PluginManager();
296
-
297
- // 注册日志插件
298
- await plugins.register(createLoggingPlugin({
299
- logProgress: true, // 记录进度
300
- }));
301
-
302
- // 注册重试插件
303
- await plugins.register(createRetryPlugin({
304
- maxRetries: 5,
305
- initialDelay: 1000,
306
- }));
307
-
308
- // 自定义插件
309
- const myPlugin = {
310
- name: 'my-plugin',
311
- hooks: {
312
- beforePrint: (buffer) => {
313
- console.log(`即将打印 ${buffer.length} 字节`);
314
- return buffer; // 可以修改数据
315
- },
316
- afterPrint: (bytesSent) => {
317
- console.log(`打印完成: ${bytesSent} 字节`);
318
- },
319
- onError: (error) => {
320
- // 上报错误
321
- reportError(error);
322
- return false; // false = 不抑制错误
323
- },
324
- },
325
- };
326
-
327
- await plugins.register(myPlugin);
328
- ```
329
-
330
- ## 📚 文档
331
-
332
- 完整文档请访问:**[https://agions.github.io/taro-bluetooth-print/](https://agions.github.io/taro-bluetooth-print/)**
333
-
334
- - [安装指南](https://agions.github.io/taro-bluetooth-print/guide/getting-started)
335
- - [功能特性](https://agions.github.io/taro-bluetooth-print/guide/features)
336
- - [核心概念](https://agions.github.io/taro-bluetooth-print/guide/core-concepts)
337
- - [高级用法](https://agions.github.io/taro-bluetooth-print/guide/advanced)
338
- - [API 参考](https://agions.github.io/taro-bluetooth-print/api)
339
- - [故障排除](https://agions.github.io/taro-bluetooth-print/guide/troubleshooting)
340
-
341
- ## 🎯 核心 API
342
-
343
- ### BluetoothPrinter
344
-
345
- | 方法 | 说明 | 返回值 |
346
- | ------------------------------------ | -------------------- | --------------- |
347
- | `connect(deviceId)` | 连接蓝牙设备 | `Promise<this>` |
348
- | `disconnect()` | 断开连接 | `Promise<void>` |
349
- | `text(content, encoding?)` | 添加文本 | `this` |
350
- | `feed(lines?)` | 换行 | `this` |
351
- | `image(data, width, height)` | 打印图片 | `this` |
352
- | `qr(content, options?)` | 打印二维码 | `this` |
353
- | `barcode(content, format, options?)` | 打印条码 (v2.2+) | `this` |
354
- | `align(alignment)` | 设置对齐 (v2.2+) | `this` |
355
- | `setSize(width, height)` | 设置字体大小 (v2.2+) | `this` |
356
- | `setBold(enabled)` | 设置粗体 (v2.2+) | `this` |
357
- | `setUnderline(enabled)` | 设置下划线 (v2.2+) | `this` |
358
- | `resetStyle()` | 重置样式 (v2.2+) | `this` |
359
- | `cut()` | 切纸 | `this` |
360
- | `setOptions(options)` | 设置适配器参数 | `this` |
361
- | `print()` | 发送打印 | `Promise<void>` |
362
- | `pause()` | 暂停打印 | `void` |
363
- | `resume()` | 恢复打印 | `Promise<void>` |
364
- | `cancel()` | 取消打印 | `void` |
365
- | `remaining()` | 获取剩余字节数 | `number` |
366
-
367
- ### TsplDriver (v2.3+)
368
-
369
- | 方法 | 说明 | 返回值 |
370
- | ------------------------------ | ------------------ | ------------ |
371
- | `size(width, height)` | 设置标签尺寸(mm) | `this` |
372
- | `gap(gap, offset?)` | 设置标签间隙 | `this` |
373
- | `speed(speed)` | 设置打印速度(1-10) | `this` |
374
- | `density(density)` | 设置打印浓度(0-15) | `this` |
375
- | `clear()` | 清除图像缓冲区 | `this` |
376
- | `text(content, options)` | 添加文本 | `this` |
377
- | `barcode(content, options)` | 添加条码 | `this` |
378
- | `qrcode(content, options)` | 添加二维码 | `this` |
379
- | `box(options)` | 绘制矩形框 | `this` |
380
- | `line(options)` | 绘制线条 | `this` |
381
- | `print(copies?, sets?)` | 打印 | `this` |
382
- | `getBuffer()` | 获取指令缓冲区 | `Uint8Array` |
383
- | `getCommands()` | 获取指令字符串 | `string` |
384
-
385
- ### PluginManager (v2.3+)
386
-
387
- | 方法 | 说明 | 返回值 |
388
- | --------------------------- | -------------- | --------------- |
389
- | `register(plugin, options)` | 注册插件 | `Promise<void>` |
390
- | `unregister(name)` | 注销插件 | `Promise<void>` |
391
- | `get(name)` | 获取插件 | `Plugin` |
392
- | `has(name)` | 检查插件是否存在 | `boolean` |
393
- | `getNames()` | 获取所有插件名 | `string[]` |
394
- | `clear()` | 清除所有插件 | `Promise<void>` |
395
-
396
- ### 事件
397
-
398
- | 事件名 | 数据类型 | 说明 |
399
- | ---------------- | --------------------- | ------------ |
400
- | `state-change` | `PrinterState` | 连接状态变化 |
401
- | `progress` | `{ sent, total }` | 打印进度 |
402
- | `error` | `BluetoothPrintError` | 错误事件 |
403
- | `connected` | `string` (deviceId) | 已连接 |
404
- | `disconnected` | `string` (deviceId) | 已断开 |
405
- | `print-complete` | `void` | 打印完成 |
406
-
407
- ## 🔧 配置选项
408
-
409
- ```typescript
410
- interface IAdapterOptions {
411
- chunkSize?: number; // 分片大小(默认: 20字节)
412
- delay?: number; // 分片延迟(默认: 20ms)
413
- retries?: number; // 重试次数(默认: 3)
414
- }
415
-
416
- interface IQrOptions {
417
- model?: 1 | 2; // 二维码模型(默认: 2)
418
- size?: number; // 模块大小 1-16(默认: 6)
419
- errorCorrection?: 'L' | 'M' | 'Q' | 'H'; // 纠错级别(默认: 'M')
420
- }
186
+ printer.on('print-complete', () => {
187
+ console.log('打印完成');
188
+ });
421
189
  ```
422
190
 
423
- ## 🌐 平台支持
424
-
425
- | 平台 | 支持情况 | 说明 |
426
- | -------------- | -------- | ---------------------------- |
427
- | 微信小程序 | ✅ | 完全支持 |
428
- | H5 | ✅ | 需要浏览器支持 Web Bluetooth |
429
- | React Native | ✅ | 通过 Taro RN |
430
- | 支付宝小程序 | ✅ | 完全支持 |
431
- | 百度小程序 | ✅ | 完全支持 |
432
- | 字节跳动小程序 | ✅ | 完全支持(抖音、头条等) |
433
-
434
- ## 🏗️ 架构设计
191
+ ## 🏗️ 架构
435
192
 
436
193
  ```
437
194
  ┌─────────────────────────────────────────────────┐
@@ -447,26 +204,15 @@ interface IQrOptions {
447
204
  ┌─────▼──────┐ ┌────▼──────┐ ┌────────────┐
448
205
  │ Taro │ │ ESC/POS │ │ Plugin │
449
206
  │ Web BT │ │ TSPL │ │ System │
450
- Alipay... │ │ (ZPL...) │ │ (v2.3+) │
451
- └────────────┘ └───────────┘ └────────────┘
452
- ```
453
- │ │
454
- ┌────▼─────┐ ┌────▼──────┐
455
- │Taro │ │ESC/POS │
456
- │Adapter │ │Driver │
457
- └──────────┘ └───────────┘
207
+ HarmonyOS │ │ ZPL │ │ (v2.3+) │
208
+ └────────────┘ │ CPCL │ └────────────┘
209
+ └───────────┘
458
210
  ```
459
211
 
460
- - **Core 层**: 核心业务逻辑
461
- - **Adapter 层**: 平台适配(Taro、Web Bluetooth 等)
462
- - **Driver 层**: 打印机协议(ESC/POS、TSPL 等)
463
-
464
212
  ## 🤝 贡献
465
213
 
466
214
  欢迎贡献!请查看 [贡献指南](./CONTRIBUTING.md)。
467
215
 
468
- ### 开发设置
469
-
470
216
  ```bash
471
217
  # 克隆仓库
472
218
  git clone https://github.com/agions/taro-bluetooth-print.git
@@ -491,7 +237,9 @@ npm run docs:dev
491
237
 
492
238
  ## 🙏 致谢
493
239
 
494
- 感谢所有贡献者的支持!
240
+ - [Taro](https://taro.jd.com/) - 跨平台开发框架
241
+ - [ESC/POS](https://www.epson-biz.com/) - 打印机指令集
242
+ - 所有贡献者和测试用户
495
243
 
496
244
  ---
497
245