From 14137c54721aa19f37ab1e8aa3941a41af3e7f4c Mon Sep 17 00:00:00 2001 From: tone Date: Fri, 12 Dec 2025 18:08:49 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=B0=83=E6=95=B4resource=E4=B8=BA?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E7=AB=AF=E6=B8=B2=E6=9F=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/backend/src/main.ts | 1 + .../app/(with-header-footer)/page.tsx | 1 - .../resource/components/ResourceCard.tsx | 16 ++------- .../resource/components/ResourceCardImage.tsx | 26 ++++++++++++++ .../(with-header-footer)/resource/page.tsx | 22 +++--------- apps/frontend/lib/api/client.ts | 34 +++++++++++++++++++ apps/frontend/lib/api/resource/list.ts | 4 +-- 7 files changed, 70 insertions(+), 34 deletions(-) create mode 100644 apps/frontend/app/(with-header-footer)/resource/components/ResourceCardImage.tsx create mode 100644 apps/frontend/lib/api/client.ts diff --git a/apps/backend/src/main.ts b/apps/backend/src/main.ts index abd22c7..745eb1c 100644 --- a/apps/backend/src/main.ts +++ b/apps/backend/src/main.ts @@ -5,6 +5,7 @@ import { ResponseInterceptor } from './common/interceptors/response.interceptor' async function bootstrap() { const app = await NestFactory.create(AppModule); + app.setGlobalPrefix('api'); app.useGlobalPipes( new ValidationPipe({ transform: true, diff --git a/apps/frontend/app/(with-header-footer)/page.tsx b/apps/frontend/app/(with-header-footer)/page.tsx index 6a35ae6..dc58af6 100644 --- a/apps/frontend/app/(with-header-footer)/page.tsx +++ b/apps/frontend/app/(with-header-footer)/page.tsx @@ -1,4 +1,3 @@ -'use client'; import favicon from '../favicon.ico'; import Image from 'next/image'; diff --git a/apps/frontend/app/(with-header-footer)/resource/components/ResourceCard.tsx b/apps/frontend/app/(with-header-footer)/resource/components/ResourceCard.tsx index 9bfb538..7176cf9 100644 --- a/apps/frontend/app/(with-header-footer)/resource/components/ResourceCard.tsx +++ b/apps/frontend/app/(with-header-footer)/resource/components/ResourceCard.tsx @@ -1,32 +1,20 @@ import { ResourceBadge } from "@/components/resource"; import { Card, CardContent } from "@/components/ui/card"; import { Resource } from "@/lib/types/resource"; -import Image from "next/image"; -import React from "react"; +import ResourceCardImage from "./ResourceCardImage"; interface ResourceCardProps extends React.HTMLProps { r: Resource; } export function ResourceCard({ r, ...props }: ResourceCardProps) { - const [imageError, setImageError] = React.useState(false); - return (
- {!imageError && 资源图片 setImageError(true)} - />} +
{r.title}
diff --git a/apps/frontend/app/(with-header-footer)/resource/components/ResourceCardImage.tsx b/apps/frontend/app/(with-header-footer)/resource/components/ResourceCardImage.tsx new file mode 100644 index 0000000..8e0c7ba --- /dev/null +++ b/apps/frontend/app/(with-header-footer)/resource/components/ResourceCardImage.tsx @@ -0,0 +1,26 @@ +'use client'; +import React from "react"; +import Image from "next/image"; + +interface ResourceCardImage { + imageUrl: string; +} + +export default function ResourceCardImage({ imageUrl }: ResourceCardImage) { + const [imageError, setImageError] = React.useState(false); + + return ( + <> + {!imageError && 资源图片 setImageError(true)} + />} + + ) +} \ No newline at end of file diff --git a/apps/frontend/app/(with-header-footer)/resource/page.tsx b/apps/frontend/app/(with-header-footer)/resource/page.tsx index 4653091..c471c44 100644 --- a/apps/frontend/app/(with-header-footer)/resource/page.tsx +++ b/apps/frontend/app/(with-header-footer)/resource/page.tsx @@ -1,6 +1,3 @@ -"use client"; - -import useSWR from "swr"; import { ResourceCard } from "./components/ResourceCard"; import { ResourceApi } from "@/lib/api"; import { @@ -9,13 +6,10 @@ import { AlertTitle, } from "@/components/ui/alert" import { AlertCircle } from "lucide-react"; -import { Skeleton } from "@/components/ui/skeleton"; -export default function Resources() { - const { data, isLoading, error } = useSWR( - '/api/resource', - () => ResourceApi.list(), - ); +export default async function Resources() { + let errorMsg = ''; + const data = await ResourceApi.list().catch(e => { errorMsg = `${e}`; return null; }); return (
@@ -24,13 +18,13 @@ export default function Resources() { 《使用条款和隐私政策》 ,继续使用或浏览表示您接受协议条款。

{ - error && ( + errorMsg && (
出错了 - {error.message} + {errorMsg}
@@ -38,12 +32,6 @@ export default function Resources() { }
- {isLoading && ( - [...Array(3).map((_, i) => ( - - ))] - )} - {data && data.map((resource) => ( ( + url: string, + options: RequestInit = {} +): Promise { + // const nextHeaders = await headers() + + const defaultHeaders: HeadersInit = { + 'Content-Type': 'application/json', + // ...nextHeaders, + } + + try { + const res = await fetch(new URL(url, process.env.API_BASE), { + ...options, + headers: { + ...defaultHeaders, + ...options.headers, + }, + cache: 'no-store', + }); + if (res.status === 200) { + const data = await res.json(); + if (data.statusCode === 200) { + return data.data; + } + throw new Error(data.message ?? '未知错误'); + } + throw new Error('请求失败'); + } catch (error) { + throw error; + } +} \ No newline at end of file diff --git a/apps/frontend/lib/api/resource/list.ts b/apps/frontend/lib/api/resource/list.ts index 848a539..40d63fd 100644 --- a/apps/frontend/lib/api/resource/list.ts +++ b/apps/frontend/lib/api/resource/list.ts @@ -1,6 +1,6 @@ import { Resource } from "@/lib/types/resource"; -import fetcher from "../fetcher"; +import { apiFetch } from "../client"; export async function list() { - return fetcher('/api/resource'); + return apiFetch('/api/resource'); } \ No newline at end of file