From f164f25fd83c365be352252c486aa051188643f8 Mon Sep 17 00:00:00 2001 From: Jiayu Liu Date: Tue, 18 Dec 2018 15:50:30 +0800 Subject: [PATCH 1/2] weixin-app: add update manager api --- types/weixin-app/index.d.ts | 33 ++++++++++++++++++++++++++++ types/weixin-app/weixin-app-tests.ts | 25 ++++++++++++++++----- 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/types/weixin-app/index.d.ts b/types/weixin-app/index.d.ts index b40fedfd46..6c37215f20 100644 --- a/types/weixin-app/index.d.ts +++ b/types/weixin-app/index.d.ts @@ -3409,6 +3409,39 @@ declare namespace wx { */ function chooseInvoiceTitle(options: ChooseInvoiceTitleOptions): void; + interface UpdateManager { + /** + * 强制小程序重启并使用新版本。在小程序新版本下载完成后(即收到 `onUpdateReady` 回调)调用。 + */ + applyUpdate(): void; + /** + * 监听向微信后台请求检查更新结果事件。微信在小程序冷启动时自动检查更新,不需由开发者主动触发。 + */ + onCheckForUpdate( + /** 向微信后台请求检查更新结果事件的回调函数 */ + callback: (result: { hasUpdate: boolean }) => void + ): void; + /** [UpdateManager.onUpdateFailed(function callback)](UpdateManager.onUpdateFailed.md) + * + * 监听小程序更新失败事件。小程序有新版本,客户端主动触发下载(无需开发者触发),下载失败(可能是网络原因等)后回调 */ + onUpdateFailed( + /** 小程序更新失败事件的回调函数 */ + callback: (res: { errMsg: string }) => void + ): void; + /** [UpdateManager.onUpdateReady(function callback)](UpdateManager.onUpdateReady.md) + * + * 监听小程序有版本更新事件。客户端主动触发下载(无需开发者触发),下载成功后回调 */ + onUpdateReady( + /** 小程序有版本更新事件的回调函数 */ + callback: () => void + ): void; + } + + /** + * 获取全局唯一的版本更新管理器,用于管理小程序更新。关于小程序的更新机制,可以查看运行机制文档。 + */ + function getUpdateManager(): UpdateManager; + interface NavigateToMiniProgramOptions extends BaseOptions { appId: string; // 要打开的小程序 appId path?: string; // 打开的页面路径,如果为空则打开首页 diff --git a/types/weixin-app/weixin-app-tests.ts b/types/weixin-app/weixin-app-tests.ts index 2c5b6f2c23..4a344127e7 100644 --- a/types/weixin-app/weixin-app-tests.ts +++ b/types/weixin-app/weixin-app-tests.ts @@ -2,11 +2,11 @@ getCurrentPages(); interface MyOwnEvent extends wx.CustomEvent< - "my-own", - { - hello: string; - } - > {} + "my-own", + { + hello: string; + } + > {} let behavior = Behavior({ behaviors: [], @@ -476,3 +476,18 @@ function testAccountInfo(): string { } wx.reportAnalytics("test-event", { a: 1, b: "2" }); + +App({ + onLaunch() { + const manager: wx.UpdateManager = wx.getUpdateManager(); + manager.onCheckForUpdate(({ hasUpdate }) => { + console.info({ hasUpdate }); + }); + manager.onUpdateReady(() => { + manager.applyUpdate(); + }); + manager.onUpdateFailed(({ errMsg }) => { + console.warn("update failed", errMsg); + }); + } +}); From 1daffc34495d0cb274236fb229fc0e76ba2c9c37 Mon Sep 17 00:00:00 2001 From: Jiayu Liu Date: Mon, 24 Dec 2018 16:58:38 +0800 Subject: [PATCH 2/2] fix doc --- types/weixin-app/index.d.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/types/weixin-app/index.d.ts b/types/weixin-app/index.d.ts index 6c37215f20..b33af483a1 100644 --- a/types/weixin-app/index.d.ts +++ b/types/weixin-app/index.d.ts @@ -3421,16 +3421,12 @@ declare namespace wx { /** 向微信后台请求检查更新结果事件的回调函数 */ callback: (result: { hasUpdate: boolean }) => void ): void; - /** [UpdateManager.onUpdateFailed(function callback)](UpdateManager.onUpdateFailed.md) - * - * 监听小程序更新失败事件。小程序有新版本,客户端主动触发下载(无需开发者触发),下载失败(可能是网络原因等)后回调 */ + /** 监听小程序更新失败事件。小程序有新版本,客户端主动触发下载(无需开发者触发),下载失败(可能是网络原因等)后回调 */ onUpdateFailed( /** 小程序更新失败事件的回调函数 */ callback: (res: { errMsg: string }) => void ): void; - /** [UpdateManager.onUpdateReady(function callback)](UpdateManager.onUpdateReady.md) - * - * 监听小程序有版本更新事件。客户端主动触发下载(无需开发者触发),下载成功后回调 */ + /** 监听小程序有版本更新事件。客户端主动触发下载(无需开发者触发),下载成功后回调 */ onUpdateReady( /** 小程序有版本更新事件的回调函数 */ callback: () => void