微信小程序学习之路——API设备

设备

系统

wx.getSystemInfo(Object)

异步获取系统信息,Object参数属性为:

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

具体其他参数请参考微信官方文档:https://developers.weixin.qq.com/miniprogram/dev/api/wx.getSystemInfo.html?search-key=wx.getSystemInfo

示例代码如下:

 wx.getSystemInfo({
    success: function(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)
    },
  })

wx.getSystemInfoSync()

示例代码如下:

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){
    //
}

网络状态

wx.getNetworkType(Object)

用于获取网络类型,Object参数属性如下:

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

示例代码如下:

wx.getNetworkType({
  success: function(res) {
    console.log(res.networkType);
  },
})

重力感应

wx.onAccelerometerChange(callback)

用于监听中立感应数据,频率:5次/秒,callback返回参数属性如下:

属性 类型 说明
x number X 轴
y number Y 轴
z number Z 轴

示例代码如下:

 data:{x:0,y:0,z:0},
  onReady:function(){
    var self = this;
    wx.onAccelerometerChange(function(res){
      self.setData({x:res.x,y:res.y,z:res.z});
    })
  }

罗盘

wx.onCompassChange(callback)

用于监听罗盘数据;频率:5次/秒,调用罗盘需要开启定位功能,callback参数的属性有direction:当前面向的方向度数,正北为0,范围为0~360,-1代表没有开启定位功能。

示例代码如下:

 wx.onCompressChange(function(res){
   console.log(res.direction)
 })

拨打电话

wx.makePhoneCall(Object)

用于调用手机拨打电话功能,Object参数的属性如下:

参数 类型 必填 说明
phoneNumber String 需要拨打的电话号码
success Function 接口调用成功的回调
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

示例代码如下:

wx.makePhoneCall({
  phoneNumber: '110',
})

扫码

wx.scanCode(Object)

调起客户端扫码界面,扫码成功后返回对应结果,Object参数如下:

属性 类型 默认值 必填 说明 最低版本
onlyFromCamera boolean false 是否只能从相机扫码,不允许从相册选择图片 1.2.0
scanType Array.<string> ['barCode', 'qrCode'] 扫码类型 1.7.0
success function   接口调用成功的回调函数  
fail function   接口调用失败的回调函数  
complete function   接口调用结束的回调函数(调用成功、失败都会执行)

示例代码如下:

wx.scanCode({
  success:function(res){
    //打印扫码内容
    console.log(res.result);
  }
})

猜你喜欢

转载自blog.csdn.net/CSDN_XUWENHAO/article/details/89056882