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