微信小程序中一些常用的 API
这里列出了一些在实际开发中比较常用的 API。
禁止截屏/录屏
wx.setVisualEffectOnCapture
wx.onUserCaptureScreen
wx.offUserCaptureScreen
wx.onScreenRecordingStateChanged
wx.offScreenRecordingStateChanged
wx.getScreenRecordingState
版本更新提示
功能描述
获取全局唯一的版本更新管理器,用于管理小程序更新。
返回值
UpdateManager 更新管理器对象
用法
- 使用 UpdateManager.onUpdateReady 监听小程序有版本更新事件。
- 使用 UpdateManager.onUpdateFailed 监听小程序更新失败事件。
- 调用 UpdateManager.applyUpdate() 方法强制小程序重启并使用新版本。在小程序新版本下载完成后(即收到 onUpdateReady 回调)调用。
示例代码
js
if (!wx.canIUse('getUpdateManager')) {
// console.log('当前机型不支持使用小程序版本更新管理器')
return
}
const updateManager = wx.getUpdateManager()
updateManager.onUpdateReady(() => {
// console.log('新版本下载完成')
wx.showModal('新版本已经准备好,重启应用', {
title: '更新提示',
confirmText: '好的',
confirmColor: '#07c160',
success: (res) => {
if (res.confirm) {
updateManager.applyUpdate()
}
},
})
})
updateManager.onUpdateFailed(() => {
// console.log('新版本下载失败')
wx.showModal('新版本已经上线,请您删除当前小程序,重新搜索打开', {
title: '更新提示',
confirmText: '好的',
confirmColor: '#07c160',
showCancel: false,
})
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27