@hd-front-end/jsbridge-sdk 1.0.1

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.
@@ -0,0 +1,1561 @@
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3
+ typeof define === 'function' && define.amd ? define(['exports'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.JSBridgeSDK = {}));
5
+ })(this, (function (exports) { 'use strict';
6
+
7
+ /**
8
+ * JSBridge 核心通信层
9
+ *
10
+ * 参考:context/soa/src/jsBridge/JsBridge.ts
11
+ * 优化:增强错误处理、超时机制、初始化重试
12
+ */
13
+ class Bridge {
14
+ constructor() {
15
+ this.bridge = null;
16
+ this.isInitialized = false;
17
+ this.platform = 'unknown';
18
+ this.initPromise = null;
19
+ this.detectPlatform();
20
+ }
21
+ /**
22
+ * 检测平台
23
+ * 优化:支持更多的 UA 特征识别
24
+ */
25
+ detectPlatform() {
26
+ if (typeof window === 'undefined' || typeof navigator === 'undefined') {
27
+ this.platform = 'unknown';
28
+ return;
29
+ }
30
+ const ua = navigator.userAgent;
31
+ // Android 检测
32
+ if (/SoaAndroid/i.test(ua) || /HDApp.*Android/i.test(ua)) {
33
+ this.platform = 'android';
34
+ return;
35
+ }
36
+ // iOS 检测
37
+ if (/iPhone|iPad|iPod/i.test(ua) && /HDApp/i.test(ua)) {
38
+ this.platform = 'ios';
39
+ return;
40
+ }
41
+ this.platform = 'unknown';
42
+ }
43
+ /**
44
+ * 初始化 JSBridge
45
+ * 优化:支持多次调用(返回同一个 Promise)、增加重试机制
46
+ */
47
+ async init() {
48
+ // 如果已初始化,直接返回成功
49
+ if (this.isInitialized) {
50
+ return true;
51
+ }
52
+ // 如果正在初始化,返回同一个 Promise
53
+ if (this.initPromise) {
54
+ return this.initPromise;
55
+ }
56
+ // 不在原生环境中
57
+ if (this.platform === 'unknown') {
58
+ console.warn('[JSBridge] 不在原生 App 环境中');
59
+ return false;
60
+ }
61
+ // 创建初始化 Promise
62
+ this.initPromise = new Promise((resolve, reject) => {
63
+ var _a;
64
+ try {
65
+ // 检查是否已有 Bridge 实例
66
+ if (window.WKWebViewJavascriptBridge) {
67
+ this.bridge = window.WKWebViewJavascriptBridge;
68
+ this.isInitialized = true;
69
+ resolve(true);
70
+ return;
71
+ }
72
+ // 初始化 WKWVJBCallbacks 队列
73
+ if (!window.WKWVJBCallbacks) {
74
+ window.WKWVJBCallbacks = [];
75
+ }
76
+ // 添加回调到队列
77
+ window.WKWVJBCallbacks.push((bridge) => {
78
+ this.bridge = bridge;
79
+ this.isInitialized = true;
80
+ resolve(true);
81
+ });
82
+ // Android 平台:触发注入
83
+ if (this.platform === 'android') {
84
+ // 兼容性检查:InjectJavascript 可能未注入或延迟注入
85
+ // 优化策略:如果 InjectJavascript 不存在,不立即报错,而是等待原生注入(通过超时机制)
86
+ if (window.InjectJavascript && typeof window.InjectJavascript.init === 'function') {
87
+ window.InjectJavascript.init();
88
+ }
89
+ else {
90
+ console.warn('[JSBridge] InjectJavascript 暂未就绪,等待原生注入或超时...');
91
+ // 不 reject,依赖下方的 setTimeout 超时机制或原生主动调用 WKWVJBCallbacks
92
+ }
93
+ }
94
+ // iOS 平台:使用 iframe 触发
95
+ if (this.platform === 'ios') {
96
+ const iframe = document.createElement('iframe');
97
+ iframe.style.display = 'none';
98
+ iframe.src = 'https://__bridge_loaded__';
99
+ document.documentElement.appendChild(iframe);
100
+ setTimeout(() => {
101
+ document.documentElement.removeChild(iframe);
102
+ }, 0);
103
+ }
104
+ // 超时保护(10 秒)
105
+ const timeoutId = setTimeout(() => {
106
+ if (!this.isInitialized) {
107
+ const error = new Error(`${this.platform} 初始化超时`);
108
+ console.error(`[JSBridge] ${error.message}`);
109
+ reject(error);
110
+ }
111
+ }, 10000);
112
+ // 清理超时定时器(当初始化成功时)
113
+ (_a = this.initPromise) === null || _a === void 0 ? void 0 : _a.then(() => clearTimeout(timeoutId));
114
+ }
115
+ catch (error) {
116
+ console.error('[JSBridge] 初始化异常:', error);
117
+ reject(error);
118
+ }
119
+ });
120
+ return this.initPromise;
121
+ }
122
+ /**
123
+ * 等待 Bridge 就绪
124
+ * 轮询检查 Bridge 实例是否存在
125
+ */
126
+ async waitForReady() {
127
+ if (this.bridge)
128
+ return;
129
+ return new Promise((resolve, reject) => {
130
+ let retryCount = 0;
131
+ const maxRetries = 20; // 最多重试 20 次 (4秒)
132
+ const interval = 200; // 每次间隔 200ms
133
+ const check = () => {
134
+ var _a;
135
+ // 再次尝试从全局获取
136
+ if (window.WKWebViewJavascriptBridge) {
137
+ this.bridge = window.WKWebViewJavascriptBridge;
138
+ this.isInitialized = true;
139
+ resolve();
140
+ return;
141
+ }
142
+ // Android 再次尝试触发注入
143
+ if (this.platform === 'android' && ((_a = window.InjectJavascript) === null || _a === void 0 ? void 0 : _a.init)) {
144
+ window.InjectJavascript.init();
145
+ }
146
+ retryCount++;
147
+ if (retryCount > maxRetries) {
148
+ reject(new Error('Bridge 等待就绪超时'));
149
+ }
150
+ else {
151
+ setTimeout(check, interval);
152
+ }
153
+ };
154
+ check();
155
+ });
156
+ }
157
+ /**
158
+ * 调用原生方法
159
+ * 优化:自动初始化、增强错误处理、支持泛型返回值
160
+ */
161
+ async call(method, data) {
162
+ // 自动初始化
163
+ if (!this.isInitialized) {
164
+ await this.init();
165
+ }
166
+ // 确保 bridge 实例就绪
167
+ if (!this.bridge) {
168
+ try {
169
+ await this.waitForReady();
170
+ }
171
+ catch (e) {
172
+ throw new Error(`调用 ${method} 失败: Bridge 未就绪`);
173
+ }
174
+ }
175
+ return new Promise((resolve, reject) => {
176
+ try {
177
+ // 超时控制(10 秒)
178
+ const timeoutId = setTimeout(() => {
179
+ reject(new Error(`${method} 调用超时`));
180
+ }, 10000);
181
+ // 调用原生方法
182
+ this.bridge.callHandler(method, data, (result) => {
183
+ clearTimeout(timeoutId);
184
+ resolve(result);
185
+ });
186
+ }
187
+ catch (error) {
188
+ console.error(`[JSBridge] ${method} 调用异常:`, error);
189
+ reject(error);
190
+ }
191
+ });
192
+ }
193
+ /**
194
+ * 注册 JS 方法供原生调用
195
+ * 优化:增加错误处理
196
+ */
197
+ async register(method, handler) {
198
+ // 自动初始化
199
+ if (!this.isInitialized) {
200
+ const success = await this.init();
201
+ if (!success) {
202
+ throw new Error('JSBridge 未初始化');
203
+ }
204
+ }
205
+ try {
206
+ this.bridge.registerHandler(method, handler);
207
+ }
208
+ catch (error) {
209
+ console.error(`[JSBridge] 注册方法失败: ${method}`, error);
210
+ throw error;
211
+ }
212
+ }
213
+ /**
214
+ * 检查是否在 App 内
215
+ */
216
+ inApp() {
217
+ return this.platform !== 'unknown';
218
+ }
219
+ /**
220
+ * 获取平台类型
221
+ */
222
+ getPlatform() {
223
+ return this.platform;
224
+ }
225
+ /**
226
+ * 检查是否已初始化
227
+ */
228
+ isReady() {
229
+ return this.isInitialized;
230
+ }
231
+ }
232
+ // 导出单例
233
+ const bridge = new Bridge();
234
+
235
+ /**
236
+ * JSBridge SDK 类型定义
237
+ */
238
+ // ==================== 日志类型 ====================
239
+ exports.LogLevel = void 0;
240
+ (function (LogLevel) {
241
+ LogLevel["DEBUG"] = "DEBUG";
242
+ LogLevel["INFO"] = "INFO";
243
+ LogLevel["WARN"] = "WARN";
244
+ LogLevel["ERROR"] = "ERROR";
245
+ })(exports.LogLevel || (exports.LogLevel = {}));
246
+
247
+ /**
248
+ * 日志 API
249
+ *
250
+ * 参考:context/soa/src/jsBridge/JsBridgeHandlers.ts (line 152-154)
251
+ * 优化:增强类型安全、支持批量日志、支持日志队列
252
+ */
253
+ /**
254
+ * 写入日志到原生端
255
+ *
256
+ * @example
257
+ * await writeLog({
258
+ * tag: 'MyApp',
259
+ * level: LogLevel.INFO,
260
+ * message: '用户点击了按钮'
261
+ * })
262
+ */
263
+ async function writeLog(request) {
264
+ try {
265
+ // 调用原生 log handler
266
+ await bridge.call('log', {
267
+ tag: request.tag || 'JSBridge',
268
+ level: request.level,
269
+ message: request.message
270
+ });
271
+ }
272
+ catch (error) {
273
+ // 日志上报失败不应该影响业务逻辑,只在控制台警告
274
+ console.warn('[JSBridge] 日志上报失败:', error);
275
+ }
276
+ }
277
+ /**
278
+ * 便捷的日志方法
279
+ *
280
+ * @example
281
+ * await log.info('用户登录成功', 'Auth')
282
+ * await log.error('网络请求失败', 'Network')
283
+ */
284
+ const log = {
285
+ /**
286
+ * DEBUG 级别日志
287
+ */
288
+ debug: async (message, tag) => {
289
+ return writeLog({
290
+ level: 'DEBUG',
291
+ message,
292
+ tag
293
+ });
294
+ },
295
+ /**
296
+ * INFO 级别日志
297
+ */
298
+ info: async (message, tag) => {
299
+ return writeLog({
300
+ level: 'INFO',
301
+ message,
302
+ tag
303
+ });
304
+ },
305
+ /**
306
+ * WARN 级别日志
307
+ */
308
+ warn: async (message, tag) => {
309
+ return writeLog({
310
+ level: 'WARN',
311
+ message,
312
+ tag
313
+ });
314
+ },
315
+ /**
316
+ * ERROR 级别日志
317
+ */
318
+ error: async (message, tag) => {
319
+ return writeLog({
320
+ level: 'ERROR',
321
+ message,
322
+ tag
323
+ });
324
+ }
325
+ };
326
+
327
+ /**
328
+ * 监控层
329
+ *
330
+ * 优化:
331
+ * - 使用原生 log 上报(不依赖 HTTP)
332
+ * - 支持性能统计
333
+ * - 支持错误追踪
334
+ * - 自动批量上报
335
+ */
336
+ let Monitor$1 = class Monitor {
337
+ constructor() {
338
+ this.records = [];
339
+ this.reportTimer = null;
340
+ this.config = {
341
+ enabled: false,
342
+ autoReport: false,
343
+ reportInterval: 60000, // 默认 60 秒
344
+ maxRecords: 100,
345
+ logTag: 'JSBridge-Monitor'
346
+ };
347
+ }
348
+ /**
349
+ * 初始化监控
350
+ */
351
+ init(config) {
352
+ this.config = {
353
+ ...this.config,
354
+ ...config
355
+ };
356
+ if (this.config.enabled && this.config.autoReport) {
357
+ this.startAutoReport();
358
+ }
359
+ }
360
+ /**
361
+ * 开始自动上报
362
+ * 优化:使用 visibilitychange 控制上报
363
+ */
364
+ startAutoReport() {
365
+ // 清理旧的定时器
366
+ if (this.reportTimer) {
367
+ clearInterval(this.reportTimer);
368
+ }
369
+ // 启动定时上报
370
+ this.reportTimer = window.setInterval(() => {
371
+ // 只在页面可见时上报
372
+ if (!document.hidden) {
373
+ this.report().catch(err => {
374
+ console.warn('[Monitor] 自动上报失败:', err);
375
+ });
376
+ }
377
+ }, this.config.reportInterval);
378
+ // 监听页面可见性变化
379
+ document.addEventListener('visibilitychange', () => {
380
+ if (!document.hidden && this.records.length > 0) {
381
+ // 页面变为可见时,立即上报积压的数据
382
+ this.report().catch(err => {
383
+ console.warn('[Monitor] 可见性变化上报失败:', err);
384
+ });
385
+ }
386
+ });
387
+ }
388
+ /**
389
+ * 停止自动上报
390
+ */
391
+ stopAutoReport() {
392
+ if (this.reportTimer) {
393
+ clearInterval(this.reportTimer);
394
+ this.reportTimer = null;
395
+ }
396
+ }
397
+ /**
398
+ * 记录 API 调用
399
+ */
400
+ record(api, duration, success, error) {
401
+ if (!this.config.enabled)
402
+ return;
403
+ const record = {
404
+ api,
405
+ timestamp: Date.now(),
406
+ duration,
407
+ success,
408
+ error,
409
+ platform: bridge.getPlatform()
410
+ };
411
+ this.records.push(record);
412
+ // 超过最大记录数时,自动上报
413
+ if (this.records.length >= this.config.maxRecords) {
414
+ this.report().catch(err => {
415
+ console.warn('[Monitor] 自动上报失败:', err);
416
+ });
417
+ }
418
+ }
419
+ /**
420
+ * 上报监控数据(通过原生 log)
421
+ */
422
+ async report() {
423
+ if (this.records.length === 0)
424
+ return;
425
+ // 复制并清空记录
426
+ const recordsToReport = this.records.slice(0, this.config.maxRecords);
427
+ this.records = this.records.slice(this.config.maxRecords);
428
+ const data = {
429
+ timestamp: Date.now(),
430
+ platform: bridge.getPlatform(),
431
+ count: recordsToReport.length,
432
+ records: recordsToReport
433
+ };
434
+ try {
435
+ // 通过 log handler 上报(INFO 级别)
436
+ await writeLog({
437
+ tag: this.config.logTag,
438
+ level: 'INFO',
439
+ message: `[MONITOR] ${JSON.stringify(data)}`
440
+ });
441
+ console.log(`[Monitor] 上报成功,记录数: ${recordsToReport.length}`);
442
+ }
443
+ catch (error) {
444
+ console.error('[Monitor] 上报失败:', error);
445
+ // 上报失败,放回队列(但限制最大数量)
446
+ this.records = [...recordsToReport, ...this.records].slice(0, this.config.maxRecords * 2);
447
+ throw error;
448
+ }
449
+ }
450
+ /**
451
+ * 上报错误(立即上报)
452
+ */
453
+ async reportError(api, error, detail) {
454
+ if (!this.config.enabled)
455
+ return;
456
+ const errorData = {
457
+ timestamp: Date.now(),
458
+ api,
459
+ error,
460
+ detail,
461
+ platform: bridge.getPlatform(),
462
+ userAgent: navigator.userAgent
463
+ };
464
+ try {
465
+ // 通过 log handler 立即上报(ERROR 级别)
466
+ await writeLog({
467
+ tag: this.config.logTag,
468
+ level: 'ERROR',
469
+ message: `[ERROR] ${JSON.stringify(errorData)}`
470
+ });
471
+ }
472
+ catch (err) {
473
+ console.error('[Monitor] 错误上报失败:', err);
474
+ }
475
+ }
476
+ /**
477
+ * 获取统计信息
478
+ */
479
+ getStats() {
480
+ const totalCalls = this.records.length;
481
+ const successCalls = this.records.filter(r => r.success).length;
482
+ const failedCalls = totalCalls - successCalls;
483
+ const avgDuration = totalCalls > 0
484
+ ? this.records.reduce((sum, r) => sum + r.duration, 0) / totalCalls
485
+ : 0;
486
+ const successRate = totalCalls > 0 ? successCalls / totalCalls : 0;
487
+ return {
488
+ totalCalls,
489
+ successCalls,
490
+ failedCalls,
491
+ avgDuration: Math.round(avgDuration),
492
+ successRate: Math.round(successRate * 10000) / 100 // 保留两位小数
493
+ };
494
+ }
495
+ /**
496
+ * 获取所有记录
497
+ */
498
+ getRecords() {
499
+ return [...this.records];
500
+ }
501
+ /**
502
+ * 清空记录
503
+ */
504
+ clear() {
505
+ this.records = [];
506
+ }
507
+ /**
508
+ * 销毁监控器
509
+ */
510
+ destroy() {
511
+ this.stopAutoReport();
512
+ this.clear();
513
+ }
514
+ };
515
+ // 导出单例
516
+ const monitor = new Monitor$1();
517
+
518
+ /**
519
+ * 调试层
520
+ *
521
+ * 优化:
522
+ * - 动态加载 vconsole(按需加载)
523
+ * - 支持调用追踪
524
+ * - 支持日志分级
525
+ */
526
+ let Debug$1 = class Debug {
527
+ constructor() {
528
+ this.vConsole = null;
529
+ this.logs = [];
530
+ this.maxLogs = 100;
531
+ this.config = {
532
+ enabled: false,
533
+ useVConsole: false,
534
+ logLevel: 'info',
535
+ showTimestamp: true
536
+ };
537
+ }
538
+ /**
539
+ * 初始化调试模式
540
+ */
541
+ async init(config) {
542
+ this.config = {
543
+ ...this.config,
544
+ ...config
545
+ };
546
+ if (this.config.enabled && this.config.useVConsole) {
547
+ await this.initVConsole();
548
+ }
549
+ }
550
+ /**
551
+ * 初始化 vconsole
552
+ * 优化:动态导入,只在需要时加载
553
+ */
554
+ async initVConsole() {
555
+ if (this.vConsole) {
556
+ console.log('[Debug] vconsole 已存在');
557
+ return;
558
+ }
559
+ // 检查是否已被其他地方初始化
560
+ if (window.vConsole) {
561
+ console.log('[Debug] vconsole 已被外部初始化');
562
+ this.vConsole = window.vConsole;
563
+ return;
564
+ }
565
+ try {
566
+ // 动态导入 vconsole
567
+ const VConsoleModule = await import('vconsole');
568
+ const VConsole = VConsoleModule.default || VConsoleModule;
569
+ this.vConsole = new VConsole({
570
+ theme: 'dark',
571
+ defaultPlugins: ['system', 'network', 'element', 'storage'],
572
+ maxLogNumber: 1000
573
+ });
574
+ console.log('[Debug] vconsole 初始化成功');
575
+ }
576
+ catch (error) {
577
+ console.warn('[Debug] vconsole 加载失败:', error);
578
+ console.warn('[Debug] 请确保已安装 vconsole: npm install vconsole');
579
+ }
580
+ }
581
+ /**
582
+ * 记录调用追踪
583
+ */
584
+ trace(api, message, data) {
585
+ if (!this.config.enabled)
586
+ return;
587
+ const log = {
588
+ timestamp: Date.now(),
589
+ level: this.config.logLevel,
590
+ api,
591
+ message,
592
+ data
593
+ };
594
+ this.logs.push(log);
595
+ // 限制日志数量
596
+ if (this.logs.length > this.maxLogs) {
597
+ this.logs.shift();
598
+ }
599
+ // 输出到控制台
600
+ this.outputLog(log);
601
+ }
602
+ /**
603
+ * 输出日志到控制台
604
+ */
605
+ outputLog(log) {
606
+ const timestamp = this.config.showTimestamp
607
+ ? `[${new Date(log.timestamp).toLocaleTimeString()}]`
608
+ : '';
609
+ const prefix = `${timestamp} [JSBridge][${log.api}]`;
610
+ const message = `${prefix} ${log.message}`;
611
+ switch (log.level) {
612
+ case 'debug':
613
+ console.debug(message, log.data || '');
614
+ break;
615
+ case 'info':
616
+ console.info(message, log.data || '');
617
+ break;
618
+ case 'warn':
619
+ console.warn(message, log.data || '');
620
+ break;
621
+ case 'error':
622
+ console.error(message, log.data || '');
623
+ break;
624
+ default:
625
+ console.log(message, log.data || '');
626
+ }
627
+ }
628
+ /**
629
+ * 获取所有日志
630
+ */
631
+ getLogs() {
632
+ return [...this.logs];
633
+ }
634
+ /**
635
+ * 清空日志
636
+ */
637
+ clearLogs() {
638
+ this.logs = [];
639
+ }
640
+ /**
641
+ * 销毁调试器
642
+ */
643
+ destroy() {
644
+ if (this.vConsole && typeof this.vConsole.destroy === 'function') {
645
+ this.vConsole.destroy();
646
+ this.vConsole = null;
647
+ }
648
+ this.clearLogs();
649
+ }
650
+ };
651
+ // 导出单例
652
+ const debug = new Debug$1();
653
+
654
+ /**
655
+ * 设备相关 API
656
+ *
657
+ * 参考:context/soa/src/jsBridge/JsBridgeHandlers.ts
658
+ */
659
+ /**
660
+ * 扫码
661
+ * 参考:JsBridgeHandlers.ts line 75-87
662
+ *
663
+ * @example
664
+ * const result = await scan({ scanType: ['qrCode', 'barCode'] })
665
+ * if (result.resp_code === 1000) {
666
+ * console.log('扫码结果:', result.resp_result)
667
+ * }
668
+ */
669
+ async function scan(request = {}) {
670
+ const startTime = Date.now();
671
+ try {
672
+ debug.trace('scan', '开始扫码', request);
673
+ const result = await bridge.call('scan', request);
674
+ // 解析返回结果(参考 JsBridgeHandlers.ts line 76-86)
675
+ const parsedResult = typeof result === 'string'
676
+ ? JSON.parse(result)
677
+ : result;
678
+ const duration = Date.now() - startTime;
679
+ const success = parsedResult.resp_code === 1000;
680
+ monitor.record('scan', duration, success, parsedResult.resp_message);
681
+ debug.trace('scan', success ? '扫码成功' : '扫码取消', parsedResult);
682
+ return parsedResult;
683
+ }
684
+ catch (error) {
685
+ const duration = Date.now() - startTime;
686
+ monitor.record('scan', duration, false, error.message);
687
+ monitor.reportError('scan', error.message, request);
688
+ debug.trace('scan', '扫码失败', error);
689
+ throw error;
690
+ }
691
+ }
692
+ /**
693
+ * 震动
694
+ *
695
+ * @param duration 震动时长(毫秒),默认 400ms
696
+ */
697
+ async function vibrate(duration = 400) {
698
+ const startTime = Date.now();
699
+ try {
700
+ debug.trace('vibrate', '开始震动', { duration });
701
+ await bridge.call('vibrate', { duration });
702
+ const elapsed = Date.now() - startTime;
703
+ monitor.record('vibrate', elapsed, true);
704
+ debug.trace('vibrate', '震动完成');
705
+ }
706
+ catch (error) {
707
+ const elapsed = Date.now() - startTime;
708
+ monitor.record('vibrate', elapsed, false, error.message);
709
+ debug.trace('vibrate', '震动失败', error);
710
+ throw error;
711
+ }
712
+ }
713
+ /**
714
+ * 获取设备信息
715
+ *
716
+ * @example
717
+ * const info = await getDeviceInfo()
718
+ * console.log('设备型号:', info.model)
719
+ * console.log('系统版本:', info.version)
720
+ */
721
+ async function getDeviceInfo() {
722
+ const startTime = Date.now();
723
+ try {
724
+ debug.trace('getDeviceInfo', '获取设备信息');
725
+ const result = await bridge.call('getDeviceInfo');
726
+ const info = typeof result === 'string'
727
+ ? JSON.parse(result)
728
+ : result;
729
+ const duration = Date.now() - startTime;
730
+ monitor.record('getDeviceInfo', duration, true);
731
+ debug.trace('getDeviceInfo', '获取成功', info);
732
+ return info;
733
+ }
734
+ catch (error) {
735
+ const duration = Date.now() - startTime;
736
+ monitor.record('getDeviceInfo', duration, false, error.message);
737
+ debug.trace('getDeviceInfo', '获取失败', error);
738
+ throw error;
739
+ }
740
+ }
741
+ /**
742
+ * 获取网络类型
743
+ *
744
+ * @example
745
+ * const network = await getNetworkType()
746
+ * console.log('网络类型:', network.networkType)
747
+ */
748
+ async function getNetworkType() {
749
+ const startTime = Date.now();
750
+ try {
751
+ debug.trace('getNetworkType', '获取网络类型');
752
+ const result = await bridge.call('getNetworkType');
753
+ const info = typeof result === 'string'
754
+ ? JSON.parse(result)
755
+ : result;
756
+ const duration = Date.now() - startTime;
757
+ monitor.record('getNetworkType', duration, true);
758
+ debug.trace('getNetworkType', '获取成功', info);
759
+ return info;
760
+ }
761
+ catch (error) {
762
+ const duration = Date.now() - startTime;
763
+ monitor.record('getNetworkType', duration, false, error.message);
764
+ debug.trace('getNetworkType', '获取失败', error);
765
+ throw error;
766
+ }
767
+ }
768
+
769
+ /**
770
+ * 媒体相关 API
771
+ *
772
+ * 参考:context/soa/src/jsBridge/JsBridgeHandlers.ts
773
+ */
774
+ /**
775
+ * 选择图片/视频
776
+ * 参考:JsBridgeHandlers.ts line 93-100
777
+ *
778
+ * @example
779
+ * const result = await chooseMedia({
780
+ * count: 9,
781
+ * mediaType: ['image'],
782
+ * sourceType: ['album', 'camera']
783
+ * })
784
+ */
785
+ async function chooseMedia(request) {
786
+ const startTime = Date.now();
787
+ try {
788
+ debug.trace('chooseMedia', '选择媒体', request);
789
+ const result = await bridge.call('chooseMedia', request);
790
+ if (!result) {
791
+ debug.trace('chooseMedia', '用户取消选择');
792
+ monitor.record('chooseMedia', Date.now() - startTime, true);
793
+ return null;
794
+ }
795
+ const mediaResult = typeof result === 'string'
796
+ ? JSON.parse(result)
797
+ : result;
798
+ const duration = Date.now() - startTime;
799
+ monitor.record('chooseMedia', duration, true);
800
+ debug.trace('chooseMedia', '选择成功', mediaResult);
801
+ return mediaResult;
802
+ }
803
+ catch (error) {
804
+ const duration = Date.now() - startTime;
805
+ monitor.record('chooseMedia', duration, false, error.message);
806
+ monitor.reportError('chooseMedia', error.message, request);
807
+ debug.trace('chooseMedia', '选择失败', error);
808
+ throw error;
809
+ }
810
+ }
811
+ /**
812
+ * 上传文件
813
+ * 参考:JsBridgeHandlers.ts line 117-122
814
+ *
815
+ * @example
816
+ * const result = await uploadFile({
817
+ * url: 'https://api.example.com/upload',
818
+ * filePath: '/path/to/file',
819
+ * name: 'file'
820
+ * })
821
+ */
822
+ async function uploadFile(request) {
823
+ const startTime = Date.now();
824
+ try {
825
+ debug.trace('uploadFile', '上传文件', request);
826
+ const result = await bridge.call('uploadFile', request);
827
+ const uploadResult = typeof result === 'string'
828
+ ? JSON.parse(result)
829
+ : result;
830
+ const duration = Date.now() - startTime;
831
+ const success = uploadResult.statusCode >= 200 && uploadResult.statusCode < 300;
832
+ monitor.record('uploadFile', duration, success, uploadResult.errMsg);
833
+ debug.trace('uploadFile', success ? '上传成功' : '上传失败', uploadResult);
834
+ return uploadResult;
835
+ }
836
+ catch (error) {
837
+ const duration = Date.now() - startTime;
838
+ monitor.record('uploadFile', duration, false, error.message);
839
+ monitor.reportError('uploadFile', error.message, request);
840
+ debug.trace('uploadFile', '上传异常', error);
841
+ throw error;
842
+ }
843
+ }
844
+ /**
845
+ * 预览图片
846
+ *
847
+ * @param urls 图片URL数组
848
+ * @param current 当前显示图片的索引,默认 0
849
+ */
850
+ async function previewImage(urls, current = 0) {
851
+ const startTime = Date.now();
852
+ try {
853
+ debug.trace('previewImage', '预览图片', { urls, current });
854
+ await bridge.call('previewImage', { urls, current });
855
+ const duration = Date.now() - startTime;
856
+ monitor.record('previewImage', duration, true);
857
+ debug.trace('previewImage', '预览完成');
858
+ }
859
+ catch (error) {
860
+ const duration = Date.now() - startTime;
861
+ monitor.record('previewImage', duration, false, error.message);
862
+ debug.trace('previewImage', '预览失败', error);
863
+ throw error;
864
+ }
865
+ }
866
+ /**
867
+ * 获取图片信息
868
+ * 参考:JsBridgeHandlers.ts line 106-111
869
+ *
870
+ * @param path 图片路径
871
+ */
872
+ async function getImageInfo(path) {
873
+ const startTime = Date.now();
874
+ try {
875
+ debug.trace('getImageInfo', '获取图片信息', { path });
876
+ const result = await bridge.call('getImageInfo', { path });
877
+ const imageInfo = typeof result === 'string'
878
+ ? JSON.parse(result)
879
+ : result;
880
+ const duration = Date.now() - startTime;
881
+ monitor.record('getImageInfo', duration, true);
882
+ debug.trace('getImageInfo', '获取成功', imageInfo);
883
+ return imageInfo;
884
+ }
885
+ catch (error) {
886
+ const duration = Date.now() - startTime;
887
+ monitor.record('getImageInfo', duration, false, error.message);
888
+ debug.trace('getImageInfo', '获取失败', error);
889
+ throw error;
890
+ }
891
+ }
892
+ /**
893
+ * 蓝牙打印
894
+ * 参考:JsBridgeHandlers.ts line 182-194
895
+ *
896
+ * @param request 打印请求
897
+ */
898
+ async function bluetoothPrint(request) {
899
+ const startTime = Date.now();
900
+ try {
901
+ debug.trace('bluetoothPrint', '蓝牙打印', request);
902
+ const result = await bridge.call('bluetoothPrint', request);
903
+ // 处理返回结果
904
+ let parsedResult = result;
905
+ if (result && typeof result === 'string') {
906
+ try {
907
+ parsedResult = JSON.parse(result);
908
+ }
909
+ catch (_a) {
910
+ // 如果解析失败,返回原始字符串
911
+ }
912
+ }
913
+ const duration = Date.now() - startTime;
914
+ monitor.record('bluetoothPrint', duration, true);
915
+ debug.trace('bluetoothPrint', '打印完成', parsedResult);
916
+ return parsedResult;
917
+ }
918
+ catch (error) {
919
+ const duration = Date.now() - startTime;
920
+ monitor.record('bluetoothPrint', duration, false, error.message);
921
+ monitor.reportError('bluetoothPrint', error.message, request);
922
+ debug.trace('bluetoothPrint', '打印失败', error);
923
+ throw error;
924
+ }
925
+ }
926
+
927
+ /**
928
+ * 路由相关 API
929
+ *
930
+ * 参考:context/soa/src/jsBridge/JsBridgeHandlers.ts
931
+ */
932
+ /**
933
+ * 设置导航栏样式
934
+ * 参考:JsBridgeHandlers.ts line 144-146
935
+ *
936
+ * @example
937
+ * await setNavigationBar({
938
+ * navigationBarTitleText: '订单详情',
939
+ * backgroundColor: '#ffffff',
940
+ * textColor: '#000000'
941
+ * })
942
+ */
943
+ async function setNavigationBar(style) {
944
+ const startTime = Date.now();
945
+ try {
946
+ debug.trace('setNavigationBar', '设置导航栏', style);
947
+ await bridge.call('setNavigationBar', style);
948
+ const duration = Date.now() - startTime;
949
+ monitor.record('setNavigationBar', duration, true);
950
+ debug.trace('setNavigationBar', '设置成功');
951
+ }
952
+ catch (error) {
953
+ const duration = Date.now() - startTime;
954
+ monitor.record('setNavigationBar', duration, false, error.message);
955
+ debug.trace('setNavigationBar', '设置失败', error);
956
+ throw error;
957
+ }
958
+ }
959
+ /**
960
+ * 关闭当前 WebView
961
+ * 参考:JsBridgeHandlers.ts line 128-130
962
+ *
963
+ * @example
964
+ * await closeWebView()
965
+ */
966
+ async function closeWebView() {
967
+ const startTime = Date.now();
968
+ try {
969
+ debug.trace('closeWebView', '关闭页面');
970
+ await bridge.call('closeWebView');
971
+ const duration = Date.now() - startTime;
972
+ monitor.record('closeWebView', duration, true);
973
+ debug.trace('closeWebView', '关闭成功');
974
+ }
975
+ catch (error) {
976
+ const duration = Date.now() - startTime;
977
+ monitor.record('closeWebView', duration, false, error.message);
978
+ debug.trace('closeWebView', '关闭失败', error);
979
+ throw error;
980
+ }
981
+ }
982
+ /**
983
+ * 注册路由跳转回调
984
+ * 参考:JsBridgeHandlers.ts line 44-46
985
+ *
986
+ * @example
987
+ * onRoute((data, callback) => {
988
+ * console.log('路由跳转:', data)
989
+ * router.push(data.path)
990
+ * callback({ success: true })
991
+ * })
992
+ */
993
+ async function onRoute(handler) {
994
+ try {
995
+ debug.trace('onRoute', '注册路由跳转回调');
996
+ await bridge.register('route', handler);
997
+ debug.trace('onRoute', '注册成功');
998
+ }
999
+ catch (error) {
1000
+ debug.trace('onRoute', '注册失败', error);
1001
+ throw error;
1002
+ }
1003
+ }
1004
+ /**
1005
+ * 注册获取路由信息回调
1006
+ * 参考:JsBridgeHandlers.ts line 51-53
1007
+ *
1008
+ * @example
1009
+ * onGetRouteInfo((data, callback) => {
1010
+ * callback({ routes: routeConfig })
1011
+ * })
1012
+ */
1013
+ async function onGetRouteInfo(handler) {
1014
+ try {
1015
+ debug.trace('onGetRouteInfo', '注册获取路由信息回调');
1016
+ await bridge.register('getRouteInfo', handler);
1017
+ debug.trace('onGetRouteInfo', '注册成功');
1018
+ }
1019
+ catch (error) {
1020
+ debug.trace('onGetRouteInfo', '注册失败', error);
1021
+ throw error;
1022
+ }
1023
+ }
1024
+ /**
1025
+ * 通知原生路由变化
1026
+ * 参考:JsBridgeHandlers.ts line 21-23
1027
+ *
1028
+ * @param to 目标路由
1029
+ * @param from 来源路由
1030
+ */
1031
+ async function notifyRouteChange(to, from) {
1032
+ const startTime = Date.now();
1033
+ try {
1034
+ debug.trace('notifyRouteChange', '通知路由变化', { to, from });
1035
+ await bridge.call('routerAfterEach', { to, from });
1036
+ const duration = Date.now() - startTime;
1037
+ monitor.record('notifyRouteChange', duration, true);
1038
+ }
1039
+ catch (error) {
1040
+ const duration = Date.now() - startTime;
1041
+ monitor.record('notifyRouteChange', duration, false, error.message);
1042
+ debug.trace('notifyRouteChange', '通知失败', error);
1043
+ // 路由变化通知失败不应该阻塞业务
1044
+ console.warn('[JSBridge] 路由变化通知失败:', error);
1045
+ }
1046
+ }
1047
+
1048
+ /**
1049
+ * 存储相关 API
1050
+ *
1051
+ * 参考:context/soa/src/jsBridge/JsBridgeHandlers.ts
1052
+ */
1053
+ /**
1054
+ * 获取 App 信息
1055
+ * 参考:JsBridgeHandlers.ts line 28-30
1056
+ *
1057
+ * @example
1058
+ * const appInfo = await getAppInfo()
1059
+ * console.log('App 版本:', appInfo.version)
1060
+ */
1061
+ async function getAppInfo() {
1062
+ const startTime = Date.now();
1063
+ try {
1064
+ debug.trace('getAppInfo', '获取 App 信息');
1065
+ const result = await bridge.call('getAppInfo');
1066
+ const appInfo = typeof result === 'string'
1067
+ ? JSON.parse(result)
1068
+ : result;
1069
+ const duration = Date.now() - startTime;
1070
+ monitor.record('getAppInfo', duration, true);
1071
+ debug.trace('getAppInfo', '获取成功', appInfo);
1072
+ return appInfo;
1073
+ }
1074
+ catch (error) {
1075
+ const duration = Date.now() - startTime;
1076
+ monitor.record('getAppInfo', duration, false, error.message);
1077
+ debug.trace('getAppInfo', '获取失败', error);
1078
+ throw error;
1079
+ }
1080
+ }
1081
+ /**
1082
+ * 获取存储数据
1083
+ *
1084
+ * @param key 存储键
1085
+ */
1086
+ async function getStorage(key) {
1087
+ const startTime = Date.now();
1088
+ try {
1089
+ debug.trace('getStorage', '获取存储数据', { key });
1090
+ const result = await bridge.call('getStorage', { key });
1091
+ const duration = Date.now() - startTime;
1092
+ monitor.record('getStorage', duration, true);
1093
+ debug.trace('getStorage', '获取成功', result);
1094
+ return result;
1095
+ }
1096
+ catch (error) {
1097
+ const duration = Date.now() - startTime;
1098
+ monitor.record('getStorage', duration, false, error.message);
1099
+ debug.trace('getStorage', '获取失败', error);
1100
+ throw error;
1101
+ }
1102
+ }
1103
+ /**
1104
+ * 设置存储数据
1105
+ *
1106
+ * @param key 存储键
1107
+ * @param data 存储数据
1108
+ */
1109
+ async function setStorage(key, data) {
1110
+ const startTime = Date.now();
1111
+ try {
1112
+ debug.trace('setStorage', '设置存储数据', { key, data });
1113
+ await bridge.call('setStorage', { key, data });
1114
+ const duration = Date.now() - startTime;
1115
+ monitor.record('setStorage', duration, true);
1116
+ debug.trace('setStorage', '设置成功');
1117
+ }
1118
+ catch (error) {
1119
+ const duration = Date.now() - startTime;
1120
+ monitor.record('setStorage', duration, false, error.message);
1121
+ debug.trace('setStorage', '设置失败', error);
1122
+ throw error;
1123
+ }
1124
+ }
1125
+ /**
1126
+ * 注册刷新 store 数据回调
1127
+ * 参考:JsBridgeHandlers.ts line 59-61
1128
+ *
1129
+ * @example
1130
+ * onRefreshStore((data, callback) => {
1131
+ * store.dispatch('refresh', data)
1132
+ * callback({ success: true })
1133
+ * })
1134
+ */
1135
+ async function onRefreshStore(handler) {
1136
+ try {
1137
+ debug.trace('onRefreshStore', '注册刷新 store 回调');
1138
+ await bridge.register('refreshStore', handler);
1139
+ debug.trace('onRefreshStore', '注册成功');
1140
+ }
1141
+ catch (error) {
1142
+ debug.trace('onRefreshStore', '注册失败', error);
1143
+ throw error;
1144
+ }
1145
+ }
1146
+ /**
1147
+ * 通知原生 App 已加载完成
1148
+ * 参考:JsBridgeHandlers.ts line 160-162
1149
+ *
1150
+ * @param data 启动数据
1151
+ */
1152
+ async function notifyAppOnload(data) {
1153
+ const startTime = Date.now();
1154
+ try {
1155
+ debug.trace('notifyAppOnload', '通知 App 加载完成', data);
1156
+ await bridge.call('uniPageLifeCycle', {
1157
+ lifecycle: 'appOnLaunch',
1158
+ data
1159
+ });
1160
+ const duration = Date.now() - startTime;
1161
+ monitor.record('notifyAppOnload', duration, true);
1162
+ debug.trace('notifyAppOnload', '通知成功');
1163
+ }
1164
+ catch (error) {
1165
+ const duration = Date.now() - startTime;
1166
+ monitor.record('notifyAppOnload', duration, false, error.message);
1167
+ debug.trace('notifyAppOnload', '通知失败', error);
1168
+ // 生命周期通知失败不应该阻塞业务
1169
+ console.warn('[JSBridge] App 加载通知失败:', error);
1170
+ }
1171
+ }
1172
+
1173
+ /**
1174
+ * 系统相关 API
1175
+ *
1176
+ * 参考:context/soa/src/jsBridge/JsBridgeHandlers.ts
1177
+ */
1178
+ /**
1179
+ * 拨打电话
1180
+ * 参考:JsBridgeHandlers.ts line 136-138
1181
+ *
1182
+ * @example
1183
+ * await makePhoneCall({ phoneNumber: '10086' })
1184
+ */
1185
+ async function makePhoneCall(request) {
1186
+ const startTime = Date.now();
1187
+ try {
1188
+ debug.trace('makePhoneCall', '拨打电话', request);
1189
+ await bridge.call('makePhoneCall', request);
1190
+ const duration = Date.now() - startTime;
1191
+ monitor.record('makePhoneCall', duration, true);
1192
+ debug.trace('makePhoneCall', '拨号成功');
1193
+ }
1194
+ catch (error) {
1195
+ const duration = Date.now() - startTime;
1196
+ monitor.record('makePhoneCall', duration, false, error.message);
1197
+ monitor.reportError('makePhoneCall', error.message, request);
1198
+ debug.trace('makePhoneCall', '拨号失败', error);
1199
+ throw error;
1200
+ }
1201
+ }
1202
+ /**
1203
+ * 注册 PDA 扫码回调
1204
+ * 参考:JsBridgeHandlers.ts line 67-69
1205
+ *
1206
+ * @example
1207
+ * onPdaScan((data, callback) => {
1208
+ * console.log('PDA 扫码结果:', data)
1209
+ * callback({ success: true })
1210
+ * })
1211
+ */
1212
+ async function onPdaScan(handler) {
1213
+ try {
1214
+ debug.trace('onPdaScan', '注册 PDA 扫码回调');
1215
+ await bridge.register('pdaScan', handler);
1216
+ debug.trace('onPdaScan', '注册成功');
1217
+ }
1218
+ catch (error) {
1219
+ debug.trace('onPdaScan', '注册失败', error);
1220
+ throw error;
1221
+ }
1222
+ }
1223
+ /**
1224
+ * 退出登录
1225
+ *
1226
+ * @example
1227
+ * await logout()
1228
+ */
1229
+ async function logout() {
1230
+ const startTime = Date.now();
1231
+ try {
1232
+ debug.trace('logout', '退出登录');
1233
+ await bridge.call('logout');
1234
+ const duration = Date.now() - startTime;
1235
+ monitor.record('logout', duration, true);
1236
+ debug.trace('logout', '退出成功');
1237
+ }
1238
+ catch (error) {
1239
+ const duration = Date.now() - startTime;
1240
+ monitor.record('logout', duration, false, error.message);
1241
+ debug.trace('logout', '退出失败', error);
1242
+ throw error;
1243
+ }
1244
+ }
1245
+
1246
+ /**
1247
+ * JSBridge SDK
1248
+ * 统一的原生能力封装
1249
+ *
1250
+ * @author HD Digital
1251
+ * @version 1.0.0
1252
+ */
1253
+ /**
1254
+ * 初始化 JSBridge SDK
1255
+ *
1256
+ * @example
1257
+ * const success = await init({
1258
+ * debug: {
1259
+ * enabled: true,
1260
+ * useVConsole: true
1261
+ * },
1262
+ * monitor: {
1263
+ * enabled: true,
1264
+ * autoReport: true
1265
+ * }
1266
+ * })
1267
+ */
1268
+ async function init(config) {
1269
+ try {
1270
+ // 1. 初始化 Debug 模式
1271
+ if (config === null || config === void 0 ? void 0 : config.debug) {
1272
+ await debug.init(config.debug);
1273
+ debug.trace('init', 'Debug 模式已启用', config.debug);
1274
+ }
1275
+ // 2. 初始化 Monitor
1276
+ if (config === null || config === void 0 ? void 0 : config.monitor) {
1277
+ monitor.init(config.monitor);
1278
+ debug.trace('init', 'Monitor 已启用', config.monitor);
1279
+ }
1280
+ // 3. 初始化 Bridge
1281
+ const success = await bridge.init();
1282
+ if (success) {
1283
+ // 记录初始化成功日志
1284
+ try {
1285
+ await writeLog({
1286
+ level: exports.LogLevel.INFO,
1287
+ message: `JSBridge 初始化成功 [${bridge.getPlatform()}]`
1288
+ });
1289
+ }
1290
+ catch (e) {
1291
+ // 忽略日志上报失败
1292
+ }
1293
+ debug.trace('init', 'JSBridge 初始化成功', {
1294
+ platform: bridge.getPlatform()
1295
+ });
1296
+ }
1297
+ else {
1298
+ debug.trace('init', 'JSBridge 初始化失败:不在 App 环境中');
1299
+ }
1300
+ return success;
1301
+ }
1302
+ catch (error) {
1303
+ // 记录初始化失败日志
1304
+ try {
1305
+ await writeLog({
1306
+ level: exports.LogLevel.ERROR,
1307
+ message: `JSBridge 初始化失败: ${error.message}`
1308
+ });
1309
+ }
1310
+ catch (e) {
1311
+ // 忽略日志上报失败
1312
+ }
1313
+ debug.trace('init', 'JSBridge 初始化异常', error);
1314
+ console.error('[JSBridge] 初始化失败:', error);
1315
+ return false;
1316
+ }
1317
+ }
1318
+ /**
1319
+ * 检查是否在 App 内
1320
+ */
1321
+ function inApp() {
1322
+ return bridge.inApp();
1323
+ }
1324
+ /**
1325
+ * 获取平台类型
1326
+ */
1327
+ function getPlatform() {
1328
+ return bridge.getPlatform();
1329
+ }
1330
+ /**
1331
+ * 检查是否已初始化
1332
+ */
1333
+ function isReady() {
1334
+ return bridge.isReady();
1335
+ }
1336
+ /**
1337
+ * JSBridge API
1338
+ * 所有可用的原生能力
1339
+ */
1340
+ const JSBridge = {
1341
+ // ==================== 设备相关 ====================
1342
+ /**
1343
+ * 扫码
1344
+ * @example
1345
+ * const result = await JSBridge.scan({ scanType: ['qrCode'] })
1346
+ */
1347
+ scan: scan,
1348
+ /**
1349
+ * 震动
1350
+ * @example
1351
+ * await JSBridge.vibrate(400)
1352
+ */
1353
+ vibrate: vibrate,
1354
+ /**
1355
+ * 获取设备信息
1356
+ * @example
1357
+ * const info = await JSBridge.getDeviceInfo()
1358
+ */
1359
+ getDeviceInfo: getDeviceInfo,
1360
+ /**
1361
+ * 获取网络类型
1362
+ * @example
1363
+ * const network = await JSBridge.getNetworkType()
1364
+ */
1365
+ getNetworkType: getNetworkType,
1366
+ // ==================== 媒体相关 ====================
1367
+ /**
1368
+ * 选择图片/视频
1369
+ * @example
1370
+ * const result = await JSBridge.chooseMedia({ count: 9, mediaType: ['image'] })
1371
+ */
1372
+ chooseMedia: chooseMedia,
1373
+ /**
1374
+ * 上传文件
1375
+ * @example
1376
+ * const result = await JSBridge.uploadFile({ url, filePath, name: 'file' })
1377
+ */
1378
+ uploadFile: uploadFile,
1379
+ /**
1380
+ * 预览图片
1381
+ * @example
1382
+ * await JSBridge.previewImage(['url1', 'url2'], 0)
1383
+ */
1384
+ previewImage: previewImage,
1385
+ /**
1386
+ * 获取图片信息
1387
+ * @example
1388
+ * const info = await JSBridge.getImageInfo(path)
1389
+ */
1390
+ getImageInfo: getImageInfo,
1391
+ /**
1392
+ * 蓝牙打印
1393
+ * @example
1394
+ * await JSBridge.bluetoothPrint({ data: printData })
1395
+ */
1396
+ bluetoothPrint: bluetoothPrint,
1397
+ // ==================== 路由相关 ====================
1398
+ /**
1399
+ * 设置导航栏
1400
+ * @example
1401
+ * await JSBridge.setNavigationBar({ navigationBarTitleText: '标题' })
1402
+ */
1403
+ setNavigationBar: setNavigationBar,
1404
+ /**
1405
+ * 关闭当前页面
1406
+ * @example
1407
+ * await JSBridge.closeWebView()
1408
+ */
1409
+ closeWebView: closeWebView,
1410
+ /**
1411
+ * 注册路由跳转回调
1412
+ * @example
1413
+ * JSBridge.onRoute((data, callback) => { router.push(data.path) })
1414
+ */
1415
+ onRoute: onRoute,
1416
+ /**
1417
+ * 注册获取路由信息回调
1418
+ * @example
1419
+ * JSBridge.onGetRouteInfo((data, callback) => { callback(routeConfig) })
1420
+ */
1421
+ onGetRouteInfo: onGetRouteInfo,
1422
+ /**
1423
+ * 通知路由变化
1424
+ * @example
1425
+ * await JSBridge.notifyRouteChange(to, from)
1426
+ */
1427
+ notifyRouteChange: notifyRouteChange,
1428
+ // ==================== 存储相关 ====================
1429
+ /**
1430
+ * 获取 App 信息
1431
+ * @example
1432
+ * const appInfo = await JSBridge.getAppInfo()
1433
+ */
1434
+ getAppInfo: getAppInfo,
1435
+ /**
1436
+ * 获取存储数据
1437
+ * @example
1438
+ * const data = await JSBridge.getStorage('key')
1439
+ */
1440
+ getStorage: getStorage,
1441
+ /**
1442
+ * 设置存储数据
1443
+ * @example
1444
+ * await JSBridge.setStorage('key', data)
1445
+ */
1446
+ setStorage: setStorage,
1447
+ /**
1448
+ * 注册刷新 store 回调
1449
+ * @example
1450
+ * JSBridge.onRefreshStore((data, callback) => { store.dispatch('refresh') })
1451
+ */
1452
+ onRefreshStore: onRefreshStore,
1453
+ /**
1454
+ * 通知 App 加载完成
1455
+ * @example
1456
+ * await JSBridge.notifyAppOnload()
1457
+ */
1458
+ notifyAppOnload: notifyAppOnload,
1459
+ // ==================== 系统相关 ====================
1460
+ /**
1461
+ * 拨打电话
1462
+ * @example
1463
+ * await JSBridge.makePhoneCall({ phoneNumber: '10086' })
1464
+ */
1465
+ makePhoneCall: makePhoneCall,
1466
+ /**
1467
+ * 注册 PDA 扫码回调
1468
+ * @example
1469
+ * JSBridge.onPdaScan((data, callback) => { console.log(data) })
1470
+ */
1471
+ onPdaScan: onPdaScan,
1472
+ /**
1473
+ * 退出登录
1474
+ * @example
1475
+ * await JSBridge.logout()
1476
+ */
1477
+ logout: logout,
1478
+ // ==================== 日志相关 ====================
1479
+ /**
1480
+ * 写入日志
1481
+ * @example
1482
+ * await JSBridge.writeLog({ level: LogLevel.INFO, message: '日志' })
1483
+ */
1484
+ writeLog,
1485
+ /**
1486
+ * 便捷日志方法
1487
+ * @example
1488
+ * await JSBridge.log.info('用户登录', 'Auth')
1489
+ * await JSBridge.log.error('请求失败', 'Network')
1490
+ */
1491
+ log
1492
+ };
1493
+ /**
1494
+ * 监控 API
1495
+ */
1496
+ const Monitor = {
1497
+ /**
1498
+ * 获取统计信息
1499
+ * @example
1500
+ * const stats = Monitor.getStats()
1501
+ */
1502
+ getStats: monitor.getStats.bind(monitor),
1503
+ /**
1504
+ * 获取所有记录
1505
+ * @example
1506
+ * const records = Monitor.getRecords()
1507
+ */
1508
+ getRecords: monitor.getRecords.bind(monitor),
1509
+ /**
1510
+ * 清空记录
1511
+ * @example
1512
+ * Monitor.clear()
1513
+ */
1514
+ clear: monitor.clear.bind(monitor),
1515
+ /**
1516
+ * 手动上报
1517
+ * @example
1518
+ * await Monitor.report()
1519
+ */
1520
+ report: monitor.report.bind(monitor),
1521
+ /**
1522
+ * 上报错误
1523
+ * @example
1524
+ * await Monitor.reportError('api', 'error message')
1525
+ */
1526
+ reportError: monitor.reportError.bind(monitor)
1527
+ };
1528
+ /**
1529
+ * Debug API
1530
+ */
1531
+ const Debug = {
1532
+ /**
1533
+ * 记录追踪日志
1534
+ * @example
1535
+ * Debug.trace('api', 'message', data)
1536
+ */
1537
+ trace: debug.trace.bind(debug),
1538
+ /**
1539
+ * 获取所有日志
1540
+ * @example
1541
+ * const logs = Debug.getLogs()
1542
+ */
1543
+ getLogs: debug.getLogs.bind(debug),
1544
+ /**
1545
+ * 清空日志
1546
+ * @example
1547
+ * Debug.clearLogs()
1548
+ */
1549
+ clearLogs: debug.clearLogs.bind(debug)
1550
+ };
1551
+
1552
+ exports.Debug = Debug;
1553
+ exports.JSBridge = JSBridge;
1554
+ exports.Monitor = Monitor;
1555
+ exports.getPlatform = getPlatform;
1556
+ exports.inApp = inApp;
1557
+ exports.init = init;
1558
+ exports.isReady = isReady;
1559
+
1560
+ }));
1561
+ //# sourceMappingURL=index.umd.js.map