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