重构文章过滤功能

This commit is contained in:
宇阳
2024-08-25 18:38:40 +08:00
parent 671ea74614
commit 3156fd957b
7 changed files with 85 additions and 45 deletions

View File

@@ -18,22 +18,21 @@ export const editArticleDataAPI = (data: Article) =>
export const getArticleDataAPI = (id?: number) => Request<Article>("GET", `/article/${id}`) export const getArticleDataAPI = (id?: number) => Request<Article>("GET", `/article/${id}`)
// 获取文章列表 // 获取文章列表
export const getArticleListAPI = getListAPI<Article>("/article") export const getArticleListAPI = (data?: QueryData) => {
if (data?.pagination) {
// export const getArticleListAPI = (data?: QueryData) => { return Request<Paginate<Article[]>>("POST", `/article/paging`, {
// if (data?.pagination) { data: { ...data?.query },
// let sort = data?.sort ? `?sort=${data?.sort}` : '?' params: {
// const query = ObjectToUrlParam(data?.query as FilterData) ? "&" + ObjectToUrlParam(data?.query as FilterData) : '' sort: data.sort,
// const { page, size } = data.pagination ...data.pagination
// const pagination = page && size ? `&page=${page}&size=${size}` : page && !size ? `&page=${page}` : size && !page ? `&size=${size}` : '' }
// if (!query && !pagination) sort = '' });
} else {
// return Request<Paginate<Article[]>>("GET", `/article${sort}${pagination}${query}`); return Request<Article[]>("POST", `/article/list`, {
// } else { data: { ...data?.query },
// let sort = data?.sort ? `?sort=${data?.sort}` : '?' params: {
// const query = ObjectToUrlParam(data?.query as FilterData) ? "&" + ObjectToUrlParam(data?.query as FilterData) : '' sort: data?.sort
// if (!query) sort = '' }
});
// return Request<Article[]>("GET", `/article/all${sort}${query}`); }
// } };
// };

View File

@@ -126,12 +126,15 @@ const ArticlePage = () => {
const onSubmit = async (values: FilterForm) => { const onSubmit = async (values: FilterForm) => {
const query: FilterArticle = { const query: FilterArticle = {
key: values.title ? values.title : null, key: values.title ? values.title : null,
startDate: values.createTime ? +values.createTime[0].valueOf() : null, startDate: values.createTime ? values.createTime[0].valueOf() + '' : null,
endDate: values.createTime ? +values.createTime[1].valueOf() : null, endDate: values.createTime ? values.createTime[1].valueOf() + '' : null,
cateIds: values.cateIds ? values?.cateIds?.join(",") : null, cateIds: values.cateIds ? values.cateIds : null,
tagId: values.tagId ? values.tagId + "" : null, tagId: values.tagId ? values.tagId + "" : null,
} }
console.log(query);
const { data } = await getArticleListAPI({ query }); const { data } = await getArticleListAPI({ query });
setArticleList(data as Article[]); setArticleList(data as Article[]);
} }

View File

@@ -6,6 +6,6 @@ export interface FilterForm {
} }
export interface FilterArticle extends FilterData { export interface FilterArticle extends FilterData {
cateIds?: string | null, cateIds?: number[] | null,
tagId?: string | null, tagId?: string | null,
} }

View File

@@ -7,7 +7,7 @@ export interface Article {
description: string, description: string,
content: string, content: string,
cover: string, cover: string,
cateIds: string, cateIds: number[],
cateList: Cate[] cateList: Cate[]
tagIds: string, tagIds: string,
tagList: Tag[] tagList: Tag[]

View File

@@ -21,8 +21,8 @@ interface Page {
interface FilterData { interface FilterData {
key?: string | null, key?: string | null,
startDate?: number | null, startDate?: string | null,
endDate?: number | null endDate?: string | null
} }
interface QueryData { interface QueryData {

View File

@@ -1,5 +1,4 @@
import Request from "@/utils/request"; import Request from "@/utils/request";
import { Article } from "@/types/app/article";
// 对象转url参数 // 对象转url参数
export const ObjectToUrlParam = (obj: Object): string => { export const ObjectToUrlParam = (obj: Object): string => {
@@ -12,22 +11,62 @@ export const ObjectToUrlParam = (obj: Object): string => {
} }
// 查询数据相关逻辑 // 查询数据相关逻辑
// export const getListAPI = <T>(api: string) => {
// return (data?: QueryData) => {
// if (data?.pagination) {
// let sort = data?.sort ? `?sort=${data?.sort}` : '?'
// const query = ObjectToUrlParam(data?.query as FilterData) ? "&" + ObjectToUrlParam(data?.query as FilterData) : ''
// const { page, size } = data.pagination
// const pagination = page && size ? `&page=${page}&size=${size}` : page && !size ? `&page=${page}` : size && !page ? `&size=${size}` : ''
// if (!query && !pagination) sort = ''
// return Request<Paginate<T[]>>("GET", `${api}${sort}${pagination}${query}`);
// } else {
// return Request<T[]>("GET", `${api}/all`, {
// sort: data?.sort,
// pattern: data?.pattern,
// ...data?.query
// });
// }
// }
// };
const buildQueryParams = (params: Record<string, any>): string => {
return Object.entries(params)
.filter(([_, value]) => value !== undefined && value !== null)
.map(([key, value]) => {
if (Array.isArray(value)) {
return `${encodeURIComponent(key)}=${encodeURIComponent(JSON.stringify(value))}`;
}
return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
})
.join('&');
};
export const getListAPI = <T>(api: string) => { export const getListAPI = <T>(api: string) => {
return (data?: QueryData) => { return (data?: QueryData) => {
if (data?.pagination) { let queryParams: Record<string, any> = {};
let sort = data?.sort ? `?sort=${data?.sort}` : '?'
const query = ObjectToUrlParam(data?.query as FilterData) ? "&" + ObjectToUrlParam(data?.query as FilterData) : ''
const { page, size } = data.pagination
const pagination = page && size ? `&page=${page}&size=${size}` : page && !size ? `&page=${page}` : size && !page ? `&size=${size}` : ''
if (!query && !pagination) sort = ''
return Request<Paginate<T[]>>("GET", `${api}${sort}${pagination}${query}`); if (data?.sort) {
} else { queryParams.sort = data.sort;
return Request<T[]>("GET", `${api}/all`, {
sort: data?.sort,
pattern: data?.pattern,
...data?.query
});
} }
}
if (data?.query) {
queryParams = { ...queryParams, ...data.query };
}
if (data?.pagination) {
const { page, size } = data.pagination;
if (page) queryParams.page = page;
if (size) queryParams.size = size;
}
const queryString = buildQueryParams(queryParams);
if (data?.pagination) {
return Request<Paginate<T[]>>("GET", `${api}?${queryString}`);
} else {
return Request<T[]>("GET", `${api}/all?${queryString}`);
}
};
}; };

View File

@@ -77,12 +77,11 @@ instance.interceptors.response.use(
// 如果是GET传参就自动识别为queryPOST为data // 如果是GET传参就自动识别为queryPOST为data
const Request = <T>(method: string, url: string, reqParams?: object) => { const Request = <T>(method: string, url: string, reqParams?: object) => {
if (!method) method = "GET";
return instance.request<any, Response<T>>({ return instance.request<any, Response<T>>({
method, method,
url, url,
[method.toLocaleUpperCase() === "GET" ? "params" : "data"]: reqParams ...reqParams
// [method.toLocaleUpperCase() === "GET" ? "params" : "data"]: reqParams
}); });
}; };