w3cschool-微信小程序开发文档-API

https://www.w3cschool.cn/weixinapp/weixinapp-network-request.html

微信小程序API 发起请求

发起 HTTPS 网络请求。使用前请注意阅读相关说明。

参数

Object object

属性类型默认值必填说明最低版本
url string   开发者服务器接口地址  
data string/object/ArrayBuffer   请求的参数  
header Object   设置请求的 header,header 中不能设置 Referer。
content-type 默认为 application/json
 
timeout number   超时时间,单位为毫秒 2.10.0
method string GET HTTP 请求方法  
dataType string json 返回的数据格式  
responseType string text 响应的数据类型 1.7.0
enableHttp2 boolean false 开启 http2 2.10.4
enableQuic boolean false 开启 quic 2.10.4
enableCache boolean false 开启 cache 2.10.4
success function   接口调用成功的回调函数  
fail function   接口调用失败的回调函数  
complete function   接口调用结束的回调函数(调用成功、失败都会执行)  

object.method 的合法值

说明最低版本
OPTIONS HTTP 请求 OPTIONS  
GET HTTP 请求 GET  
HEAD HTTP 请求 HEAD  
POST HTTP 请求 POST  
PUT HTTP 请求 PUT  
DELETE HTTP 请求 DELETE  
TRACE HTTP 请求 TRACE  
CONNECT HTTP 请求 CONNECT  

object.dataType 的合法值

说明最低版本
json 返回的数据为 JSON,返回后会对返回的数据进行一次 JSON.parse  
其他 不对返回的内容进行 JSON.parse  

object.responseType 的合法值

说明最低版本
text 响应的数据为文本  
arraybuffer 响应的数据为 ArrayBuffer  

object.success 回调函数

参数
Object res
属性类型说明最低版本
data string/Object/Arraybuffer 开发者服务器返回的数据  
statusCode number 开发者服务器返回的 HTTP 状态码  
header Object 开发者服务器返回的 HTTP Response Header 1.2.0
cookies Array.<string> 开发者服务器返回的 cookies,格式为字符串数组 2.10.0
profile Object 网络请求过程中一些调试信息 2.10.4

微信小程序API 上传、下载

2020-07-28 16:17 更新

wx.uploadFile(OBJECT)


将本地资源上传到开发者服务器。如页面通过 wx.chooseImage 等接口获取到一个本地资源的临时文件路径后,可通过此接口将本地资源上传到指定服务器。客户端发起一个HTTPS POST请求,其中Content-Typemultipart/form-data

OBJECT参数说明:

参数类型必填说明
url String 开发者服务器url
filePath String 要上传文件资源的路径
name String 文件对应的key , 开发者在服务器端通过这个key可以获取到文件二进制内容
header Object HTTP 请求 Header,header中不能设置Referer
formData Object HTTP 请求中其他额外的form data
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数说明:

参数类型说明
data String 开发者服务器返回的数据
statusCode Number HTTP状态码

 

微信小程序API Websocket

2021-04-16 16:02 更新

wx.connectSocket(OBJECT)


创建一个 WebSocket 连接;一个微信小程序同时只能有一个 WebSocket 连接,如果当前已存在一个 WebSocket 连接,会自动关闭该连接,并重新创建一个 WebSocket 连接。

OBJECT参数说明:

 

参数类型必填说明最低版本
url String 开发者服务器接口地址,必须是 wss 协议,且域名必须是后台配置的合法域名  
data Object 请求的数据  
header Object HTTP Header , header 中不能设置 Referer  
method String 默认是GET,有效值: OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT  
protocols StringArray 子协议数组 1.4.0
success Function 接口调用成功的回调函数  
fail Function 接口调用失败的回调函数  
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)  

示例代码:
wx.connectSocket({  
  url: 'test.php',
  data:{
    x: '',
    y: ''
  },
  header:{ 
    'content-type': 'application/json'
  },  protocols: ['protocol1'],
  method:"GET"
})

wx.onSocketOpen(CALLBACK)


监听WebSocket连接打开事件。

示例代码:

wx.connectSocket({
  url: 'test.php'
})
wx.onSocketOpen(function(res) {
  console.log('WebSocket连接已打开!')
})

wx.onSocketError(CALLBACK)


监听WebSocket错误。

示例代码:

wx.connectSocket({
  url: 'test.php'
})
wx.onSocketOpen(function(res){
  console.log('WebSocket连接已打开!')
})
wx.onSocketError(function(res){
  console.log('WebSocket连接打开失败,请检查!')
})

wx.sendSocketMessage(OBJECT)


通过 WebSocket 连接发送数据,需要先 wx.connectSocket,并在 wx.onSocketOpen 回调之后才能发送。

OBJECT参数说明:

参数类型必填说明
data String/ArrayBuffer 需要发送的内容
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

示例代码:

var socketOpen = false
var socketMsgQueue = []
wx.connectSocket({
  url: 'test.php'
})

wx.onSocketOpen(function(res) {
  socketOpen = true
  for (var i = 0; i < socketMsgQueue.length; i++){
     sendSocketMessage(socketMsgQueue[i])
  }
  socketMsgQueue = []
})

function sendSocketMessage(msg) {
  if (socketOpen) {
    wx.sendSocketMessage({
      data:msg
    })
  } else {
     socketMsgQueue.push(msg)
  }
}

wx.onSocketMessage(CALLBACK)


监听WebSocket接受到服务器的消息事件。

CALLBACK返回参数:

参数类型说明
data String/ArrayBuffer 服务器返回的消息

示例代码:

wx.connectSocket({
  url: 'test.php'
})

wx.onSocketMessage(function(res) {
  console.log('收到服务器内容:' + res.data)
})

wx.stopLocalServiceDiscovery(Object object)

基础库 2.4.0 开始支持,低版本需做兼容处理

停止搜索 mDNS 服务

参数

Object object

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

object.fail 回调函数

参数
Object res
属性类型说明
errMsg string 错误信息

res.errMsg 的合法值

说明最低版本
task not found 在当前没有处在搜索服务中的情况下调用 stopLocalServiceDiscovery  

wx.startLocalServiceDiscovery(Object object)

基础库 2.4.0 开始支持,低版本需做兼容处理

开始搜索局域网下的 mDNS 服务。搜索的结果会通过 wx.onLocalService* 事件返回。

微信小程序API 地图·创建MapContext对象

2020-07-22 17:15 更新

MapContext wx.createMapContext(string mapId, Object this)

创建 map 上下文 MapContext 对象。

参数

string mapId

map 组件的 id

Object this

在自定义组件下,当前组件实例的this,以操作组件内 map 组件

返回值

MapContext

微信小程序API 地图·MapContext对象

2020-10-20 11:12 更新

MapContext 实例,可通过 wx.createMapContext 获取。

MapContext 通过 id 跟一个 map 组件绑定,操作对应的 map 组件。

方法

MapContext.getCenterLocation()

获取当前地图中心的经纬度。返回的是 gcj02 坐标系,可以用于 wx.openLocation()

MapContext.moveToLocation(Object object)

将地图中心移置当前定位点,此时需设置地图组件 show-location 为true2.8.0 起支持将地图中心移动到指定位置。

MapContext.translateMarker(Object object)

平移marker,带动画

MapContext.includePoints(Object object)

缩放视野展示所有经纬度

MapContext.getRegion()

获取当前地图的视野范围

MapContext.getRotate()

获取当前地图的旋转角

MapContext.getSkew()

获取当前地图的倾斜角

MapContext.getScale()

获取当前地图的缩放级别

MapContext.setCenterOffset(Object object)

设置地图中心点偏移,向后向下为增长,屏幕比例范围(0.25~0.75),默认偏移为[0.5, 0.5]

MapContext.removeCustomLayer(Object object)

移除个性化图层。

MapContext.addCustomLayer(Object object)

添加个性化图层。

示例代码

<!-- map.wxml -->
<map id="myMap" show-location />

<button type="primary" bindtap="getCenterLocation">获取位置</button>
<button type="primary" bindtap="moveToLocation">移动位置</button>
<button type="primary" bindtap="translateMarker">移动标注</button>
<button type="primary" bindtap="includePoints">缩放视野展示所有经纬度</button>
// map.js
Page({
  onReady: function (e) {
    // 使用 wx.createMapContext 获取 map 上下文
    this.mapCtx = wx.createMapContext('myMap')
  },
  getCenterLocation: function () {
    this.mapCtx.getCenterLocation({
      success: function(res){
        console.log(res.longitude)
        console.log(res.latitude)
      }
    })
  },
  moveToLocation: function () {
    this.mapCtx.moveToLocation()
  },
  translateMarker: function() {
    this.mapCtx.translateMarker({
      markerId: 0,
      autoRotate: true,
      duration: 1000,
      destination: {
        latitude:23.10229,
        longitude:113.3345211,
      },
      animationEnd() {
        console.log('animation end')
      }
    })
  },
  includePoints: function() {
    this.mapCtx.includePoints({
      padding: [10],
      points: [{
        latitude:23.10229,
        longitude:113.3345211,
      }, {
        latitude:23.00229,
        longitude:113.3345211,
      }]
    })
  }
})

微信小程序API 图片·保存图片

wx.saveImageToPhotosAlbum(Object object)

基础库 1.2.0 开始支持,低版本需做兼容处理
调用前需要 用户授权 scope.writePhotosAlbum

保存图片到系统相册。

参数

Object object

属性类型默认值必填说明
filePath string   图片文件路径,可以是临时文件路径或永久文件路径 (本地路径) ,不支持网络路径
success function   接口调用成功的回调函数
fail function   接口调用失败的回调函数
complete function   接口调用结束的回调函数(调用成功、失败都会执行)

示例代码

wx.saveImageToPhotosAlbum({
  success(res) { }
})

微信小程序API 图片·预览图片

wx.previewMedia(Object object)

基础库 2.12.0 开始支持,低版本需做兼容处理

预览图片和视频。

参数

Object object

属性类型默认值必填说明
sources Array.<Object>   需要预览的资源列表
current current 0 当前显示的资源序号
success function   接口调用成功的回调函数
fail function   接口调用失败的回调函数
complete function   接口调用结束的回调函数(调用成功、失败都会执行)

object.sources 的结构

属性类型默认值必填说明
url String   图片或视频的地址
type String image 资源的类型,默认为图片
poster string   视频的封面图片

type 的合法值

 

 

说明最低版本
image 图片  
video 视频

 

微信小程序API 图片·全屏预览图片

2020-07-28 15:24 更新

wx.previewImage(Object object)

在新页面中全屏预览图片。预览的过程中用户可以进行保存图片、发送给朋友等操作。

微信小程序API 图片·获取图片信息

2020-07-28 15:25 更新

wx.getImageInfo(Object object)

获取图片信息。网络图片需先配置download域名才能生效。

 

微信小程序API 图片·压缩图片接口

2020-07-22 16:52 更新

wx.compressImage(Object object)

基础库 2.4.0 开始支持,低版本需做兼容处理。

压缩图片接口,可选压缩质量

wx.chooseMessageFile(Object object)

基础库 2.5.0 开始支持,低版本需做兼容处理

从客户端会话选择文件。

 

微信小程序API 图片·从本地相册选择文件

2020-07-22 16:52 更新

wx.chooseImage(Object object)

从本地相册选择图片或使用相机拍照。

参数

Object object

属性类型默认值必填说明
count number 9 最多可以选择的图片张数
sizeType Array.<string> ['original', 'compressed'] 所选的图片的尺寸
sourceType Array.<string> ['album', 'camera'] 选择图片的来源
success function   接口调用成功的回调函数
fail function   接口调用失败的回调函数
complete function   接口调用结束的回调函数(调用成功、失败都会执行)

object.sizeType 的合法值

说明最低版本
original 原图  
compressed 压缩图  

object.sourceType 的合法值

说明最低版本
album 从相册选图  
camera 使用相机  

object.success 回调函数

参数
Object res
属性类型说明最低版本
tempFilePaths Array.<string> 图片的本地临时文件路径列表 (本地路径)  
tempFiles Array.<Object> 图片的本地临时文件列表 1.2.0

res.tempFiles 的结构

属性类型说明
path string 本地临时文件路径 (本地路径)
size number 本地临时文件大小,单位 B
wx.chooseImage({
  count: 1,
  sizeType: ['original', 'compressed'],
  sourceType: ['album', 'camera'],
  success (res) {
    // tempFilePath可以作为img标签的src属性显示图片
    const tempFilePaths = res.tempFilePaths
  }
})

微信小程序API 视频·保存视频

2020-07-28 15:39 更新

wx.saveVideoToPhotosAlbum(Object object)

基础库 1.2.0 开始支持,低版本需做兼容处理
调用前需要 用户授权 scope.writePhotosAlbum

保存视频到系统相册。支持mp4视频格式。

参数

Object object

属性类型默认值必填说明
filePath string   视频文件路径,可以是临时文件路径也可以是永久文件路径 (本地路径)
success function   接口调用成功的回调函数
fail function   接口调用失败的回调函数
complete function   接口调用结束的回调函数(调用成功、失败都会执行)

示例代码

wx.saveVideoToPhotosAlbum({
  filePath: 'wxfile://xxx',
  success (res) {
    console.log(res.errMsg)
  }
})

微信小程序API 视频·拍摄或相册中选择视频

2020-07-28 15:38 更新

wx.chooseVideo(Object object)

拍摄视频或从手机相册中选视频。

参数

Object object

属性类型默认值必填说明最低版本
sourceType Array.<string> ['album', 'camera'] 视频选择的来源  
compressed boolean true 是否压缩所选择的视频文件 1.6.0
maxDuration number 60 拍摄视频最长拍摄时间,单位秒  
camera string 'back' 默认拉起的是前置或者后置摄像头。部分 Android 手机下由于系统 ROM 不支持无法生效  
success function   接口调用成功的回调函数  
fail function   接口调用失败的回调函数  
complete function   接口调用结束的回调函数(调用成功、失败都会执行)  

object.sourceType 的合法值

说明最低版本
album 从相册选择视频  
camera 使用相机拍摄视频  

object.camera 的合法值

说明最低版本
back 默认拉起后置摄像头  
front 默认拉起前置摄像头  

object.success 回调函数

参数
Object res
属性类型说明
tempFilePath string 选定视频的临时文件路径 (本地路径)
duration number 选定视频的时间长度
size number 选定视频的数据量大小
height number 返回选定视频的高度
width number 返回选定视频的宽度

示例代码

wx.chooseVideo({
  sourceType: ['album','camera'],
  maxDuration: 60,
  camera: 'back',
  success(res) {
    console.log(res.tempFilePath)
  }
})

微信小程序API 录音·开始录音

2020-07-23 10:33 更新

wx.startRecord(Object object)

调用前需要 用户授权 scope.record
从基础库 1.6.0 开始,本接口停止维护,请使用 wx.getRecorderManager 代替

开始录音。当主动调用 wx.stopRecord,或者录音超过1分钟时自动结束录音。当用户离开小程序时,此接口无法调用。

参数

Object object

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

object.success 回调函数

参数
Object res
属性类型说明
tempFilePath string 录音文件的临时路径 (本地路径)

示例代码

wx.startRecord({
  success (res) {
    const tempFilePath = res.tempFilePath
  }
})
setTimeout(function () {
  wx.stopRecord() // 结束录音
}, 10000)

微信小程序API 文件·保存文件到本地

2020-07-24 10:31 更新

wx.saveFile(Object object)

保存文件到本地。注意:saveFile 会把临时文件移动,因此调用成功后传入的 tempFilePath 将不可用

参数

Object object

属性类型默认值必填说明
tempFilePath string   需要保存的文件的临时路径 (本地路径)
success function   接口调用成功的回调函数
fail function   接口调用失败的回调函数
complete function   接口调用结束的回调函数(调用成功、失败都会执行)

object.success 回调函数

参数
Object res
属性类型说明
savedFilePath number 存储后的文件路径 (本地路径)

示例代码

wx.chooseImage({
  success: function(res) {
    const tempFilePaths = res.tempFilePaths
    wx.saveFile({
      tempFilePath: tempFilePaths[0],
      success (res) {
        const savedFilePath = res.savedFilePath
      }
    })
  }
})

微信小程序API 数据缓存

2020-07-31 14:01 更新

每个微信小程序都可以有自己的本地缓存,可以通过wx.setStorage(wx.setStorageSync)、wx.getStorage(wx.getStorageSync)、wx.clearStorage(wx.clearStorageSync)可以对本地缓存进行设置、获取和清理。同一个微信用户,同一个小程序 storage 上限为 10MB。localStorage 以用户维度隔离,同一台设备上,A 用户无法读取到 B 用户的数据。

注意: localStorage是永久存储的,但是我们不建议将关键信息全部存在localStorage,以防用户换设备的情况。

wx.setStorage(OBJECT)


将数据存储在本地缓存中指定的key中,会覆盖掉原来该key对应的内容,这是一个异步接口。

OBJECT参数说明:

参数类型必填说明
key String 本地缓存中的指定的 key
data Object/String 需要存储的内容
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

示例代码

wx.setStorage({
  key:"key"
  data:"value"
})

wx.setStorageSync(KEY,DATA)


​将data存储在本地缓存中指定的key中,会覆盖掉原来该key对应的内容,这是一个同步接口。

参数说明:

参数类型必填说明
key String 本地缓存中的指定的key
data Object/String 需要存储的内容

示例代码

try {
   wx.setStorageSync("key","value")
} catch (e) {
}

wx.getStorage(OBJECT)


从本地缓存中异步获取指定key对应的内容。

OBJECT参数说明:

参数类型必填说明
key String 本地缓存中的指定的 key
success Function 接口调用的回调函数,res = {data: key对应的内容}
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数说明:

参数类型说明
data String key对应的内容

示例代码:

wx.getStorage({
  key:'key',
  success: function(res){
      console.log(res.data)
  } 
})

wx.getStorageSync(KEY)


​从本地缓存中同步获取指定key对应的内容。

参数说明:

参数类型必填说明
key String 本地缓存中的指定的key

示例代码:

try {
  var value = wx.getStorageSync('key')
  if (value) {
      // Do something with return value
  }
} catch (e) {
  // Do something when catch error
}

wx.getStorageInfo(OBJECT)


异步获取当前storage的相关信息

OBJECT参数说明:

参数类型必填说明
success Function 接口调用的回调函数,详见返回参数说明
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数说明:

参数类型说明
keys String Array 当前storage中所有的key
currentSize Number 当前占用的空间大小, 单位kb
limitSize Number 限制的空间大小,单位kb

示例代码:

wx.getStorageInfo({
  success: function(res) {
    console.log(res.keys)
    console.log(res.currentSize)
    console.log(res.limitSize)
  }
})

wx.getStorageInfoSync


同步获取当前storage的相关信息

示例代码:

try {
  var res = wx.getStorageInfoSync()
  console.log(res.keys)
  console.log(res.currentSize)
  console.log(res.limitSize)
} catch (e) {
  // Do something when catch error
}

wx.removeStorage(OBJECT)


从本地缓存中异步移除指定 key 。

OBJECT参数说明:

参数类型必填说明
key String 本地缓存中的指定的 key
success Function 接口调用的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

示例代码:

wx.removeStorage({
  key: 'key',
  success: function(res) {
    console.log(res.data)
  } 
})

wx.removeStorageSync(KEY)


从本地缓存中同步移除指定 key 。

参数说明:

参数类型必填说明
key String 本地缓存中的指定的 key

示例代码:

try {
  wx.removeStorageSync('key')
} catch (e) {
  // Do something when catch error
}

wx.clearStorage()


​清理本地数据缓存。

示例代码:

wx.clearStorage()

wx.clearStorageSync()


同步清理本地数据缓存

示例代码:

try {
    wx.clearStorageSync()
} catch(e) {
  // Do something when catch error
}

Bug & Tip

  1. tip: 本地数据存储的大小限制为 10MB

微信小程序API 获取位置

2020-07-28 15:44 更新

wx.getLocation(OBJECT)


获取当前的地理位置、速度。当用户离开小程序后,此接口无法调用;当用户点击“显示在聊天顶部”时,此接口可继续调用。

OBJECT参数说明:

参数类型必填说明
type String 默认为"wgs84"返回gps坐标,"gcj02"返回可用于wx.openLocation的坐标
success Function 接口调用成功的回调函数,返回内容详见返回参数说明。
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数说明:

参数说明最低版本
latitude 纬度,浮点数,范围为-90~90,负数表示南纬  
longitude 经度,浮点数,范围为-180~180,负数表示西经  
speed 速度,浮点数,单位m/s  
accuracy 位置的精确度  
altitude 高度,单位 m 1.2.0
verticalAccuracy 垂直精度,单位 m(Android 无法获取,返回 0) 1.2.0
horizontalAccuracy 水平精度,单位 m 1.2.0

示例代码:

wx.getLocation({
  type: 'wgs84',
  success: function(res) {
    var latitude = res.latitude
    var longitude = res.longitude
    var speed = res.speed
    var accuracy = res.accuracy
  }
})

 

 

wx.chooseLocation(OBJECT)


打开地图选择位置。

需要用户授权 scope.userLocation

OBJECT参数说明:

参数类型必填说明
success Function 接口调用成功的回调函数,返回内容详见返回参数说明。
cancel Function 用户取消时调用
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数说明:

参数说明
name 位置名称
address 详细地址
latitude 纬度,浮点数,范围为-90~90,负数表示南纬
longitude 经度,浮点数,范围为-180~180,负数表示西经

微信小程序API 查看位置

2020-07-28 15:48 更新

wx.getLocation(OBJECT)


获取当前的地理位置、速度。当用户离开小程序后,此接口无法调用;当用户点击“显示在聊天顶部”时,此接口可继续调用。

OBJECT参数说明:

参数类型必填说明
type String 默认为 wgs84 返回 gps 坐标,gcj02 返回可用于wx.openLocation的坐标
success Function 接口调用成功的回调函数,返回内容详见返回参数说明。
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数说明:

参数说明最低版本
latitude 纬度,浮点数,范围为-90~90,负数表示南纬  
longitude 经度,浮点数,范围为-180~180,负数表示西经  
speed 速度,浮点数,单位m/s  
accuracy 位置的精确度  
altitude 高度,单位 m 1.2.0
verticalAccuracy 垂直精度,单位 m(Android 无法获取,返回 0) 1.2.0
horizontalAccuracy 水平精度,单位 m 1.2.0

示例代码:

wx.getLocation({
  type: 'wgs84',
  success: function(res) {
    var latitude = res.latitude
    var longitude = res.longitude
    var speed = res.speed
    var accuracy = res.accuracy
  }
})

wx.chooseLocation(OBJECT)


打开地图选择位置。

需要用户授权 scope.userLocation

OBJECT参数说明:

参数类型必填说明
success Function 接口调用成功的回调函数,返回内容详见返回参数说明。
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数说明:

参数说明
name 位置名称
address 详细地址
latitude 纬度,浮点数,范围为-90~90,负数表示南纬
longitude 经度,浮点数,范围为-180~180,负数表示西经

wx.openLocation(OBJECT)


​使用微信内置地图查看位置。

需要用户授权 scope.userLocation

OBJECT参数说明:

参数类型必填说明
latitude Float 纬度,范围为-90~90,负数表示南纬
longitude Float 经度,范围为-180~180,负数表示西经
scale INT 缩放比例,范围5~18,默认为18
name String 位置名
address String 地址的详细说明
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

示例代码:

wx.getLocation({
  type: 'gcj02', //返回可以用于wx.openLocation的经纬度
  success: function(res) {
    var latitude = res.latitude
    var longitude = res.longitude
    wx.openLocation({
      latitude: latitude,
      longitude: longitude,
      scale: 28
    })
  }
})

Bug & Tip

  1. bugiOS 6.3.30 type 参数不生效,只会返回 wgs84 类型的坐标信息

微信小程序API-设备-系统信息

2020-07-28 15:50 更新

 

wx.getSystemInfo(OBJECT)


 

获取系统信息。

OBJECT参数说明:

参数类型必填说明
success Function 接口调用成功的回调
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success回调参数说明:

 

参数说明最低版本
model 手机型号  
pixelRatio 设备像素比  
screenWidth 屏幕宽度 1.1.0
screenHeight 屏幕高度 1.1.0
windowWidth 可使用窗口宽度  
windowHeight 可使用窗口高度  
language 微信设置的语言  
version 微信版本号  
system 操作系统版本  
platform 客户端平台  
fontSizeSetting 用户字体大小设置。以“我-设置-通用-字体大小”中的设置为准,单位:px 1.5.0
SDKVersion 客户端基础库版本 1.1.0

 


示例代码:

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()


获取系统信息同步接口

 

同步返回参数说明:

参数说明最低版本
model 手机型号  
pixelRatio 设备像素比  
screenWidth 屏幕宽度 1.1.0
screenHeight 屏幕高度 1.1.0
windowWidth 可使用窗口宽度  
windowHeight 可使用窗口高度  
language 微信设置的语言  
version 微信版本号  
system 操作系统版本  
platform 客户端平台  
SDKVersion 客户端基础库版本 1.1.0

示例代码:

try {
var 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.canIUse(String)


判断小程序的API,回调,参数,组件等是否在当前版本可用。

String参数说明: 使用${API}.${method}.${param}.${options}或者${component}.${attribute}.${option}方式来调用,例如:

  • ${API}代表 API 名字
  • ${method}代表调用方式,有效值为returnsuccessobjectcallback
  • ${param}代表参数或者返回值
  • ${options}代表参数的可选值
  • ${component}代表组件名字
  • ${attribute}代表组件属性
  • ${option}代表组件属性的可选值

例子:

wx.canIUse('openBluetoothAdapter')wx.canIUse('getSystemInfoSync.return.screenWidth')
wx.canIUse('getSystemInfo.success.screenWidth')
wx.canIUse('showToast.object.image')
wx.canIUse('onCompassChange.callback.direction')
wx.canIUse('request.object.method.GET')
wx.canIUse('contact-button')
wx.canIUse('text.selectable')
wx.canIUse('button.open-type.contact')

微信小程序API-设备- 网络状态

2020-07-28 15:57 更新

wx.getNetworkType(OBJECT)

获取网络类型。

OBJECT参数说明:

参数类型必填说明
success Function 接口调用成功,返回网络类型 networkType
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数说明:

参数说明
networkType 网络类型
wx.getNetworkType({
  success: function(res) {
    // 返回网络类型, 有效值:
    // wifi/2g/3g/4g/unknown(Android下不常见的网络类型)/none(无网络)
    var networkType = res.networkType
  }
})

wx.onNetworkStatusChange(CALLBACK)


基础库 1.1.0 开始支持,低版本需做兼容处理

监听网络状态变化。

CALLBACK返回参数:

参数类型说明
isConnected Boolean 当前是否有网络连接
networkType String 网络类型

networkType 有效值:

说明
wifi wifi 网络
2g 2g 网络
3g 3g 网络
4g 4g 网络
none 无网络
unknown Android下不常见的网络类型

示例代码:

wx.onNetworkStatusChange(function(res) {
  console.log(res.isConnected)
  console.log(res.networkType)
})

wx.offNetworkStatusChange(function callback)

基础库 2.9.3 开始支持,低版本需做兼容处理

取消监听网络状态变化事件,参数为空,则取消所有的事件监听。

参数

function callback

网络状态变化事件的回调函数

微信小程序API-设备-拨打电话

2020-07-28 16:05 更新

wx.makePhoneCall(OBJECT)


OBJECT参数说明:

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

示例代码:

wx.makePhoneCall({
  phoneNumber: '1340000' //仅为示例,并非真实的电话号码
})

微信小程序API-设备-扫码

2020-07-28 16:06 更新

wx.scanCode(Object object)

调起客户端扫码界面进行扫码

参数

Object object

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

object.scanType 的合法值

说明最低版本
barCode 一维码  
qrCode 二维码  
datamatrix Data Matrix 码  
pdf417 PDF417 条码  

object.success 回调函数

参数
Object res
属性类型说明
result string 所扫码的内容
scanType string 所扫码的类型
charSet string 所扫码的字符集
path string 当所扫的码为当前小程序二维码时,会返回此字段,内容为二维码携带的 path
rawData string 原始数据,base64编码

res.scanType 的合法值

说明最低版本
QR_CODE 二维码  
AZTEC 一维码  
CODABAR 一维码  
CODE_39 一维码  
CODE_93 一维码  
CODE_128 一维码  
DATA_MATRIX 二维码  
EAN_8 一维码  
EAN_13 一维码  
ITF 一维码  
MAXICODE 一维码  
PDF_417 二维码  
RSS_14 一维码  
RSS_EXPANDED 一维码  
UPC_A 一维码  
UPC_E 一维码  
UPC_EAN_EXTENSION 一维码  
WX_CODE 二维码  
CODE_25 一维码  

示例代码

// 允许从相机和相册扫码
wx.scanCode({
  success (res) {
    console.log(res)
  }
})

// 只允许从相机扫码
wx.scanCode({
  onlyFromCamera: true,
  success (res) {
    console.log(res)
  }
})

微信小程序API-设备-用户截屏事件

2020-07-28 15:51 更新

wx.onUserCaptureScreen(CALLBACK)


基础库 1.4.0 开始支持,低版本需做兼容处理

监听用户主动截屏事件,用户使用系统截屏按键截屏时触发此事件

CALLBACK返回参数:

示例代码:

wx.onUserCaptureScreen(function(res) {
    console.log('用户截屏了')
})

微信小程序API 陀螺仪

2020-07-23 14:44 更新

wx.stopGyroscope(Object object)

基础库 2.3.0 开始支持,低版本需做兼容处理

停止监听陀螺仪数据。

参数

Object object

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

wx.startGyroscope(Object object)

基础库 2.3.0 开始支持,低版本需做兼容处理

开始监听陀螺仪数据。

参数

Object object

属性类型默认值必填说明
interval string normal 监听陀螺仪数据回调函数的执行频率
success function   接口调用成功的回调函数
fail function   接口调用失败的回调函数
complete function   接口调用结束的回调函数(调用成功、失败都会执行)

object.interval 的合法值

说明最低版本
game 适用于更新游戏的回调频率,在 20ms/次 左右  
ui 适用于更新 UI 的回调频率,在 60ms/次 左右  
normal 普通的回调频率,在 200ms/次 左右

wx.onGyroscopeChange(function callback)

基础库 2.3.0 开始支持,低版本需做兼容处理

监听陀螺仪数据变化事件。频率根据 wx.startGyroscope() 的 interval 参数。可以使用 wx.stopGyroscope() 停止监听。

参数

function callback

陀螺仪数据变化事件的回调函数

参数

Object res
属性类型说明
x number x 轴的角速度
y number y 轴的角速度
z number z 轴的角速度

wx.offGyroscopeChange(function callback)

基础库 2.9.3 开始支持,低版本需做兼容处理

取消监听陀螺仪数据变化事件。

参数

function callback

陀螺仪数据变化事件的回调函数

 

 

 

 

 

posted @ 2022-03-27 21:38  hanease  阅读(213)  评论(0编辑  收藏  举报