ota-native-bridge 1.7.0 → 1.8.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.
- package/README.md +3 -0
- package/dist/cjs/events.js +24 -0
- package/dist/cjs/index.js +4 -1
- package/dist/cjs/native.js +6 -1
- package/dist/cjs/plugins.js +113 -1
- package/dist/cjs/version.js +1 -1
- package/dist/events.d.ts +9 -0
- package/dist/events.js +21 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/native.d.ts +46 -3
- package/dist/native.js +6 -2
- package/dist/plugins.d.ts +49 -1
- package/dist/plugins.js +113 -1
- package/dist/types.d.ts +122 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/docs/USAGE.md +145 -3
- package/ota-native-bridge.js +62 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -56,6 +56,9 @@ if (Native.isNativePlatform()) {
|
|
|
56
56
|
| 读机型 / 系统 / CPU / Canvas / WebGL | `Device.getEnvironmentInfo()` 一次返回(含系统版本、厂商、型号);也可分别调用 `getInfo()` / `getSystemInfo()` / `getCpuInfo()` / `getCanvasRenderInfo()` / `getWebGlInfo()` |
|
|
57
57
|
| 用资源包里的图换桌面图标 | `Icon.setFromResource({ path: 'icons/app-icon.png' })`(Android 8+;iOS / Harmony 返回失败) |
|
|
58
58
|
| 加密 Client API 请求 | `Crypto.postEncrypted(url, plain, pem)` |
|
|
59
|
+
| 监听安装进度(Android) | `Ota.onInstallEvent(fn)`,返回取消函数 |
|
|
60
|
+
| 新应用是否正在迁移 / 迁移后首启 | `Migration.getStatus()` → `{ inProgress, firstLaunch, phase }`,各平台可直接调用(1.8.1+) |
|
|
61
|
+
| 换包后迁移登录态 / 数据(Android) | 旧应用 `Migration.setExtras({ extras, cookieUrls })`;新应用 `Migration.getInfo()`,详见 [USAGE §5.3.1](./docs/USAGE.md) |
|
|
59
62
|
|
|
60
63
|
## OTA 服务端地址
|
|
61
64
|
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.onNativeEvent = onNativeEvent;
|
|
4
|
+
const NATIVE_EVENT = 'nativebridgeevent';
|
|
5
|
+
function onNativeEvent(name, handler) {
|
|
6
|
+
if (typeof window === 'undefined') {
|
|
7
|
+
return () => undefined;
|
|
8
|
+
}
|
|
9
|
+
const listener = (e) => {
|
|
10
|
+
const detail = e.detail;
|
|
11
|
+
if (!detail || detail.name !== name) {
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
try {
|
|
15
|
+
handler(detail.payload);
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
// 业务回调异常不要影响后续事件
|
|
19
|
+
console.error('[ota-native-bridge] event handler failed', error);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
window.addEventListener(NATIVE_EVENT, listener);
|
|
23
|
+
return () => window.removeEventListener(NATIVE_EVENT, listener);
|
|
24
|
+
}
|
package/dist/cjs/index.js
CHANGED
|
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.Crypto = exports.postEncrypted = exports.encryptRequestBody = exports.isNativeBridgeAvailable = exports.getNativePostMessage = exports.decodeBridgePayload = exports.invokeNative = exports.isNativePlatform = exports.getPlatform = exports.whenReady = exports.Push = exports.Store = exports.Payment = exports.Capture = exports.Recorder = exports.Volume = exports.Brightness = exports.Flashlight = exports.Haptics = exports.Sensor = exports.Nfc = exports.Bluetooth = exports.Calendar = exports.Contacts = exports.Biometric = exports.Scan = exports.Picker = exports.Share = exports.Clipboard = exports.Http = exports.Media = exports.Location = exports.Notification = exports.Viewport = exports.Icon = exports.Theme = exports.Config = exports.Device = exports.Browser = exports.Ota = exports.App = exports.Native = exports.SDK_VERSION = exports.PACKAGE_NAME = void 0;
|
|
17
|
+
exports.Crypto = exports.postEncrypted = exports.encryptRequestBody = exports.isNativeBridgeAvailable = exports.getNativePostMessage = exports.decodeBridgePayload = exports.invokeNative = exports.isNativePlatform = exports.getPlatform = exports.whenReady = exports.onNativeEvent = exports.Push = exports.Store = exports.Payment = exports.Capture = exports.Recorder = exports.Volume = exports.Brightness = exports.Flashlight = exports.Haptics = exports.Sensor = exports.Nfc = exports.Bluetooth = exports.Calendar = exports.Contacts = exports.Biometric = exports.Scan = exports.Picker = exports.Share = exports.Clipboard = exports.Http = exports.Media = exports.Location = exports.Notification = exports.Viewport = exports.Icon = exports.Theme = exports.Config = exports.Device = exports.Browser = exports.Migration = exports.Ota = exports.App = exports.Native = exports.SDK_VERSION = exports.PACKAGE_NAME = void 0;
|
|
18
18
|
__exportStar(require("./types"), exports);
|
|
19
19
|
var version_1 = require("./version");
|
|
20
20
|
Object.defineProperty(exports, "PACKAGE_NAME", { enumerable: true, get: function () { return version_1.PACKAGE_NAME; } });
|
|
@@ -23,6 +23,7 @@ var native_1 = require("./native");
|
|
|
23
23
|
Object.defineProperty(exports, "Native", { enumerable: true, get: function () { return native_1.Native; } });
|
|
24
24
|
Object.defineProperty(exports, "App", { enumerable: true, get: function () { return native_1.App; } });
|
|
25
25
|
Object.defineProperty(exports, "Ota", { enumerable: true, get: function () { return native_1.Ota; } });
|
|
26
|
+
Object.defineProperty(exports, "Migration", { enumerable: true, get: function () { return native_1.Migration; } });
|
|
26
27
|
Object.defineProperty(exports, "Browser", { enumerable: true, get: function () { return native_1.Browser; } });
|
|
27
28
|
Object.defineProperty(exports, "Device", { enumerable: true, get: function () { return native_1.Device; } });
|
|
28
29
|
Object.defineProperty(exports, "Config", { enumerable: true, get: function () { return native_1.Config; } });
|
|
@@ -52,6 +53,8 @@ Object.defineProperty(exports, "Capture", { enumerable: true, get: function () {
|
|
|
52
53
|
Object.defineProperty(exports, "Payment", { enumerable: true, get: function () { return native_1.Payment; } });
|
|
53
54
|
Object.defineProperty(exports, "Store", { enumerable: true, get: function () { return native_1.Store; } });
|
|
54
55
|
Object.defineProperty(exports, "Push", { enumerable: true, get: function () { return native_1.Push; } });
|
|
56
|
+
var events_1 = require("./events");
|
|
57
|
+
Object.defineProperty(exports, "onNativeEvent", { enumerable: true, get: function () { return events_1.onNativeEvent; } });
|
|
55
58
|
var runtime_1 = require("./runtime");
|
|
56
59
|
Object.defineProperty(exports, "whenReady", { enumerable: true, get: function () { return runtime_1.whenReady; } });
|
|
57
60
|
Object.defineProperty(exports, "getPlatform", { enumerable: true, get: function () { return runtime_1.getPlatform; } });
|
package/dist/cjs/native.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Crypto = exports.Volume = exports.Viewport = exports.Theme = exports.Store = exports.Share = exports.Sensor = exports.Scan = exports.Recorder = exports.Push = exports.Picker = exports.Payment = exports.Ota = exports.Notification = exports.Nfc = exports.Media = exports.Location = exports.Icon = exports.Http = exports.Haptics = exports.Flashlight = exports.Device = exports.Contacts = exports.Config = exports.Clipboard = exports.Capture = exports.Calendar = exports.Browser = exports.Brightness = exports.Bluetooth = exports.Biometric = exports.App = exports.Native = void 0;
|
|
3
|
+
exports.Crypto = exports.Volume = exports.Viewport = exports.Theme = exports.Store = exports.Share = exports.Sensor = exports.Scan = exports.Recorder = exports.Push = exports.Picker = exports.Payment = exports.Ota = exports.Notification = exports.Nfc = exports.Migration = exports.Media = exports.Location = exports.Icon = exports.Http = exports.Haptics = exports.Flashlight = exports.Device = exports.Contacts = exports.Config = exports.Clipboard = exports.Capture = exports.Calendar = exports.Browser = exports.Brightness = exports.Bluetooth = exports.Biometric = exports.App = exports.Native = void 0;
|
|
4
4
|
const crypto_ns_1 = require("./crypto-ns");
|
|
5
5
|
Object.defineProperty(exports, "Crypto", { enumerable: true, get: function () { return crypto_ns_1.Crypto; } });
|
|
6
6
|
const plugins_1 = require("./plugins");
|
|
@@ -21,6 +21,7 @@ Object.defineProperty(exports, "Http", { enumerable: true, get: function () { re
|
|
|
21
21
|
Object.defineProperty(exports, "Icon", { enumerable: true, get: function () { return plugins_1.Icon; } });
|
|
22
22
|
Object.defineProperty(exports, "Location", { enumerable: true, get: function () { return plugins_1.Location; } });
|
|
23
23
|
Object.defineProperty(exports, "Media", { enumerable: true, get: function () { return plugins_1.Media; } });
|
|
24
|
+
Object.defineProperty(exports, "Migration", { enumerable: true, get: function () { return plugins_1.Migration; } });
|
|
24
25
|
Object.defineProperty(exports, "Nfc", { enumerable: true, get: function () { return plugins_1.Nfc; } });
|
|
25
26
|
Object.defineProperty(exports, "Notification", { enumerable: true, get: function () { return plugins_1.Notification; } });
|
|
26
27
|
Object.defineProperty(exports, "Ota", { enumerable: true, get: function () { return plugins_1.Ota; } });
|
|
@@ -35,11 +36,13 @@ Object.defineProperty(exports, "Store", { enumerable: true, get: function () { r
|
|
|
35
36
|
Object.defineProperty(exports, "Theme", { enumerable: true, get: function () { return plugins_1.Theme; } });
|
|
36
37
|
Object.defineProperty(exports, "Viewport", { enumerable: true, get: function () { return plugins_1.Viewport; } });
|
|
37
38
|
Object.defineProperty(exports, "Volume", { enumerable: true, get: function () { return plugins_1.Volume; } });
|
|
39
|
+
const events_1 = require("./events");
|
|
38
40
|
const runtime_1 = require("./runtime");
|
|
39
41
|
const version_1 = require("./version");
|
|
40
42
|
const plugins = {
|
|
41
43
|
App: plugins_1.App,
|
|
42
44
|
Ota: plugins_1.Ota,
|
|
45
|
+
Migration: plugins_1.Migration,
|
|
43
46
|
Browser: plugins_1.Browser,
|
|
44
47
|
Device: plugins_1.Device,
|
|
45
48
|
Config: plugins_1.Config,
|
|
@@ -77,6 +80,8 @@ exports.Native = {
|
|
|
77
80
|
isNativePlatform: runtime_1.isNativePlatform,
|
|
78
81
|
isNativeBridgeAvailable: runtime_1.isNativeBridgeAvailable,
|
|
79
82
|
whenReady: runtime_1.whenReady,
|
|
83
|
+
/** 订阅壳推送的事件:`Native.on('ota.install', fn)` / `Native.on('migration', fn)`,返回取消订阅函数 */
|
|
84
|
+
on: events_1.onNativeEvent,
|
|
80
85
|
Plugins: plugins,
|
|
81
86
|
...plugins,
|
|
82
87
|
/** H5 自行请求 Client API 时加密请求体(平台公钥来自 App.getInfo().platform.paramPublicKey) */
|
package/dist/cjs/plugins.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Push = exports.Store = exports.Payment = exports.Capture = exports.Recorder = exports.Volume = exports.Brightness = exports.Flashlight = exports.Haptics = exports.Sensor = exports.Nfc = exports.Bluetooth = exports.Calendar = exports.Contacts = exports.Biometric = exports.Scan = exports.Picker = exports.Share = exports.Clipboard = exports.Http = exports.Media = exports.Location = exports.Browser = exports.Ota = exports.Notification = exports.Viewport = exports.Icon = exports.Theme = exports.Config = exports.Device = exports.App = void 0;
|
|
3
|
+
exports.Push = exports.Store = exports.Payment = exports.Capture = exports.Recorder = exports.Volume = exports.Brightness = exports.Flashlight = exports.Haptics = exports.Sensor = exports.Nfc = exports.Bluetooth = exports.Calendar = exports.Contacts = exports.Biometric = exports.Scan = exports.Picker = exports.Share = exports.Clipboard = exports.Http = exports.Media = exports.Location = exports.Browser = exports.Migration = exports.Ota = exports.Notification = exports.Viewport = exports.Icon = exports.Theme = exports.Config = exports.Device = exports.App = void 0;
|
|
4
|
+
const events_1 = require("./events");
|
|
4
5
|
const runtime_1 = require("./runtime");
|
|
5
6
|
const webview_signals_1 = require("./webview-signals");
|
|
6
7
|
function toEnvironmentDevice(device, system) {
|
|
@@ -223,6 +224,117 @@ exports.Ota = {
|
|
|
223
224
|
downloadAndInstallApk(url, callback) {
|
|
224
225
|
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('downloadAndInstallApk', { url }), callback);
|
|
225
226
|
},
|
|
227
|
+
/**
|
|
228
|
+
* 订阅安装包下载与安装进度(Android)。原生更新弹窗和 `installAppPackage` 都会触发。
|
|
229
|
+
* 返回取消订阅函数。
|
|
230
|
+
*/
|
|
231
|
+
onInstallEvent(handler) {
|
|
232
|
+
return (0, events_1.onNativeEvent)('ota.install', handler);
|
|
233
|
+
},
|
|
234
|
+
/** 换包安装的当前阶段(Android)。页面刷新后用它恢复界面状态。 */
|
|
235
|
+
getInstallState(callback) {
|
|
236
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('getAppInstallState'), callback);
|
|
237
|
+
},
|
|
238
|
+
};
|
|
239
|
+
/** 等待 Bridge 注入的时间。壳里 Bridge 在页面脚本之前就在了;超时说明在普通浏览器 */
|
|
240
|
+
const MIGRATION_STATUS_READY_TIMEOUT_MS = 1500;
|
|
241
|
+
/** 不支持迁移的平台上 `Migration.getStatus()` 的结果 */
|
|
242
|
+
const MIGRATION_UNSUPPORTED_STATUS = {
|
|
243
|
+
supported: false,
|
|
244
|
+
phase: 'none',
|
|
245
|
+
inProgress: false,
|
|
246
|
+
firstLaunch: false,
|
|
247
|
+
imported: false,
|
|
248
|
+
webApplied: false,
|
|
249
|
+
fromPackage: '',
|
|
250
|
+
importedAt: 0,
|
|
251
|
+
lastError: '',
|
|
252
|
+
};
|
|
253
|
+
/**
|
|
254
|
+
* 短时间等 Bridge 注入。不复用 `whenReady`:它的等待是全局共享的,
|
|
255
|
+
* 这里的短超时会连带让别处正在等待的调用提前失败。
|
|
256
|
+
*/
|
|
257
|
+
function waitForBridgeBriefly(timeoutMs) {
|
|
258
|
+
if ((0, runtime_1.isNativeBridgeAvailable)()) {
|
|
259
|
+
return Promise.resolve();
|
|
260
|
+
}
|
|
261
|
+
if (typeof document === 'undefined') {
|
|
262
|
+
return Promise.reject(new Error('Not in browser'));
|
|
263
|
+
}
|
|
264
|
+
return new Promise((resolve, reject) => {
|
|
265
|
+
const onReady = () => {
|
|
266
|
+
clearTimeout(timer);
|
|
267
|
+
resolve();
|
|
268
|
+
};
|
|
269
|
+
const timer = setTimeout(() => {
|
|
270
|
+
document.removeEventListener('nativebridgeready', onReady);
|
|
271
|
+
reject(new Error('Bridge timeout'));
|
|
272
|
+
}, timeoutMs);
|
|
273
|
+
document.addEventListener('nativebridgeready', onReady, { once: true });
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* 判断错误是不是"这里没有迁移能力":没有 Bridge(普通浏览器),
|
|
278
|
+
* 或壳没实现该方法(iOS / Harmony / 桌面端 / 旧版 Android 壳)。
|
|
279
|
+
*/
|
|
280
|
+
function isMigrationUnsupported(error) {
|
|
281
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
282
|
+
return /Unknown method|桌面壳未实现|Native bridge not available|Bridge timeout|Not in browser/i.test(message);
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* 换包更新的数据迁移(Android)。新安装包的包名与当前应用不同时,
|
|
286
|
+
* 装好后当前应用会自动拉起新应用,把登录状态(Cookie / localStorage)、
|
|
287
|
+
* 设备序列号和 `setExtras` 的数据交给它,完成后请求卸载当前应用。
|
|
288
|
+
*/
|
|
289
|
+
exports.Migration = {
|
|
290
|
+
/**
|
|
291
|
+
* 当前是否正在迁移,以及是不是迁移后的第一次启动。用来决定要不要等待、跳过登录或引导页。
|
|
292
|
+
*
|
|
293
|
+
* 任何平台都可以直接调用,不用先判断平台:不是 Android 壳(或壳版本不带迁移)时,
|
|
294
|
+
* 返回 `{ supported: false, inProgress: false, firstLaunch: false, phase: 'none', ... }`。
|
|
295
|
+
* 其它错误照常抛出。
|
|
296
|
+
*/
|
|
297
|
+
getStatus(callback) {
|
|
298
|
+
// 普通浏览器里没有 Bridge:很快就判定为不支持,不要卡满默认的 8 秒
|
|
299
|
+
const promise = waitForBridgeBriefly(MIGRATION_STATUS_READY_TIMEOUT_MS)
|
|
300
|
+
.then(() => (0, runtime_1.callNative)('getMigrationStatus'))
|
|
301
|
+
.catch((error) => {
|
|
302
|
+
if (isMigrationUnsupported(error)) {
|
|
303
|
+
return { ...MIGRATION_UNSUPPORTED_STATUS };
|
|
304
|
+
}
|
|
305
|
+
throw error;
|
|
306
|
+
});
|
|
307
|
+
return (0, runtime_1.withCallback)(promise, callback);
|
|
308
|
+
},
|
|
309
|
+
/**
|
|
310
|
+
* 迁移前补充要带走的数据。建议在登录后和每次登录态变化时调用。
|
|
311
|
+
* 新应用通过 `Migration.getInfo().extras` 读取。
|
|
312
|
+
*/
|
|
313
|
+
setExtras(options, callback) {
|
|
314
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('setMigrationExtras', {
|
|
315
|
+
extras: options.extras ?? {},
|
|
316
|
+
cookieUrls: options.cookieUrls ?? [],
|
|
317
|
+
}), callback);
|
|
318
|
+
},
|
|
319
|
+
/** 新应用已装好但没自动打开时(后台启动被系统拦截等),由用户点按钮手动继续 */
|
|
320
|
+
continue(callback) {
|
|
321
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('continueAppMigration'), callback);
|
|
322
|
+
},
|
|
323
|
+
/** 新应用里读取迁移结果 */
|
|
324
|
+
getInfo(callback) {
|
|
325
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('getMigrationInfo'), callback);
|
|
326
|
+
},
|
|
327
|
+
/**
|
|
328
|
+
* 请求卸载:`self` 卸载当前应用(需迁移已完成),`source` 在新应用里卸载被迁移的旧应用。
|
|
329
|
+
* 迁移完成后原生会自动请求一次;用户在系统确认框里取消后,可用它再次发起。
|
|
330
|
+
*/
|
|
331
|
+
requestUninstall(target = 'self', callback) {
|
|
332
|
+
return (0, runtime_1.withCallback)((0, runtime_1.invokeNative)('requestUninstall', { target }), callback);
|
|
333
|
+
},
|
|
334
|
+
/** 订阅迁移事件。返回取消订阅函数。 */
|
|
335
|
+
onEvent(handler) {
|
|
336
|
+
return (0, events_1.onNativeEvent)('migration', handler);
|
|
337
|
+
},
|
|
226
338
|
};
|
|
227
339
|
exports.Browser = {
|
|
228
340
|
/** 打开系统浏览器。可直接传 URL 字符串:`Browser.open('https://example.com')` */
|
package/dist/cjs/version.js
CHANGED
package/dist/events.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { NativeEventMap } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* 订阅壳推送的事件(Android:`ota.install` 下载与安装、`migration` 换包数据迁移)。
|
|
4
|
+
* 返回取消订阅函数。非浏览器环境返回空函数。
|
|
5
|
+
*
|
|
6
|
+
* 页面没加载好时发出的事件不会补发;启动时请用 `Ota.getInstallState()` / `Migration.getInfo()` 读取当前状态。
|
|
7
|
+
*/
|
|
8
|
+
export declare function onNativeEvent<K extends keyof NativeEventMap>(name: K, handler: (event: NativeEventMap[K]) => void): () => void;
|
|
9
|
+
export declare function onNativeEvent(name: string, handler: (event: unknown) => void): () => void;
|
package/dist/events.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const NATIVE_EVENT = 'nativebridgeevent';
|
|
2
|
+
export function onNativeEvent(name, handler) {
|
|
3
|
+
if (typeof window === 'undefined') {
|
|
4
|
+
return () => undefined;
|
|
5
|
+
}
|
|
6
|
+
const listener = (e) => {
|
|
7
|
+
const detail = e.detail;
|
|
8
|
+
if (!detail || detail.name !== name) {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
try {
|
|
12
|
+
handler(detail.payload);
|
|
13
|
+
}
|
|
14
|
+
catch (error) {
|
|
15
|
+
// 业务回调异常不要影响后续事件
|
|
16
|
+
console.error('[ota-native-bridge] event handler failed', error);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
window.addEventListener(NATIVE_EVENT, listener);
|
|
20
|
+
return () => window.removeEventListener(NATIVE_EVENT, listener);
|
|
21
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export * from './types';
|
|
2
2
|
export { PACKAGE_NAME, SDK_VERSION } from './version';
|
|
3
|
-
export { Native, App, Ota, Browser, Device, Config, Theme, Icon, Viewport, Notification, Location, Media, Http, Clipboard, Share, Picker, Scan, Biometric, Contacts, Calendar, Bluetooth, Nfc, Sensor, Haptics, Flashlight, Brightness, Volume, Recorder, Capture, Payment, Store, Push, } from './native';
|
|
3
|
+
export { Native, App, Ota, Migration, Browser, Device, Config, Theme, Icon, Viewport, Notification, Location, Media, Http, Clipboard, Share, Picker, Scan, Biometric, Contacts, Calendar, Bluetooth, Nfc, Sensor, Haptics, Flashlight, Brightness, Volume, Recorder, Capture, Payment, Store, Push, } from './native';
|
|
4
|
+
export { onNativeEvent } from './events';
|
|
4
5
|
export type { NativeCallback, NativePlatform } from './runtime';
|
|
5
6
|
export { whenReady, getPlatform, isNativePlatform, invokeNative, decodeBridgePayload } from './runtime';
|
|
6
7
|
export { getNativePostMessage, isNativeBridgeAvailable, type NativePostMessage, } from './native-messenger';
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export * from './types';
|
|
2
2
|
export { PACKAGE_NAME, SDK_VERSION } from './version';
|
|
3
|
-
export { Native, App, Ota, Browser, Device, Config, Theme, Icon, Viewport, Notification, Location, Media, Http, Clipboard, Share, Picker, Scan, Biometric, Contacts, Calendar, Bluetooth, Nfc, Sensor, Haptics, Flashlight, Brightness, Volume, Recorder, Capture, Payment, Store, Push, } from './native';
|
|
3
|
+
export { Native, App, Ota, Migration, Browser, Device, Config, Theme, Icon, Viewport, Notification, Location, Media, Http, Clipboard, Share, Picker, Scan, Biometric, Contacts, Calendar, Bluetooth, Nfc, Sensor, Haptics, Flashlight, Brightness, Volume, Recorder, Capture, Payment, Store, Push, } from './native';
|
|
4
|
+
export { onNativeEvent } from './events';
|
|
4
5
|
export { whenReady, getPlatform, isNativePlatform, invokeNative, decodeBridgePayload } from './runtime';
|
|
5
6
|
export { getNativePostMessage, isNativeBridgeAvailable, } from './native-messenger';
|
|
6
7
|
export { encryptRequestBody, postEncrypted, } from './crypto';
|
package/dist/native.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Crypto } from './crypto-ns';
|
|
2
|
-
import { App, Biometric, Bluetooth, Brightness, Browser, Calendar, Capture, Clipboard, Config, Contacts, Device, Flashlight, Haptics, Http, Icon, Location, Media, Nfc, Notification, Ota, Payment, Picker, Push, Recorder, Scan, Sensor, Share, Store, Theme, Viewport, Volume } from './plugins';
|
|
2
|
+
import { App, Biometric, Bluetooth, Brightness, Browser, Calendar, Capture, Clipboard, Config, Contacts, Device, Flashlight, Haptics, Http, Icon, Location, Media, Migration, Nfc, Notification, Ota, Payment, Picker, Push, Recorder, Scan, Sensor, Share, Store, Theme, Viewport, Volume } from './plugins';
|
|
3
|
+
import { onNativeEvent } from './events';
|
|
3
4
|
import { getPlatform, isNativeBridgeAvailable, isNativePlatform, whenReady } from './runtime';
|
|
4
5
|
/** 插件式 Native SDK:直接调用即可,首次会自动等待壳注入完成 */
|
|
5
6
|
export declare const Native: {
|
|
@@ -49,6 +50,26 @@ export declare const Native: {
|
|
|
49
50
|
channel: string;
|
|
50
51
|
}>;
|
|
51
52
|
downloadAndInstallApk(url: string, callback?: import("./runtime").NativeCallback<import("./types").InstallAppPackageResult>): Promise<import("./types").InstallAppPackageResult>;
|
|
53
|
+
onInstallEvent(handler: (event: import("./types").OtaInstallEvent) => void): () => void;
|
|
54
|
+
getInstallState(callback?: import("./runtime").NativeCallback<import("./types").AppInstallState>): Promise<import("./types").AppInstallState>;
|
|
55
|
+
};
|
|
56
|
+
readonly Migration: {
|
|
57
|
+
getStatus(callback?: import("./runtime").NativeCallback<import("./types").MigrationStatus>): Promise<import("./types").MigrationStatus | {
|
|
58
|
+
supported: boolean;
|
|
59
|
+
phase: import("./types").MigrationPhase;
|
|
60
|
+
inProgress: boolean;
|
|
61
|
+
firstLaunch: boolean;
|
|
62
|
+
imported: boolean;
|
|
63
|
+
webApplied: boolean;
|
|
64
|
+
fromPackage: string;
|
|
65
|
+
importedAt: number;
|
|
66
|
+
lastError: string;
|
|
67
|
+
}>;
|
|
68
|
+
setExtras(options: import("./types").MigrationExtrasOptions, callback?: import("./runtime").NativeCallback<import("./types").NativeOk>): Promise<import("./types").NativeOk>;
|
|
69
|
+
continue(callback?: import("./runtime").NativeCallback<import("./types").AppInstallState>): Promise<import("./types").AppInstallState>;
|
|
70
|
+
getInfo(callback?: import("./runtime").NativeCallback<import("./types").MigrationInfo>): Promise<import("./types").MigrationInfo>;
|
|
71
|
+
requestUninstall(target?: import("./types").MigrationUninstallTarget, callback?: import("./runtime").NativeCallback<import("./types").MigrationUninstallResult>): Promise<import("./types").MigrationUninstallResult>;
|
|
72
|
+
onEvent(handler: (event: import("./types").MigrationEvent) => void): () => void;
|
|
52
73
|
};
|
|
53
74
|
readonly Browser: {
|
|
54
75
|
open(urlOrOptions: string | import("./types").OpenInBrowserOptions, callback?: import("./runtime").NativeCallback<import("./types").OpenInBrowserResult>): Promise<import("./types").OpenInBrowserResult>;
|
|
@@ -225,11 +246,13 @@ export declare const Native: {
|
|
|
225
246
|
readonly Push: {
|
|
226
247
|
getToken(callback?: import("./runtime").NativeCallback<import("./types").PushTokenResult>): Promise<import("./types").PushTokenResult>;
|
|
227
248
|
};
|
|
228
|
-
readonly sdkVersion: "1.
|
|
249
|
+
readonly sdkVersion: "1.8.1";
|
|
229
250
|
readonly getPlatform: typeof getPlatform;
|
|
230
251
|
readonly isNativePlatform: typeof isNativePlatform;
|
|
231
252
|
readonly isNativeBridgeAvailable: typeof isNativeBridgeAvailable;
|
|
232
253
|
readonly whenReady: typeof whenReady;
|
|
254
|
+
/** 订阅壳推送的事件:`Native.on('ota.install', fn)` / `Native.on('migration', fn)`,返回取消订阅函数 */
|
|
255
|
+
readonly on: typeof onNativeEvent;
|
|
233
256
|
readonly Plugins: {
|
|
234
257
|
readonly App: {
|
|
235
258
|
ping(callback?: import("./runtime").NativeCallback<{
|
|
@@ -272,6 +295,26 @@ export declare const Native: {
|
|
|
272
295
|
channel: string;
|
|
273
296
|
}>;
|
|
274
297
|
downloadAndInstallApk(url: string, callback?: import("./runtime").NativeCallback<import("./types").InstallAppPackageResult>): Promise<import("./types").InstallAppPackageResult>;
|
|
298
|
+
onInstallEvent(handler: (event: import("./types").OtaInstallEvent) => void): () => void;
|
|
299
|
+
getInstallState(callback?: import("./runtime").NativeCallback<import("./types").AppInstallState>): Promise<import("./types").AppInstallState>;
|
|
300
|
+
};
|
|
301
|
+
readonly Migration: {
|
|
302
|
+
getStatus(callback?: import("./runtime").NativeCallback<import("./types").MigrationStatus>): Promise<import("./types").MigrationStatus | {
|
|
303
|
+
supported: boolean;
|
|
304
|
+
phase: import("./types").MigrationPhase;
|
|
305
|
+
inProgress: boolean;
|
|
306
|
+
firstLaunch: boolean;
|
|
307
|
+
imported: boolean;
|
|
308
|
+
webApplied: boolean;
|
|
309
|
+
fromPackage: string;
|
|
310
|
+
importedAt: number;
|
|
311
|
+
lastError: string;
|
|
312
|
+
}>;
|
|
313
|
+
setExtras(options: import("./types").MigrationExtrasOptions, callback?: import("./runtime").NativeCallback<import("./types").NativeOk>): Promise<import("./types").NativeOk>;
|
|
314
|
+
continue(callback?: import("./runtime").NativeCallback<import("./types").AppInstallState>): Promise<import("./types").AppInstallState>;
|
|
315
|
+
getInfo(callback?: import("./runtime").NativeCallback<import("./types").MigrationInfo>): Promise<import("./types").MigrationInfo>;
|
|
316
|
+
requestUninstall(target?: import("./types").MigrationUninstallTarget, callback?: import("./runtime").NativeCallback<import("./types").MigrationUninstallResult>): Promise<import("./types").MigrationUninstallResult>;
|
|
317
|
+
onEvent(handler: (event: import("./types").MigrationEvent) => void): () => void;
|
|
275
318
|
};
|
|
276
319
|
readonly Browser: {
|
|
277
320
|
open(urlOrOptions: string | import("./types").OpenInBrowserOptions, callback?: import("./runtime").NativeCallback<import("./types").OpenInBrowserResult>): Promise<import("./types").OpenInBrowserResult>;
|
|
@@ -450,5 +493,5 @@ export declare const Native: {
|
|
|
450
493
|
};
|
|
451
494
|
};
|
|
452
495
|
};
|
|
453
|
-
export { App, Biometric, Bluetooth, Brightness, Browser, Calendar, Capture, Clipboard, Config, Contacts, Device, Flashlight, Haptics, Http, Icon, Location, Media, Nfc, Notification, Ota, Payment, Picker, Push, Recorder, Scan, Sensor, Share, Store, Theme, Viewport, Volume, };
|
|
496
|
+
export { App, Biometric, Bluetooth, Brightness, Browser, Calendar, Capture, Clipboard, Config, Contacts, Device, Flashlight, Haptics, Http, Icon, Location, Media, Migration, Nfc, Notification, Ota, Payment, Picker, Push, Recorder, Scan, Sensor, Share, Store, Theme, Viewport, Volume, };
|
|
454
497
|
export { Crypto };
|
package/dist/native.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { Crypto } from './crypto-ns';
|
|
2
|
-
import { App, Biometric, Bluetooth, Brightness, Browser, Calendar, Capture, Clipboard, Config, Contacts, Device, Flashlight, Haptics, Http, Icon, Location, Media, Nfc, Notification, Ota, Payment, Picker, Push, Recorder, Scan, Sensor, Share, Store, Theme, Viewport, Volume, } from './plugins';
|
|
2
|
+
import { App, Biometric, Bluetooth, Brightness, Browser, Calendar, Capture, Clipboard, Config, Contacts, Device, Flashlight, Haptics, Http, Icon, Location, Media, Migration, Nfc, Notification, Ota, Payment, Picker, Push, Recorder, Scan, Sensor, Share, Store, Theme, Viewport, Volume, } from './plugins';
|
|
3
|
+
import { onNativeEvent } from './events';
|
|
3
4
|
import { getPlatform, isNativeBridgeAvailable, isNativePlatform, whenReady } from './runtime';
|
|
4
5
|
import { SDK_VERSION } from './version';
|
|
5
6
|
const plugins = {
|
|
6
7
|
App,
|
|
7
8
|
Ota,
|
|
9
|
+
Migration,
|
|
8
10
|
Browser,
|
|
9
11
|
Device,
|
|
10
12
|
Config,
|
|
@@ -42,6 +44,8 @@ export const Native = {
|
|
|
42
44
|
isNativePlatform,
|
|
43
45
|
isNativeBridgeAvailable,
|
|
44
46
|
whenReady,
|
|
47
|
+
/** 订阅壳推送的事件:`Native.on('ota.install', fn)` / `Native.on('migration', fn)`,返回取消订阅函数 */
|
|
48
|
+
on: onNativeEvent,
|
|
45
49
|
Plugins: plugins,
|
|
46
50
|
...plugins,
|
|
47
51
|
/** H5 自行请求 Client API 时加密请求体(平台公钥来自 App.getInfo().platform.paramPublicKey) */
|
|
@@ -50,5 +54,5 @@ export const Native = {
|
|
|
50
54
|
if (typeof window !== 'undefined') {
|
|
51
55
|
window.Native = Native;
|
|
52
56
|
}
|
|
53
|
-
export { App, Biometric, Bluetooth, Brightness, Browser, Calendar, Capture, Clipboard, Config, Contacts, Device, Flashlight, Haptics, Http, Icon, Location, Media, Nfc, Notification, Ota, Payment, Picker, Push, Recorder, Scan, Sensor, Share, Store, Theme, Viewport, Volume, };
|
|
57
|
+
export { App, Biometric, Bluetooth, Brightness, Browser, Calendar, Capture, Clipboard, Config, Contacts, Device, Flashlight, Haptics, Http, Icon, Location, Media, Migration, Nfc, Notification, Ota, Payment, Picker, Push, Recorder, Scan, Sensor, Share, Store, Theme, Viewport, Volume, };
|
|
54
58
|
export { Crypto };
|
package/dist/plugins.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type NativeCallback } from './runtime';
|
|
2
|
-
import type { AppChannelCodes, AppIconState, AppIconsResult, AppInfo, AppInstalledResult, AppUpdateTriggerResult, ApkUpdateResult, CheckAppUpdateOptions, CheckAppsInstalledResult, DeviceIdentifiers, InstallDeviceId, DeviceInfo, CpuInfo, CanvasRenderInfo, WebGlInfo, EnvironmentInfo, EnvironmentDeviceInfo, PhoneStatePermissionResult, InstallAppPackageOptions, InstallAppPackageResult, InstallResourcePackageOptions, InstallResourcePackageResult, LaunchAppOptions, LaunchAppResult, LocationPermissionResult, LocationResult, LocationStatus, LocationWatchResult, LockStatus, NativeFeatureConfig, NotificationOptions, OpenInBrowserOptions, OpenInBrowserResult, OrientationInfo, SetFullscreenOptions, SetFullscreenResult, SetOrientationOptions, SetOrientationResult, PlayVideoOptions, PlayVideoResult, HttpRequestOptions, HttpRequestResult, ResourceUpdateResult, SafeAreaInsets, ScreenInfo, SetAppIconFromResourceOptions, SetAppIconOptions, SetAppIconResult, SetAppThemeOptions, SetNativeFeaturesOptions, SystemInfo, ThemeConfig, VideoPlayerStatus, WindowInfo, AdapterState, BiometricStatus, BrightnessState, CalendarEventOptions, CaptureResult, ClipboardResult, ContactResult, FlashlightState, MotionReading, NativeOk, PaymentOptions, PaymentResult, PedometerResult, PickerResult, PushTokenResult, RecorderResult, ReviewResult, ScanResult, ShareOptions, VolumeState } from './types';
|
|
2
|
+
import type { AppInstallState, MigrationEvent, MigrationExtrasOptions, MigrationInfo, MigrationStatus, MigrationUninstallResult, MigrationUninstallTarget, OtaInstallEvent, AppChannelCodes, AppIconState, AppIconsResult, AppInfo, AppInstalledResult, AppUpdateTriggerResult, ApkUpdateResult, CheckAppUpdateOptions, CheckAppsInstalledResult, DeviceIdentifiers, InstallDeviceId, DeviceInfo, CpuInfo, CanvasRenderInfo, WebGlInfo, EnvironmentInfo, EnvironmentDeviceInfo, PhoneStatePermissionResult, InstallAppPackageOptions, InstallAppPackageResult, InstallResourcePackageOptions, InstallResourcePackageResult, LaunchAppOptions, LaunchAppResult, LocationPermissionResult, LocationResult, LocationStatus, LocationWatchResult, LockStatus, NativeFeatureConfig, NotificationOptions, OpenInBrowserOptions, OpenInBrowserResult, OrientationInfo, SetFullscreenOptions, SetFullscreenResult, SetOrientationOptions, SetOrientationResult, PlayVideoOptions, PlayVideoResult, HttpRequestOptions, HttpRequestResult, ResourceUpdateResult, SafeAreaInsets, ScreenInfo, SetAppIconFromResourceOptions, SetAppIconOptions, SetAppIconResult, SetAppThemeOptions, SetNativeFeaturesOptions, SystemInfo, ThemeConfig, VideoPlayerStatus, WindowInfo, AdapterState, BiometricStatus, BrightnessState, CalendarEventOptions, CaptureResult, ClipboardResult, ContactResult, FlashlightState, MotionReading, NativeOk, PaymentOptions, PaymentResult, PedometerResult, PickerResult, PushTokenResult, RecorderResult, ReviewResult, ScanResult, ShareOptions, VolumeState } from './types';
|
|
3
3
|
export declare const App: {
|
|
4
4
|
ping(callback?: NativeCallback<{
|
|
5
5
|
pong: boolean;
|
|
@@ -118,6 +118,54 @@ export declare const Ota: {
|
|
|
118
118
|
}>;
|
|
119
119
|
/** 等同 `installAppPackage({ url })`,兼容旧方法名 */
|
|
120
120
|
downloadAndInstallApk(url: string, callback?: NativeCallback<InstallAppPackageResult>): Promise<InstallAppPackageResult>;
|
|
121
|
+
/**
|
|
122
|
+
* 订阅安装包下载与安装进度(Android)。原生更新弹窗和 `installAppPackage` 都会触发。
|
|
123
|
+
* 返回取消订阅函数。
|
|
124
|
+
*/
|
|
125
|
+
onInstallEvent(handler: (event: OtaInstallEvent) => void): () => void;
|
|
126
|
+
/** 换包安装的当前阶段(Android)。页面刷新后用它恢复界面状态。 */
|
|
127
|
+
getInstallState(callback?: NativeCallback<AppInstallState>): Promise<AppInstallState>;
|
|
128
|
+
};
|
|
129
|
+
/**
|
|
130
|
+
* 换包更新的数据迁移(Android)。新安装包的包名与当前应用不同时,
|
|
131
|
+
* 装好后当前应用会自动拉起新应用,把登录状态(Cookie / localStorage)、
|
|
132
|
+
* 设备序列号和 `setExtras` 的数据交给它,完成后请求卸载当前应用。
|
|
133
|
+
*/
|
|
134
|
+
export declare const Migration: {
|
|
135
|
+
/**
|
|
136
|
+
* 当前是否正在迁移,以及是不是迁移后的第一次启动。用来决定要不要等待、跳过登录或引导页。
|
|
137
|
+
*
|
|
138
|
+
* 任何平台都可以直接调用,不用先判断平台:不是 Android 壳(或壳版本不带迁移)时,
|
|
139
|
+
* 返回 `{ supported: false, inProgress: false, firstLaunch: false, phase: 'none', ... }`。
|
|
140
|
+
* 其它错误照常抛出。
|
|
141
|
+
*/
|
|
142
|
+
getStatus(callback?: NativeCallback<MigrationStatus>): Promise<MigrationStatus | {
|
|
143
|
+
supported: boolean;
|
|
144
|
+
phase: import("./types").MigrationPhase;
|
|
145
|
+
inProgress: boolean;
|
|
146
|
+
firstLaunch: boolean;
|
|
147
|
+
imported: boolean;
|
|
148
|
+
webApplied: boolean;
|
|
149
|
+
fromPackage: string;
|
|
150
|
+
importedAt: number;
|
|
151
|
+
lastError: string;
|
|
152
|
+
}>;
|
|
153
|
+
/**
|
|
154
|
+
* 迁移前补充要带走的数据。建议在登录后和每次登录态变化时调用。
|
|
155
|
+
* 新应用通过 `Migration.getInfo().extras` 读取。
|
|
156
|
+
*/
|
|
157
|
+
setExtras(options: MigrationExtrasOptions, callback?: NativeCallback<NativeOk>): Promise<NativeOk>;
|
|
158
|
+
/** 新应用已装好但没自动打开时(后台启动被系统拦截等),由用户点按钮手动继续 */
|
|
159
|
+
continue(callback?: NativeCallback<AppInstallState>): Promise<AppInstallState>;
|
|
160
|
+
/** 新应用里读取迁移结果 */
|
|
161
|
+
getInfo(callback?: NativeCallback<MigrationInfo>): Promise<MigrationInfo>;
|
|
162
|
+
/**
|
|
163
|
+
* 请求卸载:`self` 卸载当前应用(需迁移已完成),`source` 在新应用里卸载被迁移的旧应用。
|
|
164
|
+
* 迁移完成后原生会自动请求一次;用户在系统确认框里取消后,可用它再次发起。
|
|
165
|
+
*/
|
|
166
|
+
requestUninstall(target?: MigrationUninstallTarget, callback?: NativeCallback<MigrationUninstallResult>): Promise<MigrationUninstallResult>;
|
|
167
|
+
/** 订阅迁移事件。返回取消订阅函数。 */
|
|
168
|
+
onEvent(handler: (event: MigrationEvent) => void): () => void;
|
|
121
169
|
};
|
|
122
170
|
export declare const Browser: {
|
|
123
171
|
/** 打开系统浏览器。可直接传 URL 字符串:`Browser.open('https://example.com')` */
|
package/dist/plugins.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { onNativeEvent } from './events';
|
|
2
|
+
import { callNative, invokeNative, isNativeBridgeAvailable, withCallback } from './runtime';
|
|
2
3
|
import { readCanvasRenderInfo, readNavigatorCpu, readWebGlInfo } from './webview-signals';
|
|
3
4
|
function toEnvironmentDevice(device, system) {
|
|
4
5
|
return {
|
|
@@ -220,6 +221,117 @@ export const Ota = {
|
|
|
220
221
|
downloadAndInstallApk(url, callback) {
|
|
221
222
|
return withCallback(invokeNative('downloadAndInstallApk', { url }), callback);
|
|
222
223
|
},
|
|
224
|
+
/**
|
|
225
|
+
* 订阅安装包下载与安装进度(Android)。原生更新弹窗和 `installAppPackage` 都会触发。
|
|
226
|
+
* 返回取消订阅函数。
|
|
227
|
+
*/
|
|
228
|
+
onInstallEvent(handler) {
|
|
229
|
+
return onNativeEvent('ota.install', handler);
|
|
230
|
+
},
|
|
231
|
+
/** 换包安装的当前阶段(Android)。页面刷新后用它恢复界面状态。 */
|
|
232
|
+
getInstallState(callback) {
|
|
233
|
+
return withCallback(invokeNative('getAppInstallState'), callback);
|
|
234
|
+
},
|
|
235
|
+
};
|
|
236
|
+
/** 等待 Bridge 注入的时间。壳里 Bridge 在页面脚本之前就在了;超时说明在普通浏览器 */
|
|
237
|
+
const MIGRATION_STATUS_READY_TIMEOUT_MS = 1500;
|
|
238
|
+
/** 不支持迁移的平台上 `Migration.getStatus()` 的结果 */
|
|
239
|
+
const MIGRATION_UNSUPPORTED_STATUS = {
|
|
240
|
+
supported: false,
|
|
241
|
+
phase: 'none',
|
|
242
|
+
inProgress: false,
|
|
243
|
+
firstLaunch: false,
|
|
244
|
+
imported: false,
|
|
245
|
+
webApplied: false,
|
|
246
|
+
fromPackage: '',
|
|
247
|
+
importedAt: 0,
|
|
248
|
+
lastError: '',
|
|
249
|
+
};
|
|
250
|
+
/**
|
|
251
|
+
* 短时间等 Bridge 注入。不复用 `whenReady`:它的等待是全局共享的,
|
|
252
|
+
* 这里的短超时会连带让别处正在等待的调用提前失败。
|
|
253
|
+
*/
|
|
254
|
+
function waitForBridgeBriefly(timeoutMs) {
|
|
255
|
+
if (isNativeBridgeAvailable()) {
|
|
256
|
+
return Promise.resolve();
|
|
257
|
+
}
|
|
258
|
+
if (typeof document === 'undefined') {
|
|
259
|
+
return Promise.reject(new Error('Not in browser'));
|
|
260
|
+
}
|
|
261
|
+
return new Promise((resolve, reject) => {
|
|
262
|
+
const onReady = () => {
|
|
263
|
+
clearTimeout(timer);
|
|
264
|
+
resolve();
|
|
265
|
+
};
|
|
266
|
+
const timer = setTimeout(() => {
|
|
267
|
+
document.removeEventListener('nativebridgeready', onReady);
|
|
268
|
+
reject(new Error('Bridge timeout'));
|
|
269
|
+
}, timeoutMs);
|
|
270
|
+
document.addEventListener('nativebridgeready', onReady, { once: true });
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* 判断错误是不是"这里没有迁移能力":没有 Bridge(普通浏览器),
|
|
275
|
+
* 或壳没实现该方法(iOS / Harmony / 桌面端 / 旧版 Android 壳)。
|
|
276
|
+
*/
|
|
277
|
+
function isMigrationUnsupported(error) {
|
|
278
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
279
|
+
return /Unknown method|桌面壳未实现|Native bridge not available|Bridge timeout|Not in browser/i.test(message);
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* 换包更新的数据迁移(Android)。新安装包的包名与当前应用不同时,
|
|
283
|
+
* 装好后当前应用会自动拉起新应用,把登录状态(Cookie / localStorage)、
|
|
284
|
+
* 设备序列号和 `setExtras` 的数据交给它,完成后请求卸载当前应用。
|
|
285
|
+
*/
|
|
286
|
+
export const Migration = {
|
|
287
|
+
/**
|
|
288
|
+
* 当前是否正在迁移,以及是不是迁移后的第一次启动。用来决定要不要等待、跳过登录或引导页。
|
|
289
|
+
*
|
|
290
|
+
* 任何平台都可以直接调用,不用先判断平台:不是 Android 壳(或壳版本不带迁移)时,
|
|
291
|
+
* 返回 `{ supported: false, inProgress: false, firstLaunch: false, phase: 'none', ... }`。
|
|
292
|
+
* 其它错误照常抛出。
|
|
293
|
+
*/
|
|
294
|
+
getStatus(callback) {
|
|
295
|
+
// 普通浏览器里没有 Bridge:很快就判定为不支持,不要卡满默认的 8 秒
|
|
296
|
+
const promise = waitForBridgeBriefly(MIGRATION_STATUS_READY_TIMEOUT_MS)
|
|
297
|
+
.then(() => callNative('getMigrationStatus'))
|
|
298
|
+
.catch((error) => {
|
|
299
|
+
if (isMigrationUnsupported(error)) {
|
|
300
|
+
return { ...MIGRATION_UNSUPPORTED_STATUS };
|
|
301
|
+
}
|
|
302
|
+
throw error;
|
|
303
|
+
});
|
|
304
|
+
return withCallback(promise, callback);
|
|
305
|
+
},
|
|
306
|
+
/**
|
|
307
|
+
* 迁移前补充要带走的数据。建议在登录后和每次登录态变化时调用。
|
|
308
|
+
* 新应用通过 `Migration.getInfo().extras` 读取。
|
|
309
|
+
*/
|
|
310
|
+
setExtras(options, callback) {
|
|
311
|
+
return withCallback(invokeNative('setMigrationExtras', {
|
|
312
|
+
extras: options.extras ?? {},
|
|
313
|
+
cookieUrls: options.cookieUrls ?? [],
|
|
314
|
+
}), callback);
|
|
315
|
+
},
|
|
316
|
+
/** 新应用已装好但没自动打开时(后台启动被系统拦截等),由用户点按钮手动继续 */
|
|
317
|
+
continue(callback) {
|
|
318
|
+
return withCallback(invokeNative('continueAppMigration'), callback);
|
|
319
|
+
},
|
|
320
|
+
/** 新应用里读取迁移结果 */
|
|
321
|
+
getInfo(callback) {
|
|
322
|
+
return withCallback(invokeNative('getMigrationInfo'), callback);
|
|
323
|
+
},
|
|
324
|
+
/**
|
|
325
|
+
* 请求卸载:`self` 卸载当前应用(需迁移已完成),`source` 在新应用里卸载被迁移的旧应用。
|
|
326
|
+
* 迁移完成后原生会自动请求一次;用户在系统确认框里取消后,可用它再次发起。
|
|
327
|
+
*/
|
|
328
|
+
requestUninstall(target = 'self', callback) {
|
|
329
|
+
return withCallback(invokeNative('requestUninstall', { target }), callback);
|
|
330
|
+
},
|
|
331
|
+
/** 订阅迁移事件。返回取消订阅函数。 */
|
|
332
|
+
onEvent(handler) {
|
|
333
|
+
return onNativeEvent('migration', handler);
|
|
334
|
+
},
|
|
223
335
|
};
|
|
224
336
|
export const Browser = {
|
|
225
337
|
/** 打开系统浏览器。可直接传 URL 字符串:`Browser.open('https://example.com')` */
|
package/dist/types.d.ts
CHANGED
|
@@ -526,6 +526,128 @@ export interface InstallAppPackageResult {
|
|
|
526
526
|
redirectedToStore?: boolean;
|
|
527
527
|
needsInstallPermission?: boolean;
|
|
528
528
|
message?: string;
|
|
529
|
+
/** 新安装包的包名(仅 Android;下载完成后读到) */
|
|
530
|
+
targetPackage?: string;
|
|
531
|
+
/** true:新安装包与当前应用不是同一个包名,装完后会迁移数据并卸载当前应用 */
|
|
532
|
+
crossPackage?: boolean;
|
|
533
|
+
}
|
|
534
|
+
/**
|
|
535
|
+
* `Ota.onInstallEvent` 的阶段。顺序一般为:
|
|
536
|
+
* download-start → download-progress… → download-complete → install-start →
|
|
537
|
+
* install-prompt(等用户在系统框里确认)→ install-success。
|
|
538
|
+
* 同包升级装完后系统会替换并重启本应用,收不到 install-success。
|
|
539
|
+
*/
|
|
540
|
+
export type OtaInstallStage = 'download-start' | 'download-progress' | 'download-complete' | 'download-failed' | 'install-start' | 'install-prompt' | 'install-success' | 'install-canceled' | 'install-failed';
|
|
541
|
+
export interface OtaInstallEvent {
|
|
542
|
+
stage: OtaInstallStage;
|
|
543
|
+
ts: number;
|
|
544
|
+
/** download-progress:0–100,服务器没给长度时为 -1 */
|
|
545
|
+
percent?: number;
|
|
546
|
+
downloadedBytes?: number;
|
|
547
|
+
totalBytes?: number;
|
|
548
|
+
sizeBytes?: number;
|
|
549
|
+
packageName?: string;
|
|
550
|
+
versionName?: string;
|
|
551
|
+
/** install-start / install-success:是否换了包名 */
|
|
552
|
+
crossPackage?: boolean;
|
|
553
|
+
/** install-prompt:当前应用是否在前台。false 时会改用通知提示用户确认 */
|
|
554
|
+
foreground?: boolean;
|
|
555
|
+
message?: string;
|
|
556
|
+
code?: number;
|
|
557
|
+
}
|
|
558
|
+
/** 换包安装的当前阶段 */
|
|
559
|
+
export type AppInstallPhase = 'none' | 'installing' | 'installed' | 'handoff' | 'done' | 'failed';
|
|
560
|
+
/** Ota.getInstallState() / Migration.continue() */
|
|
561
|
+
export interface AppInstallState {
|
|
562
|
+
phase: AppInstallPhase;
|
|
563
|
+
targetPackage: string;
|
|
564
|
+
targetVersion: string;
|
|
565
|
+
handoffAttempts: number;
|
|
566
|
+
startedAt: number;
|
|
567
|
+
error: string;
|
|
568
|
+
}
|
|
569
|
+
/**
|
|
570
|
+
* `Migration.onEvent` 的阶段。
|
|
571
|
+
* 旧应用侧:snapshot-saved、handoff-start、handoff-blocked、handoff-failed、complete、uninstall-prompt。
|
|
572
|
+
* 新应用侧:imported、applied、import-failed、uninstall-prompt。
|
|
573
|
+
*/
|
|
574
|
+
export type MigrationStage = 'snapshot-saved' | 'handoff-start' | 'handoff-blocked' | 'handoff-failed' | 'complete' | 'imported' | 'applied' | 'import-failed' | 'uninstall-prompt';
|
|
575
|
+
export interface MigrationEvent {
|
|
576
|
+
stage: MigrationStage;
|
|
577
|
+
ts: number;
|
|
578
|
+
message?: string;
|
|
579
|
+
packageName?: string;
|
|
580
|
+
targetPackage?: string;
|
|
581
|
+
fromPackage?: string;
|
|
582
|
+
source?: string;
|
|
583
|
+
foreground?: boolean;
|
|
584
|
+
deviceIdAdopted?: boolean;
|
|
585
|
+
/** imported:旧应用里通过 Migration.setExtras 保存的数据 */
|
|
586
|
+
extras?: Record<string, unknown>;
|
|
587
|
+
}
|
|
588
|
+
/** 新应用里读取:本应用是否由旧应用迁移而来 */
|
|
589
|
+
export interface MigrationInfo {
|
|
590
|
+
imported: boolean;
|
|
591
|
+
fromPackage: string;
|
|
592
|
+
importedAt: number;
|
|
593
|
+
/** 设备序列号(getInstallDeviceId)是否已沿用旧应用的值 */
|
|
594
|
+
deviceIdAdopted: boolean;
|
|
595
|
+
/** localStorage 是否已写入页面 */
|
|
596
|
+
webApplied: boolean;
|
|
597
|
+
/** localStorage 太大被裁剪过 */
|
|
598
|
+
truncated: boolean;
|
|
599
|
+
/** 旧应用还没卸载 */
|
|
600
|
+
uninstallPending: boolean;
|
|
601
|
+
extras: Record<string, unknown>;
|
|
602
|
+
lastError: string;
|
|
603
|
+
}
|
|
604
|
+
/**
|
|
605
|
+
* 新应用的迁移阶段:
|
|
606
|
+
* - `none`:没有发生过迁移(包括不支持迁移的平台)
|
|
607
|
+
* - `importing`:正在向旧应用取数据。通常在页面起来之前就结束,只有预热页面先跑起来时才读得到
|
|
608
|
+
* - `applying`:数据已取回,localStorage 还在等页面确认写入
|
|
609
|
+
* - `completed`:数据已全部写好
|
|
610
|
+
* - `failed`:这个应用上没有迁移成功过,且最近一次尝试失败(原因见 `lastError`)
|
|
611
|
+
*/
|
|
612
|
+
export type MigrationPhase = 'none' | 'importing' | 'applying' | 'completed' | 'failed';
|
|
613
|
+
/** `Migration.getStatus()` 的结果:当前是否在迁移,以及是不是迁移后的第一次启动 */
|
|
614
|
+
export interface MigrationStatus {
|
|
615
|
+
/** 当前平台是否支持迁移。false 表示非 Android 壳,其余字段都是"没有迁移"的默认值 */
|
|
616
|
+
supported: boolean;
|
|
617
|
+
phase: MigrationPhase;
|
|
618
|
+
/** 正在迁移:`phase` 为 `importing` 或 `applying`。迁移有时限,不会永远为 true */
|
|
619
|
+
inProgress: boolean;
|
|
620
|
+
/**
|
|
621
|
+
* 是否是迁移之后的第一次启动:迁移成功,且就在取数据的那个进程里。
|
|
622
|
+
* 进程被系统回收再启动后变回 false;同一进程里页面重新加载仍是 true。
|
|
623
|
+
*/
|
|
624
|
+
firstLaunch: boolean;
|
|
625
|
+
/** 是否已从旧应用取到过数据(同 `Migration.getInfo().imported`) */
|
|
626
|
+
imported: boolean;
|
|
627
|
+
/** localStorage 是否已写入页面 */
|
|
628
|
+
webApplied: boolean;
|
|
629
|
+
fromPackage: string;
|
|
630
|
+
importedAt: number;
|
|
631
|
+
lastError: string;
|
|
632
|
+
}
|
|
633
|
+
export interface MigrationExtrasOptions {
|
|
634
|
+
/** 随迁移带给新应用的数据,如内存里的 token,读取方式:`Migration.getInfo().extras` */
|
|
635
|
+
extras?: Record<string, unknown>;
|
|
636
|
+
/**
|
|
637
|
+
* 需要一起迁移 Cookie 的接口站点。页面所在站点自动包含,
|
|
638
|
+
* 接口放在别的域名时在这里列出,如 `['https://api.example.com']`。
|
|
639
|
+
*/
|
|
640
|
+
cookieUrls?: string[];
|
|
641
|
+
}
|
|
642
|
+
export type MigrationUninstallTarget = 'self' | 'source';
|
|
643
|
+
export interface MigrationUninstallResult {
|
|
644
|
+
success: boolean;
|
|
645
|
+
packageName: string;
|
|
646
|
+
}
|
|
647
|
+
export type NativeEventName = 'ota.install' | 'migration';
|
|
648
|
+
export interface NativeEventMap {
|
|
649
|
+
'ota.install': OtaInstallEvent;
|
|
650
|
+
migration: MigrationEvent;
|
|
529
651
|
}
|
|
530
652
|
export interface ApkUpdateResult {
|
|
531
653
|
hasUpdate: boolean;
|
package/dist/version.d.ts
CHANGED
package/dist/version.js
CHANGED
package/docs/USAGE.md
CHANGED
|
@@ -84,6 +84,8 @@ if (!Native.isNativePlatform()) {
|
|
|
84
84
|
| 生物识别 | `Biometric.authenticate()` |
|
|
85
85
|
| 支付 / 评分 | `Payment.pay({ url })` / `Store.requestReview()` |
|
|
86
86
|
| 推送 token | `Push.getToken()` |
|
|
87
|
+
| 安装进度 / 换包迁移(Android,1.8.0+) | `Ota.onInstallEvent(fn)` / `Migration.onEvent(fn)`,见 §5.3.1 |
|
|
88
|
+
| 新应用是否在迁移 / 迁移后首启(1.8.1+) | `Migration.getStatus()`,各平台可直接调用,见 §5.3.1 |
|
|
87
89
|
|
|
88
90
|
各插件也可通过 `Native.App`、`Native.Ota` 访问,与直接 `import { App }` 相同。
|
|
89
91
|
|
|
@@ -732,6 +734,124 @@ Ota.checkApkUpdate().then(console.log);
|
|
|
732
734
|
// 返回示例:{ "hasUpdate": true, "versionCode": 12, "versionName": "1.0.2", "url": "https://...", "force": false }
|
|
733
735
|
```
|
|
734
736
|
|
|
737
|
+
### 5.3.1 换包更新与数据迁移(Android,包版本 **1.8.0+**)
|
|
738
|
+
|
|
739
|
+
新安装包的**包名**和当前应用不一样时(例如换了 `APP_ID` 出新包),系统把它当成另一个应用:登录态、localStorage、设备序列号都不会自动继承。壳在**安装 APK 的统一入口**里把这件事接住了,不管用户点的是**原生更新弹窗**(普通 / 强制)还是前端自定义弹窗里调用的 `Ota.installAppPackage`,走的是同一条流程,前端不用区分。
|
|
740
|
+
|
|
741
|
+
**流程**
|
|
742
|
+
|
|
743
|
+
1. 下载 APK,期间推送 `download-progress`。
|
|
744
|
+
2. 读 APK 包名。和当前应用相同:走系统安装器原地升级,和以前一样。不同:用 PackageInstaller 安装,并推送 `install-prompt` / `install-success` / `install-failed`。
|
|
745
|
+
3. 装好后,旧应用把登录态打包成一次性票据(10 分钟有效),用显式组件拉起新应用。
|
|
746
|
+
4. 新应用凭票据取走数据:Cookie、localStorage、设备序列号、`Migration.setExtras` 的内容。写好后确认,旧应用标记"已完成"。
|
|
747
|
+
5. 新应用请求卸载旧应用,旧应用回到前台时也会兜底提示一次。
|
|
748
|
+
|
|
749
|
+
**迁移的内容**
|
|
750
|
+
|
|
751
|
+
| 数据 | 怎么迁 |
|
|
752
|
+
|------|------|
|
|
753
|
+
| 登录 Cookie | 页面所在站点的 Cookie 自动带走;接口在其他域名时用 `Migration.setExtras({ cookieUrls })` 列出。迁过去的都是会话 Cookie(`Domain` / `Expires` 等属性 WebView 读不到) |
|
|
754
|
+
| localStorage | 在新页面加载的第一时间写入,页面脚本读到的就是旧值。单次最多约 300K 字符,超出会丢最大的几项(`getInfo().truncated`) |
|
|
755
|
+
| 设备序列号 | 新应用的 `Device.getInstallDeviceId()` 沿用旧值 |
|
|
756
|
+
| 前端自定义数据 | `Migration.setExtras({ extras })`,新应用 `Migration.getInfo().extras` 读取 |
|
|
757
|
+
| sessionStorage / IndexedDB | **不迁移**。需要的内容放进 `extras` |
|
|
758
|
+
|
|
759
|
+
**判断新应用是否在迁移(包版本 1.8.1+)**
|
|
760
|
+
|
|
761
|
+
新应用启动时,用 `Migration.getStatus()` 判断"现在是不是正在迁移"和"是不是迁移后的第一次启动",再决定要不要等待、跳过登录页或引导页。
|
|
762
|
+
|
|
763
|
+
```typescript
|
|
764
|
+
import { Migration } from 'ota-native-bridge';
|
|
765
|
+
|
|
766
|
+
// 迁移还没做完就等一会儿,再启动业务。迁移有时限,不会一直等下去
|
|
767
|
+
function waitMigrationDone() {
|
|
768
|
+
return Migration.getStatus().then((s) => {
|
|
769
|
+
if (!s.inProgress) return s;
|
|
770
|
+
return new Promise((r) => setTimeout(r, 300)).then(waitMigrationDone);
|
|
771
|
+
});
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
waitMigrationDone().then((s) => {
|
|
775
|
+
if (s.firstLaunch) {
|
|
776
|
+
// 迁移后的第一次启动:登录态、设备序列号都是从旧应用带来的,不要当成新用户
|
|
777
|
+
Migration.getInfo().then((info) => restoreSession(info.extras));
|
|
778
|
+
}
|
|
779
|
+
startApp();
|
|
780
|
+
});
|
|
781
|
+
```
|
|
782
|
+
|
|
783
|
+
返回值:
|
|
784
|
+
|
|
785
|
+
| 字段 | 说明 |
|
|
786
|
+
|------|------|
|
|
787
|
+
| `phase` | `none` 没有发生过迁移;`importing` 正在向旧应用取数据;`applying` 数据已取回,localStorage 等页面确认写入;`completed` 全部写好;`failed` 这个应用上没迁移成功过,且最近一次尝试失败 |
|
|
788
|
+
| `inProgress` | **正在迁移**:`phase` 是 `importing` 或 `applying` |
|
|
789
|
+
| `firstLaunch` | 迁移后的第一次启动:迁移成功,且就在取数据的那个进程里。进程被系统回收再启动后变回 `false`;同一进程里页面重新加载仍是 `true` |
|
|
790
|
+
| `imported` / `webApplied` / `fromPackage` / `importedAt` / `lastError` | 与 `Migration.getInfo()` 同名字段一致 |
|
|
791
|
+
| `supported` | 当前平台是否支持迁移。`false` 时其余字段都是"没有迁移"的默认值 |
|
|
792
|
+
|
|
793
|
+
使用时注意:
|
|
794
|
+
|
|
795
|
+
- **任何平台都能直接调用**,不用先判断平台。iOS / Harmony / 桌面端、旧版 Android 壳、普通浏览器里不会报错,返回 `supported: false`、`inProgress: false`、`firstLaunch: false`。其它类型的错误照常抛出。
|
|
796
|
+
- 迁移的大部分发生在页面起来之前(后台取数据,页面还没出现)。所以页面启动时多半读到的是 `completed` 加 `firstLaunch: true`。`importing` 只在预热页面先于迁移跑起来时才读得到;`applying` 到页面确认写入 localStorage 为止。
|
|
797
|
+
- Cookie 和设备序列号在页面起来前就已写好;localStorage 由壳在页面文档开始时写入,页面脚本读到的已经是旧值。`inProgress` 为 `true` 时,不要用新生成的设备序列号发请求。
|
|
798
|
+
- 迁移有时限,不会让页面一直卡在"迁移中":取数据超过 30 秒、写入确认超过 60 秒都不再算迁移中。超时后 `phase` 变成 `completed`,`webApplied` 仍为 `false` 表示 localStorage 没写进去(例如入口页站点和旧应用不同)。
|
|
799
|
+
- `firstLaunch` 只能表示"这个进程"。业务上要"只做一次"的事情(欢迎提示、一次性数据导入),请自己存一个标记。
|
|
800
|
+
- 旧应用还没卸载(`Migration.getInfo().uninstallPending`)不算迁移中。
|
|
801
|
+
|
|
802
|
+
**事件**
|
|
803
|
+
|
|
804
|
+
```typescript
|
|
805
|
+
import { Ota, Migration } from 'ota-native-bridge';
|
|
806
|
+
|
|
807
|
+
// 下载与安装进度。原生弹窗触发的更新也会收到
|
|
808
|
+
const offInstall = Ota.onInstallEvent((e) => {
|
|
809
|
+
switch (e.stage) {
|
|
810
|
+
case 'download-progress': render(e.percent); break; // 0-100,未知长度时为 -1
|
|
811
|
+
case 'install-prompt': tip('请在系统弹窗里确认安装'); break;
|
|
812
|
+
case 'install-success': tip('安装完成,正在迁移数据'); break;
|
|
813
|
+
case 'install-failed':
|
|
814
|
+
case 'download-failed': tip(e.message); break;
|
|
815
|
+
}
|
|
816
|
+
});
|
|
817
|
+
|
|
818
|
+
// 迁移进度(旧应用与新应用都会收到各自一侧的事件)
|
|
819
|
+
const offMigration = Migration.onEvent((e) => {
|
|
820
|
+
if (e.stage === 'handoff-blocked') showButton('打开新版本', () => Migration.continue());
|
|
821
|
+
if (e.stage === 'imported') console.log('已迁移', e.extras);
|
|
822
|
+
});
|
|
823
|
+
```
|
|
824
|
+
|
|
825
|
+
事件是 DOM 事件 `nativebridgeevent`(`detail = { name, payload }`),`Ota.onInstallEvent` / `Migration.onEvent` / `Native.on(name, fn)` 都是对它的封装。**页面还没加载好时发出的事件不补发**,页面启动时请用 `Ota.getInstallState()`、`Migration.getInfo()` 读当前状态。
|
|
826
|
+
|
|
827
|
+
`Ota.onInstallEvent` 的 `stage` 一般顺序为:`download-start` → `download-progress`(多次)→ `download-complete` → `install-start` → `install-prompt`(等用户在系统框里确认)→ `install-success`。失败或取消时为 `download-failed` / `install-canceled` / `install-failed`。同包升级装完后系统会替换并重启本应用,收不到 `install-success`。`install-prompt` 的 `foreground` 为 `false` 时,壳会改用通知提示用户确认。
|
|
828
|
+
|
|
829
|
+
**接口**
|
|
830
|
+
|
|
831
|
+
| 方法 | 说明 |
|
|
832
|
+
|------|------|
|
|
833
|
+
| `Ota.onInstallEvent(fn)` | 订阅下载与安装事件,返回取消订阅函数 |
|
|
834
|
+
| `Native.on(name, fn)` | 订阅原始事件,`name` 为 `'ota.install'` 或 `'migration'`,返回取消订阅函数 |
|
|
835
|
+
| `Ota.getInstallState()` | 换包安装的阶段:`{ phase, targetPackage, targetVersion, handoffAttempts, startedAt, error }`,`phase` 为 `none / installing / installed / handoff / done / failed` |
|
|
836
|
+
| `Ota.installAppPackage(options)` | 结果新增 `targetPackage`、`crossPackage` |
|
|
837
|
+
| `Migration.setExtras({ extras?, cookieUrls? })` | 迁移前补充要带走的数据。建议登录后和登录态变化时调用 |
|
|
838
|
+
| `Migration.continue()` | 新应用已装好但没自动打开时,由用户点按钮重试交接 |
|
|
839
|
+
| `Migration.getStatus()` | 新应用里判断是否正在迁移、是否迁移后首次启动:`{ supported, phase, inProgress, firstLaunch, imported, webApplied, fromPackage, importedAt, lastError }`(包版本 **1.8.1+**,各平台都可调用) |
|
|
840
|
+
| `Migration.getInfo()` | 新应用里读迁移结果:`{ imported, fromPackage, importedAt, deviceIdAdopted, webApplied, truncated, uninstallPending, extras, lastError }` |
|
|
841
|
+
| `Migration.requestUninstall('self' \| 'source')` | `self` 卸载当前应用(迁移完成后),`source` 在新应用里卸载旧应用。用户在系统框里取消后可再次发起 |
|
|
842
|
+
| `Migration.onEvent(fn)` | 订阅迁移事件 |
|
|
843
|
+
|
|
844
|
+
`Migration` 事件的 `stage`:旧应用侧 `snapshot-saved`、`handoff-start`、`handoff-blocked`、`handoff-failed`、`complete`、`uninstall-prompt`;新应用侧 `imported`、`applied`、`import-failed`、`uninstall-prompt`。
|
|
845
|
+
|
|
846
|
+
**限制**
|
|
847
|
+
|
|
848
|
+
- 仅 **Android** 壳支持,iOS / Harmony / 桌面端调用 `Migration.*` 会报 `Unknown method`。唯一的例外是 `Migration.getStatus()`:它在这些平台上直接返回"没有迁移",不报错。
|
|
849
|
+
- 安装和卸载的**系统确认框一定会弹**,Android 不允许普通应用静默安装或卸载。
|
|
850
|
+
- Android 10 起后台应用不能直接拉起别的应用。装完时旧应用不在前台,会改发一条通知,点通知或回到旧应用时继续交接(最多重试 5 次),也可以由前端 `Migration.continue()` 手动触发。
|
|
851
|
+
- 谷歌商店渠道(`canSideloadApkUpdate=false`)不走应用内安装,直接跳商店,没有迁移。
|
|
852
|
+
- 新旧应用能互信靠的是同签名,或者两边打包进去的同一把平台密钥(HMAC)。用不同签名、也没有平台配置的包不会迁移。
|
|
853
|
+
- 新应用会先让预热页面起来,再把迁移来的 localStorage 写入并重新加载入口页。服务端可能短暂见到一次新设备序列号,请求以 `Device.getInstallDeviceId()` 为准时请等 `Migration.getStatus().inProgress` 变为 `false`(或 `Migration.getInfo().imported === true`)后再读。
|
|
854
|
+
|
|
735
855
|
### 5.4 Client API 请求参数加密(必选)
|
|
736
856
|
|
|
737
857
|
凡 H5 / 自定义逻辑向 `/api/client/v1/**` 发起的 JSON POST(含 WebSocket 首条 subscribe),**必须**使用创建平台时生成的**参数公钥**加密。明文直传会被服务端拒绝(HTTP 400)。
|
|
@@ -1021,7 +1141,7 @@ Device.requestPhoneStatePermission().then(function () {
|
|
|
1021
1141
|
|
|
1022
1142
|
#### `Device.getInstallDeviceId()`
|
|
1023
1143
|
|
|
1024
|
-
壳在第一次启动时生成 32 位十六进制设备序列号(UUID v4)并保存。值由安全随机数产生,与签名证书、`ANDROID_ID` / IDFV / ODID 无关,也不受 `collectDeviceIdsAfterConsent` 控制。同一安装上的覆盖更新保持不变。iOS 写在仅本机的 Keychain 中,同一 Bundle ID、同一开发者 Team 卸载后重装仍是原值,换机备份不会把序列号复制到另一台设备。Android 写在应用私有偏好中,并排除云备份和换机迁移;覆盖安装不变,卸载重装会生成新值。Harmony 写在应用 preferences 中,覆盖安装不变,卸载重装会生成新值。
|
|
1144
|
+
壳在第一次启动时生成 32 位十六进制设备序列号(UUID v4)并保存。值由安全随机数产生,与签名证书、`ANDROID_ID` / IDFV / ODID 无关,也不受 `collectDeviceIdsAfterConsent` 控制。同一安装上的覆盖更新保持不变。Android 换了包名的更新,新应用会沿用旧应用的值(见 §5.3.1)。iOS 写在仅本机的 Keychain 中,同一 Bundle ID、同一开发者 Team 卸载后重装仍是原值,换机备份不会把序列号复制到另一台设备。Android 写在应用私有偏好中,并排除云备份和换机迁移;覆盖安装不变,卸载重装会生成新值。Harmony 写在应用 preferences 中,覆盖安装不变,卸载重装会生成新值。
|
|
1025
1145
|
|
|
1026
1146
|
```javascript
|
|
1027
1147
|
Device.getInstallDeviceId().then(function (result) {
|
|
@@ -1594,7 +1714,9 @@ Http.request({
|
|
|
1594
1714
|
| `Ota.setUid(uid)` | 仅设置上报用户 ID(主动设置后不再被请求头 Hook 覆盖) | `{ success: true, uid: "user-123" }` |
|
|
1595
1715
|
| `Ota.setChannel(channel)` | 仅设置上报渠道码(主动设置后不再被请求头 Hook 覆盖) | `{ success: true, channel: "yyb" }` |
|
|
1596
1716
|
| `Ota.installResourcePackage(options)` | 安装资源包 | `{ success, needsRestart, restarted, message }` |
|
|
1597
|
-
| `Ota.installAppPackage(options)` | 安装 APK 等 | `{ success, message, needsInstallPermission? }` |
|
|
1717
|
+
| `Ota.installAppPackage(options)` | 安装 APK 等 | `{ success, message, needsInstallPermission?, targetPackage?, crossPackage? }` |
|
|
1718
|
+
| `Ota.onInstallEvent(fn)` / `Ota.getInstallState()` | 下载与安装进度、换包安装阶段(Android) | 见 §5.3.1 |
|
|
1719
|
+
| `Migration.setExtras` / `continue` / `getStatus` / `getInfo` / `requestUninstall` / `onEvent` | 换包数据迁移(Android) | 见 §5.3.1 |
|
|
1598
1720
|
| `Ota.downloadAndInstallApk(url)` | 等同 `installAppPackage({ url })` | 同上 |
|
|
1599
1721
|
| `Ota.checkResourceUpdate` / `Ota.checkApkUpdate` | 按 URL 检测 | `{ hasUpdate, version?/versionName?, url? }` |
|
|
1600
1722
|
| `Ota.applyResourceUpdate` | 安装资源包不重启 | `{ success: true, needsRestart: true, restarted: false }` |
|
|
@@ -1860,7 +1982,27 @@ A:先看 `Icon.get()` 的 `supportsCustomFromResource`。iOS 和 HarmonyOS 会
|
|
|
1860
1982
|
|
|
1861
1983
|
---
|
|
1862
1984
|
|
|
1863
|
-
## 11.
|
|
1985
|
+
## 11. 版本更新记录
|
|
1986
|
+
|
|
1987
|
+
以下只列影响接入的变化,完整类型见 `dist/types.d.ts`。
|
|
1988
|
+
|
|
1989
|
+
**1.8.1**
|
|
1990
|
+
|
|
1991
|
+
- 新增 `Migration.getStatus()`:新应用判断自己是否正在迁移、是否是迁移后的第一次启动,各平台都能直接调用。详见 §5.3.1。需要同时升级 Android 壳;旧壳上返回"不支持"。
|
|
1992
|
+
|
|
1993
|
+
**1.8.0**
|
|
1994
|
+
|
|
1995
|
+
- 新增换包更新与数据迁移(Android):`Migration` 插件,以及 `Ota.onInstallEvent`、`Ota.getInstallState`、`Native.on`、`onNativeEvent`。详见 §5.3.1。
|
|
1996
|
+
- `Ota.installAppPackage` 的结果新增 `targetPackage`、`crossPackage`。
|
|
1997
|
+
- `Device.getInstallDeviceId()` 在换包更新后沿用旧应用的设备序列号(Android)。
|
|
1998
|
+
|
|
1999
|
+
**1.7.0**
|
|
2000
|
+
|
|
2001
|
+
- 新增 `Device.setOrientation` / `Device.setFullscreen`,见各 API 标注的 `1.7.0+`。
|
|
2002
|
+
|
|
2003
|
+
---
|
|
2004
|
+
|
|
2005
|
+
## 12. 相关文档
|
|
1864
2006
|
|
|
1865
2007
|
- [README.md](../README.md) — 包结构与快速上手
|
|
1866
2008
|
- 仓库 `AppUpgradeOTASystem` 根目录 `README.md` — OTA 系统整体说明
|
package/ota-native-bridge.js
CHANGED
|
@@ -91,6 +91,22 @@
|
|
|
91
91
|
return readyPromise;
|
|
92
92
|
}
|
|
93
93
|
|
|
94
|
+
function waitBriefly(maxMs) {
|
|
95
|
+
if (getNativeMessenger()) return Promise.resolve();
|
|
96
|
+
if (typeof document === 'undefined') return Promise.reject(new Error('Not in browser'));
|
|
97
|
+
return new Promise(function (resolve, reject) {
|
|
98
|
+
var timer = setTimeout(function () {
|
|
99
|
+
document.removeEventListener('nativebridgeready', onReady);
|
|
100
|
+
reject(new Error('Bridge timeout'));
|
|
101
|
+
}, maxMs);
|
|
102
|
+
function onReady() {
|
|
103
|
+
clearTimeout(timer);
|
|
104
|
+
resolve();
|
|
105
|
+
}
|
|
106
|
+
document.addEventListener('nativebridgeready', onReady, { once: true });
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
94
110
|
function invokeNative(method, params) {
|
|
95
111
|
return waitForBridge().then(function () { return callNative(method, params); });
|
|
96
112
|
}
|
|
@@ -173,6 +189,17 @@
|
|
|
173
189
|
setBadgeCount: function (count, cb) { return withCallback(invokeNative('setBadgeCount', { count: count }), cb); }
|
|
174
190
|
};
|
|
175
191
|
|
|
192
|
+
function onNativeEvent(name, handler) {
|
|
193
|
+
if (typeof window === 'undefined') return function () {};
|
|
194
|
+
var listener = function (e) {
|
|
195
|
+
var d = e && e.detail;
|
|
196
|
+
if (!d || d.name !== name) return;
|
|
197
|
+
try { handler(d.payload); } catch (err) { if (typeof console !== 'undefined') console.error('[ota-native-bridge] event handler failed', err); }
|
|
198
|
+
};
|
|
199
|
+
window.addEventListener('nativebridgeevent', listener);
|
|
200
|
+
return function () { window.removeEventListener('nativebridgeevent', listener); };
|
|
201
|
+
}
|
|
202
|
+
|
|
176
203
|
var Ota = {
|
|
177
204
|
checkResourceUpdate: function (url, cb) { return withCallback(invokeNative('checkResourceUpdate', url ? { url: url } : {}), cb); },
|
|
178
205
|
applyResourceUpdate: function (url, cb) { return withCallback(invokeNative('applyResourceUpdate', { url: url }), cb); },
|
|
@@ -196,7 +223,37 @@
|
|
|
196
223
|
setChannel: function (channel, cb) { return withCallback(invokeNative('setOtaChannel', { channel: channel || '' }), cb); },
|
|
197
224
|
downloadAndInstallApk: function (url, cb) {
|
|
198
225
|
return withCallback(invokeNative('downloadAndInstallApk', { url: url }), cb);
|
|
199
|
-
}
|
|
226
|
+
},
|
|
227
|
+
onInstallEvent: function (handler) { return onNativeEvent('ota.install', handler); },
|
|
228
|
+
getInstallState: function (cb) { return withCallback(invokeNative('getAppInstallState'), cb); }
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
var Migration = {
|
|
232
|
+
setExtras: function (opts, cb) {
|
|
233
|
+
opts = opts || {};
|
|
234
|
+
return withCallback(invokeNative('setMigrationExtras', { extras: opts.extras || {}, cookieUrls: opts.cookieUrls || [] }), cb);
|
|
235
|
+
},
|
|
236
|
+
'continue': function (cb) { return withCallback(invokeNative('continueAppMigration'), cb); },
|
|
237
|
+
getInfo: function (cb) { return withCallback(invokeNative('getMigrationInfo'), cb); },
|
|
238
|
+
getStatus: function (cb) {
|
|
239
|
+
// 普通浏览器里没有 Bridge:很快判定为不支持,不要卡满默认的 8 秒
|
|
240
|
+
// 不复用 waitForBridge:它的等待是全局共享的,短超时会让别处正在等待的调用提前失败
|
|
241
|
+
var promise = waitBriefly(1500)
|
|
242
|
+
.then(function () { return callNative('getMigrationStatus'); })
|
|
243
|
+
.catch(function (error) {
|
|
244
|
+
var message = error instanceof Error ? error.message : String(error);
|
|
245
|
+
if (/Unknown method|桌面壳未实现|Native bridge not available|Bridge timeout|Not in browser/i.test(message)) {
|
|
246
|
+
return {
|
|
247
|
+
supported: false, phase: 'none', inProgress: false, firstLaunch: false,
|
|
248
|
+
imported: false, webApplied: false, fromPackage: '', importedAt: 0, lastError: ''
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
throw error;
|
|
252
|
+
});
|
|
253
|
+
return withCallback(promise, cb);
|
|
254
|
+
},
|
|
255
|
+
requestUninstall: function (target, cb) { return withCallback(invokeNative('requestUninstall', { target: target || 'self' }), cb); },
|
|
256
|
+
onEvent: function (handler) { return onNativeEvent('migration', handler); }
|
|
200
257
|
};
|
|
201
258
|
|
|
202
259
|
var Browser = {
|
|
@@ -375,7 +432,7 @@
|
|
|
375
432
|
};
|
|
376
433
|
|
|
377
434
|
var plugins = {
|
|
378
|
-
App: App, Ota: Ota, Browser: Browser, Device: Device, Config: Config, Theme: Theme, Icon: Icon,
|
|
435
|
+
App: App, Ota: Ota, Migration: Migration, Browser: Browser, Device: Device, Config: Config, Theme: Theme, Icon: Icon,
|
|
379
436
|
Viewport: Viewport, Notification: Notification, Location: Location, Media: Media, Http: Http,
|
|
380
437
|
Clipboard: Clipboard, Share: Share, Picker: Picker, Scan: Scan, Biometric: Biometric,
|
|
381
438
|
Contacts: Contacts, Calendar: Calendar, Bluetooth: Bluetooth, Nfc: Nfc, Sensor: Sensor,
|
|
@@ -384,14 +441,16 @@
|
|
|
384
441
|
};
|
|
385
442
|
|
|
386
443
|
global.Native = {
|
|
387
|
-
sdkVersion: '1.
|
|
444
|
+
sdkVersion: '1.8.1',
|
|
388
445
|
getPlatform: getPlatform,
|
|
389
446
|
isNativePlatform: isNativePlatform,
|
|
390
447
|
isNativeBridgeAvailable: function () { return !!getNativeMessenger(); },
|
|
391
448
|
whenReady: waitForBridge,
|
|
449
|
+
on: onNativeEvent,
|
|
392
450
|
Plugins: plugins,
|
|
393
451
|
App: App,
|
|
394
452
|
Ota: Ota,
|
|
453
|
+
Migration: Migration,
|
|
395
454
|
Browser: Browser,
|
|
396
455
|
Device: Device,
|
|
397
456
|
Config: Config,
|