vue3+uniapp+ts对uni.requets进行二次封装,要求包括:请求拦截器、响应拦截器、发送请求、上传文件、下载文件功能,并给出调用代码
时间: 2024-06-08 19:10:49 浏览: 9
好的,以下是对 uni.request 进行二次封装的示例代码:
```typescript
import { RequestOption, GeneralCallbackResult, UploadFileOption, DownloadFileOption } from '@dcloudio/uni-app';
/**
* 请求拦截器
*/
function requestInterceptor(options: RequestOption): RequestOption {
// 在此处可以进行请求前的处理,比如添加 token 信息等
return options;
}
/**
* 响应拦截器
*/
function responseInterceptor(response: GeneralCallbackResult): GeneralCallbackResult {
// 在此处可以进行响应后的处理,比如统一处理错误码等
return response;
}
/**
* 发送请求
* @param options 请求参数
* @returns Promise<unknown>
*/
export function sendRequest(options: RequestOption): Promise<unknown> {
// 添加请求拦截器
options = requestInterceptor(options);
// 发送请求并返回 Promise 对象
return new Promise((resolve, reject) => {
uni.request({
...options,
success: (res) => {
// 添加响应拦截器
res = responseInterceptor(res);
resolve(res);
},
fail: (err) => {
reject(err);
}
});
});
}
/**
* 上传文件
* @param options 上传文件参数
* @returns Promise<unknown>
*/
export function uploadFile(options: UploadFileOption): Promise<unknown> {
// 添加请求拦截器
options = requestInterceptor(options);
// 上传文件并返回 Promise 对象
return new Promise((resolve, reject) => {
uni.uploadFile({
...options,
success: (res) => {
// 添加响应拦截器
res = responseInterceptor(res);
resolve(res);
},
fail: (err) => {
reject(err);
}
});
});
}
/**
* 下载文件
* @param options 下载文件参数
* @returns Promise<unknown>
*/
export function downloadFile(options: DownloadFileOption): Promise<unknown> {
// 添加请求拦截器
options = requestInterceptor(options);
// 下载文件并返回 Promise 对象
return new Promise((resolve, reject) => {
uni.downloadFile({
...options,
success: (res) => {
// 添加响应拦截器
res = responseInterceptor(res);
resolve(res);
},
fail: (err) => {
reject(err);
}
});
});
}
```
调用示例:
```typescript
// 发送请求
sendRequest({
url: 'https://example.com/api/getData',
method: 'GET',
data: {
page: 1,
pageSize: 10
}
}).then(res => {
console.log(res);
}).catch(err => {
console.error(err);
});
// 上传文件
uploadFile({
url: 'https://example.com/api/uploadFile',
filePath: 'path/to/file',
name: 'file',
formData: {
'user': 'test'
}
}).then(res => {
console.log(res);
}).catch(err => {
console.error(err);
});
// 下载文件
downloadFile({
url: 'https://example.com/uploads/file.jpg',
filePath: 'downloads/file.jpg'
}).then(res => {
console.log(res);
}).catch(err => {
console.error(err);
});
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)