Files
blog-admin-ui/src/store/blog.ts
2025-09-22 19:42:28 +08:00

149 lines
4.1 KiB
TypeScript

import { defineStore } from 'pinia'
import {
IBlog, IBlogCategory, IBlogComment,
IBlogLatest, IBlogNestedComment, IBlogStats, INewBlog, IQueryBlog
} from '@/types/blog'
import {
addBlogApi, addBlogCommentApi, deleteBlogApi,
queryBlogApi, queryBlogByConditionApi, queryBlogByIdApi,
queryBlogCategoryApi, queryBlogCommentApi, queryBlogStatsApi, queryLatestBlogApi, updateBlogApi
} from '@/apis/blog.ts'
import { IHandleApi } from 'vue3-common/types'
import { ElMessage } from 'element-plus'
import { ImagePathEnum } from '@/types'
import { flattenToTwoLevel } from '@/utils/blog/blogUtil.ts'
export const useBlogStore = defineStore('blog', {
state: () => ({
blogList: [] as IBlog[],
blogCommentList: [] as IBlogComment[],
blogNestedCommentList: [] as IBlogNestedComment[],
currentBlogComment: {
id: 0,
blogId: 0,
name: '',
website: '',
content: '',
ipAddress: '',
userAgent: '',
parentId: 0,
isApproved: true,
createTime: ''
} as IBlogComment,
currentBlogId: 0,
currentBlog: {
id: 0,
title: '',
topValue: 0,
isGreat: false,
categoryId: 0,
category: '',
summary: '',
content: '',
wordCount: 0,
readDuration: 0,
visitCount: 0,
createTime: '',
updateTime: ''
} as IBlog,
newBlog: {} as INewBlog,
blogCategoryList: [] as IBlogCategory[],
blogStats: {} as IBlogStats,
latestBlogList: [] as IBlogLatest[],
queryBlog: {} as IQueryBlog,
blogImagePath: ImagePathEnum.BLOG,
blogApiType: 'ADD' as IHandleApi,
pageInfo: {
currentPage: 1,
pageSize: 10,
totalSize: 1
},
isScrollEnd: false,
progressValue: '0%'
}),
actions: {
async queryBlogList() {
const { currentPage, pageSize } = this.pageInfo
const result = await queryBlogApi(currentPage, pageSize)
this.blogList = result.records
this.pageInfo.totalSize = result.total
},
async queryBlogByCondition(query: IQueryBlog) {
this.blogList = await queryBlogByConditionApi(query)
},
async queryBlogById(id: number) {
this.currentBlog = await queryBlogByIdApi(id)
await this.queryBlogComment(this.currentBlogId)
this.blogNestedCommentList = flattenToTwoLevel(this.blogCommentList)
},
async queryBlogByIdApi(id: number) {
this.currentBlog = await queryBlogByIdApi(id)
},
async queryBlogComment(blogId: number) {
this.blogCommentList = await queryBlogCommentApi(blogId)
},
async addBlogComment(blogComment: IBlogComment) {
await addBlogCommentApi(blogComment)
await this.queryBlogById(blogComment.blogId)
},
async queryBlogCategory() {
this.blogCategoryList = await queryBlogCategoryApi()
},
async queryBlogStats() {
this.blogStats = await queryBlogStatsApi()
},
async queryLatestBlog() {
this.latestBlogList = await queryLatestBlogApi()
},
async handleBlogApi(id?: number) {
switch (this.blogApiType) {
case 'ADD':
await addBlogApi(this.newBlog)
ElMessage.success('新增博客成功')
break
case 'UPDATE':
await updateBlogApi(this.newBlog.id as number, this.newBlog)
ElMessage.success('更新博客成功')
break
case 'DELETE':
await deleteBlogApi(id as number)
ElMessage.success('删除博客成功')
break
default:
break
}
await this.refreshInfo()
},
async refreshInfo() {
this.pageInfo.currentPage = 1
await this.queryBlogList()
await this.queryBlogStats()
await this.queryLatestBlog()
},
getEmptyBlog(): INewBlog {
return {
id: 0,
category: '',
content: '',
isGreat: false,
title: '',
topValue: 0
}
},
getEmptyBlogComment(): IBlogComment {
return {
blogId: 0,
content: '',
createTime: '',
id: 0,
ipAddress: '',
isApproved: true,
name: '',
parentId: 0,
userAgent: '',
website: ''
}
}
}
})