refactor(api): 更新API调用方式为GET并调整数据处理

- 将多个API方法的请求方式从POST改为GET,以提高一致性和性能。
- 更新相关组件以适应新的数据结构,确保从API响应中正确访问结果。
- 移除不必要的API调用,简化代码结构。
This commit is contained in:
宇阳
2026-05-13 20:09:50 +08:00
parent b95545dae2
commit a0e790873e
20 changed files with 39 additions and 56 deletions

View File

@@ -29,8 +29,8 @@ export default defineConfig([
{
rules: {
// 禁止使用 any 类型
'@typescript-eslint/no-explicit-any': 'warn', // 改为警告,鼓励使用具体类型
'no-unused-vars': 'warn', // 改为警告,提醒未使用的变量
'@typescript-eslint/no-explicit-any': 'off', // 改为警告,鼓励使用具体类型
'no-unused-vars': 'off', // 改为警告,提醒未使用的变量
'react-refresh/only-export-components': 'off',
'react/display-name': 'off',
'react/prop-types': 'off', // TypeScript 项目不需要 prop-types 验证

View File

@@ -4,7 +4,7 @@ import { Request } from '@/utils';
// 获取分类列表
export const getCateListAPI = async () => {
return await Request<Paginate<Cate[]>>('GET', '/cate?pattern=tree')
return await Request<Paginate<Cate[]>>('GET', '/cate')
}
// 获取指定分类中的所有文章

View File

@@ -6,15 +6,9 @@ export const addCommentDataAPI = async (data: Comment) => {
return await Request('POST', `/comment`, data);
}
// 获取评论列表
export const getCommentListAPI = async () => {
return await Request<Comment[]>('POST', `/comment/list`);
}
// 分页获取评论数据
export const getCommentPagingAPI = async () => {
return await Request<Paginate<Comment[]>>('POST', `/comment/paging`);
}
// 获取评论列表
export const getCommentListAPI = () => Request<Paginate<Comment[]>>('GET', `/comment`)
// 获取当前文章中所有评论
export const getArticleCommentListAPI = async (articleId: number, paginate: Page) => {

View File

@@ -5,4 +5,4 @@ import { Footprint } from '@/types/app/footprint'
export const getFootprintDataAPI = (id?: number) => Request<Footprint>('GET', `/footprint/${id}`)
// 获取足迹列表
export const getFootprintListAPI = () => Request<Footprint[]>('POST', '/footprint/list');
export const getFootprintListAPI = () => Request<Footprint[]>('GET', '/footprint');

View File

@@ -1,10 +1,7 @@
import { Request } from '@/utils'
import { Record } from '@/types/app/record'
// 获取说说列表
export const getRecordListAPI = (data?: QueryData) => Request<Record[]>('POST', `/record/list`, {
data: { ...data?.query },
})
// 分页获取说说列表
export const getRecordPagingAPI = (data?: QueryData) => Request<Paginate<Record[]>>('POST', `/record/paging?page=${data?.pagination?.page}&size=${data?.pagination?.size ? data.pagination?.size : 8}`)
export const getRecordPagingAPI = (params?: Page) => Request<Paginate<Record[]>>('GET', `/record`, { params })
// export const getRecordPagingAPI = (data?: QueryData) => Request<Paginate<Record[]>>('GET', `/record?page=${data?.pagination?.page}&size=${data?.pagination?.size ? data.pagination?.size : 8}`)

View File

@@ -5,4 +5,4 @@ import { Swiper } from '@/types/app/swiper'
export const getSwiperDataAPI = (id?: number) => Request<Swiper>('GET', `/swiper/${id}`)
// 获取轮播图数据列表
export const getSwiperListAPI = () => Request<Swiper[]>('POST', `/swiper/list`)
export const getSwiperListAPI = () => Request<Paginate<Swiper[]>>('GET', `/swiper`)

View File

@@ -4,20 +4,10 @@ import { Article } from '@/types/app/article';
// 获取标签列表
export const getTagListAPI = async () => {
return await Request<Tag[]>('POST', `/tag/list`);
}
// 获取标签列表+文章数量统计
export const getTagListWithArticleCountAPI = async () => {
return await Request<Tag[]>('GET', `/tag/article/count`);
return await Request<Paginate<Tag[]>>('GET', `/tag`);
}
// 获取指定标签中的所有文章
export const getTagArticleListAPI = async (id: number, page: number) => {
return await Request<Paginate<Article[]>>('GET', `/article/tag/${id}?page=${page}`)
}
// 分页获取标签数据
export const getTagPagingAPI = async (data: QueryData) => {
return await Request<Paginate<Tag[]>>('POST', `/tag/paging?page=${data.pagination?.page}&&size=8`, data.query);
export const getTagArticleListAPI = async (id: number, params: Page) => {
return await Request<Paginate<Article[]>>('GET', `/tag/${id}/articles`, { params })
}

View File

@@ -13,7 +13,7 @@ export async function GET() {
const web = webResponse?.data?.value as Web;
const { data: user } = await getAuthorDataAPI();
const { data: article } = await getArticlePagingAPI({ page: 1, size: 8 });
const { data: record } = await getRecordPagingAPI({ pagination: { page: 1, size: 8 } });
const { data: record } = await getRecordPagingAPI({ page: 1, size: 8 });
const articleList = article?.result ?? [];
const recordList = record?.result ?? [];
@@ -51,12 +51,12 @@ export async function GET() {
content: item?.content,
author: user?.name
? [
{
name: user.name,
email: user.email,
link: web?.url,
},
]
{
name: user.name,
email: user.email,
link: web?.url,
},
]
: [],
copyright: 'ThriveX 现代化博客管理系统',
date: new Date(+item?.createTime),

View File

@@ -9,7 +9,7 @@ export default () => {
const [list, setList] = useState<Tag[]>([]);
const getTagData = async () => {
const { data } = await getTagListAPI();
setList(data);
setList(data?.result ?? []);
};
useEffect(() => {

View File

@@ -29,10 +29,11 @@ export default ({ aTotal }: Props) => {
const [commentList, setCommentList] = useState<Comment[]>([]);
const [linkList, setLinkList] = useState<Web[]>([]);
// 优化:移除这三个获取列表接口,使用一个接口来获取相关数据
const getData = async () => {
await Promise.all([getCateListAPI(), getCommentListAPI(), getWebListAPI()]).then(([cateList, commentList, linkList]) => {
setCateList(cateList?.data.result ?? []);
setCommentList(commentList?.data ?? []);
setCommentList(commentList?.data.result ?? []);
setLinkList(linkList?.data ?? []);
});
};

View File

@@ -8,7 +8,7 @@ interface Props {
}
const imgClass = 'object-cover w-full h-full min-w-full min-h-full transition-transform duration-500 group-hover/img:scale-110';
const boxClass = 'group/img relative overflow-hidden rounded-2xl bg-slate-100 dark:bg-slate-800 cursor-pointer border border-slate-100 dark:border-slate-700 flex items-center justify-center';
const boxClass = 'group/img relative overflow-hidden rounded-2xl cursor-pointer flex items-center justify-center';
export default ({ list }: Props) => {
if (!list?.length) return null;

View File

@@ -26,7 +26,7 @@ export default () => {
const fetchRecordList = useCallback(async (page: number, append: boolean = false) => {
setLoading(true);
try {
const { data: recordData } = await getRecordPagingAPI({ pagination: { page, size: 8 } });
const { data: recordData } = await getRecordPagingAPI({ page, size: 8 });
if (recordData?.result && recordData?.result?.length > 0) {
if (append) {

View File

@@ -1,4 +1,4 @@
import { getTagListWithArticleCountAPI } from '@/api/tag';
import { getTagListAPI } from '@/api/tag';
import { Metadata } from 'next';
import TagsPageClient from './components/TagsPageClient';
@@ -8,6 +8,6 @@ export const metadata: Metadata = {
};
export default async () => {
const { data } = await getTagListWithArticleCountAPI();
return <TagsPageClient tags={data ?? []} />;
const { data } = await getTagListAPI();
return <TagsPageClient tags={data?.result ?? []} />;
};

View File

@@ -32,7 +32,7 @@ const Classics = async ({ data }: ClassicsProps) => {
};
return (
<div className="space-y-4">
<div className="space-y-2">
{data?.result?.map((item, index) => (
<div key={item.id} className="relative overflow-hidden flex h-[190px] md:h-60 lg:h-52 xl:h-60 bg-black-b tw_container">
{index % 2 === 0 && (

View File

@@ -20,10 +20,10 @@ export default function Dynamic({ className = '' }: { className?: string }) {
const getRecordList = async () => {
try {
const { data } = await getRecordPagingAPI({ pagination: { page: 1, size: 8 } });
const { data } = await getRecordPagingAPI({ page: 1, size: 8 });
setList(data?.result ?? []);
} catch (error) {
console.error('Failed to fetch records:', error);
console.error('获取说说失败:', error);
} finally {
setIsLoading(false);
}

View File

@@ -26,7 +26,7 @@ export default async ({ page }: { page: number }) => {
return (
<div className={`w-full md:w-[90%] ${sidebar?.length ? 'lg:w-[68%] xl:w-[73%]' : 'w-full'} mx-auto transition-width`}>
{!!swiper?.length && <Swiper data={swiper} />}
{!!swiper.result?.length && <Swiper data={swiper.result} />}
<Dynamic className="my-2" />
{theme.is_article_layout === 'classics' && <Classics data={data} />}

View File

@@ -2,7 +2,7 @@ import HCaptcha from '@hcaptcha/react-hcaptcha';
import { forwardRef, Ref } from 'react';
import { useConfigStore } from '@/stores';
export default forwardRef(({ setToken }: { setToken: (token: string) => void }, ref: Ref<HCaptcha>) => {
export default forwardRef(({ setToken }: { setToken: () => void }, ref: Ref<HCaptcha>) => {
const config = useConfigStore();
const sitekey = config?.other?.hcaptcha_key;

View File

@@ -3,7 +3,7 @@
import Image from 'next/image';
import Link from 'next/link';
import { useEffect, useState } from 'react';
import { getCommentPagingAPI } from '@/api/comment';
import { getCommentListAPI } from '@/api/comment';
import NewCommentSvg from '@/assets/svg/other/comments.svg';
import RandomAvatar from '@/components/RandomAvatar';
import { Comment } from '@/types/app/comment';
@@ -13,13 +13,13 @@ import { RiTimeLine } from 'react-icons/ri';
const NewComments = () => {
const [list, setList] = useState<Comment[]>([]);
const getCommentPaging = async () => {
const { data } = await getCommentPagingAPI();
const getCommentList = async () => {
const { data } = await getCommentListAPI();
setList(data?.result ?? []);
};
useEffect(() => {
getCommentPaging();
getCommentList();
}, []);
return (

View File

@@ -40,7 +40,7 @@ export default ({ data, className }: { data: SwiperType[]; className?: string })
return (
<>
<div className={`group relative w-full h-[200px] sm:h-[270px] lg:h-[350px] rounded-2xl overflow-hidden after:content-[''] after:w-full after:h-[60%] after:absolute after:bottom-0 after:left-0 after:bg-[linear-gradient(to_top,#2c333e,transparent)] ${className}`} onMouseEnter={() => setIsHovered(true)} onMouseLeave={() => setIsHovered(false)}>
<div className={`group relative w-full h-[200px] mb-2 sm:h-[270px] lg:h-[350px] rounded-2xl overflow-hidden after:content-[''] after:w-full after:h-[60%] after:absolute after:bottom-0 after:left-0 after:bg-[linear-gradient(to_top,#2c333e,transparent)] ${className}`} onMouseEnter={() => setIsHovered(true)} onMouseLeave={() => setIsHovered(false)}>
<div onClick={handlePrev} className="flex justify-center items-center w-11 h-11 bg-[#fff3] rounded-full hover:bg-transparent hover:backdrop-blur-md hover:scale-110 group-hover:opacity-100 opacity-0 transition-[transform,opacity] absolute top-1/2 left-3.5 -translate-y-1/2 z-20 cursor-pointer">
<BiChevronLeft className="text-4xl text-gray-100" />
</div>

1
优化.txt Normal file
View File

@@ -0,0 +1 @@
1、频繁使用的接口数据存储在全局