mirror of
https://github.com/m-xlsea/ruoyi-plus-soybean.git
synced 2025-09-24 07:49:47 +08:00
feat(projects): 迁移登录完成
This commit is contained in:
@ -6,13 +6,6 @@ import CustomAxiosInstance from './instance';
|
||||
|
||||
type RequestMethod = 'get' | 'post' | 'put' | 'delete';
|
||||
|
||||
type RequestResultHook<T = any> = {
|
||||
data: Ref<T | null>;
|
||||
error: Ref<Service.RequestError | null>;
|
||||
loading: Ref<boolean>;
|
||||
network: Ref<boolean>;
|
||||
};
|
||||
|
||||
interface RequestParam {
|
||||
url: string;
|
||||
method?: RequestMethod;
|
||||
@ -20,9 +13,95 @@ interface RequestParam {
|
||||
axiosConfig?: AxiosRequestConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建请求
|
||||
* @param axiosConfig - axios配置
|
||||
*/
|
||||
export function createRequest(axiosConfig: AxiosRequestConfig) {
|
||||
const customInstance = new CustomAxiosInstance(axiosConfig);
|
||||
|
||||
/**
|
||||
* 异步promise请求
|
||||
* @param param - 请求参数
|
||||
* - url: 请求地址
|
||||
* - method: 请求方法(默认get)
|
||||
* - data: 请求的body的data
|
||||
* - axiosConfig: axios配置
|
||||
*/
|
||||
async function asyncRequest<T = any>(param: RequestParam): Promise<Service.RequestResult<T>> {
|
||||
const { url } = param;
|
||||
const method = param.method || 'get';
|
||||
const { instance } = customInstance;
|
||||
const res = (await getRequestResponse(
|
||||
instance,
|
||||
method,
|
||||
url,
|
||||
param.data,
|
||||
param.axiosConfig
|
||||
)) as Service.RequestResult<T>;
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* get请求
|
||||
* @param url - 请求地址
|
||||
* @param config - axios配置
|
||||
*/
|
||||
function get<T = any>(url: string, config?: AxiosRequestConfig) {
|
||||
return asyncRequest<T>({ url, method: 'get', axiosConfig: config });
|
||||
}
|
||||
|
||||
/**
|
||||
* post请求
|
||||
* @param url - 请求地址
|
||||
* @param data - 请求的body的data
|
||||
* @param config - axios配置
|
||||
*/
|
||||
function post<T = any>(url: string, data?: any, config?: AxiosRequestConfig) {
|
||||
return asyncRequest<T>({ url, method: 'post', data, axiosConfig: config });
|
||||
}
|
||||
/**
|
||||
* put请求
|
||||
* @param url - 请求地址
|
||||
* @param data - 请求的body的data
|
||||
* @param config - axios配置
|
||||
*/
|
||||
function put<T = any>(url: string, data?: any, config?: AxiosRequestConfig) {
|
||||
return asyncRequest<T>({ url, method: 'put', data, axiosConfig: config });
|
||||
}
|
||||
|
||||
/**
|
||||
* delete请求
|
||||
* @param url - 请求地址
|
||||
* @param config - axios配置
|
||||
*/
|
||||
function handleDelete<T = any>(url: string, config: AxiosRequestConfig) {
|
||||
return asyncRequest<T>({ url, method: 'delete', axiosConfig: config });
|
||||
}
|
||||
|
||||
return {
|
||||
get,
|
||||
post,
|
||||
put,
|
||||
delete: handleDelete
|
||||
};
|
||||
}
|
||||
|
||||
type RequestResultHook<T = any> = {
|
||||
data: Ref<T | null>;
|
||||
error: Ref<Service.RequestError | null>;
|
||||
loading: Ref<boolean>;
|
||||
network: Ref<boolean>;
|
||||
};
|
||||
|
||||
/**
|
||||
* 创建hooks请求
|
||||
* @param axiosConfig - axios配置
|
||||
*/
|
||||
export function createHookRequest(axiosConfig: AxiosRequestConfig) {
|
||||
const customInstance = new CustomAxiosInstance(axiosConfig);
|
||||
|
||||
/**
|
||||
* hooks请求
|
||||
* @param param - 请求参数
|
||||
@ -30,70 +109,82 @@ export function createRequest(axiosConfig: AxiosRequestConfig) {
|
||||
* - method: 请求方法(默认get)
|
||||
* - data: 请求的body的data
|
||||
* - axiosConfig: axios配置
|
||||
* @param hookMode - 是否启用hook写法
|
||||
*/
|
||||
function request<T = any>(param: RequestParam, hookMode: true): RequestResultHook<T>;
|
||||
function request<T = any>(param: RequestParam, hookMode: false): Promise<Service.RequestResult<T>>;
|
||||
function request<T = any>(
|
||||
param: RequestParam,
|
||||
hookMode: boolean
|
||||
): RequestResultHook<T> | Promise<Service.RequestResult<T>> {
|
||||
function useRequest<T = any>(param: RequestParam): RequestResultHook<T> {
|
||||
const { loading, startLoading, endLoading } = useLoading();
|
||||
const { bool: network, setBool: setNetwork } = useBoolean(window.navigator.onLine);
|
||||
|
||||
startLoading();
|
||||
const data = ref<T | null>(null) as Ref<T | null>;
|
||||
const error = ref<Service.RequestError | null>(null);
|
||||
|
||||
function handleRequestResult(response: any) {
|
||||
const res = response as Service.RequestResult<T>;
|
||||
data.value = res.data;
|
||||
error.value = res.error;
|
||||
endLoading();
|
||||
setNetwork(window.navigator.onLine);
|
||||
}
|
||||
|
||||
const { url } = param;
|
||||
const method = param.method || 'get';
|
||||
const { instance } = customInstance;
|
||||
if (hookMode) {
|
||||
return useRequest(instance, method, url, param.data, param.axiosConfig);
|
||||
}
|
||||
return asyncRequest(instance, method, url, param.data, param.axiosConfig);
|
||||
|
||||
getRequestResponse(instance, method, url, param.data, param.axiosConfig).then(handleRequestResult);
|
||||
|
||||
return {
|
||||
data,
|
||||
error,
|
||||
loading,
|
||||
network
|
||||
};
|
||||
}
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
function useRequest<T = any>(
|
||||
instance: AxiosInstance,
|
||||
method: RequestMethod,
|
||||
url: string,
|
||||
bodyData?: any,
|
||||
config?: AxiosRequestConfig
|
||||
): RequestResultHook<T> {
|
||||
const { loading, startLoading, endLoading } = useLoading();
|
||||
const { bool: network, setBool: setNetwork } = useBoolean(window.navigator.onLine);
|
||||
|
||||
startLoading();
|
||||
const data = ref<T | null>(null) as Ref<T | null>;
|
||||
const error = ref<Service.RequestError | null>(null);
|
||||
|
||||
function handleRequestResult(response: any) {
|
||||
const res = response as Service.RequestResult<T>;
|
||||
data.value = res.data;
|
||||
error.value = res.error;
|
||||
endLoading();
|
||||
setNetwork(window.navigator.onLine);
|
||||
/**
|
||||
* get请求
|
||||
* @param url - 请求地址
|
||||
* @param config - axios配置
|
||||
*/
|
||||
function get<T = any>(url: string, config?: AxiosRequestConfig) {
|
||||
return useRequest<T>({ url, method: 'get', axiosConfig: config });
|
||||
}
|
||||
|
||||
getRequestResponse(instance, method, url, bodyData, config).then(handleRequestResult);
|
||||
/**
|
||||
* post请求
|
||||
* @param url - 请求地址
|
||||
* @param data - 请求的body的data
|
||||
* @param config - axios配置
|
||||
*/
|
||||
function post<T = any>(url: string, data?: any, config?: AxiosRequestConfig) {
|
||||
return useRequest<T>({ url, method: 'post', data, axiosConfig: config });
|
||||
}
|
||||
/**
|
||||
* put请求
|
||||
* @param url - 请求地址
|
||||
* @param data - 请求的body的data
|
||||
* @param config - axios配置
|
||||
*/
|
||||
function put<T = any>(url: string, data?: any, config?: AxiosRequestConfig) {
|
||||
return useRequest<T>({ url, method: 'put', data, axiosConfig: config });
|
||||
}
|
||||
|
||||
/**
|
||||
* delete请求
|
||||
* @param url - 请求地址
|
||||
* @param config - axios配置
|
||||
*/
|
||||
function handleDelete<T = any>(url: string, config: AxiosRequestConfig) {
|
||||
return useRequest<T>({ url, method: 'delete', axiosConfig: config });
|
||||
}
|
||||
|
||||
return {
|
||||
data,
|
||||
error,
|
||||
loading,
|
||||
network
|
||||
get,
|
||||
post,
|
||||
put,
|
||||
delete: handleDelete
|
||||
};
|
||||
}
|
||||
|
||||
async function asyncRequest<T = any>(
|
||||
instance: AxiosInstance,
|
||||
method: RequestMethod,
|
||||
url: string,
|
||||
bodyData?: any,
|
||||
config?: AxiosRequestConfig
|
||||
): Promise<Service.RequestResult<T>> {
|
||||
const res = (await getRequestResponse(instance, method, url, bodyData, config)) as Service.RequestResult<T>;
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
async function getRequestResponse(
|
||||
instance: AxiosInstance,
|
||||
method: RequestMethod,
|
||||
|
Reference in New Issue
Block a user