diff --git a/src/api/demo/account.ts b/src/api/demo/account.ts index 764dca47fc9bcb4d2e380b2075d2b2ac897b26ef..a884fd0c410a6b39ba4b1967a8ec36d4d6062519 100644 --- a/src/api/demo/account.ts +++ b/src/api/demo/account.ts @@ -1,14 +1,12 @@ import { defHttp } from '/@/utils/http/axios'; import { GetAccountInfoModel } from './model/accountModel'; +const { get } = defHttp; + enum Api { ACCOUNT_INFO = '/account/getAccountInfo', } // Get personal center-basic settings -export function accountInfoApi() { - return defHttp.request({ - url: Api.ACCOUNT_INFO, - method: 'GET', - }); -} + +export const accountInfoApi = () => get({ url: Api.ACCOUNT_INFO }); diff --git a/src/api/demo/error.ts b/src/api/demo/error.ts index 84766dea2ae43d4ff0497a2ea4dcfbd90e36ba63..14d8e2ad1ddf984f8739cfa35c94e594a8097f61 100644 --- a/src/api/demo/error.ts +++ b/src/api/demo/error.ts @@ -1,5 +1,7 @@ import { defHttp } from '/@/utils/http/axios'; +const { get } = defHttp; + enum Api { // The address does not exist Error = '/error', @@ -8,9 +10,5 @@ enum Api { /** * @description: Trigger ajax error */ -export function fireErrorApi() { - return defHttp.request({ - url: Api.Error, - method: 'GET', - }); -} + +export const fireErrorApi = () => get({ url: Api.Error }); diff --git a/src/api/demo/select.ts b/src/api/demo/select.ts index 6703e3d09f92e40d5e5f8b5243e78d0a8d2c08ca..f27aeae24fa44f8d4a55ee12541937071b178603 100644 --- a/src/api/demo/select.ts +++ b/src/api/demo/select.ts @@ -1,5 +1,6 @@ import { defHttp } from '/@/utils/http/axios'; import { DemoOptionsGetResultModel } from './model/optionsModel'; +const { get } = defHttp; enum Api { OPTIONS_LIST = '/select/getDemoOptions', @@ -8,9 +9,4 @@ enum Api { /** * @description: Get sample options value */ -export function optionsListApi() { - return defHttp.request({ - url: Api.OPTIONS_LIST, - method: 'GET', - }); -} +export const optionsListApi = () => get({ url: Api.OPTIONS_LIST }); diff --git a/src/api/demo/table.ts b/src/api/demo/table.ts index 1e10c16a110ba0eb976bf6e3d7b94c5775b947c4..4f1012e7eb3c2d04f0c9cd61098bda976a20e159 100644 --- a/src/api/demo/table.ts +++ b/src/api/demo/table.ts @@ -1,6 +1,8 @@ import { defHttp } from '/@/utils/http/axios'; import { DemoParams, DemoListGetResultModel } from './model/tableModel'; +const { get } = defHttp; + enum Api { DEMO_LIST = '/table/getDemoList', } @@ -8,13 +10,12 @@ enum Api { /** * @description: Get sample list value */ -export function demoListApi(params: DemoParams) { - return defHttp.request({ + +export const demoListApi = (params: DemoParams) => + get({ url: Api.DEMO_LIST, - method: 'GET', params, headers: { ignoreCancelToken: true, }, }); -} diff --git a/src/api/sys/menu.ts b/src/api/sys/menu.ts index b29f1ba75aaf66015399ae4c02fbfa92de5cb91b..b81f72d8e442b6dc1b1d7af2788740c13d3790c5 100644 --- a/src/api/sys/menu.ts +++ b/src/api/sys/menu.ts @@ -1,7 +1,8 @@ import { defHttp } from '/@/utils/http/axios'; - import { getMenuListByIdParams, getMenuListByIdParamsResultModel } from './model/menuModel'; +const { get } = defHttp; + enum Api { GetMenuListById = '/getMenuListById', } @@ -9,10 +10,7 @@ enum Api { /** * @description: Get user menu based on id */ -export function getMenuListById(params: getMenuListByIdParams) { - return defHttp.request({ - url: Api.GetMenuListById, - method: 'GET', - params, - }); -} + +export const getMenuListById = (params: getMenuListByIdParams) => { + return get({ url: Api.GetMenuListById, params }); +}; diff --git a/src/api/sys/user.ts b/src/api/sys/user.ts index 157fc7be2046dcf4db2c40ecd0c30fd27795a828..f5b4b1e8f736293bde5a704d59e15417dc151af7 100644 --- a/src/api/sys/user.ts +++ b/src/api/sys/user.ts @@ -7,6 +7,7 @@ import { } from './model/userModel'; import { ErrorMessageMode } from '/@/utils/http/axios/types'; +const { post, get } = defHttp; enum Api { Login = '/login', GetUserInfoById = '/getUserInfoById', @@ -17,10 +18,9 @@ enum Api { * @description: user login api */ export function loginApi(params: LoginParams, mode: ErrorMessageMode = 'modal') { - return defHttp.request( + return post( { url: Api.Login, - method: 'POST', params, }, { @@ -33,17 +33,15 @@ export function loginApi(params: LoginParams, mode: ErrorMessageMode = 'modal') * @description: getUserInfoById */ export function getUserInfoById(params: GetUserInfoByUserIdParams) { - return defHttp.request({ + return get({ url: Api.GetUserInfoById, - method: 'GET', params, }); } export function getPermCodeByUserId(params: GetUserInfoByUserIdParams) { - return defHttp.request({ + return get({ url: Api.GetPermCodeByUserId, - method: 'GET', params, }); } diff --git a/src/utils/http/axios/Axios.ts b/src/utils/http/axios/Axios.ts index 083845ff1201ce2c9fe7555ca793cfa1cd5e63f3..56cc126adfd455079225ca595728cb1777dcd5f3 100644 --- a/src/utils/http/axios/Axios.ts +++ b/src/utils/http/axios/Axios.ts @@ -165,6 +165,22 @@ export class VAxios { }; } + get(config: AxiosRequestConfig, options?: RequestOptions): Promise { + return this.request({ ...config, method: 'GET' }, options); + } + + post(config: AxiosRequestConfig, options?: RequestOptions): Promise { + return this.request({ ...config, method: 'POST' }, options); + } + + put(config: AxiosRequestConfig, options?: RequestOptions): Promise { + return this.request({ ...config, method: 'PUT' }, options); + } + + delete(config: AxiosRequestConfig, options?: RequestOptions): Promise { + return this.request({ ...config, method: 'DELETE' }, options); + } + request(config: AxiosRequestConfig, options?: RequestOptions): Promise { let conf: AxiosRequestConfig = cloneDeep(config); const transform = this.getTransform(); @@ -173,7 +189,7 @@ export class VAxios { const opt: RequestOptions = Object.assign({}, requestOptions, options); - const { beforeRequestHook, requestCatch, transformRequestData } = transform || {}; + const { beforeRequestHook, requestCatchHook, transformRequestHook } = transform || {}; if (beforeRequestHook && isFunction(beforeRequestHook)) { conf = beforeRequestHook(conf, opt); } @@ -183,16 +199,16 @@ export class VAxios { this.axiosInstance .request>(conf) .then((res: AxiosResponse) => { - if (transformRequestData && isFunction(transformRequestData)) { - const ret = transformRequestData(res, opt); + if (transformRequestHook && isFunction(transformRequestHook)) { + const ret = transformRequestHook(res, opt); ret !== errorResult ? resolve(ret) : reject(new Error('request error!')); return; } resolve((res as unknown) as Promise); }) .catch((e: Error) => { - if (requestCatch && isFunction(requestCatch)) { - reject(requestCatch(e)); + if (requestCatchHook && isFunction(requestCatchHook)) { + reject(requestCatchHook(e)); return; } reject(e); diff --git a/src/utils/http/axios/axiosCancel.ts b/src/utils/http/axios/axiosCancel.ts index 5c4b1bd3574679095b14c76d67ca390dfd1d1b1b..98d59c21a314b88f331c6f8043d1b33fe962a26c 100644 --- a/src/utils/http/axios/axiosCancel.ts +++ b/src/utils/http/axios/axiosCancel.ts @@ -3,14 +3,14 @@ import axios from 'axios'; import { isFunction } from '/@/utils/is'; -// 声明一个 Map 用于存储每个请求的标识 和 取消函数 +// Used to store the identification and cancellation function of each request let pendingMap = new Map(); export const getPendingUrl = (config: AxiosRequestConfig) => [config.method, config.url].join('&'); export class AxiosCanceler { /** - * 添加请求 + * Add request * @param {Object} config */ addPending(config: AxiosRequestConfig) { @@ -20,14 +20,14 @@ export class AxiosCanceler { config.cancelToken || new axios.CancelToken((cancel) => { if (!pendingMap.has(url)) { - // 如果 pending 中不存在当前请求,则添加进去 + // If there is no current request in pending, add it pendingMap.set(url, cancel); } }); } /** - * @description: 清空所有pending + * @description: Clear all pending */ removeAllPending() { pendingMap.forEach((cancel) => { @@ -37,14 +37,15 @@ export class AxiosCanceler { } /** - * 移除请求 + * Removal request * @param {Object} config */ removePending(config: AxiosRequestConfig) { const url = getPendingUrl(config); if (pendingMap.has(url)) { - // 如果在 pending 中存在当前请求标识,需要取消当前请求,并且移除 + // If there is a current request identifier in pending, + // the current request needs to be cancelled and removed const cancel = pendingMap.get(url); cancel && cancel(url); pendingMap.delete(url); @@ -52,7 +53,7 @@ export class AxiosCanceler { } /** - * @description: 重置 + * @description: reset */ reset(): void { pendingMap = new Map(); diff --git a/src/utils/http/axios/axiosTransform.ts b/src/utils/http/axios/axiosTransform.ts index 639e4f57d933955ed6660ec7907f6aed52fd126a..3defed8216243bf1ec64180651bfc4bc3366de3d 100644 --- a/src/utils/http/axios/axiosTransform.ts +++ b/src/utils/http/axios/axiosTransform.ts @@ -1,25 +1,25 @@ /** - * 数据处理类,可以根据项目自行配置 + * Data processing class, can be configured according to the project */ import type { AxiosRequestConfig, AxiosResponse } from 'axios'; import type { RequestOptions, Result } from './types'; export abstract class AxiosTransform { /** - * @description: 请求之前处理配置 + * @description: Process configuration before request * @description: Process configuration before request */ beforeRequestHook?: (config: AxiosRequestConfig, options: RequestOptions) => AxiosRequestConfig; /** - * @description: 请求成功处理 + * @description: Request successfully processed */ - transformRequestData?: (res: AxiosResponse, options: RequestOptions) => any; + transformRequestHook?: (res: AxiosResponse, options: RequestOptions) => any; /** * @description: 请求失败处理 */ - requestCatch?: (e: Error) => Promise; + requestCatchHook?: (e: Error) => Promise; /** * @description: 请求之前的拦截器 diff --git a/src/utils/http/axios/const.ts b/src/utils/http/axios/const.ts index 02b7c243fa86e9124561d192827ee5351b2432ca..2d45c68c3491f273d2d453b0c78c4ba87d2f6ae0 100644 --- a/src/utils/http/axios/const.ts +++ b/src/utils/http/axios/const.ts @@ -1,2 +1,2 @@ -// 接口返回值data不能为这个,否则会判为请求失败 +// The interface return value data cannot be this, otherwise the request will be judged as a failure export const errorResult = '__ERROR_RESULT__'; diff --git a/src/utils/http/axios/helper.ts b/src/utils/http/axios/helper.ts index 8372a0f8d5aa19a19ec112e27685cd5dedab66fe..b9387acaf326afcb108521cb1b4f67a8c8e80c75 100644 --- a/src/utils/http/axios/helper.ts +++ b/src/utils/http/axios/helper.ts @@ -13,15 +13,12 @@ export function createNow(join: boolean, restful = false): string | object { if (restful) { return `?_t=${now}`; } - - return { - _t: now, - }; + return { _t: now }; } const DATE_TIME_FORMAT = 'YYYY-MM-DD HH:mm'; /** - * @description: 格式化请求参数时间 + * @description: Format request parameter time */ export function formatRequestDate(params: any) { for (const key in params) { diff --git a/src/utils/http/axios/index.ts b/src/utils/http/axios/index.ts index af94a26c4aa961620e9db1bd1139fdc567a39f3b..7888def5159e67ce1a88e0a20e0dae76a2530979 100644 --- a/src/utils/http/axios/index.ts +++ b/src/utils/http/axios/index.ts @@ -32,7 +32,7 @@ const transform: AxiosTransform = { /** * @description: 处理请求数据 */ - transformRequestData: (res: AxiosResponse, options: RequestOptions) => { + transformRequestHook: (res: AxiosResponse, options: RequestOptions) => { const { t } = useI18n(); const { isTransformRequestResult } = options; // 不进行任何处理,直接返回 diff --git a/src/utils/http/axios/types.ts b/src/utils/http/axios/types.ts index b151ed589ee93fbad01c6e2909ad981b4aed6d27..ed8f846b3324c11d5152c7c3939a305335479e39 100644 --- a/src/utils/http/axios/types.ts +++ b/src/utils/http/axios/types.ts @@ -1,22 +1,21 @@ import type { AxiosRequestConfig } from 'axios'; -import { AxiosTransform } from './axiosTransform'; - +import type { AxiosTransform } from './axiosTransform'; export type ErrorMessageMode = 'none' | 'modal' | 'message' | undefined; export interface RequestOptions { - // 请求参数拼接到url + // Splicing request parameters to url joinParamsToUrl?: boolean; - // 格式化请求参数时间 + // Format request parameter time formatDate?: boolean; - // 是否处理请求结果 + // Whether to process the request result isTransformRequestResult?: boolean; - // 是否加入url + // Whether to join url joinPrefix?: boolean; - // 接口地址, 不填则使用默认apiUrl + // Interface address, use the default apiUrl if you leave it blank apiUrl?: string; - // 错误消息提示类型 + // Error message prompt type errorMessageMode?: ErrorMessageMode; - // 是否加入时间戳 + // Whether to add a timestamp joinTime?: boolean; } @@ -32,15 +31,16 @@ export interface Result { message: string; result: T; } -// multipart/form-data:上传文件 + +// multipart/form-data: upload file export interface UploadFileParams { - // 其他参数 + // Other parameters data?: Indexable; - // 文件参数的接口字段名 + // File parameter interface field name name?: string; - // 文件 + // file name file: File | Blob; - // 文件名 + // file name filename?: string; [key: string]: any; }