@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,645 @@
1
+ /**
2
+ * JSBridge SDK 类型定义
3
+ */
4
+ type Platform = 'android' | 'ios' | 'unknown';
5
+ interface JSBridgeConfig {
6
+ debug?: DebugConfig;
7
+ monitor?: MonitorConfig;
8
+ }
9
+ interface DebugConfig {
10
+ enabled: boolean;
11
+ useVConsole?: boolean;
12
+ logLevel?: 'debug' | 'info' | 'warn' | 'error';
13
+ showTimestamp?: boolean;
14
+ }
15
+ interface MonitorConfig {
16
+ enabled: boolean;
17
+ autoReport?: boolean;
18
+ reportInterval?: number;
19
+ maxRecords?: number;
20
+ logTag?: string;
21
+ }
22
+ declare enum LogLevel {
23
+ DEBUG = "DEBUG",
24
+ INFO = "INFO",
25
+ WARN = "WARN",
26
+ ERROR = "ERROR"
27
+ }
28
+ interface LogRequest {
29
+ tag?: string;
30
+ level: LogLevel;
31
+ message: string;
32
+ }
33
+ interface ScanRequest {
34
+ scanType?: ('qrCode' | 'barCode')[];
35
+ hideAlbum?: boolean;
36
+ }
37
+ interface ScanResult {
38
+ resp_code: number;
39
+ resp_result: string;
40
+ resp_message?: string;
41
+ }
42
+ interface DeviceInfo {
43
+ model: string;
44
+ system: string;
45
+ version: string;
46
+ platform: string;
47
+ [key: string]: any;
48
+ }
49
+ interface NetworkInfo {
50
+ networkType: 'wifi' | '2g' | '3g' | '4g' | '5g' | 'none' | 'unknown';
51
+ }
52
+ interface ChooseMediaRequest {
53
+ count?: number;
54
+ mediaType?: ('image' | 'video')[];
55
+ sourceType?: ('album' | 'camera')[];
56
+ maxDuration?: number;
57
+ sizeType?: ('original' | 'compressed')[];
58
+ }
59
+ interface MediaFile {
60
+ tempFilePath: string;
61
+ size: number;
62
+ duration?: number;
63
+ height?: number;
64
+ width?: number;
65
+ thumbTempFilePath?: string;
66
+ fileType?: 'image' | 'video';
67
+ }
68
+ interface ChooseMediaResult {
69
+ tempFiles: MediaFile[];
70
+ type?: 'image' | 'video';
71
+ }
72
+ interface ImageInfo {
73
+ width: number;
74
+ height: number;
75
+ path: string;
76
+ orientation?: string;
77
+ type?: string;
78
+ }
79
+ interface UploadFileRequest {
80
+ url: string;
81
+ filePath: string;
82
+ name: string;
83
+ header?: Record<string, string>;
84
+ formData?: Record<string, any>;
85
+ timeout?: number;
86
+ }
87
+ interface UploadFileResult {
88
+ data: string;
89
+ statusCode: number;
90
+ errMsg?: string;
91
+ }
92
+ interface PrintRequest {
93
+ data: any;
94
+ [key: string]: any;
95
+ }
96
+ interface NavigationBarStyle {
97
+ navigationBarTitleText?: string;
98
+ backgroundColor?: string;
99
+ textColor?: string;
100
+ hideNavigationBar?: boolean;
101
+ }
102
+ interface RouteInfo {
103
+ path: string;
104
+ query?: Record<string, any>;
105
+ }
106
+ interface StorageData {
107
+ key: string;
108
+ data: any;
109
+ }
110
+ interface AppInfo {
111
+ appId: string;
112
+ version: string;
113
+ platform: Platform;
114
+ [key: string]: any;
115
+ }
116
+ interface PhoneCallRequest {
117
+ phoneNumber: string;
118
+ }
119
+ interface MonitorRecord {
120
+ api: string;
121
+ timestamp: number;
122
+ duration: number;
123
+ success: boolean;
124
+ error?: string;
125
+ platform: Platform;
126
+ }
127
+ interface MonitorStats {
128
+ totalCalls: number;
129
+ successCalls: number;
130
+ failedCalls: number;
131
+ avgDuration: number;
132
+ successRate: number;
133
+ }
134
+ interface DebugLog {
135
+ timestamp: number;
136
+ level: 'debug' | 'info' | 'warn' | 'error';
137
+ api: string;
138
+ message: string;
139
+ data?: any;
140
+ }
141
+
142
+ declare global {
143
+ interface Window {
144
+ WKWebViewJavascriptBridge: any;
145
+ WKWVJBCallbacks: Array<(bridge: any) => void>;
146
+ InjectJavascript: {
147
+ init: () => void;
148
+ };
149
+ }
150
+ }
151
+
152
+ /**
153
+ * 日志 API
154
+ *
155
+ * 参考:context/soa/src/jsBridge/JsBridgeHandlers.ts (line 152-154)
156
+ * 优化:增强类型安全、支持批量日志、支持日志队列
157
+ */
158
+
159
+ /**
160
+ * 写入日志到原生端
161
+ *
162
+ * @example
163
+ * await writeLog({
164
+ * tag: 'MyApp',
165
+ * level: LogLevel.INFO,
166
+ * message: '用户点击了按钮'
167
+ * })
168
+ */
169
+ declare function writeLog(request: LogRequest): Promise<void>;
170
+
171
+ /**
172
+ * 设备相关 API
173
+ *
174
+ * 参考:context/soa/src/jsBridge/JsBridgeHandlers.ts
175
+ */
176
+
177
+ /**
178
+ * 扫码
179
+ * 参考:JsBridgeHandlers.ts line 75-87
180
+ *
181
+ * @example
182
+ * const result = await scan({ scanType: ['qrCode', 'barCode'] })
183
+ * if (result.resp_code === 1000) {
184
+ * console.log('扫码结果:', result.resp_result)
185
+ * }
186
+ */
187
+ declare function scan(request?: ScanRequest): Promise<ScanResult>;
188
+ /**
189
+ * 震动
190
+ *
191
+ * @param duration 震动时长(毫秒),默认 400ms
192
+ */
193
+ declare function vibrate(duration?: number): Promise<void>;
194
+ /**
195
+ * 获取设备信息
196
+ *
197
+ * @example
198
+ * const info = await getDeviceInfo()
199
+ * console.log('设备型号:', info.model)
200
+ * console.log('系统版本:', info.version)
201
+ */
202
+ declare function getDeviceInfo(): Promise<DeviceInfo>;
203
+ /**
204
+ * 获取网络类型
205
+ *
206
+ * @example
207
+ * const network = await getNetworkType()
208
+ * console.log('网络类型:', network.networkType)
209
+ */
210
+ declare function getNetworkType(): Promise<NetworkInfo>;
211
+
212
+ /**
213
+ * 媒体相关 API
214
+ *
215
+ * 参考:context/soa/src/jsBridge/JsBridgeHandlers.ts
216
+ */
217
+
218
+ /**
219
+ * 选择图片/视频
220
+ * 参考:JsBridgeHandlers.ts line 93-100
221
+ *
222
+ * @example
223
+ * const result = await chooseMedia({
224
+ * count: 9,
225
+ * mediaType: ['image'],
226
+ * sourceType: ['album', 'camera']
227
+ * })
228
+ */
229
+ declare function chooseMedia(request: ChooseMediaRequest): Promise<ChooseMediaResult | null>;
230
+ /**
231
+ * 上传文件
232
+ * 参考:JsBridgeHandlers.ts line 117-122
233
+ *
234
+ * @example
235
+ * const result = await uploadFile({
236
+ * url: 'https://api.example.com/upload',
237
+ * filePath: '/path/to/file',
238
+ * name: 'file'
239
+ * })
240
+ */
241
+ declare function uploadFile(request: UploadFileRequest): Promise<UploadFileResult>;
242
+ /**
243
+ * 预览图片
244
+ *
245
+ * @param urls 图片URL数组
246
+ * @param current 当前显示图片的索引,默认 0
247
+ */
248
+ declare function previewImage(urls: string[], current?: number): Promise<void>;
249
+ /**
250
+ * 获取图片信息
251
+ * 参考:JsBridgeHandlers.ts line 106-111
252
+ *
253
+ * @param path 图片路径
254
+ */
255
+ declare function getImageInfo(path: string): Promise<ImageInfo>;
256
+ /**
257
+ * 蓝牙打印
258
+ * 参考:JsBridgeHandlers.ts line 182-194
259
+ *
260
+ * @param request 打印请求
261
+ */
262
+ declare function bluetoothPrint(request: PrintRequest): Promise<any>;
263
+
264
+ /**
265
+ * 路由相关 API
266
+ *
267
+ * 参考:context/soa/src/jsBridge/JsBridgeHandlers.ts
268
+ */
269
+
270
+ /**
271
+ * 设置导航栏样式
272
+ * 参考:JsBridgeHandlers.ts line 144-146
273
+ *
274
+ * @example
275
+ * await setNavigationBar({
276
+ * navigationBarTitleText: '订单详情',
277
+ * backgroundColor: '#ffffff',
278
+ * textColor: '#000000'
279
+ * })
280
+ */
281
+ declare function setNavigationBar(style: NavigationBarStyle): Promise<void>;
282
+ /**
283
+ * 关闭当前 WebView
284
+ * 参考:JsBridgeHandlers.ts line 128-130
285
+ *
286
+ * @example
287
+ * await closeWebView()
288
+ */
289
+ declare function closeWebView(): Promise<void>;
290
+ /**
291
+ * 注册路由跳转回调
292
+ * 参考:JsBridgeHandlers.ts line 44-46
293
+ *
294
+ * @example
295
+ * onRoute((data, callback) => {
296
+ * console.log('路由跳转:', data)
297
+ * router.push(data.path)
298
+ * callback({ success: true })
299
+ * })
300
+ */
301
+ declare function onRoute(handler: (data: any, callback: (response: any) => void) => void): Promise<void>;
302
+ /**
303
+ * 注册获取路由信息回调
304
+ * 参考:JsBridgeHandlers.ts line 51-53
305
+ *
306
+ * @example
307
+ * onGetRouteInfo((data, callback) => {
308
+ * callback({ routes: routeConfig })
309
+ * })
310
+ */
311
+ declare function onGetRouteInfo(handler: (data: any, callback: (response: any) => void) => void): Promise<void>;
312
+ /**
313
+ * 通知原生路由变化
314
+ * 参考:JsBridgeHandlers.ts line 21-23
315
+ *
316
+ * @param to 目标路由
317
+ * @param from 来源路由
318
+ */
319
+ declare function notifyRouteChange(to: RouteInfo, from: RouteInfo): Promise<void>;
320
+
321
+ /**
322
+ * 存储相关 API
323
+ *
324
+ * 参考:context/soa/src/jsBridge/JsBridgeHandlers.ts
325
+ */
326
+
327
+ /**
328
+ * 获取 App 信息
329
+ * 参考:JsBridgeHandlers.ts line 28-30
330
+ *
331
+ * @example
332
+ * const appInfo = await getAppInfo()
333
+ * console.log('App 版本:', appInfo.version)
334
+ */
335
+ declare function getAppInfo(): Promise<AppInfo>;
336
+ /**
337
+ * 获取存储数据
338
+ *
339
+ * @param key 存储键
340
+ */
341
+ declare function getStorage(key: string): Promise<any>;
342
+ /**
343
+ * 设置存储数据
344
+ *
345
+ * @param key 存储键
346
+ * @param data 存储数据
347
+ */
348
+ declare function setStorage(key: string, data: any): Promise<void>;
349
+ /**
350
+ * 注册刷新 store 数据回调
351
+ * 参考:JsBridgeHandlers.ts line 59-61
352
+ *
353
+ * @example
354
+ * onRefreshStore((data, callback) => {
355
+ * store.dispatch('refresh', data)
356
+ * callback({ success: true })
357
+ * })
358
+ */
359
+ declare function onRefreshStore(handler: (data: any, callback: (response: any) => void) => void): Promise<void>;
360
+ /**
361
+ * 通知原生 App 已加载完成
362
+ * 参考:JsBridgeHandlers.ts line 160-162
363
+ *
364
+ * @param data 启动数据
365
+ */
366
+ declare function notifyAppOnload(data?: any): Promise<void>;
367
+
368
+ /**
369
+ * 系统相关 API
370
+ *
371
+ * 参考:context/soa/src/jsBridge/JsBridgeHandlers.ts
372
+ */
373
+
374
+ /**
375
+ * 拨打电话
376
+ * 参考:JsBridgeHandlers.ts line 136-138
377
+ *
378
+ * @example
379
+ * await makePhoneCall({ phoneNumber: '10086' })
380
+ */
381
+ declare function makePhoneCall(request: PhoneCallRequest): Promise<void>;
382
+ /**
383
+ * 注册 PDA 扫码回调
384
+ * 参考:JsBridgeHandlers.ts line 67-69
385
+ *
386
+ * @example
387
+ * onPdaScan((data, callback) => {
388
+ * console.log('PDA 扫码结果:', data)
389
+ * callback({ success: true })
390
+ * })
391
+ */
392
+ declare function onPdaScan(handler: (data: any, callback: (response: any) => void) => void): Promise<void>;
393
+ /**
394
+ * 退出登录
395
+ *
396
+ * @example
397
+ * await logout()
398
+ */
399
+ declare function logout(): Promise<void>;
400
+
401
+ /**
402
+ * 初始化 JSBridge SDK
403
+ *
404
+ * @example
405
+ * const success = await init({
406
+ * debug: {
407
+ * enabled: true,
408
+ * useVConsole: true
409
+ * },
410
+ * monitor: {
411
+ * enabled: true,
412
+ * autoReport: true
413
+ * }
414
+ * })
415
+ */
416
+ declare function init(config?: JSBridgeConfig): Promise<boolean>;
417
+ /**
418
+ * 检查是否在 App 内
419
+ */
420
+ declare function inApp(): boolean;
421
+ /**
422
+ * 获取平台类型
423
+ */
424
+ declare function getPlatform(): Platform;
425
+ /**
426
+ * 检查是否已初始化
427
+ */
428
+ declare function isReady(): boolean;
429
+ /**
430
+ * JSBridge API
431
+ * 所有可用的原生能力
432
+ */
433
+ declare const JSBridge: {
434
+ /**
435
+ * 扫码
436
+ * @example
437
+ * const result = await JSBridge.scan({ scanType: ['qrCode'] })
438
+ */
439
+ scan: typeof scan;
440
+ /**
441
+ * 震动
442
+ * @example
443
+ * await JSBridge.vibrate(400)
444
+ */
445
+ vibrate: typeof vibrate;
446
+ /**
447
+ * 获取设备信息
448
+ * @example
449
+ * const info = await JSBridge.getDeviceInfo()
450
+ */
451
+ getDeviceInfo: typeof getDeviceInfo;
452
+ /**
453
+ * 获取网络类型
454
+ * @example
455
+ * const network = await JSBridge.getNetworkType()
456
+ */
457
+ getNetworkType: typeof getNetworkType;
458
+ /**
459
+ * 选择图片/视频
460
+ * @example
461
+ * const result = await JSBridge.chooseMedia({ count: 9, mediaType: ['image'] })
462
+ */
463
+ chooseMedia: typeof chooseMedia;
464
+ /**
465
+ * 上传文件
466
+ * @example
467
+ * const result = await JSBridge.uploadFile({ url, filePath, name: 'file' })
468
+ */
469
+ uploadFile: typeof uploadFile;
470
+ /**
471
+ * 预览图片
472
+ * @example
473
+ * await JSBridge.previewImage(['url1', 'url2'], 0)
474
+ */
475
+ previewImage: typeof previewImage;
476
+ /**
477
+ * 获取图片信息
478
+ * @example
479
+ * const info = await JSBridge.getImageInfo(path)
480
+ */
481
+ getImageInfo: typeof getImageInfo;
482
+ /**
483
+ * 蓝牙打印
484
+ * @example
485
+ * await JSBridge.bluetoothPrint({ data: printData })
486
+ */
487
+ bluetoothPrint: typeof bluetoothPrint;
488
+ /**
489
+ * 设置导航栏
490
+ * @example
491
+ * await JSBridge.setNavigationBar({ navigationBarTitleText: '标题' })
492
+ */
493
+ setNavigationBar: typeof setNavigationBar;
494
+ /**
495
+ * 关闭当前页面
496
+ * @example
497
+ * await JSBridge.closeWebView()
498
+ */
499
+ closeWebView: typeof closeWebView;
500
+ /**
501
+ * 注册路由跳转回调
502
+ * @example
503
+ * JSBridge.onRoute((data, callback) => { router.push(data.path) })
504
+ */
505
+ onRoute: typeof onRoute;
506
+ /**
507
+ * 注册获取路由信息回调
508
+ * @example
509
+ * JSBridge.onGetRouteInfo((data, callback) => { callback(routeConfig) })
510
+ */
511
+ onGetRouteInfo: typeof onGetRouteInfo;
512
+ /**
513
+ * 通知路由变化
514
+ * @example
515
+ * await JSBridge.notifyRouteChange(to, from)
516
+ */
517
+ notifyRouteChange: typeof notifyRouteChange;
518
+ /**
519
+ * 获取 App 信息
520
+ * @example
521
+ * const appInfo = await JSBridge.getAppInfo()
522
+ */
523
+ getAppInfo: typeof getAppInfo;
524
+ /**
525
+ * 获取存储数据
526
+ * @example
527
+ * const data = await JSBridge.getStorage('key')
528
+ */
529
+ getStorage: typeof getStorage;
530
+ /**
531
+ * 设置存储数据
532
+ * @example
533
+ * await JSBridge.setStorage('key', data)
534
+ */
535
+ setStorage: typeof setStorage;
536
+ /**
537
+ * 注册刷新 store 回调
538
+ * @example
539
+ * JSBridge.onRefreshStore((data, callback) => { store.dispatch('refresh') })
540
+ */
541
+ onRefreshStore: typeof onRefreshStore;
542
+ /**
543
+ * 通知 App 加载完成
544
+ * @example
545
+ * await JSBridge.notifyAppOnload()
546
+ */
547
+ notifyAppOnload: typeof notifyAppOnload;
548
+ /**
549
+ * 拨打电话
550
+ * @example
551
+ * await JSBridge.makePhoneCall({ phoneNumber: '10086' })
552
+ */
553
+ makePhoneCall: typeof makePhoneCall;
554
+ /**
555
+ * 注册 PDA 扫码回调
556
+ * @example
557
+ * JSBridge.onPdaScan((data, callback) => { console.log(data) })
558
+ */
559
+ onPdaScan: typeof onPdaScan;
560
+ /**
561
+ * 退出登录
562
+ * @example
563
+ * await JSBridge.logout()
564
+ */
565
+ logout: typeof logout;
566
+ /**
567
+ * 写入日志
568
+ * @example
569
+ * await JSBridge.writeLog({ level: LogLevel.INFO, message: '日志' })
570
+ */
571
+ writeLog: typeof writeLog;
572
+ /**
573
+ * 便捷日志方法
574
+ * @example
575
+ * await JSBridge.log.info('用户登录', 'Auth')
576
+ * await JSBridge.log.error('请求失败', 'Network')
577
+ */
578
+ log: {
579
+ debug: (message: string, tag?: string) => Promise<void>;
580
+ info: (message: string, tag?: string) => Promise<void>;
581
+ warn: (message: string, tag?: string) => Promise<void>;
582
+ error: (message: string, tag?: string) => Promise<void>;
583
+ };
584
+ };
585
+ /**
586
+ * 监控 API
587
+ */
588
+ declare const Monitor: {
589
+ /**
590
+ * 获取统计信息
591
+ * @example
592
+ * const stats = Monitor.getStats()
593
+ */
594
+ getStats: () => MonitorStats;
595
+ /**
596
+ * 获取所有记录
597
+ * @example
598
+ * const records = Monitor.getRecords()
599
+ */
600
+ getRecords: () => MonitorRecord[];
601
+ /**
602
+ * 清空记录
603
+ * @example
604
+ * Monitor.clear()
605
+ */
606
+ clear: () => void;
607
+ /**
608
+ * 手动上报
609
+ * @example
610
+ * await Monitor.report()
611
+ */
612
+ report: () => Promise<void>;
613
+ /**
614
+ * 上报错误
615
+ * @example
616
+ * await Monitor.reportError('api', 'error message')
617
+ */
618
+ reportError: (api: string, error: string, detail?: any) => Promise<void>;
619
+ };
620
+ /**
621
+ * Debug API
622
+ */
623
+ declare const Debug: {
624
+ /**
625
+ * 记录追踪日志
626
+ * @example
627
+ * Debug.trace('api', 'message', data)
628
+ */
629
+ trace: (api: string, message: string, data?: any) => void;
630
+ /**
631
+ * 获取所有日志
632
+ * @example
633
+ * const logs = Debug.getLogs()
634
+ */
635
+ getLogs: () => DebugLog[];
636
+ /**
637
+ * 清空日志
638
+ * @example
639
+ * Debug.clearLogs()
640
+ */
641
+ clearLogs: () => void;
642
+ };
643
+
644
+ export { Debug, JSBridge, LogLevel, Monitor, getPlatform, inApp, init, isReady };
645
+ export type { AppInfo, ChooseMediaRequest, ChooseMediaResult, DebugConfig, DebugLog, DeviceInfo, ImageInfo, JSBridgeConfig, LogRequest, MediaFile, MonitorConfig, MonitorRecord, MonitorStats, NavigationBarStyle, NetworkInfo, PhoneCallRequest, Platform, PrintRequest, RouteInfo, ScanRequest, ScanResult, StorageData, UploadFileRequest, UploadFileResult };