# 数据请求

### 目录约定

目录组织如下：

```diff
src
 ├── typings
 |   ├── index.d.ts
 |   ├── model            // 定义数据请求types类型
 |   │   ├── api.d.ts
+|   │   ├── user.d.ts
+|   │   └── order.d.ts
 |   └── types
 └── api                  // 定义数据请求层
+|   ├── user.ts
+|   └── order.ts
 └── app.ts
```

### 基础用法

**定义数据请求层**

```js
import http from '@/utils/http';

export enum Api {
  GetUserList = '/user/list',
  GetUserInfoById = '/user/info',
  SaveUserInfo = '/user/info/save',
}

// http.request，默认是GET方式
export async function getUserList() {
  return await http.request({ url: Api.GetUserList, method: 'GET' });
}

// http.get
export async function getUserInfoById(params: GetUserInfoParams) {
  return await http.get(Api.GetUserInfoById, params);
}

// http.post
export async function saveUserInfoById(data: GetUserInfoModel) {
  return await http.post(Api.SaveUserInfo, data);
}
```

**定义数据请求 types 类型**

```ts
/**
 * @namespace 用户模块
 * @description: 查询用户信息
 */
type GetUserInfoParams = {
  id: string;
};

/**
 * @namespace 用户模块
 * @description: 保存用户信息返回值
 */
interface GetUserInfoModel {
  // 用户id
  id: number;
  // 用户名
  username: string;
  // 真实名字
  realName: string;
  // 介绍
  desc?: string;
}
```

### 请求重试

```js
import http from '@/utils/http';

enum Api {
  GetUserInfoById = '/user/info',
}

export async function getUserInfoById(params) {
  return await http.request({
    url: Api.GetUserInfoById,
    params,
    retry: 3, // 重试次数
    retryDelay: 2000 // 重试间隔时间
  });
}
```
