mirror of
https://github.com/pure-admin/pure-admin-thin.git
synced 2025-04-24 23:47:17 +08:00
perf: 通用CRUD
This commit is contained in:
parent
08ed64d7ef
commit
7443ac3ac4
@ -1,39 +1,58 @@
|
||||
import { http } from "@/utils/http";
|
||||
import type { PureHttpRequestConfig } from "@/utils/http/types.d";
|
||||
import type { AxiosRequestConfig } from "axios";
|
||||
import type { ApiAbstract } from "@/utils/http/ApiAbstract";
|
||||
|
||||
export const baseUrlApi = (url: string) => `/api/${url}`;
|
||||
export const baseUrlAuth = (url: string) => `/auth/${url}`;
|
||||
export const baseUrlAvatar = (url: string) => `/avatar/${url}`;
|
||||
|
||||
export const get = (params: any) => {
|
||||
return http.request<ApiAbstract>("get", baseUrlApi("menus"), {
|
||||
params
|
||||
});
|
||||
};
|
||||
/** 单独抽离的CRUD工具函数 */
|
||||
class crud {
|
||||
/** 单独抽离的get工具函数 */
|
||||
public get<T, P>(
|
||||
url: string,
|
||||
params?: AxiosRequestConfig<T>,
|
||||
config?: PureHttpRequestConfig
|
||||
): Promise<ApiAbstract<P>> {
|
||||
return http.get<T, ApiAbstract<P>>(baseUrlApi(url), params, config);
|
||||
}
|
||||
|
||||
export const add = (data: any) => {
|
||||
return http.request("post", baseUrlApi("menus"), {
|
||||
data
|
||||
});
|
||||
};
|
||||
/** 单独抽离的post工具函数 */
|
||||
public post<T, P>(
|
||||
url: string,
|
||||
params?: AxiosRequestConfig<T>,
|
||||
config?: PureHttpRequestConfig
|
||||
): Promise<ApiAbstract<P>> {
|
||||
return http.post<T, ApiAbstract<P>>(baseUrlApi(url), params, config);
|
||||
}
|
||||
|
||||
export const del = (ids: number[] | any) => {
|
||||
return http.request("delete", baseUrlApi("menus"), {
|
||||
data: ids
|
||||
});
|
||||
};
|
||||
export const edit = (data: any) => {
|
||||
return http.request<ApiAbstract>("put", baseUrlApi("menus"), {
|
||||
data
|
||||
});
|
||||
};
|
||||
export const download = (data: any) => {
|
||||
return http.request<Blob>(
|
||||
"get",
|
||||
baseUrlApi("menus/download"),
|
||||
{
|
||||
data
|
||||
},
|
||||
{ responseType: "blob" }
|
||||
);
|
||||
};
|
||||
/** 单独抽离的put工具函数 */
|
||||
public put<T, P>(
|
||||
url: string,
|
||||
params?: AxiosRequestConfig<T>,
|
||||
config?: PureHttpRequestConfig
|
||||
): Promise<ApiAbstract<P>> {
|
||||
return http.put<T, ApiAbstract<P>>(baseUrlApi(url), params, config);
|
||||
}
|
||||
|
||||
/** 单独抽离的delete工具函数 */
|
||||
public delete<T, P>(
|
||||
url: string,
|
||||
params?: AxiosRequestConfig<T>,
|
||||
config?: PureHttpRequestConfig
|
||||
): Promise<ApiAbstract<P>> {
|
||||
return http.delete<T, ApiAbstract<P>>(baseUrlApi(url), params, config);
|
||||
}
|
||||
|
||||
/** 单独抽离的delete工具函数 */
|
||||
public download<T, P>(
|
||||
url: string,
|
||||
params?: AxiosRequestConfig<T>,
|
||||
config?: PureHttpRequestConfig
|
||||
): Promise<P> {
|
||||
return http.get<T, P>(baseUrlApi(url + "/download"), params, config);
|
||||
}
|
||||
}
|
||||
|
||||
export const CRUD = new crud();
|
||||
|
@ -161,6 +161,23 @@ class PureHttp {
|
||||
): Promise<P> {
|
||||
return this.request<P>("get", url, params, config);
|
||||
}
|
||||
/** 单独抽离的put工具函数 */
|
||||
public put<T, P>(
|
||||
url: string,
|
||||
params?: AxiosRequestConfig<T>,
|
||||
config?: PureHttpRequestConfig
|
||||
): Promise<P> {
|
||||
return this.request<P>("put", url, params, config);
|
||||
}
|
||||
|
||||
/** 单独抽离的delete工具函数 */
|
||||
public delete<T, P>(
|
||||
url: string,
|
||||
params?: AxiosRequestConfig<T>,
|
||||
config?: PureHttpRequestConfig
|
||||
): Promise<P> {
|
||||
return this.request<P>("delete", url, params, config);
|
||||
}
|
||||
}
|
||||
|
||||
export const http = new PureHttp();
|
||||
|
Loading…
x
Reference in New Issue
Block a user