172 lines
4.5 KiB
TypeScript
172 lines
4.5 KiB
TypeScript
import { defineStore } from 'pinia'
|
|
import {
|
|
IAlterLog,
|
|
IBlog, IBlogCategory, IBlogComment,
|
|
IBlogLatest, IBlogNestedComment, IBlogStats, INewBlog, IQueryBlog
|
|
} from '@/types/blog'
|
|
import {
|
|
addBlogApi,
|
|
addBlogCommentApi,
|
|
deleteBlogApi, queryBlogAlterApi,
|
|
queryBlogApi,
|
|
queryBlogByIdApi,
|
|
queryBlogCategoryApi,
|
|
queryBlogCommentApi,
|
|
queryBlogStatsApi,
|
|
queryLatestBlogApi,
|
|
queryUnapprovedBlogApi,
|
|
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[],
|
|
unapprovedBlogList: [] as IBlog[],
|
|
blogCommentList: [] as IBlogComment[],
|
|
blogNestedCommentList: [] as IBlogNestedComment[],
|
|
alterLogList: [] as IAlterLog[],
|
|
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: {
|
|
title: '',
|
|
topValue: 1,
|
|
isGreat: false,
|
|
category: '',
|
|
content: '',
|
|
isApproved: 0
|
|
} 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%',
|
|
isRefresh: false
|
|
}),
|
|
actions: {
|
|
async queryBlogList() {
|
|
const { currentPage, pageSize } = this.pageInfo
|
|
const result = await queryBlogApi(currentPage, pageSize)
|
|
this.blogList = result.records
|
|
this.pageInfo.totalSize = result.total
|
|
},
|
|
async queryUnapprovedBlogList() {
|
|
this.unapprovedBlogList = await queryUnapprovedBlogApi()
|
|
},
|
|
async queryBlogAlterLogList() {
|
|
this.alterLogList = await queryBlogAlterApi('puppy-service')
|
|
},
|
|
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,
|
|
isApproved: 0
|
|
}
|
|
},
|
|
getEmptyBlogComment(): IBlogComment {
|
|
return {
|
|
blogId: 0,
|
|
content: '',
|
|
createTime: '',
|
|
id: 0,
|
|
ipAddress: '',
|
|
isApproved: true,
|
|
name: '',
|
|
parentId: 0,
|
|
userAgent: '',
|
|
website: ''
|
|
}
|
|
}
|
|
}
|
|
})
|