diff --git a/types/weixin-app/index.d.ts b/types/weixin-app/index.d.ts index b40fedfd46..b33af483a1 100644 --- a/types/weixin-app/index.d.ts +++ b/types/weixin-app/index.d.ts @@ -3409,6 +3409,35 @@ declare namespace wx { */ function chooseInvoiceTitle(options: ChooseInvoiceTitleOptions): void; + interface UpdateManager { + /** + * 强制小程序重启并使用新版本。在小程序新版本下载完成后(即收到 `onUpdateReady` 回调)调用。 + */ + applyUpdate(): void; + /** + * 监听向微信后台请求检查更新结果事件。微信在小程序冷启动时自动检查更新,不需由开发者主动触发。 + */ + onCheckForUpdate( + /** 向微信后台请求检查更新结果事件的回调函数 */ + callback: (result: { hasUpdate: boolean }) => void + ): void; + /** 监听小程序更新失败事件。小程序有新版本,客户端主动触发下载(无需开发者触发),下载失败(可能是网络原因等)后回调 */ + onUpdateFailed( + /** 小程序更新失败事件的回调函数 */ + callback: (res: { errMsg: string }) => void + ): void; + /** 监听小程序有版本更新事件。客户端主动触发下载(无需开发者触发),下载成功后回调 */ + 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); + }); + } +});