qsh-webview-sdk 2.0.9 → 2.2.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.
- package/README.md +762 -748
- package/dist/index.d.ts +56 -0
- package/dist/qsh-webview-sdk.es.js +1111 -858
- package/dist/qsh-webview-sdk.es.js.map +1 -1
- package/dist/qsh-webview-sdk.umd.js +1 -1
- package/dist/qsh-webview-sdk.umd.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,749 +1,763 @@
|
|
|
1
|
-
# QSH WebView SDK
|
|
2
|
-
|
|
3
|
-
一个重构后的 QSH WebView SDK,支持微信小程序和 APP 端的 WebView 与原生端通信。
|
|
4
|
-
|
|
5
|
-
## 特性
|
|
6
|
-
|
|
7
|
-
- ✅ 支持微信小程序 WebView
|
|
8
|
-
- ✅ 支持 APP 端 (Plus/NVUE/UVUE)
|
|
9
|
-
- ✅ 模块化架构,易于维护
|
|
10
|
-
- ✅ TypeScript 类型支持
|
|
11
|
-
- ✅ UMD 和 ESM 双格式输出
|
|
12
|
-
- ✅ 自动环境检测
|
|
13
|
-
|
|
14
|
-
## 安装
|
|
15
|
-
|
|
16
|
-
### NPM 安装
|
|
17
|
-
|
|
18
|
-
```bash
|
|
19
|
-
npm install qsh-webview-sdk
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
### CDN 引入
|
|
23
|
-
|
|
24
|
-
```html
|
|
25
|
-
<script src="path/to/qsh-webview-sdk.umd.js"></script>
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
## 使用方法
|
|
29
|
-
|
|
30
|
-
### 在 HTML 中引入
|
|
31
|
-
|
|
32
|
-
```html
|
|
33
|
-
<!-- 引入 QSH WebView SDK(内部会在微信内的小程序 web-view 环境自动注入 jweixin) -->
|
|
34
|
-
<script type="text/javascript" src="path/to/qsh-webview-sdk.umd.js"></script>
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
> 说明:通过 UMD 方式引入后,默认在全局暴露 `window.qsh`。
|
|
38
|
-
|
|
39
|
-
### NPM 安装与使用(ESM)
|
|
40
|
-
|
|
41
|
-
```bash
|
|
42
|
-
npm install qsh-webview-sdk
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
```javascript
|
|
46
|
-
import qsh from 'qsh-webview-sdk';
|
|
47
|
-
|
|
48
|
-
// 等待 SDK 就绪后再调用 API(内部也有自动等待机制)
|
|
49
|
-
qsh.ready().then(() => {
|
|
50
|
-
qsh.navigateTo({ url: '/pages/test/test' });
|
|
51
|
-
});
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
###
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
//
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
//
|
|
117
|
-
qsh.
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
| `
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
|
177
|
-
|
|
178
|
-
| `
|
|
179
|
-
| `
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
npm run
|
|
285
|
-
```
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
```bash
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
├──
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
├──
|
|
335
|
-
│ ├──
|
|
336
|
-
│ ├──
|
|
337
|
-
│ ├──
|
|
338
|
-
│ └──
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
-
|
|
368
|
-
|
|
369
|
-
- [
|
|
370
|
-
- [x]
|
|
371
|
-
- [
|
|
372
|
-
- [
|
|
373
|
-
- [ ]
|
|
374
|
-
- [ ]
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
});
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
qsh.
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
})
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
//
|
|
547
|
-
qsh.
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
```
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
//
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
//
|
|
579
|
-
qsh.
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
console.log('
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
success: (res) => {
|
|
663
|
-
console.log('
|
|
664
|
-
console.log('
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
}
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
```javascript
|
|
734
|
-
|
|
735
|
-
import
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
}
|
|
1
|
+
# QSH WebView SDK
|
|
2
|
+
|
|
3
|
+
一个重构后的 QSH WebView SDK,支持微信小程序和 APP 端的 WebView 与原生端通信。
|
|
4
|
+
|
|
5
|
+
## 特性
|
|
6
|
+
|
|
7
|
+
- ✅ 支持微信小程序 WebView
|
|
8
|
+
- ✅ 支持 APP 端 (Plus/NVUE/UVUE)
|
|
9
|
+
- ✅ 模块化架构,易于维护
|
|
10
|
+
- ✅ TypeScript 类型支持
|
|
11
|
+
- ✅ UMD 和 ESM 双格式输出
|
|
12
|
+
- ✅ 自动环境检测
|
|
13
|
+
|
|
14
|
+
## 安装
|
|
15
|
+
|
|
16
|
+
### NPM 安装
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install qsh-webview-sdk
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
### CDN 引入
|
|
23
|
+
|
|
24
|
+
```html
|
|
25
|
+
<script src="path/to/qsh-webview-sdk.umd.js"></script>
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## 使用方法
|
|
29
|
+
|
|
30
|
+
### 在 HTML 中引入
|
|
31
|
+
|
|
32
|
+
```html
|
|
33
|
+
<!-- 引入 QSH WebView SDK(内部会在微信内的小程序 web-view 环境自动注入 jweixin) -->
|
|
34
|
+
<script type="text/javascript" src="path/to/qsh-webview-sdk.umd.js"></script>
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
> 说明:通过 UMD 方式引入后,默认在全局暴露 `window.qsh`。
|
|
38
|
+
|
|
39
|
+
### NPM 安装与使用(ESM)
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
npm install qsh-webview-sdk
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
```javascript
|
|
46
|
+
import qsh from 'qsh-webview-sdk';
|
|
47
|
+
|
|
48
|
+
// 等待 SDK 就绪后再调用 API(内部也有自动等待机制)
|
|
49
|
+
qsh.ready().then(() => {
|
|
50
|
+
qsh.navigateTo({ url: '/pages/test/test' });
|
|
51
|
+
});
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### 微信 clientId 注入
|
|
55
|
+
|
|
56
|
+
```javascript
|
|
57
|
+
import qsh from 'qsh-webview-sdk';
|
|
58
|
+
|
|
59
|
+
if(qsh.environment.isWeixinMiniProgram) {
|
|
60
|
+
qsh.config({
|
|
61
|
+
clientId: 'your-client-id',
|
|
62
|
+
isProd: false
|
|
63
|
+
})
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// isProd 默认为 false: 根据环境传入
|
|
67
|
+
// 依赖微信 JS-SDK 的能力前,建议先注入 clientId
|
|
68
|
+
// 已调用 qsh.config() 时,qsh.ready() 会在微信小程序 web-view 中额外等待 wx.config 完成
|
|
69
|
+
await qsh.ready();
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### 初始化
|
|
73
|
+
|
|
74
|
+
- 浏览器环境下,SDK 会在 DOM 就绪后自动初始化,并触发 `UniAppJSBridgeReady` 事件。
|
|
75
|
+
- SSR/Node 环境不会执行自动初始化;在客户端 Hydration 后会按上述规则自动初始化。
|
|
76
|
+
- 所有对外 API(如 `navigateTo`、`postMessage`、`getEnv` 等)内置自动等待机制:即使在未就绪时调用,也会在就绪后自动执行,无需手动监听事件。
|
|
77
|
+
- 仍可使用 `qsh.ready()` 等待准备完成;如需“控制初始化时机”,可手动调用 `qsh.init()`。该方法是幂等的,多次调用仅首次生效。
|
|
78
|
+
|
|
79
|
+
> 注:当检测到运行于微信内且为小程序 web-view 场景时,SDK 将自动按需注入 `jweixin-1.6.2.js`(若页面已引入则不会重复注入)。若你需要自定义版本或自行控制加载时机,可在页面提前引入 jweixin,SDK 将跳过自动注入。
|
|
80
|
+
|
|
81
|
+
### 基础使用
|
|
82
|
+
|
|
83
|
+
```javascript
|
|
84
|
+
// 直接调用,无需等待(内部会自动等待就绪)
|
|
85
|
+
qsh.navigateTo({ url: '/pages/detail/detail?id=1' });
|
|
86
|
+
|
|
87
|
+
// 如需在 UI 上显示“就绪状态”,可以使用 ready()
|
|
88
|
+
qsh.ready().then(() => {
|
|
89
|
+
console.log('SDK 已准备就绪');
|
|
90
|
+
});
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### 导航功能
|
|
94
|
+
|
|
95
|
+
```javascript
|
|
96
|
+
// 跳转到新页面
|
|
97
|
+
qsh.navigateTo({
|
|
98
|
+
url: '/pages/detail/detail?id=1'
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
// 返回上一页
|
|
102
|
+
qsh.navigateBack({
|
|
103
|
+
delta: 1
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
// 切换 Tab
|
|
107
|
+
qsh.switchTab({
|
|
108
|
+
url: '/pages/home/home'
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
// 重启应用
|
|
112
|
+
qsh.reLaunch({
|
|
113
|
+
url: '/pages/index/index'
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
// 重定向
|
|
117
|
+
qsh.redirectTo({
|
|
118
|
+
url: '/pages/login/login'
|
|
119
|
+
});
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### 消息通信
|
|
123
|
+
|
|
124
|
+
```javascript
|
|
125
|
+
// 发送消息到原生端
|
|
126
|
+
qsh.postMessage({
|
|
127
|
+
data: {
|
|
128
|
+
action: 'share',
|
|
129
|
+
title: '分享标题',
|
|
130
|
+
content: '分享内容'
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
// 获取环境信息
|
|
135
|
+
qsh.getEnv(function(result) {
|
|
136
|
+
console.log('环境信息:', result);
|
|
137
|
+
// result: { miniprogram: true, weixin: true }
|
|
138
|
+
// 或 { app: true, plus: true } 或 { h5: true }
|
|
139
|
+
});
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### 环境检测
|
|
143
|
+
|
|
144
|
+
```javascript
|
|
145
|
+
// 获取当前环境信息
|
|
146
|
+
const env = qsh.environment;
|
|
147
|
+
|
|
148
|
+
console.log('环境类型:', env.type); // 'weixin' | 'plus' | 'nvue' | 'uvue' | 'UniApp' | 'h5'
|
|
149
|
+
console.log('是否微信小程序:', env.isWeixinMiniProgram);
|
|
150
|
+
console.log('是否 APP:', env.isAppPlus);
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
<!-- 加密能力(SM2/SM3/SM4)已在 v2.1.0 起移除,如需相关能力请在业务侧引入专用加密库。 -->
|
|
154
|
+
|
|
155
|
+
## API 文档
|
|
156
|
+
|
|
157
|
+
### 导航 API
|
|
158
|
+
|
|
159
|
+
| 方法 | 说明 | 参数 |
|
|
160
|
+
|------|------|------|
|
|
161
|
+
| `navigateTo` | 保留当前页面,跳转到应用内的某个页面 | `{ url: string }` |
|
|
162
|
+
| `navigateBack` | 关闭当前页面,返回上一页面或多级页面 | `{ delta?: number }` |
|
|
163
|
+
| `switchTab` | 跳转到 tabBar 页面 | `{ url: string }` |
|
|
164
|
+
| `reLaunch` | 关闭所有页面,打开到应用内的某个页面 | `{ url: string }` |
|
|
165
|
+
| `redirectTo` | 关闭当前页面,跳转到应用内的某个页面 | `{ url: string }` |
|
|
166
|
+
|
|
167
|
+
### 消息 API
|
|
168
|
+
|
|
169
|
+
| 方法 | 说明 | 参数 |
|
|
170
|
+
|------|------|------|
|
|
171
|
+
| `postMessage` | 向原生端发送消息 | `{ data?: any }` |
|
|
172
|
+
| `getEnv` | 获取当前环境信息 | `callback: Function` |
|
|
173
|
+
|
|
174
|
+
### 图片 API
|
|
175
|
+
|
|
176
|
+
| 方法 | 说明 | 参数 |
|
|
177
|
+
|------|------|------|
|
|
178
|
+
| `chooseImage` | 选择图片(自动环境适配) | `{ count?: number; sizeType?: string[]; sourceType?: string[]; success?: Function; fail?: Function; complete?: Function }` |
|
|
179
|
+
| `chooseImageAsync` | 选择图片(Promise 版) | `{ count?: number; sizeType?: string[]; sourceType?: string[] }` |
|
|
180
|
+
|
|
181
|
+
### 扫码 API
|
|
182
|
+
|
|
183
|
+
| 方法 | 说明 | 参数 |
|
|
184
|
+
|------|------|------|
|
|
185
|
+
| `scanCode` | 扫码(自动环境适配) | `{ onlyFromCamera?: boolean; scanType?: string[]; success?: Function; fail?: Function; complete?: Function }` |
|
|
186
|
+
| `scanCodeAsync` | 扫码(Promise 版) | `{ onlyFromCamera?: boolean; scanType?: string[] }` |
|
|
187
|
+
|
|
188
|
+
### 位置 API
|
|
189
|
+
|
|
190
|
+
| 方法 | 说明 | 参数 |
|
|
191
|
+
|------|------|------|
|
|
192
|
+
| `getLocation` | 获取当前位置 | `{ type?: string; altitude?: boolean; success?: Function; fail?: Function }` |
|
|
193
|
+
| `getLocationAsync` | 获取位置(Promise 版) | `{ type?: string; altitude?: boolean }` |
|
|
194
|
+
| `openLocation` | 查看位置(打开地图) | `{ latitude: number; longitude: number; name?: string; address?: string; scale?: number; success?: Function; fail?: Function }` |
|
|
195
|
+
| `openLocationAsync` | 查看位置(Promise 版) | `{ latitude: number; longitude: number; name?: string; address?: string; scale?: number }` |
|
|
196
|
+
| `startLocationUpdate` | 开启前台持续定位(仅 APP) | `{ type?: string; needFullAccuracy?: boolean; success?: Function; fail?: Function }` |
|
|
197
|
+
| `stopLocationUpdate` | 停止前台持续定位(仅 APP) | `{ success?: Function; fail?: Function }` |
|
|
198
|
+
| `onLocationChange` | 监听实时位置变化(仅 APP) | `callback: Function` |
|
|
199
|
+
| `offLocationChange` | 取消监听位置变化(仅 APP) | `callback?: Function` |
|
|
200
|
+
| `onLocationChangeError` | 监听位置更新错误(仅 APP) | `callback: Function` |
|
|
201
|
+
| `offLocationChangeError` | 取消监听位置更新错误(仅 APP) | `callback?: Function` |
|
|
202
|
+
|
|
203
|
+
#### 实现原理
|
|
204
|
+
|
|
205
|
+
- 微信小程序侧
|
|
206
|
+
- 自动按需加载并配置 jweixin,等待 `qsh.weixin.isConfigReady()` 为 `true` 后,SDK 内部调用 `wx.chooseImage(options)`;开发者统一调用 `qsh.chooseImage(options)`。
|
|
207
|
+
- 透传 `count`/`sizeType`/`sourceType`,回调结果遵循微信规范:`res.localIds`。
|
|
208
|
+
- UniApp 侧(APP/NVUE/UVUE/H5+)
|
|
209
|
+
- WebView 通过桥接向宿主发送 `chooseImage` 指令:内部使用 `callApiInWebView('chooseImage', params)`。
|
|
210
|
+
- 宿主侧调用 `uni.chooseImage` 并将结果回传给 H5 页面;结果包含 `res.tempFilePaths`、`res.tempFiles`。
|
|
211
|
+
- 调用时机与等待
|
|
212
|
+
- 所有 API 内置 `qsh.ready()` 等待机制,即使在未就绪时调用也会在 Bridge 就绪后自动执行。
|
|
213
|
+
- 微信侧还会在配置完成后再调用;配置失败将进入 `fail` 回调。
|
|
214
|
+
|
|
215
|
+
#### 返回结果差异
|
|
216
|
+
|
|
217
|
+
- 微信:`res.localIds: string[]`
|
|
218
|
+
- UniApp:`res.tempFilePaths: string[]`, `res.tempFiles: Array<{ path: string, size: number }>`
|
|
219
|
+
|
|
220
|
+
#### 使用示例
|
|
221
|
+
|
|
222
|
+
1) 回调版
|
|
223
|
+
|
|
224
|
+
```javascript
|
|
225
|
+
qsh.chooseImage({
|
|
226
|
+
count: 1,
|
|
227
|
+
sizeType: ['compressed'],
|
|
228
|
+
sourceType: ['album', 'camera'],
|
|
229
|
+
success(res) {
|
|
230
|
+
// 微信:res.localIds
|
|
231
|
+
// UniApp:res.tempFilePaths / res.tempFiles
|
|
232
|
+
},
|
|
233
|
+
fail(err) {
|
|
234
|
+
console.error(err);
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
2) Promise 版
|
|
240
|
+
|
|
241
|
+
```javascript
|
|
242
|
+
try {
|
|
243
|
+
const res = await qsh.chooseImageAsync({ count: 1 });
|
|
244
|
+
// 处理 res
|
|
245
|
+
} catch (e) {
|
|
246
|
+
console.error(e);
|
|
247
|
+
}
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
3) 常量辅助
|
|
251
|
+
|
|
252
|
+
```javascript
|
|
253
|
+
qsh.chooseImage({
|
|
254
|
+
sizeType: [qsh.ImageSizeTypes.COMPRESSED],
|
|
255
|
+
sourceType: [qsh.ImageSourceTypes.ALBUM]
|
|
256
|
+
});
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
<!-- 加密 API 已移除 -->
|
|
260
|
+
|
|
261
|
+
### 环境属性
|
|
262
|
+
|
|
263
|
+
| 属性 | 类型 | 说明 |
|
|
264
|
+
|------|------|------|
|
|
265
|
+
| `environment.type` | `string` | 环境类型 |
|
|
266
|
+
| `environment.isWeixinMiniProgram` | `boolean` | 是否微信小程序 |
|
|
267
|
+
| `environment.isAppPlus` | `boolean` | 是否 APP Plus |
|
|
268
|
+
| `environment.isNvue` | `boolean` | 是否 NVUE |
|
|
269
|
+
| `environment.isUvue` | `boolean` | 是否 UVUE |
|
|
270
|
+
| `environment.isUniApp` | `boolean` | 是否 UniApp |
|
|
271
|
+
| `environment.isHtml5Plus` | `boolean` | 是否 Html5Plus |
|
|
272
|
+
|
|
273
|
+
## 开发
|
|
274
|
+
|
|
275
|
+
### 安装依赖
|
|
276
|
+
|
|
277
|
+
```bash
|
|
278
|
+
npm install
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
### 开发模式
|
|
282
|
+
|
|
283
|
+
```bash
|
|
284
|
+
npm run dev
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
### 构建
|
|
288
|
+
|
|
289
|
+
```bash
|
|
290
|
+
npm run build
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
## 本地调试
|
|
294
|
+
|
|
295
|
+
### Vue3 演示系统(推荐)
|
|
296
|
+
|
|
297
|
+
使用完整的 Vue3 演示系统进行调试和测试:
|
|
298
|
+
|
|
299
|
+
```bash
|
|
300
|
+
npm run play
|
|
301
|
+
# 或
|
|
302
|
+
npm run play:vue3
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
访问:http://localhost:5173
|
|
306
|
+
|
|
307
|
+
**功能**:
|
|
308
|
+
- 11 个功能演示页面
|
|
309
|
+
- 完整的交互界面
|
|
310
|
+
- 实时日志记录
|
|
311
|
+
- 移动端适配
|
|
312
|
+
|
|
313
|
+
说明:`vue3-playground/vite.config.js` 已将包名 `qsh-webview-sdk` 定位到仓库 `src/index.js`,用于直接联调当前源码。
|
|
314
|
+
|
|
315
|
+
### 其他命令
|
|
316
|
+
|
|
317
|
+
```bash
|
|
318
|
+
# 生产构建
|
|
319
|
+
npm run build:vue3
|
|
320
|
+
|
|
321
|
+
# 预览构建
|
|
322
|
+
npm run preview:vue3
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
## 项目结构
|
|
326
|
+
|
|
327
|
+
```
|
|
328
|
+
src/
|
|
329
|
+
├── core/ # 核心模块
|
|
330
|
+
│ ├── environment.js # 环境检测
|
|
331
|
+
│ ├── bridge.js # 桥接初始化
|
|
332
|
+
│ ├── messenger.js # 消息通信
|
|
333
|
+
│ ├── plugin-manager.js # 插件管理器
|
|
334
|
+
│ ├── interceptor.js # 拦截器链
|
|
335
|
+
│ ├── state-store.js # 状态仓库
|
|
336
|
+
│ ├── error-codes.js # 错误码定义
|
|
337
|
+
│ ├── error-normalizer.js # 错误标准化
|
|
338
|
+
│ └── ...
|
|
339
|
+
├── platforms/ # 平台适配
|
|
340
|
+
│ ├── weixin.js # 微信小程序
|
|
341
|
+
│ └── app.js # APP 端
|
|
342
|
+
├── api/ # API 接口
|
|
343
|
+
│ ├── image.js # 图片 API
|
|
344
|
+
│ ├── scan.js # 扫码 API
|
|
345
|
+
│ ├── location.js # 位置 API
|
|
346
|
+
│ ├── navigation.js # 导航 API
|
|
347
|
+
│ └── message.js # 消息 API
|
|
348
|
+
├── index.js # 主入口
|
|
349
|
+
└── index.d.ts # 类型定义
|
|
350
|
+
|
|
351
|
+
vue3-playground/ # Vue3 演示系统(功能完整)
|
|
352
|
+
├── src/
|
|
353
|
+
│ ├── router/ # 路由配置
|
|
354
|
+
│ ├── views/ # 11 个演示页面
|
|
355
|
+
│ ├── components/ # 公共组件
|
|
356
|
+
│ └── composables/ # 组合式函数
|
|
357
|
+
└── ...
|
|
358
|
+
|
|
359
|
+
uni-playground/ # UniApp 宿主端(用于真机测试)
|
|
360
|
+
references/ # 开发参考(如原版 uni.webview.1.5.6.js)
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
## 开发计划(Roadmap)
|
|
364
|
+
|
|
365
|
+
以下为近期需要支持的功能清单,按平台划分(未勾选表示待实现)。
|
|
366
|
+
|
|
367
|
+
### 小程序(web-view)
|
|
368
|
+
|
|
369
|
+
- [x] 扫码 ✅
|
|
370
|
+
- [x] 获取位置 ✅
|
|
371
|
+
- [x] 查看位置 ✅
|
|
372
|
+
- [x] 照片选择 ✅
|
|
373
|
+
- [ ] 相机
|
|
374
|
+
- [ ] 打开地图选择位置
|
|
375
|
+
- [ ] 人脸核身
|
|
376
|
+
|
|
377
|
+
### APP(UniApp/Plus/NVUE/UVUE)
|
|
378
|
+
|
|
379
|
+
- [x] 扫码 ✅
|
|
380
|
+
- [x] 获取位置 ✅
|
|
381
|
+
- [x] 查看位置 ✅
|
|
382
|
+
- [x] 照片选择 ✅
|
|
383
|
+
- [x] 持续定位 ✅
|
|
384
|
+
- [ ] 相机
|
|
385
|
+
- [ ] 蓝牙
|
|
386
|
+
- [ ] WiFi
|
|
387
|
+
- [ ] 微信分享
|
|
388
|
+
- [x] 照片选择 ✅
|
|
389
|
+
- [ ] 文件选择
|
|
390
|
+
- [ ] 打开地图选择位置
|
|
391
|
+
- [ ] 人脸核身
|
|
392
|
+
- [ ] 打印服务
|
|
393
|
+
|
|
394
|
+
### 扫码功能
|
|
395
|
+
|
|
396
|
+
#### 基础使用
|
|
397
|
+
|
|
398
|
+
```javascript
|
|
399
|
+
// 回调方式
|
|
400
|
+
qsh.scanCode({
|
|
401
|
+
success: (res) => {
|
|
402
|
+
console.log('扫码结果:', res.result);
|
|
403
|
+
console.log('扫码类型:', res.scanType);
|
|
404
|
+
},
|
|
405
|
+
fail: (err) => {
|
|
406
|
+
if (err.code === qsh.errors.codes.SCAN_CANCELLED) {
|
|
407
|
+
console.log('用户取消扫码');
|
|
408
|
+
} else {
|
|
409
|
+
console.error('扫码失败:', err);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
// Promise 版本
|
|
415
|
+
try {
|
|
416
|
+
const result = await qsh.scanCodeAsync({
|
|
417
|
+
scanType: ['qrCode']
|
|
418
|
+
});
|
|
419
|
+
console.log('扫码结果:', result.result);
|
|
420
|
+
} catch (error) {
|
|
421
|
+
console.error('扫码失败:', error);
|
|
422
|
+
}
|
|
423
|
+
```
|
|
424
|
+
|
|
425
|
+
#### 参数说明
|
|
426
|
+
|
|
427
|
+
```javascript
|
|
428
|
+
qsh.scanCode({
|
|
429
|
+
onlyFromCamera: true, // 只从相机扫码(默认 true)
|
|
430
|
+
scanType: ['qrCode', 'barCode'], // 扫码类型
|
|
431
|
+
success: (res) => {
|
|
432
|
+
// res.result - 扫码内容
|
|
433
|
+
// res.scanType - 扫码类型
|
|
434
|
+
// res.charSet - 字符集(仅 APP/微信)
|
|
435
|
+
}
|
|
436
|
+
});
|
|
437
|
+
```
|
|
438
|
+
|
|
439
|
+
#### 扫码类型
|
|
440
|
+
|
|
441
|
+
| 类型 | 说明 | 常量 |
|
|
442
|
+
|------|------|------|
|
|
443
|
+
| `qrCode` | 二维码 | `qsh.ScanTypes.QR_CODE` |
|
|
444
|
+
| `barCode` | 一维码(条形码) | `qsh.ScanTypes.BAR_CODE` |
|
|
445
|
+
| `datamatrix` | Data Matrix 码 | `qsh.ScanTypes.DATA_MATRIX` |
|
|
446
|
+
| `pdf417` | PDF417 条码 | `qsh.ScanTypes.PDF417` |
|
|
447
|
+
|
|
448
|
+
#### 平台差异
|
|
449
|
+
|
|
450
|
+
- **微信小程序**:调用 `wx.scanQRCode`,自动配置
|
|
451
|
+
- **APP 端**:调用 `uni.scanCode`,通过 WebView 桥接
|
|
452
|
+
- **返回格式**:两端已统一为 `{ result, scanType, charSet }`
|
|
453
|
+
|
|
454
|
+
参考文档:[UniApp scanCode API](https://uniapp.dcloud.net.cn/api/system/barcode.html#scancode)
|
|
455
|
+
|
|
456
|
+
### 位置功能
|
|
457
|
+
|
|
458
|
+
#### 获取当前位置
|
|
459
|
+
|
|
460
|
+
```javascript
|
|
461
|
+
// 回调方式
|
|
462
|
+
qsh.getLocation({
|
|
463
|
+
type: 'wgs84', // 坐标类型:wgs84(GPS), gcj02(国测局), bd09(百度)
|
|
464
|
+
altitude: false, // 是否返回高度信息
|
|
465
|
+
success: (res) => {
|
|
466
|
+
console.log('纬度:', res.latitude);
|
|
467
|
+
console.log('经度:', res.longitude);
|
|
468
|
+
console.log('精度:', res.accuracy);
|
|
469
|
+
console.log('速度:', res.speed);
|
|
470
|
+
},
|
|
471
|
+
fail: (error) => {
|
|
472
|
+
if (error.code === qsh.errors.codes.LOCATION_NO_PERMISSION) {
|
|
473
|
+
console.log('无定位权限');
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
// Promise 版本
|
|
479
|
+
try {
|
|
480
|
+
const location = await qsh.getLocationAsync({
|
|
481
|
+
type: 'gcj02',
|
|
482
|
+
altitude: true
|
|
483
|
+
});
|
|
484
|
+
console.log('位置:', location.latitude, location.longitude);
|
|
485
|
+
} catch (error) {
|
|
486
|
+
console.error('定位失败:', error);
|
|
487
|
+
}
|
|
488
|
+
```
|
|
489
|
+
|
|
490
|
+
#### 查看位置(打开地图)
|
|
491
|
+
|
|
492
|
+
```javascript
|
|
493
|
+
// 打开地图查看指定位置
|
|
494
|
+
qsh.openLocation({
|
|
495
|
+
latitude: 39.908823,
|
|
496
|
+
longitude: 116.397470,
|
|
497
|
+
name: '天安门',
|
|
498
|
+
address: '北京市东城区东长安街',
|
|
499
|
+
scale: 15, // 缩放级别 1-28
|
|
500
|
+
success: () => {
|
|
501
|
+
console.log('地图已打开');
|
|
502
|
+
}
|
|
503
|
+
});
|
|
504
|
+
|
|
505
|
+
// Promise 版本
|
|
506
|
+
await qsh.openLocationAsync({
|
|
507
|
+
latitude: 39.908823,
|
|
508
|
+
longitude: 116.397470,
|
|
509
|
+
name: '天安门'
|
|
510
|
+
});
|
|
511
|
+
```
|
|
512
|
+
|
|
513
|
+
#### 坐标类型
|
|
514
|
+
|
|
515
|
+
| 类型 | 说明 | 常量 |
|
|
516
|
+
|------|------|------|
|
|
517
|
+
| `wgs84` | GPS 坐标 | `qsh.CoordinateTypes.WGS84` |
|
|
518
|
+
| `gcj02` | 国测局坐标(火星坐标) | `qsh.CoordinateTypes.GCJ02` |
|
|
519
|
+
| `bd09` | 百度坐标 | `qsh.CoordinateTypes.BD09` |
|
|
520
|
+
|
|
521
|
+
#### 平台差异
|
|
522
|
+
|
|
523
|
+
- **微信小程序**:调用 `wx.getLocation` / `wx.openLocation`
|
|
524
|
+
- **APP 端**:调用 `uni.getLocation` / `uni.openLocation`(通过 WebView 桥接)
|
|
525
|
+
- **返回格式**:已统一
|
|
526
|
+
|
|
527
|
+
参考文档:[UniApp getLocation](https://uniapp.dcloud.net.cn/api/location/location.html)
|
|
528
|
+
|
|
529
|
+
#### 持续定位(仅 APP)
|
|
530
|
+
|
|
531
|
+
```javascript
|
|
532
|
+
// 1. 开启前台持续定位服务
|
|
533
|
+
qsh.startLocationUpdate({
|
|
534
|
+
type: 'wgs84', // 坐标类型
|
|
535
|
+
needFullAccuracy: true, // 是否需要高精度
|
|
536
|
+
success: () => console.log('定位服务已开启'),
|
|
537
|
+
fail: (err) => console.error('开启失败:', err)
|
|
538
|
+
});
|
|
539
|
+
|
|
540
|
+
// 2. 监听位置变化
|
|
541
|
+
qsh.onLocationChange((res) => {
|
|
542
|
+
console.log('位置:', res.latitude, res.longitude);
|
|
543
|
+
console.log('精度:', res.accuracy, '米');
|
|
544
|
+
});
|
|
545
|
+
|
|
546
|
+
// 3. 监听定位错误(可选)
|
|
547
|
+
qsh.onLocationChangeError((err) => {
|
|
548
|
+
console.error('定位出错:', err);
|
|
549
|
+
});
|
|
550
|
+
|
|
551
|
+
// 4. 停止监听
|
|
552
|
+
qsh.offLocationChange();
|
|
553
|
+
qsh.offLocationChangeError();
|
|
554
|
+
|
|
555
|
+
// 5. 停止定位服务
|
|
556
|
+
qsh.stopLocationUpdate({
|
|
557
|
+
success: () => console.log('定位服务已停止')
|
|
558
|
+
});
|
|
559
|
+
```
|
|
560
|
+
|
|
561
|
+
### 相机功能
|
|
562
|
+
|
|
563
|
+
```javascript
|
|
564
|
+
// 相机拍照(小程序/APP)
|
|
565
|
+
qsh.takePhoto({
|
|
566
|
+
quality: 'high', // 图片质量:low, normal, high
|
|
567
|
+
sizeType: 'compressed', // 图片尺寸:original, compressed
|
|
568
|
+
success: (res) => {
|
|
569
|
+
console.log('照片路径:', res.tempImagePath);
|
|
570
|
+
},
|
|
571
|
+
fail: (err) => console.error(err)
|
|
572
|
+
});
|
|
573
|
+
```
|
|
574
|
+
|
|
575
|
+
### 蓝牙功能(仅 APP)
|
|
576
|
+
|
|
577
|
+
```javascript
|
|
578
|
+
// 初始化蓝牙
|
|
579
|
+
qsh.openBluetoothAdapter({
|
|
580
|
+
success: () => console.log('蓝牙初始化成功'),
|
|
581
|
+
fail: (err) => console.error('蓝牙初始化失败:', err)
|
|
582
|
+
});
|
|
583
|
+
|
|
584
|
+
// 开始搜索设备
|
|
585
|
+
qsh.startBluetoothDevicesDiscovery({
|
|
586
|
+
services: ['0000FFE0-0000-1000-8000-00805F9B34FB'],
|
|
587
|
+
success: () => console.log('开始搜索设备'),
|
|
588
|
+
fail: (err) => console.error('搜索失败:', err)
|
|
589
|
+
});
|
|
590
|
+
|
|
591
|
+
// 监听设备发现
|
|
592
|
+
qsh.onBluetoothDeviceFound((devices) => {
|
|
593
|
+
console.log('发现设备:', devices);
|
|
594
|
+
});
|
|
595
|
+
|
|
596
|
+
// 连接设备
|
|
597
|
+
qsh.createBLEConnection({
|
|
598
|
+
deviceId: 'device-id',
|
|
599
|
+
success: () => console.log('连接成功'),
|
|
600
|
+
fail: (err) => console.error('连接失败:', err)
|
|
601
|
+
});
|
|
602
|
+
```
|
|
603
|
+
|
|
604
|
+
### WiFi 功能(仅 APP)
|
|
605
|
+
|
|
606
|
+
```javascript
|
|
607
|
+
// 获取 WiFi 信息
|
|
608
|
+
qsh.getWifiInfo({
|
|
609
|
+
success: (res) => {
|
|
610
|
+
console.log('WiFi SSID:', res.wifi.SSID);
|
|
611
|
+
console.log('WiFi BSSID:', res.wifi.BSSID);
|
|
612
|
+
},
|
|
613
|
+
fail: (err) => console.error(err)
|
|
614
|
+
});
|
|
615
|
+
|
|
616
|
+
// 连接 WiFi
|
|
617
|
+
qsh.connectWifi({
|
|
618
|
+
SSID: 'wifi-name',
|
|
619
|
+
password: 'wifi-password',
|
|
620
|
+
success: () => console.log('连接成功'),
|
|
621
|
+
fail: (err) => console.error('连接失败:', err)
|
|
622
|
+
});
|
|
623
|
+
```
|
|
624
|
+
|
|
625
|
+
### 微信分享(仅 APP)
|
|
626
|
+
|
|
627
|
+
```javascript
|
|
628
|
+
// 分享到微信
|
|
629
|
+
qsh.shareToWeixin({
|
|
630
|
+
type: 'webpage', // 分享类型:text, image, webpage, music, video
|
|
631
|
+
title: '分享标题',
|
|
632
|
+
description: '分享描述',
|
|
633
|
+
webpageUrl: 'https://example.com',
|
|
634
|
+
imageUrl: 'https://example.com/image.jpg',
|
|
635
|
+
success: () => console.log('分享成功'),
|
|
636
|
+
fail: (err) => console.error('分享失败:', err)
|
|
637
|
+
});
|
|
638
|
+
```
|
|
639
|
+
|
|
640
|
+
### 文件选择(仅 APP)
|
|
641
|
+
|
|
642
|
+
```javascript
|
|
643
|
+
// 选择文件
|
|
644
|
+
qsh.chooseFile({
|
|
645
|
+
type: 'all', // 文件类型:all, image, video, audio, file
|
|
646
|
+
count: 1,
|
|
647
|
+
success: (res) => {
|
|
648
|
+
console.log('文件路径:', res.tempFilePaths);
|
|
649
|
+
console.log('文件信息:', res.tempFiles);
|
|
650
|
+
},
|
|
651
|
+
fail: (err) => console.error(err)
|
|
652
|
+
});
|
|
653
|
+
```
|
|
654
|
+
|
|
655
|
+
### 地图选择位置
|
|
656
|
+
|
|
657
|
+
```javascript
|
|
658
|
+
// 打开地图选择位置(小程序/APP)
|
|
659
|
+
qsh.chooseLocation({
|
|
660
|
+
latitude: 39.908823, // 初始纬度
|
|
661
|
+
longitude: 116.397470, // 初始经度
|
|
662
|
+
success: (res) => {
|
|
663
|
+
console.log('选择位置:', res.name);
|
|
664
|
+
console.log('地址:', res.address);
|
|
665
|
+
console.log('纬度:', res.latitude);
|
|
666
|
+
console.log('经度:', res.longitude);
|
|
667
|
+
},
|
|
668
|
+
fail: (err) => console.error(err)
|
|
669
|
+
});
|
|
670
|
+
```
|
|
671
|
+
|
|
672
|
+
### 人脸核身
|
|
673
|
+
|
|
674
|
+
```javascript
|
|
675
|
+
// 人脸核身(小程序/APP)
|
|
676
|
+
qsh.faceVerify({
|
|
677
|
+
name: '姓名',
|
|
678
|
+
idCardNumber: '身份证号',
|
|
679
|
+
title: '人脸核身',
|
|
680
|
+
url: 'https://example.com/face-result'
|
|
681
|
+
});
|
|
682
|
+
```
|
|
683
|
+
|
|
684
|
+
### 打印服务(仅 APP)
|
|
685
|
+
|
|
686
|
+
```javascript
|
|
687
|
+
// 打印文本
|
|
688
|
+
qsh.printText({
|
|
689
|
+
content: '打印内容',
|
|
690
|
+
printerName: '打印机名称',
|
|
691
|
+
success: () => console.log('打印成功'),
|
|
692
|
+
fail: (err) => console.error('打印失败:', err)
|
|
693
|
+
});
|
|
694
|
+
|
|
695
|
+
// 打印图片
|
|
696
|
+
qsh.printImage({
|
|
697
|
+
imagePath: '/path/to/image.jpg',
|
|
698
|
+
printerName: '打印机名称',
|
|
699
|
+
success: () => console.log('打印成功'),
|
|
700
|
+
fail: (err) => console.error('打印失败:', err)
|
|
701
|
+
});
|
|
702
|
+
```
|
|
703
|
+
|
|
704
|
+
## 兼容性
|
|
705
|
+
|
|
706
|
+
- 微信小程序 WebView
|
|
707
|
+
- UniApp APP 端 (Android/iOS)
|
|
708
|
+
- H5+ 环境
|
|
709
|
+
- NVUE 页面
|
|
710
|
+
- UVUE 页面 (UniApp X)
|
|
711
|
+
|
|
712
|
+
## License
|
|
713
|
+
|
|
714
|
+
MIT
|
|
715
|
+
|
|
716
|
+
### CommonJS/Require 使用
|
|
717
|
+
|
|
718
|
+
```javascript
|
|
719
|
+
// 安装:npm install qsh-webview-sdk
|
|
720
|
+
const qsh = require('qsh-webview-sdk');
|
|
721
|
+
|
|
722
|
+
qsh.ready().then(() => {
|
|
723
|
+
qsh.postMessage({ data: { action: 'init' } });
|
|
724
|
+
});
|
|
725
|
+
```
|
|
726
|
+
|
|
727
|
+
### 在框架中使用
|
|
728
|
+
|
|
729
|
+
以下示例均利用 `qsh.ready()` 确保就绪;注意 SDK 自带自动等待,直接调用 API 也可正常执行。
|
|
730
|
+
|
|
731
|
+
1) Vue 3
|
|
732
|
+
|
|
733
|
+
```javascript
|
|
734
|
+
<script setup>
|
|
735
|
+
import { onMounted } from 'vue';
|
|
736
|
+
import qsh from 'qsh-webview-sdk';
|
|
737
|
+
|
|
738
|
+
onMounted(async () => {
|
|
739
|
+
await qsh.ready();
|
|
740
|
+
qsh.navigateTo({ url: '/pages/demo/index' });
|
|
741
|
+
});
|
|
742
|
+
</script>
|
|
743
|
+
```
|
|
744
|
+
|
|
745
|
+
2) React
|
|
746
|
+
|
|
747
|
+
```javascript
|
|
748
|
+
import { useEffect } from 'react';
|
|
749
|
+
import qsh from 'qsh-webview-sdk';
|
|
750
|
+
|
|
751
|
+
export default function App() {
|
|
752
|
+
useEffect(() => {
|
|
753
|
+
let cancelled = false;
|
|
754
|
+
qsh.ready().then(() => {
|
|
755
|
+
if (!cancelled) {
|
|
756
|
+
qsh.postMessage({ data: { action: 'mounted' } });
|
|
757
|
+
}
|
|
758
|
+
});
|
|
759
|
+
return () => { cancelled = true; };
|
|
760
|
+
}, []);
|
|
761
|
+
return null;
|
|
762
|
+
}
|
|
749
763
|
```
|