Skip to content

微信小程序中一些常用的 API

这里列出了一些在实际开发中比较常用的 API。

禁止截屏/录屏

wx.setVisualEffectOnCapture
wx.onUserCaptureScreen
wx.offUserCaptureScreen
wx.onScreenRecordingStateChanged
wx.offScreenRecordingStateChanged
wx.getScreenRecordingState

版本更新提示

wx.getUpdateManager

功能描述

获取全局唯一的版本更新管理器,用于管理小程序更新。

返回值

UpdateManager 更新管理器对象

用法

示例代码
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,
  })
})