微信小程序 - API接口

微信小程序提供了丰富的API接口,用于实现各种原生功能和系统能力。

API分类

网络API

发起网络请求、WebSocket连接等

媒体API

图片、音频、视频处理功能

设备API

获取设备信息、传感器等

界面API

页面跳转、交互反馈等

常用API示例

// 网络请求 wx.request({ url: 'https://example.com/api/data', method: 'GET', header: { 'content-type': 'application/json' // 默认值 }, success (res) { console.log(res.data) } }) // 获取用户信息 wx.getUserInfo({ withCredentials: false, lang: 'zh_CN', success: res => { console.log(res.userInfo) } }) // 本地存储 wx.setStorage({ key: 'key', data: 'value' }) wx.getStorage({ key: 'key', success (res) { console.log(res.data) } }) // 页面跳转 wx.navigateTo({ url: '../logs/logs' }) // 地理位置 wx.getLocation({ type: 'gcj02', // 返回可以用于wx.openLocation的经纬度 success (res) { const latitude = res.latitude const longitude = res.longitude wx.openLocation({ latitude, longitude, scale: 18 }) } }) // 交互反馈 wx.showModal({ title: '提示', content: '这是一个模态弹窗', success (res) { if (res.confirm) { console.log('用户点击确定') } else if (res.cancel) { console.log('用户点击取消') } } })
← Wechat Components Wechat Publish →