美文网首页
微信小程序之获取系统信息

微信小程序之获取系统信息

作者: 荒剑离 | 来源:发表于2020-02-14 21:18 被阅读0次

    在实际应用中,经常会遇到小程序在不同机型界面呈现上的兼容适配问题,这就需要我们能够获取到系统信息。恰好小程序就为我们提供了这样的API:
    wx.getSystemInfo(Object object)和同步版本Object wx.getSystemInfoSync()

    wx.getSystemInfo({
      success (res) {
        console.log(res.model)
        console.log(res.pixelRatio)
        console.log(res.windowWidth)
        console.log(res.windowHeight)
        console.log(res.language)
        console.log(res.version)
        console.log(res.platform)
      }
    })
    
    try {
      const res = wx.getSystemInfoSync()
      console.log(res.model)
      console.log(res.pixelRatio)
      console.log(res.windowWidth)
      console.log(res.windowHeight)
      console.log(res.language)
      console.log(res.version)
      console.log(res.platform)
    } catch (e) {
      // Do something when catch error
    }
    

    具体属性以wx.getSystemInfo(Object object)为例:

    Object object

    属性 类型 默认值 必填 说明
    success function 接口调用成功的回调函数
    fail function 接口调用失败的回调函数
    complete function 接口调用结束的回调函数(调用成功、失败都会执行)

    object.success 回调函数

    Object res
    属性 类型 说明 最低版本
    brand string 设备品牌 1.5.0
    model string 设备型号
    pixelRatio number 设备像素比
    screenWidth number 屏幕宽度,单位px 1.1.0
    screenHeight number 屏幕高度,单位px 1.1.0
    windowWidth number 可使用窗口宽度,单位px
    windowHeight number 可使用窗口高度,单位px
    statusBarHeight number 状态栏的高度,单位px 1.9.0
    language string 微信设置的语言
    version string 微信版本号
    system string 操作系统及版本
    platform string 客户端平台
    fontSizeSetting number 用户字体大小(单位px)。以微信客户端「我-设置-通用-字体大小」中的设置为准 1.5.0
    SDKVersion string 客户端基础库版本 1.1.0
    benchmarkLevel number 设备性能等级(仅Android小游戏)。取值为:-2 或 0(该设备无法运行小游戏),-1(性能未知),>=1(设备性能值,该值越高,设备性能越好,目前最高不到50) 1.8.0
    albumAuthorized boolean 允许微信使用相册的开关(仅 iOS 有效) 2.6.0
    cameraAuthorized boolean 允许微信使用摄像头的开关 2.6.0
    locationAuthorized boolean 允许微信使用定位的开关 2.6.0
    microphoneAuthorized boolean 允许微信使用麦克风的开关 2.6.0
    notificationAuthorized boolean 允许微信通知的开关 2.6.0
    notificationAlertAuthorized boolean 允许微信通知带有提醒的开关(仅 iOS 有效) 2.6.0
    notificationBadgeAuthorized boolean 允许微信通知带有标记的开关(仅 iOS 有效) 2.6.0
    notificationSoundAuthorized boolean 允许微信通知带有声音的开关(仅 iOS 有效) 2.6.0
    bluetoothEnabled boolean 蓝牙的系统开关 2.6.0
    locationEnabled boolean 地理位置的系统开关 2.6.0
    wifiEnabled boolean Wi-Fi 的系统开关 2.6.0
    safeArea Object 在竖屏正方向下的安全区域 2.7.0
    res.safeArea 的结构

    safearea的定义:去掉状态栏,去掉导航栏,去掉tabbar栏,剩余的屏幕大小就是用户可视化安全区域。

    属性 类型 说明
    left number 安全区域左上角横坐标
    right number 安全区域右下角横坐标
    top number 安全区域左上角纵坐标
    bottom number 安全区域右下角纵坐标
    width number 安全区域的宽度,单位逻辑像素
    height number 安全区域的高度,单位逻辑像素

    相关文章

      网友评论

          本文标题:微信小程序之获取系统信息

          本文链接:https://www.haomeiwen.com/subject/bbojfhtx.html