import type { AxiosResponse } from 'axios'; import { emitEvent } from '@/utils/eventBus'; export interface ReqReturn{ data:AxiosResponse | T |null; error:any; } type ReqFn = (params: D) => Promise | T>; type ReqFnSelect = (params?: D) => Promise | T>; export async function reqCatch(req: ReqFn | ReqFnSelect, params?: D): Promise> { try { const data = await req(params); return { data, error: null }; } catch (e) { // 可在此处定义全局的错误处理方法,也可在外部处理 return { data: null, error: e }; } } interface ReqReturnV2{ data:AxiosResponse|null; error:any; } export type catchRt = Promise>; export async function reqCatchV2(req:()=>Promise>):catchRt { // 设置请求函数的返回值,R类型可以自动推导 try { const data = await req(); return { data, error: null }; } catch (e) { // 可在此处定义全局的错误处理方法,也可在外部处理 emitEvent('responseError', e); return { data: null, error: e }; } }