# 微信小程序 API 速查 ## 路由导航 ```javascript // 保留当前页跳转(最多10层) wx.navigateTo({ url: '/pages/detail/detail?id=123', success(res) { res.eventChannel.emit('sendData', { data: 'hello' }) } }) // 被打开页接收 onLoad() { this.getOpenerEventChannel().on('sendData', data => {}) } // 关闭当前页跳转 wx.redirectTo({ url: '/pages/other/other' }) // 关闭所有页面 wx.reLaunch({ url: '/pages/index/index' }) // 返回 wx.navigateBack({ delta: 1 }) // TabBar页面 wx.switchTab({ url: '/pages/home/home' }) ``` ## 数据存储 ```javascript // 同步 wx.setStorageSync('key', { name: 'value' }) const data = wx.getStorageSync('key') wx.removeStorageSync('key') // 异步 wx.setStorage({ key: 'key', data: value, encrypt: true }) wx.getStorage({ key: 'key', success(res) { console.log(res.data) } }) ``` ## 网络请求 ```javascript wx.request({ url: 'https://api.example.com/data', method: 'POST', data: { id: 1 }, header: { 'Authorization': 'Bearer token' }, timeout: 10000, success(res) { console.log(res.data) }, fail(err) { console.error(err) } }) // 上传 wx.uploadFile({ url: 'https://api.example.com/upload', filePath: tempFilePath, name: 'file', formData: { userId: '123' } }) // 下载 wx.downloadFile({ url: 'https://example.com/file.pdf', success(res) { wx.openDocument({ filePath: res.tempFilePath }) } }) ``` ## 用户授权 ```javascript // 登录 wx.login({ success(res) { /* 发送 res.code 到后端 */ } }) // 获取手机号(button触发) //