From 712f556d6728bd6f8a57c07b31714d1b99d96ead Mon Sep 17 00:00:00 2001
From: xy <10816187@qq.com>
Date: Fri, 10 Jul 2026 18:59:54 +0800
Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=87=E7=AB=A0=E5=88=86=E9=A1=B5?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/components/PostList.astro | 154 ++++++++++++++++++++++++++
src/consts.ts | 2 +
src/pages/index.astro | 113 ++-----------------
src/pages/page/[...page].astro | 30 +++++
src/types/{index.d.ts => global.d.ts} | 0
src/types/index.ts | 1 +
src/types/post.ts | 8 ++
7 files changed, 206 insertions(+), 102 deletions(-)
create mode 100644 src/components/PostList.astro
create mode 100644 src/pages/page/[...page].astro
rename src/types/{index.d.ts => global.d.ts} (100%)
create mode 100644 src/types/index.ts
create mode 100644 src/types/post.ts
diff --git a/src/components/PostList.astro b/src/components/PostList.astro
new file mode 100644
index 0000000..48b33c4
--- /dev/null
+++ b/src/components/PostList.astro
@@ -0,0 +1,154 @@
+---
+import type {Post} from "@/types/post";
+import {formatDatetime} from "@/lib/format.ts";
+
+interface Props {
+ posts: Post[]
+ page: number
+ totalPage: number
+}
+
+const {posts, page, totalPage} = Astro.props
+
+posts.map((post) => {
+ post.published_at = formatDatetime(post.published_at, 'YYYY 年 M 月 D 日')
+})
+---
+
+
+
+
+{totalPage>1&&
+}
+
+
diff --git a/src/consts.ts b/src/consts.ts
index 1c0d7b0..ae2f97f 100644
--- a/src/consts.ts
+++ b/src/consts.ts
@@ -1,3 +1,5 @@
export const metadata = {
siteTitle: "Mobius",
+
+ postPageSize: 12
}
diff --git a/src/pages/index.astro b/src/pages/index.astro
index edf7c68..25675a4 100644
--- a/src/pages/index.astro
+++ b/src/pages/index.astro
@@ -1,111 +1,20 @@
---
import Layout from "@/layouts/Layout.astro";
-
+import PostList from "@/components/PostList.astro";
+import type {Post} from "@/types/post";
import {http} from "@/lib/request";
-import {formatDatetime} from "@/lib/format.ts";
+import { metadata } from "@/consts.ts";
-interface Post {
- title: string;
- summary: string;
- slug: string;
- cover: string;
- category_name: string;
- published_at: string;
-}
+const {list,total} = await http.get>("post", {
+ searchParams: {
+ page: 1,
+ page_size: metadata.postPageSize
+ }
+});
-const {list} = await http.get>("post");
-
-list.map((post) => {
- post.published_at = formatDatetime(post.published_at, 'YYYY 年 M 月 D 日')
-})
+const totalPage = Math.ceil(total / metadata.postPageSize)
---
-
+
-
-
diff --git a/src/pages/page/[...page].astro b/src/pages/page/[...page].astro
new file mode 100644
index 0000000..c044ef9
--- /dev/null
+++ b/src/pages/page/[...page].astro
@@ -0,0 +1,30 @@
+---
+import Layout from "@/layouts/Layout.astro";
+import PostList from "@/components/PostList.astro";
+import type {Post} from "@/types/post";
+import {http} from "@/lib/request";
+import {metadata} from "@/consts.ts";
+
+const {page} = Astro.params
+
+if (isNaN(Number(page))) {
+ return Astro.redirect('/')
+}
+
+const {list, total} = await http.get>("post", {
+ searchParams: {
+ page,
+ page_size: metadata.postPageSize
+ }
+});
+
+const totalPage = Math.ceil(total / metadata.postPageSize)
+
+if (totalPage < page) {
+ return Astro.redirect('/404')
+}
+---
+
+
+
+
diff --git a/src/types/index.d.ts b/src/types/global.d.ts
similarity index 100%
rename from src/types/index.d.ts
rename to src/types/global.d.ts
diff --git a/src/types/index.ts b/src/types/index.ts
new file mode 100644
index 0000000..6071fef
--- /dev/null
+++ b/src/types/index.ts
@@ -0,0 +1 @@
+export * from './post.ts'
diff --git a/src/types/post.ts b/src/types/post.ts
new file mode 100644
index 0000000..86ac85d
--- /dev/null
+++ b/src/types/post.ts
@@ -0,0 +1,8 @@
+export type Post = {
+ title: string;
+ summary: string;
+ slug: string;
+ cover: string;
+ category_name: string;
+ published_at: string;
+}