微信小程序设备信息和版本更新 - 新闻资讯 - 云南小程序开发|云南软件开发|云南网站建设-昆明葵宇信息科技有限公司

159-8711-8523

云南网建设/小程序开发/软件开发

知识

不管是网站,软件还是小程序,都要直接或间接能为您产生价值,我们在追求其视觉表现的同时,更侧重于功能的便捷,营销的便利,运营的高效,让网站成为营销工具,让软件能切实提升企业内部管理水平和效率。优秀的程序为后期升级提供便捷的支持!

您当前位置>首页 » 新闻资讯 » 小程序相关 >

微信小程序设备信息和版本更新

发表时间:2021-2-28

发布人:葵宇科技

浏览次数:40

封装until.js

// 获取小程序以及设备信息
function get(success, fail=undefined) {

  // 设备信息
  const info = {
    // 系统设备信息原对象
    info: {},
    // 是否为iOS
    isIOS: false,
    // 是否为iphoneX XR XS...等iOS系统全面屏手机
    isIphoneX: false,
    // 是否为安卓
    isAndroid: false,
    // 是否为Mac
    isMac: false,
    // 是否为Windows
    isWindows: false,
    // 设备像素比 (px 与 rpx 的转换比例, 公式:px * pixelRatio = rpx)
    pixelRatio: 1,
    // 屏幕宽度
    screenWidth: 0,
    screenWidthRPX: 0,
    // 屏幕高度
    screenHeight: 0,
    screenHeightRPX: 0,
    // 状态栏高度
    statusBarHeight: 0,
    statusBarHeightRPX: 0,
    // 导航栏高度(不包括状态栏,单纯的导航栏高度)
    navigationBarHeight: 0,
    navigationBarHeightRPX: 0,
    // 导航栏高度(包括状态栏,整个导航栏高度)
    navigationHeight: 0,
    navigationHeightRPX: 0,
    // 底部TabBar菜单栏高度
    tabBarHeight: 0,
    tabBarHeightRPX: 0
  }

  // 加载系统信息进行更换
  wx.getSystemInfo({
    // 获取成功
    success: (res) => {
      // 右上角菜单胶囊范围
      let menuButtonRect = wx.getMenuButtonBoundingClientRect()

      // 状态栏默认高度
      const statusBarHeight = 20
      // 导航栏默认高度
      const navigationBarHeight = 44
      // TabBar默认高度
      const tabBarHeight = 48

      // 记录原始数据
      info.info = res

      // 是否为iOS
      info.isIOS = (res.system.indexOf('iOS') !== -1)
      // 是否为iOS系统全面屏手机
      if (info.isIOS) {
        // 如果为 iOS 且安全区域上面超过 默认状态栏高度 则为 X 系列
        info.isIphoneX = (res.safeArea.top > statusBarHeight)
      }

      // 是否为安卓
      info.isAndroid = (res.system.indexOf('Android') !== -1)

      // 是否为Mac
      info.isMac = (res.system.indexOf('macOS') !== -1)

      // 是否为Windows
      info.isWindows = (res.system.indexOf('Windows') !== -1)

      // 设备像素比(750 / 屏幕宽度)
      // 系统给成的 res.pixelRatio 值不对,所以使用自己换算出来的比例
      info.pixelRatio = 750 / res.windowWidth
      

      // 屏幕宽度
      info.screenWidth = res.screenWidth
      // 屏幕宽度 - RPX
      info.screenWidthRPX = info.screenWidth * info.pixelRatio

      // 屏幕高度
      info.screenHeight = res.screenHeight
      // 屏幕高度 - RPX
      info.screenHeightRPX = info.screenHeight * info.pixelRatio

      // 状态栏高度
      info.statusBarHeight = Math.max(res.statusBarHeight, statusBarHeight)
      // 状态栏高度 - RPX
      info.statusBarHeightRPX = info.statusBarHeight * info.pixelRatio
      
      // 导航栏高度
      const menuBarHeight = (menuButtonRect.top - info.statusBarHeight) * 2 + menuButtonRect.height
      info.navigationBarHeight = Math.max(menuBarHeight, navigationBarHeight)
      // 导航栏高度 - 如果为奇数则转成偶数
      if (info.navigationBarHeight % 2) { info.navigationBarHeight += 1 }
      // 导航栏高度 - RPX
      info.navigationBarHeightRPX = info.navigationBarHeight * info.pixelRatio

      // 导航栏高度
      info.navigationHeight = (info.statusBarHeight + info.navigationBarHeight)
      // 导航栏高度 - RPX
      info.navigationHeightRPX = info.navigationHeight * info.pixelRatio

      // 底部TabBar菜单栏高度
      info.tabBarHeight = Math.max(info.screenHeight - info.navigationHeight - res.windowHeight, tabBarHeight)
      // 底部TabBar菜单栏高度 - 如果为奇数则转成偶数
      if (info.tabBarHeight % 2) { info.tabBarHeight += 1 }
      // 底部TabBar菜单栏高度 - RPX
      info.tabBarHeightRPX = info.tabBarHeight * info.pixelRatio
      console.log(info)
      // 返回
      if (success) { success(info) }
    },
    // 获取失败
    fail: (err) => {
      if (fail) { fail(err) }
    }
  })
}

// 检查小程序版本并更新
function update(success=undefined, fail=undefined) {
  // 检查是否支持版本更新
  if (wx.canIUse('getUpdateManager')) {
    // 获取版本更新对象
    var updateManager = wx.getUpdateManager()
    // 检测是否有新版本
    updateManager.onCheckForUpdate((res) => {
      // 有新版本
      if (res.hasUpdate) {
        // 更新成功回调
        updateManager.onUpdateReady((res) => {
          // 有回调实现
          if (success) {
            // 自己写提示,返回版本更新对象,方便外部使用
            success(updateManager, res)
          } else {
            // 使用内部更新提示
            wx.showModal({
              title: '更新提示',
              content: '新版本已经准备好,是否重启应用?',
              success: (res) => {
                // 确定重启,在 onUpdateReady 回调中使用 applyUpdate 强制小程序重启使用新版本。
                if (res.confirm) { updateManager.applyUpdate() }
              }
            })
          }
        })
        // 更新失败回调
        updateManager.onUpdateFailed((err) => {
          // 有回调实现
          if (fail) {
            // 自己写提示
            fail(err)
          } else {
            // 使用内部更新提示
            wx.showModal({
              title: '更新提示',
              content: '新版本下载失败,请检查网络!',
              showCancel: false
            })
          }
        })
      } else {
        // 无新版本
      }
    })
  }
}

// 导出使用
module.exports = {
  // 获取小程序以及设备信息
  get,
  // 检查小程序版本并更新
  update
}

app.js引用

// 导入 until.js
const system = require("./utils.js")
// 小程序主入口
App({
  // 设备信息存放,设备信息推荐存放到 app.js 文件中作为全局参数使用
  // 然后到每个页面通过 const app = getApp() 获取使用即可 app.systemInfo
  systemInfo: {},
  // 启动函数
  onLaunch: function (options) {
    // 检查版本更新,推荐放这里,也可以放到手动触发的地方调用
    system.update()
    // 获取设备信息
    system.get((info) => {
      // 记录设备信息
      this.systemInfo = info
    })
  }
})

相关案例查看更多