数据结构优化
This commit is contained in:
@@ -502,9 +502,20 @@ export const deleteWork = async (id: string): Promise<void> => {
|
||||
}
|
||||
|
||||
// 文章管理API
|
||||
export const getAdminPosts = async (): Promise<PaginationResponse<Post>> => {
|
||||
export const getAdminPosts = async (
|
||||
page: number = 1,
|
||||
pageSize: number = 20,
|
||||
keyword?: string
|
||||
): Promise<PaginationResponse<Post>> => {
|
||||
try {
|
||||
const response = await fetch(`${API_BASE}/admin/posts?pageSize=1000`, {
|
||||
const params = new URLSearchParams()
|
||||
params.append('page', page.toString())
|
||||
params.append('pageSize', pageSize.toString())
|
||||
if (keyword) {
|
||||
params.append('keyword', keyword)
|
||||
}
|
||||
|
||||
const response = await fetch(`${API_BASE}/admin/posts?${params.toString()}`, {
|
||||
headers: getAuthHeaders()
|
||||
})
|
||||
if (!response.ok) {
|
||||
@@ -519,7 +530,14 @@ export const getAdminPosts = async (): Promise<PaginationResponse<Post>> => {
|
||||
}
|
||||
}
|
||||
|
||||
export const fetchPosts = async (query?: string, categoryId?: number, tagId?: number, columnId?: number): Promise<Post[]> => {
|
||||
export const fetchPosts = async (
|
||||
query?: string,
|
||||
categoryId?: number,
|
||||
tagId?: number,
|
||||
columnId?: number,
|
||||
page?: number,
|
||||
pageSize?: number
|
||||
): Promise<PaginationResponse<Post>> => {
|
||||
try {
|
||||
let url = `${API_BASE}/posts`
|
||||
const params = new URLSearchParams()
|
||||
@@ -527,6 +545,8 @@ export const fetchPosts = async (query?: string, categoryId?: number, tagId?: nu
|
||||
if (categoryId) params.append('category', categoryId.toString())
|
||||
if (tagId) params.append('tag', tagId.toString())
|
||||
if (columnId) params.append('column', columnId.toString())
|
||||
if (page) params.append('page', page.toString())
|
||||
if (pageSize) params.append('pageSize', pageSize.toString())
|
||||
|
||||
if (params.toString()) {
|
||||
url += `?${params.toString()}`
|
||||
@@ -535,10 +555,28 @@ export const fetchPosts = async (query?: string, categoryId?: number, tagId?: nu
|
||||
const response = await fetch(url)
|
||||
if (!response.ok) throw new Error('Failed to fetch posts')
|
||||
const data = await response.json()
|
||||
return data.result || []
|
||||
|
||||
// 返回分页格式的响应
|
||||
if (data.result && typeof data.result === 'object' && 'list' in data.result) {
|
||||
return data.result as PaginationResponse<Post>
|
||||
}
|
||||
|
||||
// 兼容旧格式(直接返回数组)
|
||||
const list = Array.isArray(data.result) ? data.result : []
|
||||
return {
|
||||
list,
|
||||
total: list.length,
|
||||
page: page || 1,
|
||||
size: pageSize || list.length
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error fetching posts:', error)
|
||||
return []
|
||||
return {
|
||||
list: [],
|
||||
total: 0,
|
||||
page: page || 1,
|
||||
size: pageSize || 10
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -607,6 +645,38 @@ export const updatePost = async (id: number | string, postData: Omit<Post, 'id'>
|
||||
}
|
||||
}
|
||||
|
||||
// 更新文章关联关系(只更新分类、专栏、标签,不更新内容)
|
||||
export const updatePostRelations = async (
|
||||
id: number | string,
|
||||
relations: { categoryId: number; columnId: number | null; tagIds: number[] }
|
||||
): Promise<void> => {
|
||||
try {
|
||||
const payload: any = {
|
||||
categoryId: relations.categoryId,
|
||||
tagIds: relations.tagIds
|
||||
}
|
||||
|
||||
if (relations.columnId !== null && relations.columnId > 0) {
|
||||
payload.columnId = relations.columnId
|
||||
} else {
|
||||
payload.columnId = null
|
||||
}
|
||||
|
||||
const response = await fetch(`${API_BASE}/admin/posts/${id}/relations`, {
|
||||
method: 'PATCH',
|
||||
headers: getAuthHeaders(),
|
||||
body: JSON.stringify(payload)
|
||||
})
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json()
|
||||
throw new Error(errorData.message || '更新文章关联失败')
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Update post relations error:', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
export const togglePostStatus = async (id: number, isPublished: number): Promise<void> => {
|
||||
try {
|
||||
const response = await fetch(`${API_BASE}/admin/posts/${id}/status`, {
|
||||
@@ -1104,6 +1174,7 @@ export interface PublicSettings {
|
||||
site_author?: string
|
||||
site_keywords?: string
|
||||
visible_menus?: string
|
||||
posts_per_page?: string
|
||||
}
|
||||
|
||||
export const getPublicSettings = async (): Promise<PublicSettings> => {
|
||||
|
||||
Reference in New Issue
Block a user