mirror of
https://github.com/ialley-workshop-open/uni-halo.git
synced 2026-09-12 16:40:40 +08:00
refactor: 架构升级
This commit is contained in:
@@ -0,0 +1,174 @@
|
||||
# UniApp API 参考文档
|
||||
|
||||
本文档提供 uni-app 所有 API 的完整参考信息。
|
||||
|
||||
## API 分类索引
|
||||
|
||||
### 1. 网络请求(Network)
|
||||
|
||||
- [uni.request](./network.md#uni-request) - 发起网络请求
|
||||
- [uni.uploadFile](./network.md#uni-uploadfile) - 上传文件
|
||||
- [uni.downloadFile](./network.md#uni-downloadfile) - 下载文件
|
||||
- [uni.connectSocket](./network.md#uni-connectsocket) - 创建 WebSocket 连接
|
||||
- [uni.onSocketOpen](./network.md#uni-onsocketopen) - 监听 WebSocket 连接打开事件
|
||||
- [uni.onSocketError](./network.md#uni-onsocketerror) - 监听 WebSocket 错误事件
|
||||
- [uni.sendSocketMessage](./network.md#uni-sendsocketmessage) - 通过 WebSocket 发送数据
|
||||
- [uni.onSocketMessage](./network.md#uni-onsocketmessage) - 监听 WebSocket 接受到服务器的消息事件
|
||||
- [uni.closeSocket](./network.md#uni-closesocket) - 关闭 WebSocket 连接
|
||||
- [uni.onSocketClose](./network.md#uni-onsocketclose) - 监听 WebSocket 连接关闭事件
|
||||
|
||||
### 2. 数据存储(Storage)
|
||||
|
||||
- [uni.setStorage](./storage.md#uni-setstorage) - 将数据存储在本地缓存中指定的 key 中
|
||||
- [uni.setStorageSync](./storage.md#uni-setstoragesync) - 同步将数据存储在本地缓存中指定的 key 中
|
||||
- [uni.getStorage](./storage.md#uni-getstorage) - 从本地缓存中异步获取指定 key 的内容
|
||||
- [uni.getStorageSync](./storage.md#uni-getstoragesync) - 从本地缓存中同步获取指定 key 的内容
|
||||
- [uni.getStorageInfo](./storage.md#uni-getstorageinfo) - 异步获取当前 storage 的相关信息
|
||||
- [uni.getStorageInfoSync](./storage.md#uni-getstorageinfosync) - 同步获取当前 storage 的相关信息
|
||||
- [uni.removeStorage](./storage.md#uni-removestorage) - 从本地缓存中异步移除指定 key
|
||||
- [uni.removeStorageSync](./storage.md#uni-removestoragesync) - 从本地缓存中同步移除指定 key
|
||||
- [uni.clearStorage](./storage.md#uni-clearstorage) - 清理本地数据缓存
|
||||
- [uni.clearStorageSync](./storage.md#uni-clearstoragesync) - 同步清理本地数据缓存
|
||||
|
||||
### 3. 设备信息(Device)
|
||||
|
||||
- [uni.getSystemInfo](./device.md#uni-getsysteminfo) - 获取系统信息
|
||||
- [uni.getSystemInfoSync](./device.md#uni-getsysteminfosync) - 同步获取系统信息
|
||||
- [uni.getNetworkType](./device.md#uni-getnetworktype) - 获取网络类型
|
||||
- [uni.onNetworkStatusChange](./device.md#uni-onnetworkstatuschange) - 监听网络状态变化
|
||||
- [uni.getBatteryInfo](./device.md#uni-getbatteryinfo) - 获取设备电量信息
|
||||
- [uni.getBatteryInfoSync](./device.md#uni-getbatteryinfosync) - 同步获取设备电量信息
|
||||
- [uni.setScreenBrightness](./device.md#uni-setscreenbrightness) - 设置屏幕亮度
|
||||
- [uni.getScreenBrightness](./device.md#uni-getscreenbrightness) - 获取屏幕亮度
|
||||
- [uni.setKeepScreenOn](./device.md#uni-setkeepscreenon) - 设置是否保持屏幕常亮
|
||||
|
||||
### 4. 界面交互(UI)
|
||||
|
||||
- [uni.showToast](./ui.md#uni-showtoast) - 显示消息提示框
|
||||
- [uni.showLoading](./ui.md#uni-showloading) - 显示加载提示框
|
||||
- [uni.hideToast](./ui.md#uni-hidetoast) - 隐藏消息提示框
|
||||
- [uni.hideLoading](./ui.md#uni-hideloading) - 隐藏加载提示框
|
||||
- [uni.showModal](./ui.md#uni-showmodal) - 显示模态弹窗
|
||||
- [uni.showActionSheet](./ui.md#uni-showactionsheet) - 显示操作菜单
|
||||
- [uni.setNavigationBarTitle](./ui.md#uni-setnavigationbartitle) - 设置当前页面标题
|
||||
- [uni.setNavigationBarColor](./ui.md#uni-setnavigationbarcolor) - 设置页面导航栏颜色
|
||||
- [uni.showNavigationBarLoading](./ui.md#uni-shownavigationbarloading) - 显示导航栏加载动画
|
||||
- [uni.hideNavigationBarLoading](./ui.md#uni-hidenavigationbarloading) - 隐藏导航栏加载动画
|
||||
- [uni.setTabBarBadge](./ui.md#uni-settabbarbadge) - 为 tabBar 某一项的右上角添加文本
|
||||
- [uni.removeTabBarBadge](./ui.md#uni-removetabbarbadge) - 移除 tabBar 某一项右上角的文本
|
||||
- [uni.showTabBarRedDot](./ui.md#uni-showtabbarreddot) - 显示 tabBar 某一项的右上角的红点
|
||||
- [uni.hideTabBarRedDot](./ui.md#uni-hidetabbarreddot) - 隐藏 tabBar 某一项的右上角的红点
|
||||
- [uni.setTabBarStyle](./ui.md#uni-settabbarstyle) - 动态设置 tabBar 的整体样式
|
||||
- [uni.setTabBarItem](./ui.md#uni-settabbaritem) - 动态设置 tabBar 某一项的内容
|
||||
|
||||
### 5. 位置服务(Location)
|
||||
|
||||
- [uni.getLocation](./location.md#uni-getlocation) - 获取当前地理位置
|
||||
- [uni.openLocation](./location.md#uni-openlocation) - 使用微信内置地图查看位置
|
||||
- [uni.chooseLocation](./location.md#uni-chooselocation) - 打开地图选择位置
|
||||
|
||||
### 6. 媒体处理(Media)
|
||||
|
||||
- [uni.chooseImage](./media.md#uni-chooseimage) - 从本地相册选择图片或使用相机拍照
|
||||
- [uni.previewImage](./media.md#uni-previewimage) - 预览图片
|
||||
- [uni.getImageInfo](./media.md#uni-getimageinfo) - 获取图片信息
|
||||
- [uni.saveImageToPhotosAlbum](./media.md#uni-saveimagetophotosalbum) - 保存图片到系统相册
|
||||
- [uni.chooseVideo](./media.md#uni-choosevideo) - 从本地相册选择视频或使用相机拍摄视频
|
||||
- [uni.saveVideoToPhotosAlbum](./media.md#uni-savevideotophotosalbum) - 保存视频到系统相册
|
||||
- [uni.chooseMedia](./media.md#uni-choosemedia) - 拍摄或从手机相册中选择图片或视频
|
||||
- [uni.chooseFile](./media.md#uni-choosefile) - 从本地选择文件
|
||||
|
||||
### 7. 页面路由(Navigation)
|
||||
|
||||
- [uni.navigateTo](./navigation.md#uni-navigateto) - 保留当前页面,跳转到应用内的某个页面
|
||||
- [uni.redirectTo](./navigation.md#uni-redirectto) - 关闭当前页面,跳转到应用内的某个页面
|
||||
- [uni.reLaunch](./navigation.md#uni-relaunch) - 关闭所有页面,打开到应用内的某个页面
|
||||
- [uni.switchTab](./navigation.md#uni-switchtab) - 跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面
|
||||
- [uni.navigateBack](./navigation.md#uni-navigateback) - 关闭当前页面,返回上一页面或多级页面
|
||||
|
||||
### 8. 文件操作(File)
|
||||
|
||||
- [uni.saveFile](./file.md#uni-savefile) - 保存文件到本地
|
||||
- [uni.getFileInfo](./file.md#uni-getfileinfo) - 获取文件信息
|
||||
- [uni.getSavedFileList](./file.md#uni-getsavedfilelist) - 获取已保存的文件列表
|
||||
- [uni.getSavedFileInfo](./file.md#uni-getsavedfileinfo) - 获取已保存的文件信息
|
||||
- [uni.removeSavedFile](./file.md#uni-removesavedfile) - 删除已保存的文件
|
||||
|
||||
### 9. 支付(Payment)
|
||||
|
||||
- [uni.requestPayment](./payment.md#uni-requestpayment) - 发起支付
|
||||
|
||||
### 10. 分享(Share)
|
||||
|
||||
- [uni.share](./share.md#uni-share) - 分享内容
|
||||
|
||||
### 11. 其他 API(Other)
|
||||
|
||||
- [uni.canIUse](./other.md#uni-caniuse) - 判断应用的版本是否支持某个 API
|
||||
- [uni.getProvider](./other.md#uni-getprovider) - 获取服务供应商
|
||||
- [uni.login](./other.md#uni-login) - 登录
|
||||
- [uni.getUserInfo](./other.md#uni-getuserinfo) - 获取用户信息
|
||||
- [uni.checkSession](./other.md#uni-checksession) - 检查登录状态是否过期
|
||||
- [uni.authorize](./other.md#uni-authorize) - 提前向用户发起授权请求
|
||||
- [uni.openSetting](./other.md#uni-opensetting) - 调起客户端小程序设置页面
|
||||
- [uni.getSetting](./other.md#uni-getsetting) - 获取用户的当前设置
|
||||
|
||||
## API 调用规范
|
||||
|
||||
### 回调函数
|
||||
|
||||
uni-app API 支持两种调用方式:
|
||||
|
||||
1. **回调函数方式**:
|
||||
```javascript
|
||||
uni.request({
|
||||
url: 'https://api.example.com/data',
|
||||
success: (res) => {
|
||||
console.log(res.data)
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error(err)
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
2. **Promise 方式**(部分 API 支持):
|
||||
```javascript
|
||||
uni.request({
|
||||
url: 'https://api.example.com/data'
|
||||
}).then(res => {
|
||||
console.log(res.data)
|
||||
}).catch(err => {
|
||||
console.error(err)
|
||||
})
|
||||
```
|
||||
|
||||
### 同步 API
|
||||
|
||||
部分 API 提供同步版本(以 Sync 结尾),如:
|
||||
- `uni.getStorageSync()` - 同步获取存储
|
||||
- `uni.setStorageSync()` - 同步设置存储
|
||||
- `uni.getSystemInfoSync()` - 同步获取系统信息
|
||||
|
||||
### 平台兼容性
|
||||
|
||||
每个 API 的详细平台支持情况见对应 API 的文档。使用前请检查平台兼容性,必要时使用条件编译:
|
||||
|
||||
```javascript
|
||||
// #ifdef MP-WEIXIN
|
||||
uni.requestPayment({
|
||||
// 微信小程序支付
|
||||
})
|
||||
// #endif
|
||||
|
||||
// #ifdef APP-PLUS
|
||||
uni.requestPayment({
|
||||
// App 支付
|
||||
})
|
||||
// #endif
|
||||
```
|
||||
|
||||
## 参考资源
|
||||
|
||||
- [uni-app API 文档](https://doc.dcloud.net.cn/uni-app-x/api/)
|
||||
- [uni-app 条件编译](https://uniapp.dcloud.net.cn/tutorial/platform.html)
|
||||
@@ -0,0 +1,63 @@
|
||||
# 设备信息 API
|
||||
|
||||
## 概述
|
||||
|
||||
设备信息 API 用于获取设备系统信息、网络状态等。
|
||||
|
||||
## API 列表
|
||||
|
||||
### uni.getSystemInfo
|
||||
|
||||
获取系统信息。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/system/system-info.html#getsysteminfo
|
||||
|
||||
**返回值**:
|
||||
- `brand` - 手机品牌
|
||||
- `model` - 手机型号
|
||||
- `pixelRatio` - 设备像素比
|
||||
- `screenWidth` - 屏幕宽度
|
||||
- `screenHeight` - 屏幕高度
|
||||
- `windowWidth` - 可使用窗口宽度
|
||||
- `windowHeight` - 可使用窗口高度
|
||||
- `statusBarHeight` - 状态栏的高度
|
||||
- `language` - 应用设置的语言
|
||||
- `version` - 版本号
|
||||
- `platform` - 平台
|
||||
|
||||
**示例**:
|
||||
```javascript
|
||||
uni.getSystemInfo({
|
||||
success: (res) => {
|
||||
console.log('屏幕宽度', res.screenWidth)
|
||||
console.log('屏幕高度', res.screenHeight)
|
||||
console.log('状态栏高度', res.statusBarHeight)
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### uni.getSystemInfoSync
|
||||
|
||||
同步获取系统信息。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/system/system-info.html#getsysteminfosync
|
||||
|
||||
### uni.getNetworkType
|
||||
|
||||
获取网络类型。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/system/network.html#getnetworktype
|
||||
|
||||
**返回值**:
|
||||
- `networkType` - 网络类型(wifi、2g、3g、4g、unknown、none)
|
||||
|
||||
### uni.onNetworkStatusChange
|
||||
|
||||
监听网络状态变化。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/system/network.html#onnetworkstatuschange
|
||||
|
||||
## 参考资源
|
||||
|
||||
- [uni-app 系统信息文档](https://doc.dcloud.net.cn/uni-app-x/api/system/system-info.html)
|
||||
- [uni-app 网络状态文档](https://doc.dcloud.net.cn/uni-app-x/api/system/network.html)
|
||||
@@ -0,0 +1,35 @@
|
||||
# 文件操作 API
|
||||
|
||||
## 概述
|
||||
|
||||
文件操作 API 用于保存文件、获取文件信息等。
|
||||
|
||||
## API 列表
|
||||
|
||||
### uni.saveFile
|
||||
|
||||
保存文件到本地。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/file/file.html#savefile
|
||||
|
||||
### uni.getFileInfo
|
||||
|
||||
获取文件信息。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/file/file.html#getfileinfo
|
||||
|
||||
### uni.getSavedFileList
|
||||
|
||||
获取已保存的文件列表。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/file/file.html#getsavedfilelist
|
||||
|
||||
### uni.removeSavedFile
|
||||
|
||||
删除已保存的文件。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/file/file.html#removesavedfile
|
||||
|
||||
## 参考资源
|
||||
|
||||
- [uni-app 文件文档](https://doc.dcloud.net.cn/uni-app-x/api/file/file.html)
|
||||
@@ -0,0 +1,59 @@
|
||||
# 位置服务 API
|
||||
|
||||
## 概述
|
||||
|
||||
位置服务 API 用于获取地理位置、打开地图等。
|
||||
|
||||
## API 列表
|
||||
|
||||
### uni.getLocation
|
||||
|
||||
获取当前地理位置。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/location/location.html#getlocation
|
||||
|
||||
**参数**:
|
||||
- `type` (String) - 默认为 wgs84 返回 gps 坐标,gcj02 返回可用于 uni.openLocation 的坐标
|
||||
- `altitude` (Boolean) - 传入 true 会返回高度信息
|
||||
- `geocode` (Boolean) - 默认 false,传入 true 会解析地址信息
|
||||
|
||||
**返回值**:
|
||||
- `latitude` - 纬度
|
||||
- `longitude` - 经度
|
||||
- `speed` - 速度
|
||||
- `accuracy` - 位置的精确度
|
||||
- `altitude` - 高度
|
||||
- `verticalAccuracy` - 垂直精度
|
||||
- `horizontalAccuracy` - 水平精度
|
||||
|
||||
**示例**:
|
||||
```javascript
|
||||
uni.getLocation({
|
||||
type: 'gcj02',
|
||||
success: (res) => {
|
||||
console.log('当前位置', res.latitude, res.longitude)
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### uni.openLocation
|
||||
|
||||
使用微信内置地图查看位置。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/location/open-location.html#openlocation
|
||||
|
||||
**参数**:
|
||||
- `latitude` (Number) - 纬度
|
||||
- `longitude` (Number) - 经度
|
||||
- `name` (String) - 位置名称
|
||||
- `address` (String) - 地址的详细说明
|
||||
|
||||
### uni.chooseLocation
|
||||
|
||||
打开地图选择位置。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/location/choose-location.html#chooselocation
|
||||
|
||||
## 参考资源
|
||||
|
||||
- [uni-app 位置服务文档](https://doc.dcloud.net.cn/uni-app-x/api/location/location.html)
|
||||
@@ -0,0 +1,67 @@
|
||||
# 媒体处理 API
|
||||
|
||||
## 概述
|
||||
|
||||
媒体处理 API 用于选择图片、预览图片、选择视频等。
|
||||
|
||||
## API 列表
|
||||
|
||||
### uni.chooseImage
|
||||
|
||||
从本地相册选择图片或使用相机拍照。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/media/image.html#chooseimage
|
||||
|
||||
**参数**:
|
||||
- `count` (Number) - 最多可以选择的图片张数,默认 9
|
||||
- `sizeType` (Array) - 所选的图片的尺寸(original、compressed)
|
||||
- `sourceType` (Array) - 选择图片的来源(album、camera)
|
||||
|
||||
**返回值**:
|
||||
- `tempFilePaths` - 图片的本地文件路径列表
|
||||
- `tempFiles` - 图片的本地文件列表
|
||||
|
||||
**示例**:
|
||||
```javascript
|
||||
uni.chooseImage({
|
||||
count: 1,
|
||||
sizeType: ['original', 'compressed'],
|
||||
sourceType: ['album', 'camera'],
|
||||
success: (res) => {
|
||||
console.log('选择的图片', res.tempFilePaths)
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### uni.previewImage
|
||||
|
||||
预览图片。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/media/image.html#previewimage
|
||||
|
||||
**参数**:
|
||||
- `urls` (Array) - 需要预览的图片 http 链接列表
|
||||
- `current` (Number) - 当前显示图片的索引
|
||||
|
||||
### uni.getImageInfo
|
||||
|
||||
获取图片信息。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/media/image.html#getimageinfo
|
||||
|
||||
### uni.saveImageToPhotosAlbum
|
||||
|
||||
保存图片到系统相册。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/media/image.html#saveimagetophotosalbum
|
||||
|
||||
### uni.chooseVideo
|
||||
|
||||
从本地相册选择视频或使用相机拍摄视频。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/media/video.html#choosevideo
|
||||
|
||||
## 参考资源
|
||||
|
||||
- [uni-app 图片文档](https://doc.dcloud.net.cn/uni-app-x/api/media/image.html)
|
||||
- [uni-app 视频文档](https://doc.dcloud.net.cn/uni-app-x/api/media/video.html)
|
||||
@@ -0,0 +1,60 @@
|
||||
# 页面路由 API
|
||||
|
||||
## 概述
|
||||
|
||||
页面路由 API 用于页面跳转和导航。
|
||||
|
||||
## API 列表
|
||||
|
||||
### uni.navigateTo
|
||||
|
||||
保留当前页面,跳转到应用内的某个页面。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/router.html#navigateto
|
||||
|
||||
**参数**:
|
||||
- `url` (String) - 需要跳转的应用内非 tabBar 的页面的路径,路径后可以带参数
|
||||
- `success` (Function) - 接口调用成功的回调函数
|
||||
- `fail` (Function) - 接口调用失败的回调函数
|
||||
- `complete` (Function) - 接口调用结束的回调函数
|
||||
|
||||
**示例**:
|
||||
```javascript
|
||||
uni.navigateTo({
|
||||
url: '/pages/detail/detail?id=123&name=test',
|
||||
success: () => {
|
||||
console.log('跳转成功')
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### uni.redirectTo
|
||||
|
||||
关闭当前页面,跳转到应用内的某个页面。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/router.html#redirectto
|
||||
|
||||
### uni.reLaunch
|
||||
|
||||
关闭所有页面,打开到应用内的某个页面。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/router.html#relaunch
|
||||
|
||||
### uni.switchTab
|
||||
|
||||
跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/router.html#switchtab
|
||||
|
||||
### uni.navigateBack
|
||||
|
||||
关闭当前页面,返回上一页面或多级页面。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/router.html#navigateback
|
||||
|
||||
**参数**:
|
||||
- `delta` (Number) - 返回的页面数,如果 delta 大于现有页面数,则返回到首页
|
||||
|
||||
## 参考资源
|
||||
|
||||
- [uni-app 路由文档](https://doc.dcloud.net.cn/uni-app-x/api/router.html)
|
||||
@@ -0,0 +1,276 @@
|
||||
# 网络请求 API
|
||||
|
||||
## uni.request
|
||||
|
||||
发起网络请求。
|
||||
|
||||
### 参数
|
||||
|
||||
| 参数名 | 类型 | 必填 | 说明 | 平台差异说明 |
|
||||
|--------|------|------|------|--------------|
|
||||
| url | String | 是 | 开发者服务器接口地址 | - |
|
||||
| data | Object/String/ArrayBuffer | 否 | 请求的参数 | - |
|
||||
| header | Object | 否 | 设置请求的 header,header 中不能设置 Referer | - |
|
||||
| method | String | 否 | HTTP 请求方法,默认为 GET | - |
|
||||
| timeout | Number | 否 | 超时时间,单位为 ms,默认为 60000 | - |
|
||||
| dataType | String | 否 | 如果设为 json,会尝试对返回的数据做一次 JSON.parse | - |
|
||||
| responseType | String | 否 | 设置响应的数据类型,默认为 text | - |
|
||||
| sslVerify | Boolean | 否 | 验证 ssl 证书 | App |
|
||||
| withCredentials | Boolean | 否 | 跨域请求时是否携带凭证(cookies) | H5 |
|
||||
| firstIpv4 | Boolean | 否 | DNS 解析时优先使用 ipv4 | App |
|
||||
|
||||
### 返回值
|
||||
|
||||
| 参数名 | 类型 | 说明 |
|
||||
|--------|------|------|
|
||||
| data | Object/String/ArrayBuffer | 服务器返回的数据 |
|
||||
| statusCode | Number | HTTP 状态码 |
|
||||
| header | Object | 服务器返回的 header |
|
||||
| cookies | Array | 服务器返回的 cookies |
|
||||
|
||||
### 平台支持
|
||||
|
||||
| 平台 | 支持情况 |
|
||||
|------|---------|
|
||||
| H5 | ✅ |
|
||||
| 微信小程序 | ✅ |
|
||||
| 支付宝小程序 | ✅ |
|
||||
| 百度小程序 | ✅ |
|
||||
| 字节跳动小程序 | ✅ |
|
||||
| QQ 小程序 | ✅ |
|
||||
| 快手小程序 | ✅ |
|
||||
| App | ✅ |
|
||||
| 快应用 | ✅ |
|
||||
|
||||
### 示例
|
||||
|
||||
```javascript
|
||||
// GET 请求
|
||||
uni.request({
|
||||
url: 'https://api.example.com/data',
|
||||
method: 'GET',
|
||||
data: {
|
||||
id: 123
|
||||
},
|
||||
success: (res) => {
|
||||
console.log(res.data)
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error(err)
|
||||
}
|
||||
})
|
||||
|
||||
// POST 请求
|
||||
uni.request({
|
||||
url: 'https://api.example.com/data',
|
||||
method: 'POST',
|
||||
data: {
|
||||
name: 'John',
|
||||
age: 30
|
||||
},
|
||||
header: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
success: (res) => {
|
||||
console.log(res.data)
|
||||
}
|
||||
})
|
||||
|
||||
// 使用 Promise
|
||||
uni.request({
|
||||
url: 'https://api.example.com/data'
|
||||
}).then(res => {
|
||||
console.log(res.data)
|
||||
}).catch(err => {
|
||||
console.error(err)
|
||||
})
|
||||
```
|
||||
|
||||
## uni.uploadFile
|
||||
|
||||
上传文件。
|
||||
|
||||
### 参数
|
||||
|
||||
| 参数名 | 类型 | 必填 | 说明 | 平台差异说明 |
|
||||
|--------|------|------|------|--------------|
|
||||
| url | String | 是 | 开发者服务器 url | - |
|
||||
| filePath | String | 是 | 要上传文件资源的路径 | - |
|
||||
| name | String | 是 | 文件对应的 key,开发者在服务端可以通过这个 key 获取文件的二进制内容 | - |
|
||||
| header | Object | 否 | HTTP 请求 Header,header 中不能设置 Referer | - |
|
||||
| formData | Object | 否 | HTTP 请求中其他额外的 form data | - |
|
||||
| timeout | Number | 否 | 超时时间,单位为 ms,默认为 60000 | - |
|
||||
|
||||
### 返回值
|
||||
|
||||
| 参数名 | 类型 | 说明 |
|
||||
|--------|------|------|
|
||||
| data | String | 服务器返回的数据 |
|
||||
| statusCode | Number | HTTP 状态码 |
|
||||
|
||||
### 平台支持
|
||||
|
||||
| 平台 | 支持情况 |
|
||||
|------|---------|
|
||||
| H5 | ✅ |
|
||||
| 微信小程序 | ✅ |
|
||||
| 支付宝小程序 | ✅ |
|
||||
| 百度小程序 | ✅ |
|
||||
| 字节跳动小程序 | ✅ |
|
||||
| QQ 小程序 | ✅ |
|
||||
| 快手小程序 | ✅ |
|
||||
| App | ✅ |
|
||||
| 快应用 | ❌ |
|
||||
|
||||
### 示例
|
||||
|
||||
```javascript
|
||||
uni.chooseImage({
|
||||
count: 1,
|
||||
success: (res) => {
|
||||
const tempFilePaths = res.tempFilePaths
|
||||
uni.uploadFile({
|
||||
url: 'https://api.example.com/upload',
|
||||
filePath: tempFilePaths[0],
|
||||
name: 'file',
|
||||
formData: {
|
||||
'user': 'test'
|
||||
},
|
||||
success: (uploadRes) => {
|
||||
console.log(uploadRes.data)
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## uni.downloadFile
|
||||
|
||||
下载文件。
|
||||
|
||||
### 参数
|
||||
|
||||
| 参数名 | 类型 | 必填 | 说明 | 平台差异说明 |
|
||||
|--------|------|------|------|--------------|
|
||||
| url | String | 是 | 下载资源的 url | - |
|
||||
| header | Object | 否 | HTTP 请求 Header,header 中不能设置 Referer | - |
|
||||
| timeout | Number | 否 | 超时时间,单位为 ms,默认为 60000 | - |
|
||||
|
||||
### 返回值
|
||||
|
||||
| 参数名 | 类型 | 说明 |
|
||||
|--------|------|------|
|
||||
| tempFilePath | String | 临时文件路径,下载后的文件会存储到一个临时文件 |
|
||||
| statusCode | Number | HTTP 状态码 |
|
||||
|
||||
### 平台支持
|
||||
|
||||
| 平台 | 支持情况 |
|
||||
|------|---------|
|
||||
| H5 | ✅ |
|
||||
| 微信小程序 | ✅ |
|
||||
| 支付宝小程序 | ✅ |
|
||||
| 百度小程序 | ✅ |
|
||||
| 字节跳动小程序 | ✅ |
|
||||
| QQ 小程序 | ✅ |
|
||||
| 快手小程序 | ✅ |
|
||||
| App | ✅ |
|
||||
| 快应用 | ❌ |
|
||||
|
||||
### 示例
|
||||
|
||||
```javascript
|
||||
uni.downloadFile({
|
||||
url: 'https://example.com/file.pdf',
|
||||
success: (res) => {
|
||||
if (res.statusCode === 200) {
|
||||
console.log('下载成功', res.tempFilePath)
|
||||
// 可以调用 uni.openDocument 打开文件
|
||||
uni.openDocument({
|
||||
filePath: res.tempFilePath,
|
||||
success: () => {
|
||||
console.log('打开文档成功')
|
||||
}
|
||||
})
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('下载失败', err)
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## uni.connectSocket
|
||||
|
||||
创建 WebSocket 连接。
|
||||
|
||||
### 参数
|
||||
|
||||
| 参数名 | 类型 | 必填 | 说明 | 平台差异说明 |
|
||||
|--------|------|------|------|--------------|
|
||||
| url | String | 是 | 服务器接口地址,必须是 wss 协议,且域名必须是后台配置的合法域名 | - |
|
||||
| header | Object | 否 | HTTP Header,header 中不能设置 Referer | - |
|
||||
| protocols | Array | 否 | 子协议数组 | H5 |
|
||||
| method | String | 否 | 请求方法 | App |
|
||||
|
||||
### 平台支持
|
||||
|
||||
| 平台 | 支持情况 |
|
||||
|------|---------|
|
||||
| H5 | ✅ |
|
||||
| 微信小程序 | ✅ |
|
||||
| 支付宝小程序 | ✅ |
|
||||
| 百度小程序 | ✅ |
|
||||
| 字节跳动小程序 | ✅ |
|
||||
| QQ 小程序 | ✅ |
|
||||
| 快手小程序 | ✅ |
|
||||
| App | ✅ |
|
||||
| 快应用 | ❌ |
|
||||
|
||||
### 示例
|
||||
|
||||
```javascript
|
||||
// 创建 WebSocket 连接
|
||||
uni.connectSocket({
|
||||
url: 'wss://example.com/websocket',
|
||||
success: () => {
|
||||
console.log('连接成功')
|
||||
}
|
||||
})
|
||||
|
||||
// 监听 WebSocket 连接打开事件
|
||||
uni.onSocketOpen((res) => {
|
||||
console.log('WebSocket 连接已打开')
|
||||
})
|
||||
|
||||
// 监听 WebSocket 错误事件
|
||||
uni.onSocketError((res) => {
|
||||
console.error('WebSocket 错误', res)
|
||||
})
|
||||
|
||||
// 发送数据
|
||||
uni.sendSocketMessage({
|
||||
data: JSON.stringify({
|
||||
type: 'message',
|
||||
content: 'Hello'
|
||||
})
|
||||
})
|
||||
|
||||
// 监听接收消息
|
||||
uni.onSocketMessage((res) => {
|
||||
console.log('收到消息', res.data)
|
||||
})
|
||||
|
||||
// 关闭连接
|
||||
uni.closeSocket()
|
||||
```
|
||||
|
||||
## 参考资源
|
||||
|
||||
- [uni-app 网络请求文档](https://doc.dcloud.net.cn/uni-app-x/api/network/request.html)
|
||||
- [uni-app 文件上传文档](https://doc.dcloud.net.cn/uni-app-x/api/network/upload.html)
|
||||
- [uni-app 文件下载文档](https://doc.dcloud.net.cn/uni-app-x/api/network/download.html)
|
||||
- [uni-app WebSocket 文档](https://doc.dcloud.net.cn/uni-app-x/api/network/websocket.html)
|
||||
@@ -0,0 +1,58 @@
|
||||
# 其他 API
|
||||
|
||||
## 概述
|
||||
|
||||
其他 API 包括工具类 API、登录 API 等。
|
||||
|
||||
## API 列表
|
||||
|
||||
### uni.canIUse
|
||||
|
||||
判断应用的版本是否支持某个 API。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/system/info.html#caniuse
|
||||
|
||||
**示例**:
|
||||
```javascript
|
||||
if (uni.canIUse('getSystemInfo')) {
|
||||
uni.getSystemInfo({
|
||||
success: (res) => {
|
||||
console.log(res)
|
||||
}
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
### uni.login
|
||||
|
||||
登录。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/login/login.html#login
|
||||
|
||||
### uni.getUserInfo
|
||||
|
||||
获取用户信息。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/login/login.html#getuserinfo
|
||||
|
||||
### uni.authorize
|
||||
|
||||
提前向用户发起授权请求。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/authorize.html#authorize
|
||||
|
||||
### uni.openSetting
|
||||
|
||||
调起客户端小程序设置页面。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/setting/open-setting.html#opensetting
|
||||
|
||||
### uni.getSetting
|
||||
|
||||
获取用户的当前设置。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/setting/get-setting.html#getsetting
|
||||
|
||||
## 参考资源
|
||||
|
||||
- [uni-app API 文档](https://doc.dcloud.net.cn/uni-app-x/api/)
|
||||
@@ -0,0 +1,43 @@
|
||||
# 支付 API
|
||||
|
||||
## 概述
|
||||
|
||||
支付 API 用于发起支付。
|
||||
|
||||
## API 列表
|
||||
|
||||
### uni.requestPayment
|
||||
|
||||
发起支付。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/payment/payment.html#requestpayment
|
||||
|
||||
**参数**(微信小程序):
|
||||
- `provider` (String) - 支付服务提供商(wxpay)
|
||||
- `timeStamp` (String) - 时间戳
|
||||
- `nonceStr` (String) - 随机字符串
|
||||
- `package` (String) - 统一下单接口返回的 prepay_id 参数值
|
||||
- `signType` (String) - 签名算法
|
||||
- `paySign` (String) - 签名
|
||||
|
||||
**示例**:
|
||||
```javascript
|
||||
uni.requestPayment({
|
||||
provider: 'wxpay',
|
||||
timeStamp: String(Date.now()),
|
||||
nonceStr: 'nonceStr',
|
||||
package: 'prepay_id=xxx',
|
||||
signType: 'MD5',
|
||||
paySign: 'paySign',
|
||||
success: (res) => {
|
||||
console.log('支付成功', res)
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('支付失败', err)
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## 参考资源
|
||||
|
||||
- [uni-app 支付文档](https://doc.dcloud.net.cn/uni-app-x/api/payment/payment.html)
|
||||
@@ -0,0 +1,25 @@
|
||||
# 分享 API
|
||||
|
||||
## 概述
|
||||
|
||||
分享 API 用于分享内容。
|
||||
|
||||
## API 列表
|
||||
|
||||
### uni.share
|
||||
|
||||
分享内容。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/share/share.html#share
|
||||
|
||||
**参数**:
|
||||
- `provider` (String) - 分享服务提供商(weixin、sinaweibo、qq)
|
||||
- `type` (Number) - 分享类型(0-图文、1-纯文字、2-纯图片、3-音乐、4-视频、5-小程序)
|
||||
- `href` (String) - 分享链接
|
||||
- `title` (String) - 分享标题
|
||||
- `summary` (String) - 分享摘要
|
||||
- `imageUrl` (String) - 分享图片
|
||||
|
||||
## 参考资源
|
||||
|
||||
- [uni-app 分享文档](https://doc.dcloud.net.cn/uni-app-x/api/share/share.html)
|
||||
@@ -0,0 +1,65 @@
|
||||
# 数据存储 API
|
||||
|
||||
## 概述
|
||||
|
||||
数据存储 API 用于在本地存储和获取数据。
|
||||
|
||||
## API 列表
|
||||
|
||||
### uni.setStorage
|
||||
|
||||
将数据存储在本地缓存中指定的 key 中。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/storage/storage.html#setstorage
|
||||
|
||||
**参数**:
|
||||
- `key` (String) - 本地缓存中指定的 key
|
||||
- `data` (Any) - 需要存储的内容
|
||||
- `success` (Function) - 接口调用成功的回调函数
|
||||
- `fail` (Function) - 接口调用失败的回调函数
|
||||
- `complete` (Function) - 接口调用结束的回调函数
|
||||
|
||||
**示例**:
|
||||
```javascript
|
||||
uni.setStorage({
|
||||
key: 'userInfo',
|
||||
data: { name: 'John', age: 30 },
|
||||
success: () => {
|
||||
console.log('存储成功')
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### uni.getStorage
|
||||
|
||||
从本地缓存中异步获取指定 key 的内容。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/storage/storage.html#getstorage
|
||||
|
||||
### uni.removeStorage
|
||||
|
||||
从本地缓存中异步移除指定 key。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/storage/storage.html#removestorage
|
||||
|
||||
### uni.clearStorage
|
||||
|
||||
清理本地数据缓存。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/storage/storage.html#clearstorage
|
||||
|
||||
### uni.setStorageSync
|
||||
|
||||
同步将数据存储在本地缓存中指定的 key 中。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/storage/storage.html#setstoragesync
|
||||
|
||||
### uni.getStorageSync
|
||||
|
||||
从本地缓存中同步获取指定 key 的内容。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/storage/storage.html#getstoragesync
|
||||
|
||||
## 参考资源
|
||||
|
||||
- [uni-app 数据存储文档](https://doc.dcloud.net.cn/uni-app-x/api/storage/storage.html)
|
||||
@@ -0,0 +1,75 @@
|
||||
# 界面交互 API
|
||||
|
||||
## 概述
|
||||
|
||||
界面交互 API 用于显示提示、对话框、操作菜单等。
|
||||
|
||||
## API 列表
|
||||
|
||||
### uni.showToast
|
||||
|
||||
显示消息提示框。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/ui/prompt.html#showtoast
|
||||
|
||||
**参数**:
|
||||
- `title` (String) - 提示的内容
|
||||
- `icon` (String) - 图标类型(success、error、loading、none)
|
||||
- `image` (String) - 自定义图标的本地路径
|
||||
- `duration` (Number) - 提示的延迟时间,默认 2000
|
||||
- `mask` (Boolean) - 是否显示透明蒙层,防止触摸穿透
|
||||
|
||||
**示例**:
|
||||
```javascript
|
||||
uni.showToast({
|
||||
title: '操作成功',
|
||||
icon: 'success',
|
||||
duration: 2000
|
||||
})
|
||||
```
|
||||
|
||||
### uni.showModal
|
||||
|
||||
显示模态弹窗。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/ui/prompt.html#showmodal
|
||||
|
||||
**参数**:
|
||||
- `title` (String) - 提示的标题
|
||||
- `content` (String) - 提示的内容
|
||||
- `showCancel` (Boolean) - 是否显示取消按钮
|
||||
- `cancelText` (String) - 取消按钮的文字
|
||||
- `confirmText` (String) - 确认按钮的文字
|
||||
|
||||
**返回值**:
|
||||
- `confirm` (Boolean) - 为 true 时,表示用户点击了确定按钮
|
||||
- `cancel` (Boolean) - 为 true 时,表示用户点击了取消按钮
|
||||
|
||||
### uni.showActionSheet
|
||||
|
||||
显示操作菜单。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/ui/prompt.html#showactionsheet
|
||||
|
||||
### uni.showLoading
|
||||
|
||||
显示加载提示框。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/ui/prompt.html#showloading
|
||||
|
||||
### uni.hideLoading
|
||||
|
||||
隐藏加载提示框。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/ui/prompt.html#hideloading
|
||||
|
||||
### uni.setNavigationBarTitle
|
||||
|
||||
设置当前页面标题。
|
||||
|
||||
**详细文档**:https://doc.dcloud.net.cn/uni-app-x/api/ui/navigation-bar.html#setnavigationbartitle
|
||||
|
||||
## 参考资源
|
||||
|
||||
- [uni-app 界面交互文档](https://doc.dcloud.net.cn/uni-app-x/api/ui/prompt.html)
|
||||
- [uni-app 导航栏文档](https://doc.dcloud.net.cn/uni-app-x/api/ui/navigation-bar.html)
|
||||
Reference in New Issue
Block a user