Commit a821d9a3a279f0e6a5b7dbb316725d603ce30f74
1 parent
7c2f8516
perf: imporve axios logic
Showing
13 changed files
with
77 additions
and
74 deletions
src/api/demo/account.ts
1 | 1 | import { defHttp } from '/@/utils/http/axios'; |
2 | 2 | import { GetAccountInfoModel } from './model/accountModel'; |
3 | 3 | |
4 | +const { get } = defHttp; | |
5 | + | |
4 | 6 | enum Api { |
5 | 7 | ACCOUNT_INFO = '/account/getAccountInfo', |
6 | 8 | } |
7 | 9 | |
8 | 10 | // Get personal center-basic settings |
9 | -export function accountInfoApi() { | |
10 | - return defHttp.request<GetAccountInfoModel>({ | |
11 | - url: Api.ACCOUNT_INFO, | |
12 | - method: 'GET', | |
13 | - }); | |
14 | -} | |
11 | + | |
12 | +export const accountInfoApi = () => get<GetAccountInfoModel>({ url: Api.ACCOUNT_INFO }); | ... | ... |
src/api/demo/error.ts
1 | 1 | import { defHttp } from '/@/utils/http/axios'; |
2 | 2 | |
3 | +const { get } = defHttp; | |
4 | + | |
3 | 5 | enum Api { |
4 | 6 | // The address does not exist |
5 | 7 | Error = '/error', |
... | ... | @@ -8,9 +10,5 @@ enum Api { |
8 | 10 | /** |
9 | 11 | * @description: Trigger ajax error |
10 | 12 | */ |
11 | -export function fireErrorApi() { | |
12 | - return defHttp.request({ | |
13 | - url: Api.Error, | |
14 | - method: 'GET', | |
15 | - }); | |
16 | -} | |
13 | + | |
14 | +export const fireErrorApi = () => get({ url: Api.Error }); | ... | ... |
src/api/demo/select.ts
1 | 1 | import { defHttp } from '/@/utils/http/axios'; |
2 | 2 | import { DemoOptionsGetResultModel } from './model/optionsModel'; |
3 | +const { get } = defHttp; | |
3 | 4 | |
4 | 5 | enum Api { |
5 | 6 | OPTIONS_LIST = '/select/getDemoOptions', |
... | ... | @@ -8,9 +9,4 @@ enum Api { |
8 | 9 | /** |
9 | 10 | * @description: Get sample options value |
10 | 11 | */ |
11 | -export function optionsListApi() { | |
12 | - return defHttp.request<DemoOptionsGetResultModel>({ | |
13 | - url: Api.OPTIONS_LIST, | |
14 | - method: 'GET', | |
15 | - }); | |
16 | -} | |
12 | +export const optionsListApi = () => get<DemoOptionsGetResultModel>({ url: Api.OPTIONS_LIST }); | ... | ... |
src/api/demo/table.ts
1 | 1 | import { defHttp } from '/@/utils/http/axios'; |
2 | 2 | import { DemoParams, DemoListGetResultModel } from './model/tableModel'; |
3 | 3 | |
4 | +const { get } = defHttp; | |
5 | + | |
4 | 6 | enum Api { |
5 | 7 | DEMO_LIST = '/table/getDemoList', |
6 | 8 | } |
... | ... | @@ -8,13 +10,12 @@ enum Api { |
8 | 10 | /** |
9 | 11 | * @description: Get sample list value |
10 | 12 | */ |
11 | -export function demoListApi(params: DemoParams) { | |
12 | - return defHttp.request<DemoListGetResultModel>({ | |
13 | + | |
14 | +export const demoListApi = (params: DemoParams) => | |
15 | + get<DemoListGetResultModel>({ | |
13 | 16 | url: Api.DEMO_LIST, |
14 | - method: 'GET', | |
15 | 17 | params, |
16 | 18 | headers: { |
17 | 19 | ignoreCancelToken: true, |
18 | 20 | }, |
19 | 21 | }); |
20 | -} | ... | ... |
src/api/sys/menu.ts
1 | 1 | import { defHttp } from '/@/utils/http/axios'; |
2 | - | |
3 | 2 | import { getMenuListByIdParams, getMenuListByIdParamsResultModel } from './model/menuModel'; |
4 | 3 | |
4 | +const { get } = defHttp; | |
5 | + | |
5 | 6 | enum Api { |
6 | 7 | GetMenuListById = '/getMenuListById', |
7 | 8 | } |
... | ... | @@ -9,10 +10,7 @@ enum Api { |
9 | 10 | /** |
10 | 11 | * @description: Get user menu based on id |
11 | 12 | */ |
12 | -export function getMenuListById(params: getMenuListByIdParams) { | |
13 | - return defHttp.request<getMenuListByIdParamsResultModel>({ | |
14 | - url: Api.GetMenuListById, | |
15 | - method: 'GET', | |
16 | - params, | |
17 | - }); | |
18 | -} | |
13 | + | |
14 | +export const getMenuListById = (params: getMenuListByIdParams) => { | |
15 | + return get<getMenuListByIdParamsResultModel>({ url: Api.GetMenuListById, params }); | |
16 | +}; | ... | ... |
src/api/sys/user.ts
... | ... | @@ -7,6 +7,7 @@ import { |
7 | 7 | } from './model/userModel'; |
8 | 8 | import { ErrorMessageMode } from '/@/utils/http/axios/types'; |
9 | 9 | |
10 | +const { post, get } = defHttp; | |
10 | 11 | enum Api { |
11 | 12 | Login = '/login', |
12 | 13 | GetUserInfoById = '/getUserInfoById', |
... | ... | @@ -17,10 +18,9 @@ enum Api { |
17 | 18 | * @description: user login api |
18 | 19 | */ |
19 | 20 | export function loginApi(params: LoginParams, mode: ErrorMessageMode = 'modal') { |
20 | - return defHttp.request<LoginResultModel>( | |
21 | + return post<LoginResultModel>( | |
21 | 22 | { |
22 | 23 | url: Api.Login, |
23 | - method: 'POST', | |
24 | 24 | params, |
25 | 25 | }, |
26 | 26 | { |
... | ... | @@ -33,17 +33,15 @@ export function loginApi(params: LoginParams, mode: ErrorMessageMode = 'modal') |
33 | 33 | * @description: getUserInfoById |
34 | 34 | */ |
35 | 35 | export function getUserInfoById(params: GetUserInfoByUserIdParams) { |
36 | - return defHttp.request<GetUserInfoByUserIdModel>({ | |
36 | + return get<GetUserInfoByUserIdModel>({ | |
37 | 37 | url: Api.GetUserInfoById, |
38 | - method: 'GET', | |
39 | 38 | params, |
40 | 39 | }); |
41 | 40 | } |
42 | 41 | |
43 | 42 | export function getPermCodeByUserId(params: GetUserInfoByUserIdParams) { |
44 | - return defHttp.request<string[]>({ | |
43 | + return get<string[]>({ | |
45 | 44 | url: Api.GetPermCodeByUserId, |
46 | - method: 'GET', | |
47 | 45 | params, |
48 | 46 | }); |
49 | 47 | } | ... | ... |
src/utils/http/axios/Axios.ts
... | ... | @@ -165,6 +165,22 @@ export class VAxios { |
165 | 165 | }; |
166 | 166 | } |
167 | 167 | |
168 | + get<T = any>(config: AxiosRequestConfig, options?: RequestOptions): Promise<T> { | |
169 | + return this.request({ ...config, method: 'GET' }, options); | |
170 | + } | |
171 | + | |
172 | + post<T = any>(config: AxiosRequestConfig, options?: RequestOptions): Promise<T> { | |
173 | + return this.request({ ...config, method: 'POST' }, options); | |
174 | + } | |
175 | + | |
176 | + put<T = any>(config: AxiosRequestConfig, options?: RequestOptions): Promise<T> { | |
177 | + return this.request({ ...config, method: 'PUT' }, options); | |
178 | + } | |
179 | + | |
180 | + delete<T = any>(config: AxiosRequestConfig, options?: RequestOptions): Promise<T> { | |
181 | + return this.request({ ...config, method: 'DELETE' }, options); | |
182 | + } | |
183 | + | |
168 | 184 | request<T = any>(config: AxiosRequestConfig, options?: RequestOptions): Promise<T> { |
169 | 185 | let conf: AxiosRequestConfig = cloneDeep(config); |
170 | 186 | const transform = this.getTransform(); |
... | ... | @@ -173,7 +189,7 @@ export class VAxios { |
173 | 189 | |
174 | 190 | const opt: RequestOptions = Object.assign({}, requestOptions, options); |
175 | 191 | |
176 | - const { beforeRequestHook, requestCatch, transformRequestData } = transform || {}; | |
192 | + const { beforeRequestHook, requestCatchHook, transformRequestHook } = transform || {}; | |
177 | 193 | if (beforeRequestHook && isFunction(beforeRequestHook)) { |
178 | 194 | conf = beforeRequestHook(conf, opt); |
179 | 195 | } |
... | ... | @@ -183,16 +199,16 @@ export class VAxios { |
183 | 199 | this.axiosInstance |
184 | 200 | .request<any, AxiosResponse<Result>>(conf) |
185 | 201 | .then((res: AxiosResponse<Result>) => { |
186 | - if (transformRequestData && isFunction(transformRequestData)) { | |
187 | - const ret = transformRequestData(res, opt); | |
202 | + if (transformRequestHook && isFunction(transformRequestHook)) { | |
203 | + const ret = transformRequestHook(res, opt); | |
188 | 204 | ret !== errorResult ? resolve(ret) : reject(new Error('request error!')); |
189 | 205 | return; |
190 | 206 | } |
191 | 207 | resolve((res as unknown) as Promise<T>); |
192 | 208 | }) |
193 | 209 | .catch((e: Error) => { |
194 | - if (requestCatch && isFunction(requestCatch)) { | |
195 | - reject(requestCatch(e)); | |
210 | + if (requestCatchHook && isFunction(requestCatchHook)) { | |
211 | + reject(requestCatchHook(e)); | |
196 | 212 | return; |
197 | 213 | } |
198 | 214 | reject(e); | ... | ... |
src/utils/http/axios/axiosCancel.ts
... | ... | @@ -3,14 +3,14 @@ import axios from 'axios'; |
3 | 3 | |
4 | 4 | import { isFunction } from '/@/utils/is'; |
5 | 5 | |
6 | -// 声明一个 Map 用于存储每个请求的标识 和 取消函数 | |
6 | +// Used to store the identification and cancellation function of each request | |
7 | 7 | let pendingMap = new Map<string, Canceler>(); |
8 | 8 | |
9 | 9 | export const getPendingUrl = (config: AxiosRequestConfig) => [config.method, config.url].join('&'); |
10 | 10 | |
11 | 11 | export class AxiosCanceler { |
12 | 12 | /** |
13 | - * 添加请求 | |
13 | + * Add request | |
14 | 14 | * @param {Object} config |
15 | 15 | */ |
16 | 16 | addPending(config: AxiosRequestConfig) { |
... | ... | @@ -20,14 +20,14 @@ export class AxiosCanceler { |
20 | 20 | config.cancelToken || |
21 | 21 | new axios.CancelToken((cancel) => { |
22 | 22 | if (!pendingMap.has(url)) { |
23 | - // 如果 pending 中不存在当前请求,则添加进去 | |
23 | + // If there is no current request in pending, add it | |
24 | 24 | pendingMap.set(url, cancel); |
25 | 25 | } |
26 | 26 | }); |
27 | 27 | } |
28 | 28 | |
29 | 29 | /** |
30 | - * @description: 清空所有pending | |
30 | + * @description: Clear all pending | |
31 | 31 | */ |
32 | 32 | removeAllPending() { |
33 | 33 | pendingMap.forEach((cancel) => { |
... | ... | @@ -37,14 +37,15 @@ export class AxiosCanceler { |
37 | 37 | } |
38 | 38 | |
39 | 39 | /** |
40 | - * 移除请求 | |
40 | + * Removal request | |
41 | 41 | * @param {Object} config |
42 | 42 | */ |
43 | 43 | removePending(config: AxiosRequestConfig) { |
44 | 44 | const url = getPendingUrl(config); |
45 | 45 | |
46 | 46 | if (pendingMap.has(url)) { |
47 | - // 如果在 pending 中存在当前请求标识,需要取消当前请求,并且移除 | |
47 | + // If there is a current request identifier in pending, | |
48 | + // the current request needs to be cancelled and removed | |
48 | 49 | const cancel = pendingMap.get(url); |
49 | 50 | cancel && cancel(url); |
50 | 51 | pendingMap.delete(url); |
... | ... | @@ -52,7 +53,7 @@ export class AxiosCanceler { |
52 | 53 | } |
53 | 54 | |
54 | 55 | /** |
55 | - * @description: 重置 | |
56 | + * @description: reset | |
56 | 57 | */ |
57 | 58 | reset(): void { |
58 | 59 | pendingMap = new Map<string, Canceler>(); | ... | ... |
src/utils/http/axios/axiosTransform.ts
1 | 1 | /** |
2 | - * 数据处理类,可以根据项目自行配置 | |
2 | + * Data processing class, can be configured according to the project | |
3 | 3 | */ |
4 | 4 | import type { AxiosRequestConfig, AxiosResponse } from 'axios'; |
5 | 5 | import type { RequestOptions, Result } from './types'; |
6 | 6 | |
7 | 7 | export abstract class AxiosTransform { |
8 | 8 | /** |
9 | - * @description: 请求之前处理配置 | |
9 | + * @description: Process configuration before request | |
10 | 10 | * @description: Process configuration before request |
11 | 11 | */ |
12 | 12 | beforeRequestHook?: (config: AxiosRequestConfig, options: RequestOptions) => AxiosRequestConfig; |
13 | 13 | |
14 | 14 | /** |
15 | - * @description: 请求成功处理 | |
15 | + * @description: Request successfully processed | |
16 | 16 | */ |
17 | - transformRequestData?: (res: AxiosResponse<Result>, options: RequestOptions) => any; | |
17 | + transformRequestHook?: (res: AxiosResponse<Result>, options: RequestOptions) => any; | |
18 | 18 | |
19 | 19 | /** |
20 | 20 | * @description: 请求失败处理 |
21 | 21 | */ |
22 | - requestCatch?: (e: Error) => Promise<any>; | |
22 | + requestCatchHook?: (e: Error) => Promise<any>; | |
23 | 23 | |
24 | 24 | /** |
25 | 25 | * @description: 请求之前的拦截器 | ... | ... |
src/utils/http/axios/const.ts
src/utils/http/axios/helper.ts
... | ... | @@ -13,15 +13,12 @@ export function createNow(join: boolean, restful = false): string | object { |
13 | 13 | if (restful) { |
14 | 14 | return `?_t=${now}`; |
15 | 15 | } |
16 | - | |
17 | - return { | |
18 | - _t: now, | |
19 | - }; | |
16 | + return { _t: now }; | |
20 | 17 | } |
21 | 18 | |
22 | 19 | const DATE_TIME_FORMAT = 'YYYY-MM-DD HH:mm'; |
23 | 20 | /** |
24 | - * @description: 格式化请求参数时间 | |
21 | + * @description: Format request parameter time | |
25 | 22 | */ |
26 | 23 | export function formatRequestDate(params: any) { |
27 | 24 | for (const key in params) { | ... | ... |
src/utils/http/axios/index.ts
... | ... | @@ -32,7 +32,7 @@ const transform: AxiosTransform = { |
32 | 32 | /** |
33 | 33 | * @description: 处理请求数据 |
34 | 34 | */ |
35 | - transformRequestData: (res: AxiosResponse<Result>, options: RequestOptions) => { | |
35 | + transformRequestHook: (res: AxiosResponse<Result>, options: RequestOptions) => { | |
36 | 36 | const { t } = useI18n(); |
37 | 37 | const { isTransformRequestResult } = options; |
38 | 38 | // 不进行任何处理,直接返回 | ... | ... |
src/utils/http/axios/types.ts
1 | 1 | import type { AxiosRequestConfig } from 'axios'; |
2 | -import { AxiosTransform } from './axiosTransform'; | |
3 | - | |
2 | +import type { AxiosTransform } from './axiosTransform'; | |
4 | 3 | export type ErrorMessageMode = 'none' | 'modal' | 'message' | undefined; |
5 | 4 | |
6 | 5 | export interface RequestOptions { |
7 | - // 请求参数拼接到url | |
6 | + // Splicing request parameters to url | |
8 | 7 | joinParamsToUrl?: boolean; |
9 | - // 格式化请求参数时间 | |
8 | + // Format request parameter time | |
10 | 9 | formatDate?: boolean; |
11 | - // 是否处理请求结果 | |
10 | + // Whether to process the request result | |
12 | 11 | isTransformRequestResult?: boolean; |
13 | - // 是否加入url | |
12 | + // Whether to join url | |
14 | 13 | joinPrefix?: boolean; |
15 | - // 接口地址, 不填则使用默认apiUrl | |
14 | + // Interface address, use the default apiUrl if you leave it blank | |
16 | 15 | apiUrl?: string; |
17 | - // 错误消息提示类型 | |
16 | + // Error message prompt type | |
18 | 17 | errorMessageMode?: ErrorMessageMode; |
19 | - // 是否加入时间戳 | |
18 | + // Whether to add a timestamp | |
20 | 19 | joinTime?: boolean; |
21 | 20 | } |
22 | 21 | |
... | ... | @@ -32,15 +31,16 @@ export interface Result<T = any> { |
32 | 31 | message: string; |
33 | 32 | result: T; |
34 | 33 | } |
35 | -// multipart/form-data:上传文件 | |
34 | + | |
35 | +// multipart/form-data: upload file | |
36 | 36 | export interface UploadFileParams { |
37 | - // 其他参数 | |
37 | + // Other parameters | |
38 | 38 | data?: Indexable; |
39 | - // 文件参数的接口字段名 | |
39 | + // File parameter interface field name | |
40 | 40 | name?: string; |
41 | - // 文件 | |
41 | + // file name | |
42 | 42 | file: File | Blob; |
43 | - // 文件名 | |
43 | + // file name | |
44 | 44 | filename?: string; |
45 | 45 | [key: string]: any; |
46 | 46 | } | ... | ... |