@hd-front-end/jsbridge-sdk 1.0.2 → 1.0.4

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 (26) hide show
  1. package/README.md +4 -2
  2. package/dist/index.d.ts +201 -2
  3. package/dist/index.esm.js +444 -1
  4. package/dist/index.esm.js.map +1 -1
  5. package/dist/index.js +446 -0
  6. package/dist/index.js.map +1 -1
  7. package/dist/index.umd.js +446 -0
  8. package/dist/index.umd.js.map +1 -1
  9. package/docs/00-/351/241/271/347/233/256/346/246/202/350/247/210.md +282 -0
  10. package/docs/01-/346/236/266/346/236/204/350/256/276/350/256/241.md +623 -0
  11. package/docs/02-/346/212/200/346/234/257/345/256/236/347/216/260.md +867 -0
  12. package/docs/03-API/344/275/277/347/224/250/346/226/207/346/241/243.md +1104 -0
  13. package/docs/04-/346/265/213/350/257/225/346/226/271/346/241/210.md +360 -0
  14. package/docs/05-/350/277/201/347/247/273/346/214/207/345/215/227.md +181 -0
  15. package/docs/06-/346/236/266/346/236/204/345/233/276/351/233/206.md +738 -0
  16. package/docs/07-/346/226/260/346/241/245/346/216/245/346/226/271/346/263/225/346/211/251/345/261/225/350/257/264/346/230/216.md +139 -0
  17. package/docs/CODE_REVIEW.md +65 -0
  18. package/docs/EVALUATION.md +72 -0
  19. package/docs/README.md +258 -0
  20. package/docs//345/205/263/351/224/256/351/227/256/351/242/230/350/247/243/347/255/224.md +495 -0
  21. package/docs//346/226/207/346/241/243/346/225/264/345/220/210/350/257/264/346/230/216.md +265 -0
  22. package/docs//346/233/264/346/226/260/346/227/245/345/277/227.md +669 -0
  23. package/docs//347/224/237/344/272/247/347/272/247-/345/277/253/351/200/237/345/274/200/345/247/213-v2.md +673 -0
  24. package/docs//347/224/237/344/272/247/347/272/247-/346/236/266/346/236/204/350/256/276/350/256/241-v2.md +730 -0
  25. package/docs//350/256/276/350/256/241/347/220/206/345/277/265/350/257/264/346/230/216.md +438 -0
  26. package/package.json +3 -2
@@ -0,0 +1,1104 @@
1
+ # JSBridge SDK API 使用文档(生产级 v2)
2
+
3
+ > **当前版本**:生产级 v2 - 完整 API 文档
4
+
5
+ ## 1. 快速开始
6
+
7
+ ### 1.1 安装
8
+
9
+ ```bash
10
+ npm install @hd-front-end/jsbridge-sdk
11
+ ```
12
+
13
+ ### 1.2 初始化
14
+
15
+ ```typescript
16
+ import { init, JSBridge } from '@hd-front-end/jsbridge-sdk'
17
+
18
+ // 初始化配置
19
+ await init({
20
+ debug: {
21
+ enabled: process.env.NODE_ENV === 'development',
22
+ useVConsole: true
23
+ },
24
+ monitor: {
25
+ enabled: process.env.NODE_ENV === 'production',
26
+ autoReport: true,
27
+ reportInterval: 60000,
28
+ logTag: 'JSBridge-Monitor'
29
+ }
30
+ })
31
+
32
+ // 使用 API
33
+ const result = await JSBridge.scan()
34
+ ```
35
+
36
+ ## 2. 核心 API
37
+
38
+ ### 2.1 初始化 API
39
+
40
+ #### init(config?)
41
+
42
+ 初始化 JSBridge SDK。
43
+
44
+ **参数**:
45
+
46
+ ```typescript
47
+ interface JSBridgeConfig {
48
+ debug?: {
49
+ enabled: boolean // 是否启用 debug
50
+ useVConsole?: boolean // 是否启用 vconsole
51
+ logLevel?: 'debug' | 'info' | 'warn' | 'error'
52
+ showTimestamp?: boolean // 显示时间戳
53
+ showPlatform?: boolean // 显示平台
54
+ }
55
+ monitor?: {
56
+ enabled: boolean // 是否启用监控
57
+ autoReport?: boolean // 自动上报
58
+ reportInterval?: number // 上报间隔(ms)
59
+ maxCacheSize?: number // 最大缓存条数
60
+ logTag?: string // 日志 TAG
61
+ }
62
+ }
63
+ ```
64
+
65
+ **返回值**:`Promise<boolean>`
66
+
67
+ **示例**:
68
+
69
+ ```typescript
70
+ const success = await init({
71
+ debug: {
72
+ enabled: true,
73
+ useVConsole: true,
74
+ logLevel: 'debug'
75
+ },
76
+ monitor: {
77
+ enabled: true,
78
+ autoReport: true,
79
+ reportInterval: 60000,
80
+ logTag: 'JSBridge-Monitor'
81
+ }
82
+ })
83
+
84
+ if (success) {
85
+ console.log('初始化成功')
86
+ }
87
+ ```
88
+
89
+ #### inApp()
90
+
91
+ 检查是否在原生 App 环境中。
92
+
93
+ **返回值**:`boolean`
94
+
95
+ **示例**:
96
+
97
+ ```typescript
98
+ import { inApp } from '@hd-front-end/jsbridge-sdk'
99
+
100
+ if (inApp()) {
101
+ // 在原生环境,可以使用 JSBridge API
102
+ } else {
103
+ // 不在原生环境
104
+ }
105
+ ```
106
+
107
+ #### getPlatform()
108
+
109
+ 获取当前平台。
110
+
111
+ **返回值**:`Platform` ('android' | 'ios' | 'unknown')
112
+
113
+ **示例**:
114
+
115
+ ```typescript
116
+ import { getPlatform, Platform } from '@hd-front-end/jsbridge-sdk'
117
+
118
+ const platform = getPlatform()
119
+
120
+ if (platform === Platform.Android) {
121
+ console.log('Android 平台')
122
+ } else if (platform === Platform.iOS) {
123
+ console.log('iOS 平台')
124
+ }
125
+ ```
126
+
127
+ ## 3. 设备能力 API
128
+
129
+ ### 3.1 scan(options?)
130
+
131
+ 调用原生扫码功能。
132
+
133
+ **参数**:
134
+
135
+ ```typescript
136
+ interface ScanOptions {
137
+ scanType?: string[] // 扫码类型:['qrCode', 'barCode']
138
+ hideAlbum?: boolean // 是否隐藏相册入口
139
+ timeoutInterval?: string // 超时时间(秒)
140
+ }
141
+ ```
142
+
143
+ **返回值**:
144
+
145
+ ```typescript
146
+ interface ScanResult {
147
+ resp_code: number // 响应码:1000-成功, 10-取消
148
+ resp_result: string // 扫码结果
149
+ resp_message: string // 响应消息
150
+ }
151
+ ```
152
+
153
+ **示例**:
154
+
155
+ ```typescript
156
+ import { JSBridge } from '@hd-front-end/jsbridge-sdk'
157
+
158
+ try {
159
+ const result = await JSBridge.scan({
160
+ scanType: ['qrCode'],
161
+ hideAlbum: true,
162
+ timeoutInterval: '30'
163
+ })
164
+
165
+ if (result.resp_code === 1000) {
166
+ console.log('扫码成功:', result.resp_result)
167
+ } else if (result.resp_code === 10) {
168
+ console.log('用户取消')
169
+ }
170
+ } catch (error) {
171
+ console.error('扫码失败:', error)
172
+ }
173
+ ```
174
+
175
+ ### 3.2 vibrate(duration?)
176
+
177
+ 触发设备振动。
178
+
179
+ **参数**:
180
+ - `duration` (number, 可选): 振动时长(毫秒),默认 200ms
181
+
182
+ **返回值**:`Promise<void>`
183
+
184
+ **示例**:
185
+
186
+ ```typescript
187
+ // 振动 200ms
188
+ await JSBridge.vibrate()
189
+
190
+ // 自定义振动时长
191
+ await JSBridge.vibrate(500)
192
+ ```
193
+
194
+ ### 3.3 getDeviceInfo()
195
+
196
+ 获取设备信息。
197
+
198
+ **返回值**:
199
+
200
+ ```typescript
201
+ interface DeviceInfo {
202
+ brand: string // 设备品牌
203
+ model: string // 设备型号
204
+ system: string // 系统版本
205
+ platform: string // 平台
206
+ // ... 其他设备信息
207
+ }
208
+ ```
209
+
210
+ **示例**:
211
+
212
+ ```typescript
213
+ const deviceInfo = await JSBridge.getDeviceInfo()
214
+ console.log('设备信息:', deviceInfo)
215
+ ```
216
+
217
+ ### 3.4 getNetworkType()
218
+
219
+ 获取网络类型。
220
+
221
+ **返回值**:`Promise<string>` - 'wifi' | '4g' | '3g' | 'none' 等
222
+
223
+ **示例**:
224
+
225
+ ```typescript
226
+ const networkType = await JSBridge.getNetworkType()
227
+ console.log('网络类型:', networkType)
228
+ ```
229
+
230
+ ## 4. 媒体能力 API
231
+
232
+ ### 4.1 chooseMedia(options)
233
+
234
+ 选择图片或视频。
235
+
236
+ **参数**:
237
+
238
+ ```typescript
239
+ interface MediaOptions {
240
+ count?: number // 最多可选数量,默认 9
241
+ mediaType?: string[] // 媒体类型:['image', 'video']
242
+ sourceType?: string[] // 来源:['album', 'camera']
243
+ maxDuration?: number // 视频最大时长(秒)
244
+ sizeType?: string[] // 图片质量:['original', 'compressed']
245
+ camera?: string // 相机:'back' | 'front'
246
+ }
247
+ ```
248
+
249
+ **返回值**:
250
+
251
+ ```typescript
252
+ interface MediaResult {
253
+ resp_code: number
254
+ resp_message: string
255
+ tempFilePaths: string[] // 临时文件路径
256
+ tempFiles: Array<{
257
+ path: string
258
+ size: number
259
+ duration?: number // 视频时长
260
+ }>
261
+ }
262
+ ```
263
+
264
+ **示例**:
265
+
266
+ ```typescript
267
+ // 选择图片
268
+ const result = await JSBridge.chooseMedia({
269
+ count: 1,
270
+ mediaType: ['image'],
271
+ sourceType: ['album', 'camera'],
272
+ sizeType: ['compressed']
273
+ })
274
+
275
+ console.log('选择的图片:', result.tempFilePaths)
276
+
277
+ // 选择视频
278
+ const videoResult = await JSBridge.chooseMedia({
279
+ count: 1,
280
+ mediaType: ['video'],
281
+ maxDuration: 60
282
+ })
283
+ ```
284
+
285
+ ### 4.2 uploadFile(options)
286
+
287
+ 上传文件。
288
+
289
+ **参数**:
290
+
291
+ ```typescript
292
+ interface UploadOptions {
293
+ url: string // 上传地址
294
+ filePath: string // 文件路径
295
+ name: string // 文件对应的 key
296
+ header?: Record<string, string> // HTTP 请求头
297
+ formData?: Record<string, any> // 额外表单数据
298
+ }
299
+ ```
300
+
301
+ **返回值**:
302
+
303
+ ```typescript
304
+ interface UploadResult {
305
+ resp_code: number
306
+ resp_message: string
307
+ statusCode: number // HTTP 状态码
308
+ data: string // 服务器返回数据
309
+ }
310
+ ```
311
+
312
+ **示例**:
313
+
314
+ ```typescript
315
+ const result = await JSBridge.uploadFile({
316
+ url: 'https://api.example.com/upload',
317
+ filePath: '/tmp/image.jpg',
318
+ name: 'file',
319
+ header: {
320
+ 'Authorization': 'Bearer token'
321
+ },
322
+ formData: {
323
+ userId: '123'
324
+ }
325
+ })
326
+
327
+ if (result.resp_code === 1000) {
328
+ console.log('上传成功:', result.data)
329
+ }
330
+ ```
331
+
332
+ ### 4.3 previewImage(options)
333
+
334
+ 预览图片。
335
+
336
+ **参数**:
337
+
338
+ ```typescript
339
+ interface PreviewOptions {
340
+ urls: string[] // 图片 URL 列表
341
+ current?: number // 当前显示的索引,默认 0
342
+ }
343
+ ```
344
+
345
+ **返回值**:`Promise<void>`
346
+
347
+ **示例**:
348
+
349
+ ```typescript
350
+ await JSBridge.previewImage({
351
+ urls: [
352
+ 'https://example.com/image1.jpg',
353
+ 'https://example.com/image2.jpg'
354
+ ],
355
+ current: 0
356
+ })
357
+ ```
358
+
359
+ ### 4.4 getImageInfo(options)
360
+
361
+ 获取图片信息。
362
+
363
+ **参数**:
364
+
365
+ ```typescript
366
+ interface ImageInfoOptions {
367
+ src: string // 图片路径
368
+ }
369
+ ```
370
+
371
+ **返回值**:
372
+
373
+ ```typescript
374
+ interface ImageInfo {
375
+ width: number // 图片宽度
376
+ height: number // 图片高度
377
+ path: string // 图片路径
378
+ type: string // 图片类型
379
+ }
380
+ ```
381
+
382
+ **示例**:
383
+
384
+ ```typescript
385
+ const info = await JSBridge.getImageInfo({
386
+ src: '/tmp/image.jpg'
387
+ })
388
+
389
+ console.log(`图片尺寸: ${info.width}x${info.height}`)
390
+ ```
391
+
392
+ ### 4.5 bluetoothPrint(options)
393
+
394
+ 蓝牙打印。
395
+
396
+ **参数**:
397
+
398
+ ```typescript
399
+ interface PrintOptions {
400
+ content: string // 打印内容
401
+ // ... 其他打印配置
402
+ }
403
+ ```
404
+
405
+ **返回值**:
406
+
407
+ ```typescript
408
+ interface PrintResult {
409
+ resp_code: number
410
+ resp_message: string
411
+ }
412
+ ```
413
+
414
+ **示例**:
415
+
416
+ ```typescript
417
+ const result = await JSBridge.bluetoothPrint({
418
+ content: '打印内容'
419
+ })
420
+ ```
421
+
422
+ ## 5. 路由能力 API
423
+
424
+ ### 5.1 setNavigationBar(options)
425
+
426
+ 设置导航栏。
427
+
428
+ **参数**:
429
+
430
+ ```typescript
431
+ interface NavBarOptions {
432
+ title?: string // 标题
433
+ frontColor?: string // 前景色
434
+ backgroundColor?: string // 背景色
435
+ }
436
+ ```
437
+
438
+ **返回值**:`Promise<void>`
439
+
440
+ **示例**:
441
+
442
+ ```typescript
443
+ await JSBridge.setNavigationBar({
444
+ title: '商品详情',
445
+ frontColor: '#ffffff',
446
+ backgroundColor: '#000000'
447
+ })
448
+ ```
449
+
450
+ ### 5.2 closeWebView()
451
+
452
+ 关闭当前 WebView。
453
+
454
+ **返回值**:`Promise<void>`
455
+
456
+ **示例**:
457
+
458
+ ```typescript
459
+ await JSBridge.closeWebView()
460
+ ```
461
+
462
+ ### 5.3 onRoute(handler)
463
+
464
+ 注册路由跳转回调。
465
+
466
+ **参数**:
467
+ - `handler` (Function): 路由回调函数
468
+
469
+ **示例**:
470
+
471
+ ```typescript
472
+ JSBridge.onRoute((data) => {
473
+ console.log('原生触发路由跳转:', data)
474
+ router.push(data.path)
475
+ })
476
+ ```
477
+
478
+ ### 5.4 notifyRouteChange(data)
479
+
480
+ 通知原生路由变化。
481
+
482
+ **参数**:
483
+ - `data` (any): 路由数据
484
+
485
+ **返回值**:`Promise<void>`
486
+
487
+ **示例**:
488
+
489
+ ```typescript
490
+ await JSBridge.notifyRouteChange({
491
+ path: '/product/detail',
492
+ query: { id: '123' }
493
+ })
494
+ ```
495
+
496
+ ## 6. 存储能力 API
497
+
498
+ ### 6.1 getStorage(key?)
499
+
500
+ 获取存储数据。
501
+
502
+ **参数**:
503
+ - `key` (string, 可选): 存储的 key
504
+
505
+ **返回值**:`Promise<any>`
506
+
507
+ **示例**:
508
+
509
+ ```typescript
510
+ // 获取所有数据
511
+ const allData = await JSBridge.getStorage()
512
+
513
+ // 获取指定 key 的数据
514
+ const userData = await JSBridge.getStorage('userData')
515
+ ```
516
+
517
+ ### 6.2 setStorage(data)
518
+
519
+ 设置存储数据。
520
+
521
+ **参数**:
522
+ - `data` (any): 要存储的数据
523
+
524
+ **返回值**:`Promise<void>`
525
+
526
+ **示例**:
527
+
528
+ ```typescript
529
+ await JSBridge.setStorage({
530
+ userData: { name: '张三', age: 20 }
531
+ })
532
+ ```
533
+
534
+ ### 6.3 getAppInfo()
535
+
536
+ 获取 App 信息。
537
+
538
+ **返回值**:`Promise<string>` - JSON 字符串
539
+
540
+ **示例**:
541
+
542
+ ```typescript
543
+ const appInfoStr = await JSBridge.getAppInfo()
544
+ const appInfo = JSON.parse(appInfoStr)
545
+ console.log('App 信息:', appInfo)
546
+ ```
547
+
548
+ ### 6.4 onRefreshStore(handler)
549
+
550
+ 注册刷新 Store 回调。
551
+
552
+ **参数**:
553
+ - `handler` (Function): 刷新回调函数
554
+
555
+ **示例**:
556
+
557
+ ```typescript
558
+ JSBridge.onRefreshStore((data) => {
559
+ console.log('原生触发刷新 Store:', data)
560
+ store.commit('updateUserInfo', data)
561
+ })
562
+ ```
563
+
564
+ ### 6.5 notifyAppOnload(data)
565
+
566
+ 通知原生 App 已加载完成。
567
+
568
+ **参数**:
569
+ - `data` (any): 加载数据
570
+
571
+ **返回值**:`Promise<void>`
572
+
573
+ **示例**:
574
+
575
+ ```typescript
576
+ await JSBridge.notifyAppOnload({
577
+ name: 'MyApp',
578
+ version: '1.0.0'
579
+ })
580
+ ```
581
+
582
+ ## 7. 系统能力 API
583
+
584
+ ### 7.1 makePhoneCall(options)
585
+
586
+ 拨打电话。
587
+
588
+ **参数**:
589
+
590
+ ```typescript
591
+ interface PhoneCallOptions {
592
+ phoneNumber: string // 电话号码
593
+ }
594
+ ```
595
+
596
+ **返回值**:`Promise<void>`
597
+
598
+ **示例**:
599
+
600
+ ```typescript
601
+ await JSBridge.makePhoneCall({
602
+ phoneNumber: '10086'
603
+ })
604
+ ```
605
+
606
+ ### 7.2 onPdaScan(handler)
607
+
608
+ 注册 PDA 扫码回调。
609
+
610
+ **参数**:
611
+ - `handler` (Function): 扫码回调函数
612
+
613
+ **示例**:
614
+
615
+ ```typescript
616
+ JSBridge.onPdaScan((data) => {
617
+ console.log('PDA 扫码:', data)
618
+ })
619
+ ```
620
+
621
+ ## 8. 日志能力 API(新增)
622
+
623
+ ### 8.1 log.debug(message, tag?)
624
+
625
+ 记录 DEBUG 级别日志。
626
+
627
+ **参数**:
628
+ - `message` (string): 日志内容
629
+ - `tag` (string, 可选): 日志标签
630
+
631
+ **返回值**:`Promise<void>`
632
+
633
+ **示例**:
634
+
635
+ ```typescript
636
+ await JSBridge.log.debug('调试信息', 'Debug')
637
+ await JSBridge.log.debug(`API 响应: ${JSON.stringify(data)}`, 'API')
638
+ ```
639
+
640
+ ### 8.2 log.info(message, tag?)
641
+
642
+ 记录 INFO 级别日志。
643
+
644
+ **参数**:
645
+ - `message` (string): 日志内容
646
+ - `tag` (string, 可选): 日志标签
647
+
648
+ **返回值**:`Promise<void>`
649
+
650
+ **示例**:
651
+
652
+ ```typescript
653
+ await JSBridge.log.info('用户点击了购买按钮', 'UserBehavior')
654
+ await JSBridge.log.info('订单创建成功', 'OrderService')
655
+ await JSBridge.log.info(`页面加载耗时: ${duration}ms`, 'Performance')
656
+ ```
657
+
658
+ ### 8.3 log.warn(message, tag?)
659
+
660
+ 记录 WARN 级别日志。
661
+
662
+ **参数**:
663
+ - `message` (string): 日志内容
664
+ - `tag` (string, 可选): 日志标签
665
+
666
+ **返回值**:`Promise<void>`
667
+
668
+ **示例**:
669
+
670
+ ```typescript
671
+ await JSBridge.log.warn('库存不足', 'Inventory')
672
+ await JSBridge.log.warn('接口响应较慢', 'Performance')
673
+ ```
674
+
675
+ ### 8.4 log.error(message, tag?)
676
+
677
+ 记录 ERROR 级别日志。
678
+
679
+ **参数**:
680
+ - `message` (string): 日志内容
681
+ - `tag` (string, 可选): 日志标签
682
+
683
+ **返回值**:`Promise<void>`
684
+
685
+ **示例**:
686
+
687
+ ```typescript
688
+ await JSBridge.log.error('支付失败', 'Payment')
689
+ await JSBridge.log.error(`订单创建失败: ${error.message}`, 'OrderService')
690
+ ```
691
+
692
+ ### 8.5 writeLog(request)
693
+
694
+ 通用日志方法。
695
+
696
+ **参数**:
697
+
698
+ ```typescript
699
+ enum LogLevel {
700
+ DEBUG = 'DEBUG',
701
+ INFO = 'INFO',
702
+ WARN = 'WARN',
703
+ ERROR = 'ERROR'
704
+ }
705
+
706
+ interface LogRequest {
707
+ tag?: string // 日志标签
708
+ level: LogLevel // 日志级别
709
+ message: string // 日志内容
710
+ }
711
+ ```
712
+
713
+ **返回值**:`Promise<void>`
714
+
715
+ **示例**:
716
+
717
+ ```typescript
718
+ import { JSBridge, LogLevel } from '@hd-front-end/jsbridge-sdk'
719
+
720
+ await JSBridge.writeLog({
721
+ level: LogLevel.INFO,
722
+ message: '用户行为日志',
723
+ tag: 'UserBehavior'
724
+ })
725
+ ```
726
+
727
+ ### 8.6 日志使用场景
728
+
729
+ #### 用户行为日志
730
+
731
+ ```typescript
732
+ // 进入页面
733
+ await JSBridge.log.info('用户进入商品详情页', 'UserBehavior')
734
+
735
+ // 点击按钮
736
+ await JSBridge.log.info('用户点击加入购物车', 'UserBehavior')
737
+
738
+ // 完成操作
739
+ await JSBridge.log.info('用户完成支付', 'UserBehavior')
740
+ ```
741
+
742
+ #### 业务错误日志
743
+
744
+ ```typescript
745
+ try {
746
+ await createOrder()
747
+ } catch (error) {
748
+ await JSBridge.log.error(
749
+ `订单创建失败: ${error.message}`,
750
+ 'OrderService'
751
+ )
752
+ }
753
+ ```
754
+
755
+ #### 性能日志
756
+
757
+ ```typescript
758
+ const startTime = Date.now()
759
+ await loadData()
760
+ const duration = Date.now() - startTime
761
+
762
+ await JSBridge.log.info(
763
+ `页面加载耗时: ${duration}ms`,
764
+ 'Performance'
765
+ )
766
+ ```
767
+
768
+ #### API 调用日志
769
+
770
+ ```typescript
771
+ // 请求前
772
+ await JSBridge.log.debug('开始请求商品列表', 'API')
773
+
774
+ try {
775
+ const response = await fetch('/api/products')
776
+ await JSBridge.log.debug(
777
+ `请求成功: ${response.data.length} 条数据`,
778
+ 'API'
779
+ )
780
+ } catch (error) {
781
+ await JSBridge.log.error(
782
+ `请求失败: ${error.message}`,
783
+ 'API'
784
+ )
785
+ }
786
+ ```
787
+
788
+ ## 9. 监控 API
789
+
790
+ ### 9.1 Monitor.getStats(api?)
791
+
792
+ 获取性能统计。
793
+
794
+ **参数**:
795
+ - `api` (string, 可选): API 名称,不传则返回所有 API 统计
796
+
797
+ **返回值**:
798
+
799
+ ```typescript
800
+ interface PerformanceStats {
801
+ totalCalls: number // 总调用次数
802
+ successCalls: number // 成功次数
803
+ failedCalls: number // 失败次数
804
+ avgDuration: number // 平均耗时
805
+ maxDuration: number // 最大耗时
806
+ minDuration: number // 最小耗时
807
+ }
808
+ ```
809
+
810
+ **示例**:
811
+
812
+ ```typescript
813
+ import { Monitor } from '@hd-front-end/jsbridge-sdk'
814
+
815
+ // 获取单个 API 统计
816
+ const scanStats = Monitor.getStats('scan')
817
+ console.log('扫码统计:', scanStats)
818
+
819
+ // 获取所有 API 统计
820
+ const allStats = Monitor.getStats()
821
+ allStats.forEach((stats, api) => {
822
+ console.log(`${api}:`, stats)
823
+ })
824
+ ```
825
+
826
+ ### 9.2 Monitor.getRecords(limit?)
827
+
828
+ 获取调用记录。
829
+
830
+ **参数**:
831
+ - `limit` (number, 可选): 返回记录数量
832
+
833
+ **返回值**:`ApiCallRecord[]`
834
+
835
+ **示例**:
836
+
837
+ ```typescript
838
+ // 获取最近 10 条记录
839
+ const records = Monitor.getRecords(10)
840
+
841
+ records.forEach(record => {
842
+ const status = record.success ? '✓' : '✗'
843
+ console.log(`${status} ${record.api}: ${record.duration}ms`)
844
+ })
845
+ ```
846
+
847
+ ### 9.3 Monitor.report()
848
+
849
+ 手动上报监控数据(通过 log)。
850
+
851
+ **返回值**:`Promise<void>`
852
+
853
+ **示例**:
854
+
855
+ ```typescript
856
+ await Monitor.report()
857
+ console.log('监控数据已上报')
858
+ ```
859
+
860
+ ### 9.4 Monitor.reportError(api, error, detail?)
861
+
862
+ 立即上报错误(通过 log)。
863
+
864
+ **参数**:
865
+ - `api` (string): API 名称
866
+ - `error` (string): 错误信息
867
+ - `detail` (any, 可选): 错误详情
868
+
869
+ **返回值**:`Promise<void>`
870
+
871
+ **示例**:
872
+
873
+ ```typescript
874
+ await Monitor.reportError('scan', '扫码失败', {
875
+ errorCode: 'E001',
876
+ detail: '相机权限被拒绝'
877
+ })
878
+ ```
879
+
880
+ ### 9.5 Monitor.clear()
881
+
882
+ 清空监控数据。
883
+
884
+ **返回值**:`void`
885
+
886
+ **示例**:
887
+
888
+ ```typescript
889
+ Monitor.clear()
890
+ ```
891
+
892
+ ## 10. Debug API
893
+
894
+ ### 10.1 Debug.trace(api, params?)
895
+
896
+ 追踪 API 调用。
897
+
898
+ **参数**:
899
+ - `api` (string): API 名称
900
+ - `params` (any, 可选): API 参数
901
+
902
+ **返回值**:`void`
903
+
904
+ **示例**:
905
+
906
+ ```typescript
907
+ import { Debug } from '@hd-front-end/jsbridge-sdk'
908
+
909
+ Debug.trace('scan', { scanType: ['qrCode'] })
910
+ ```
911
+
912
+ ### 10.2 Debug.getLogs(level?, limit?)
913
+
914
+ 获取日志。
915
+
916
+ **参数**:
917
+ - `level` (string, 可选): 日志级别
918
+ - `limit` (number, 可选): 返回日志数量
919
+
920
+ **返回值**:`any[]`
921
+
922
+ **示例**:
923
+
924
+ ```typescript
925
+ // 获取所有错误日志
926
+ const errorLogs = Debug.getLogs('error')
927
+
928
+ // 获取最近 10 条日志
929
+ const recentLogs = Debug.getLogs(undefined, 10)
930
+ ```
931
+
932
+ ### 10.3 Debug.clearLogs()
933
+
934
+ 清空日志。
935
+
936
+ **返回值**:`void`
937
+
938
+ **示例**:
939
+
940
+ ```typescript
941
+ Debug.clearLogs()
942
+ ```
943
+
944
+ ## 11. 完整示例
945
+
946
+ ### 11.1 App.vue 初始化
947
+
948
+ ```typescript
949
+ import { init, JSBridge, getPlatform } from '@hd-front-end/jsbridge-sdk'
950
+
951
+ const isDev = process.env.NODE_ENV === 'development'
952
+
953
+ export default {
954
+ async onLaunch() {
955
+ // #ifdef H5-HD
956
+ // 初始化
957
+ const success = await init({
958
+ debug: {
959
+ enabled: isDev,
960
+ useVConsole: isDev && /vconsole=true/.test(location.search),
961
+ logLevel: isDev ? 'debug' : 'error'
962
+ },
963
+ monitor: {
964
+ enabled: !isDev,
965
+ autoReport: !isDev,
966
+ reportInterval: 60000,
967
+ logTag: 'JSBridge-Monitor'
968
+ }
969
+ })
970
+
971
+ if (!success) return
972
+
973
+ console.log('平台:', getPlatform())
974
+
975
+ // 记录启动日志
976
+ await JSBridge.log.info('App 启动成功', 'AppLifeCycle')
977
+
978
+ // 注册原生回调
979
+ JSBridge.onRoute((data) => this.$Router.replaceAll(data))
980
+ JSBridge.onRefreshStore((data) => this.refreshStore(data))
981
+
982
+ // 获取初始数据
983
+ const appInfo = await JSBridge.getAppInfo()
984
+ this.refreshStore(JSON.parse(appInfo), true)
985
+
986
+ // 通知加载完成
987
+ await JSBridge.notifyAppOnload({ name: 'MyApp' })
988
+ // #endif
989
+ }
990
+ }
991
+ ```
992
+
993
+ ### 11.2 业务组件使用
994
+
995
+ ```typescript
996
+ import { JSBridge, inApp } from '@hd-front-end/jsbridge-sdk'
997
+
998
+ export default {
999
+ methods: {
1000
+ async handleScan() {
1001
+ if (!inApp()) {
1002
+ uni.showToast({ title: '请在 App 中使用', icon: 'none' })
1003
+ return
1004
+ }
1005
+
1006
+ // 记录用户行为
1007
+ await JSBridge.log.info('用户点击扫码按钮', 'UserBehavior')
1008
+
1009
+ try {
1010
+ const result = await JSBridge.scan()
1011
+
1012
+ if (result.resp_code === 1000) {
1013
+ // 记录成功
1014
+ await JSBridge.log.info(
1015
+ `扫码成功: ${result.resp_result}`,
1016
+ 'ScanResult'
1017
+ )
1018
+
1019
+ uni.showToast({ title: '扫码成功', icon: 'success' })
1020
+ }
1021
+ } catch (error) {
1022
+ // 记录错误
1023
+ await JSBridge.log.error(
1024
+ `扫码失败: ${error.message}`,
1025
+ 'ScanError'
1026
+ )
1027
+
1028
+ uni.showToast({ title: '扫码失败', icon: 'error' })
1029
+ }
1030
+ }
1031
+ }
1032
+ }
1033
+ ```
1034
+
1035
+ ## 12. 最佳实践
1036
+
1037
+ ### 12.1 统一错误处理
1038
+
1039
+ ```typescript
1040
+ async function safeCall<T>(
1041
+ apiCall: () => Promise<T>,
1042
+ apiName: string,
1043
+ tag: string
1044
+ ): Promise<T | null> {
1045
+ try {
1046
+ await JSBridge.log.debug(`开始调用 ${apiName}`, tag)
1047
+ const result = await apiCall()
1048
+ await JSBridge.log.info(`${apiName} 成功`, tag)
1049
+ return result
1050
+ } catch (error) {
1051
+ await JSBridge.log.error(
1052
+ `${apiName} 失败: ${error.message}`,
1053
+ tag
1054
+ )
1055
+ return null
1056
+ }
1057
+ }
1058
+
1059
+ // 使用
1060
+ const result = await safeCall(
1061
+ () => JSBridge.scan(),
1062
+ '扫码',
1063
+ 'ScanService'
1064
+ )
1065
+ ```
1066
+
1067
+ ### 12.2 封装日志工具
1068
+
1069
+ ```typescript
1070
+ export const Logger = {
1071
+ behavior(action: string, detail?: any) {
1072
+ return JSBridge.log.info(
1073
+ `[行为] ${action}${detail ? ': ' + JSON.stringify(detail) : ''}`,
1074
+ 'UserBehavior'
1075
+ )
1076
+ },
1077
+
1078
+ error(module: string, error: string) {
1079
+ return JSBridge.log.error(`[${module}] ${error}`, 'BusinessError')
1080
+ },
1081
+
1082
+ performance(metric: string, duration: number) {
1083
+ return JSBridge.log.info(`[性能] ${metric}: ${duration}ms`, 'Performance')
1084
+ }
1085
+ }
1086
+
1087
+ // 使用
1088
+ await Logger.behavior('点击购买', { productId: '123' })
1089
+ await Logger.error('订单', '创建失败')
1090
+ await Logger.performance('页面加载', 1234)
1091
+ ```
1092
+
1093
+ ## 13. 文档导航
1094
+
1095
+ - **[00-项目概览.md](./00-项目概览.md)** - 项目概述
1096
+ - **[01-架构设计.md](./01-架构设计.md)** - 架构设计
1097
+ - **[02-技术实现.md](./02-技术实现.md)** - 技术实现
1098
+ - **[04-测试方案.md](./04-测试方案.md)** - 测试方案
1099
+ - **[05-迁移指南.md](./05-迁移指南.md)** - 迁移指南
1100
+
1101
+ ---
1102
+
1103
+ **完整的 API 文档,包含日志能力** 🎯
1104
+