2021-03-05 19:10:39 +08:00
|
|
|
|
# @fesjs/plugin-request
|
|
|
|
|
|
|
|
|
|
基于 axios 封装的 request,内置防止重复请求、请求节流、错误处理等功能。
|
2022-05-12 10:59:32 +08:00
|
|
|
|
|
2021-03-05 19:10:39 +08:00
|
|
|
|
## 启用方式
|
|
|
|
|
|
|
|
|
|
在 `package.json` 中引入依赖:
|
2022-05-12 10:59:32 +08:00
|
|
|
|
|
2021-03-05 19:10:39 +08:00
|
|
|
|
```json
|
|
|
|
|
{
|
|
|
|
|
"dependencies": {
|
|
|
|
|
"@fesjs/fes": "^2.0.0",
|
|
|
|
|
"@fesjs/plugin-request": "^2.0.0"
|
2022-05-12 10:59:32 +08:00
|
|
|
|
}
|
2021-03-05 19:10:39 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
2022-05-12 10:59:32 +08:00
|
|
|
|
|
2021-03-05 19:10:39 +08:00
|
|
|
|
## 配置
|
|
|
|
|
|
|
|
|
|
### 构建时配置
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
export default {
|
|
|
|
|
request: {
|
2022-05-12 10:59:32 +08:00
|
|
|
|
dataField: 'result',
|
2021-03-05 19:10:39 +08:00
|
|
|
|
},
|
2022-05-12 10:59:32 +08:00
|
|
|
|
};
|
2021-03-05 19:10:39 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### dataField
|
|
|
|
|
|
2022-05-12 10:59:32 +08:00
|
|
|
|
- 类型: `string`
|
|
|
|
|
- 默认值: `''`
|
|
|
|
|
- 详情:
|
2021-03-05 19:10:39 +08:00
|
|
|
|
|
2022-05-16 14:48:25 +08:00
|
|
|
|
`dataField` 对应接口中的数据字段。假设接口统一的规范是 `{ code: string, result: any}`,可配置 `dataField: 'result'`, 直接获取数据。如果个别接口不符合这个规范,可在第三个参数加上 `dataField: false`。
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
// 构建时配置 dataField: 'result'
|
|
|
|
|
|
|
|
|
|
import { request } from '@fesjs/fes';
|
|
|
|
|
|
|
|
|
|
// 假设相应体为: {code: '0', result: {say: 'hello'}}
|
|
|
|
|
const result = await request('/path/to/query/');
|
|
|
|
|
|
|
|
|
|
// {say: 'hello'}
|
|
|
|
|
console.log(result);
|
|
|
|
|
|
|
|
|
|
// 假设相应体为: {code: '0', data: {say: 'hello'}},其中 result 字段换成了 data
|
|
|
|
|
const response1 = await request('/special/to/query/', null, { dataField: false });
|
|
|
|
|
|
|
|
|
|
// {code: '0', data: {say: 'hello'}}
|
|
|
|
|
console.log(response1);
|
|
|
|
|
|
|
|
|
|
// 或者:假设相应体为: {code: '0', data: {say: 'hello'}},其中 result 字段换成了 data
|
|
|
|
|
const response2 = await request('/special/to/query/', null, { dataField: 'data' });
|
|
|
|
|
|
|
|
|
|
// {say: 'hello'}
|
|
|
|
|
console.log(response2);
|
|
|
|
|
```
|
2021-03-05 19:10:39 +08:00
|
|
|
|
|
|
|
|
|
### 运行时配置
|
|
|
|
|
|
|
|
|
|
在 `app.js` 中进行运行时配置。
|
2022-05-12 10:59:32 +08:00
|
|
|
|
|
2021-03-05 19:10:39 +08:00
|
|
|
|
```js
|
|
|
|
|
export const request = {
|
|
|
|
|
// 格式化 response.data (只有 response.data 类型为 object 才会调用)
|
|
|
|
|
responseDataAdaptor: (data) => {
|
2022-04-11 20:02:07 +08:00
|
|
|
|
data.code = data.code === '200' ? '0' : data.code;
|
|
|
|
|
return data;
|
2021-03-05 19:10:39 +08:00
|
|
|
|
},
|
2021-07-27 16:23:00 +08:00
|
|
|
|
// 关闭 response data 校验(只判断 xhr status)
|
|
|
|
|
closeResDataCheck: false,
|
2021-03-05 19:10:39 +08:00
|
|
|
|
// 请求拦截器
|
|
|
|
|
requestInterceptors: [],
|
2022-04-11 20:02:07 +08:00
|
|
|
|
// 响应拦截器
|
2021-03-05 19:10:39 +08:00
|
|
|
|
responseInterceptors: [],
|
|
|
|
|
// 错误处理
|
|
|
|
|
// 内部以 reponse.data.code === '0' 判断请求是否成功
|
|
|
|
|
// 若使用其他字段判断,可以使用 responseDataAdaptor 对响应数据进行格式
|
|
|
|
|
errorHandler: {
|
2021-07-27 16:23:00 +08:00
|
|
|
|
11199(response) {
|
|
|
|
|
// 特殊 code 处理逻辑
|
2021-03-05 19:10:39 +08:00
|
|
|
|
},
|
2022-05-12 10:59:32 +08:00
|
|
|
|
404(error) {},
|
2021-07-27 16:23:00 +08:00
|
|
|
|
default(error) {
|
|
|
|
|
// 异常统一处理
|
2022-05-12 10:59:32 +08:00
|
|
|
|
},
|
2021-03-05 19:10:39 +08:00
|
|
|
|
},
|
|
|
|
|
// 其他 axios 配置
|
2022-05-12 10:59:32 +08:00
|
|
|
|
...otherConfigs,
|
|
|
|
|
};
|
2021-03-05 19:10:39 +08:00
|
|
|
|
```
|
2021-07-27 16:23:00 +08:00
|
|
|
|
|
|
|
|
|
#### skipErrorHandler
|
|
|
|
|
|
2022-05-12 10:59:32 +08:00
|
|
|
|
- 类型: `boolean | string | number | array<string | number>`
|
|
|
|
|
- 默认值: ``
|
|
|
|
|
- 详情:
|
2021-07-27 16:23:00 +08:00
|
|
|
|
|
|
|
|
|
指定当前请求的某些错误状态不走 `errorHandler`,单独进行处理。如果设置为 `true`,当前请求的错误处理都不走 `errorHandler`。
|
|
|
|
|
|
2022-05-12 10:59:32 +08:00
|
|
|
|
- 示列:
|
2021-07-27 16:23:00 +08:00
|
|
|
|
|
|
|
|
|
```js
|
2022-05-12 10:59:32 +08:00
|
|
|
|
import { request } from '@fesjs/fes';
|
2021-07-27 16:23:00 +08:00
|
|
|
|
|
|
|
|
|
request('/api/login', null, {
|
2022-05-12 10:59:32 +08:00
|
|
|
|
skipErrorHandler: '110',
|
2021-07-27 16:23:00 +08:00
|
|
|
|
})
|
2022-05-12 10:59:32 +08:00
|
|
|
|
.then((res) => {
|
|
|
|
|
// do something
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
// 这里处理 code 为 110 的异常
|
|
|
|
|
// 此时 errorHandler[110] 函数不会生效,也不会执行 errorHandler.default
|
|
|
|
|
});
|
2021-07-27 16:23:00 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-05 19:10:39 +08:00
|
|
|
|
## 使用
|
|
|
|
|
|
|
|
|
|
### 发起一个普通 post 请求
|
|
|
|
|
|
|
|
|
|
```js
|
2022-05-12 10:59:32 +08:00
|
|
|
|
import { request } from '@fesjs/fes';
|
2021-03-05 19:10:39 +08:00
|
|
|
|
|
|
|
|
|
request('/api/login', {
|
|
|
|
|
username: 'robby',
|
2022-05-12 10:59:32 +08:00
|
|
|
|
password: '123456',
|
2021-03-05 19:10:39 +08:00
|
|
|
|
})
|
2022-05-12 10:59:32 +08:00
|
|
|
|
.then((res) => {
|
|
|
|
|
// do something
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
// 处理异常
|
|
|
|
|
});
|
2021-03-05 19:10:39 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-07-27 16:23:00 +08:00
|
|
|
|
### merge 重复请求
|
|
|
|
|
|
|
|
|
|
连续发送多个请求,会被合并成一个请求,不会报 `REPEAT` 接口错误。
|
|
|
|
|
|
|
|
|
|
当发生 `REPEAT` 请求异常,并且确保自身代码合理的情况下,可以使用该配置。
|
|
|
|
|
|
|
|
|
|
```js
|
2022-05-12 10:59:32 +08:00
|
|
|
|
import { request } from '@fesjs/fes';
|
2021-07-27 16:23:00 +08:00
|
|
|
|
|
2022-05-12 10:59:32 +08:00
|
|
|
|
request(
|
|
|
|
|
'/api/login',
|
|
|
|
|
{
|
|
|
|
|
username: 'robby',
|
|
|
|
|
password: '123456',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
mergeRequest: true, // 在一个请求没有回来前,重复发送的请求会合并成一个请求
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.then((res) => {
|
|
|
|
|
// do something
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
// 处理异常
|
|
|
|
|
});
|
2021-07-27 16:23:00 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-05 19:10:39 +08:00
|
|
|
|
### 请求缓存
|
|
|
|
|
|
|
|
|
|
```js
|
2022-05-12 10:59:32 +08:00
|
|
|
|
import { request } from '@fesjs/fes';
|
2021-03-05 19:10:39 +08:00
|
|
|
|
|
2022-05-12 10:59:32 +08:00
|
|
|
|
request(
|
|
|
|
|
'/api/login',
|
|
|
|
|
{
|
|
|
|
|
username: 'robby',
|
|
|
|
|
password: '123456',
|
2021-03-05 19:10:39 +08:00
|
|
|
|
},
|
2022-05-12 10:59:32 +08:00
|
|
|
|
{
|
|
|
|
|
cache: {
|
|
|
|
|
cacheType: 'ram', // ram: 内存,session: sessionStorage,local:localStorage
|
|
|
|
|
cacheTime: 1000 * 60 * 3, // 缓存时间:默认3min
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.then((res) => {
|
|
|
|
|
// do something
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
// 处理异常
|
|
|
|
|
});
|
2021-03-05 19:10:39 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
若 `cache` 传 `true`,则默认使用 `ram` 缓存类型,缓存时间 3min。
|
|
|
|
|
|
|
|
|
|
### 结合 use 使用
|
|
|
|
|
|
|
|
|
|
```js
|
2022-05-12 10:59:32 +08:00
|
|
|
|
import { useRequest } from '@fesjs/fes';
|
2021-03-05 19:10:39 +08:00
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
setup() {
|
2022-05-12 10:59:32 +08:00
|
|
|
|
const { loading, data, error } = useRequest('/api/login', {
|
2021-03-05 19:10:39 +08:00
|
|
|
|
username: 'robby',
|
2022-05-12 10:59:32 +08:00
|
|
|
|
password: '123456',
|
|
|
|
|
});
|
2021-03-05 19:10:39 +08:00
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
loading,
|
|
|
|
|
data,
|
2022-05-12 10:59:32 +08:00
|
|
|
|
error,
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
};
|
2021-03-05 19:10:39 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## API
|
|
|
|
|
|
|
|
|
|
### request
|
|
|
|
|
|
2022-05-12 10:59:32 +08:00
|
|
|
|
- **类型**:函数
|
2021-03-05 19:10:39 +08:00
|
|
|
|
|
2022-05-12 10:59:32 +08:00
|
|
|
|
- **详情**:请求后端接口
|
|
|
|
|
- **参数**:
|
|
|
|
|
- url: 后端接口 url
|
|
|
|
|
- data: 参数
|
|
|
|
|
- options: 配置( 支持 axios 所有配置)
|
|
|
|
|
- **返回值**: Promise
|
2021-03-05 19:10:39 +08:00
|
|
|
|
|
|
|
|
|
### useRequest
|
|
|
|
|
|
|
|
|
|
request 的封装,返回响应式 `loading`、`error`、 `data`
|