You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
138 lines
3.8 KiB
138 lines
3.8 KiB
// hooks/usePost.ts
|
|
import { ref, reactive, computed } from 'vue'
|
|
import type { Post, PostListParams, FilterForm, Pagination, Stats } from '@/views/post'
|
|
import { postApi } from '@/utils/post'
|
|
import { ElMessage } from 'element-plus'
|
|
|
|
export function usePostManagement() {
|
|
const loading = ref(false)
|
|
const posts = ref<Post[]>([])
|
|
const selectedPosts = ref<Post[]>([])
|
|
|
|
const filterForm = reactive<FilterForm>({
|
|
title: '',
|
|
category: '',
|
|
status: ''
|
|
})
|
|
|
|
const pagination = reactive<Pagination>({
|
|
currentPage: 1,
|
|
pageSize: 20,
|
|
total: 0
|
|
})
|
|
|
|
// 计算统计数据
|
|
const stats = computed<Stats>(() => {
|
|
const totalPosts = posts.value.length
|
|
const totalViews = posts.value.reduce((sum, post) => sum + post.views, 0)
|
|
const totalLikes = posts.value.reduce((sum, post) => sum + post.likes, 0)
|
|
const totalComments = posts.value.reduce((sum, post) => sum + (post.comments || 0), 0)
|
|
const avgViews = totalPosts > 0 ? totalViews / totalPosts : 0
|
|
const avgLikes = totalPosts > 0 ? totalLikes / totalPosts : 0
|
|
|
|
return {
|
|
totalPosts,
|
|
totalViews,
|
|
totalLikes,
|
|
totalComments,
|
|
avgViews,
|
|
avgLikes
|
|
}
|
|
})
|
|
|
|
// 获取帖子列表
|
|
const fetchPosts = async () => {
|
|
loading.value = true
|
|
try {
|
|
const params: PostListParams = {
|
|
page: pagination.currentPage,
|
|
pageSize: pagination.pageSize,
|
|
title: filterForm.title || undefined,
|
|
category: filterForm.category || undefined,
|
|
status: filterForm.status || undefined
|
|
}
|
|
|
|
const response = await postApi.getMyPosts(params)
|
|
posts.value = response.data
|
|
pagination.total = response.total
|
|
} catch (error) {
|
|
ElMessage.error('获取帖子列表失败')
|
|
console.error('Fetch posts error:', error)
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
// 删除帖子
|
|
const deletePost = async (postId: number): Promise<boolean> => {
|
|
try {
|
|
await postApi.deletePost(postId)
|
|
ElMessage.success('删除成功')
|
|
return true
|
|
} catch (error) {
|
|
ElMessage.error('删除失败')
|
|
console.error('Delete post error:', error)
|
|
return false
|
|
}
|
|
}
|
|
|
|
// 批量删除
|
|
const batchDeletePosts = async (postIds: number[]): Promise<boolean> => {
|
|
try {
|
|
await postApi.batchDeletePosts(postIds)
|
|
ElMessage.success(`成功删除 ${postIds.length} 个帖子`)
|
|
return true
|
|
} catch (error) {
|
|
ElMessage.error('批量删除失败')
|
|
console.error('Batch delete posts error:', error)
|
|
return false
|
|
}
|
|
}
|
|
|
|
// 搜索
|
|
const handleSearch = () => {
|
|
pagination.currentPage = 1
|
|
fetchPosts()
|
|
}
|
|
|
|
// 重置筛选
|
|
const handleReset = () => {
|
|
Object.keys(filterForm).forEach(key => {
|
|
filterForm[key as keyof FilterForm] = ''
|
|
})
|
|
handleSearch()
|
|
}
|
|
|
|
// 选择变化
|
|
const handleSelectionChange = (selection: Post[]) => {
|
|
selectedPosts.value = selection
|
|
}
|
|
|
|
// 分页变化
|
|
const handleSizeChange = (newSize: number) => {
|
|
pagination.pageSize = newSize
|
|
fetchPosts()
|
|
}
|
|
|
|
const handleCurrentChange = (newPage: number) => {
|
|
pagination.currentPage = newPage
|
|
fetchPosts()
|
|
}
|
|
|
|
return {
|
|
loading,
|
|
posts,
|
|
selectedPosts,
|
|
filterForm,
|
|
pagination,
|
|
stats,
|
|
fetchPosts,
|
|
deletePost,
|
|
batchDeletePosts,
|
|
handleSearch,
|
|
handleReset,
|
|
handleSelectionChange,
|
|
handleSizeChange,
|
|
handleCurrentChange
|
|
}
|
|
} |