1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
import { getPayload } from 'payload'
import config from '@payload-config'
import type { Media } from '@/../payload-types'
// Payload 文章类型
export interface PayloadPost {
id: number
title: string
slug: string
description?: string
content: unknown // Lexical 富文本内容
featuredImage?: Media | number
author?: string
tags?: { tag: string; id?: string }[]
status: 'draft' | 'published'
publishedAt?: string
createdAt: string
updatedAt: string
}
// 转换后的文章类型(用于前端展示)
export interface BlogPost {
id: number
title: string
slug: string
url: string
description: string
content: unknown
image?: string
author: string
tags: string[]
date: Date
createdAt: Date
updatedAt: Date
}
// 获取 Payload 实例
async function getPayloadClient() {
return getPayload({ config })
}
// 将 Payload 文章转换为博客文章格式
function transformPost(post: PayloadPost): BlogPost {
// 处理封面图片 URL
let imageUrl: string | undefined
if (post.featuredImage && typeof post.featuredImage === 'object') {
imageUrl = post.featuredImage.url || undefined
}
// 处理标签数组
const tags = post.tags?.map((t) => t.tag).filter(Boolean) || []
// 处理日期
const date = post.publishedAt ? new Date(post.publishedAt) : new Date(post.createdAt)
return {
id: post.id,
title: post.title,
slug: post.slug,
url: `/posts/${post.slug}`,
description: post.description || '',
content: post.content,
image: imageUrl,
author: post.author || 'Admin',
tags,
date,
createdAt: new Date(post.createdAt),
updatedAt: new Date(post.updatedAt),
}
}
// 获取所有已发布的文章(按发布时间排序)
export async function getPublishedPosts(options?: {
limit?: number
page?: number
}): Promise<{ posts: BlogPost[]; totalDocs: number; totalPages: number }> {
const payload = await getPayloadClient()
const result = await payload.find({
collection: 'posts',
where: {
status: { equals: 'published' },
},
sort: '-publishedAt',
limit: options?.limit || 10,
page: options?.page || 1,
depth: 1, // 获取关联的 media 数据
})
return {
posts: result.docs.map((doc) => transformPost(doc as unknown as PayloadPost)),
totalDocs: result.totalDocs,
totalPages: result.totalPages,
}
}
// 根据 slug 获取单篇文章
export async function getPostBySlug(slug: string): Promise<BlogPost | null> {
const payload = await getPayloadClient()
const result = await payload.find({
collection: 'posts',
where: {
slug: { equals: slug },
status: { equals: 'published' },
},
limit: 1,
depth: 1,
})
if (result.docs.length === 0) {
return null
}
return transformPost(result.docs[0] as unknown as PayloadPost)
}
// 获取所有已发布文章的 slug(用于静态生成)
export async function getAllPostSlugs(): Promise<string[]> {
const payload = await getPayloadClient()
const result = await payload.find({
collection: 'posts',
where: {
status: { equals: 'published' },
},
limit: 1000,
depth: 0,
})
return result.docs.map((doc) => (doc as unknown as PayloadPost).slug)
}
// 根据标签获取文章
export async function getPostsByTag(
tag: string,
options?: { limit?: number; page?: number }
): Promise<{ posts: BlogPost[]; totalDocs: number; totalPages: number }> {
const payload = await getPayloadClient()
const result = await payload.find({
collection: 'posts',
where: {
and: [
{ status: { equals: 'published' } },
{ 'tags.tag': { equals: tag } },
],
},
sort: '-publishedAt',
limit: options?.limit || 10,
page: options?.page || 1,
depth: 1,
})
return {
posts: result.docs.map((doc) => transformPost(doc as unknown as PayloadPost)),
totalDocs: result.totalDocs,
totalPages: result.totalPages,
}
}
// 获取所有标签
export async function getAllTags(): Promise<{ tag: string; count: number }[]> {
const payload = await getPayloadClient()
const result = await payload.find({
collection: 'posts',
where: {
status: { equals: 'published' },
},
limit: 1000,
depth: 0,
})
// 统计标签
const tagCounts = new Map<string, number>()
for (const doc of result.docs) {
const post = doc as unknown as PayloadPost
if (post.tags) {
for (const { tag } of post.tags) {
if (tag) {
tagCounts.set(tag, (tagCounts.get(tag) || 0) + 1)
}
}
}
}
return Array.from(tagCounts.entries())
.map(([tag, count]) => ({ tag, count }))
.sort((a, b) => b.count - a.count)
}
|