feat(components): 评论组件增加分页

This commit is contained in:
ronger 2023-03-24 15:20:25 +08:00
parent e1686c9c6f
commit 1fff1791ec
3 changed files with 58 additions and 46 deletions

View File

@ -33,7 +33,7 @@
后发布评论 后发布评论
</el-col> </el-col>
<el-col> <el-col>
<el-col :key="comment.idComment" v-for="comment in comment.data"> <el-col :key="comment.idComment" v-for="comment in comments.list">
<el-card :id="'comment-' + comment.idComment" style="margin-top: 1rem;"> <el-card :id="'comment-' + comment.idComment" style="margin-top: 1rem;">
<el-col :sm="1" :xl="1" :xs="3"> <el-col :sm="1" :xl="1" :xs="3">
<el-avatar :src="comment.commenter.userAvatarURL" v-show="comment.commenter.userAvatarURL"></el-avatar> <el-avatar :src="comment.commenter.userAvatarURL" v-show="comment.commenter.userAvatarURL"></el-avatar>
@ -131,6 +131,19 @@
</el-col> </el-col>
</el-col> </el-col>
</el-col> </el-col>
<el-col>
<div class="vertical-container text-center">
<el-pagination :hide-on-single-page="true"
layout="prev, pager, next"
:page-size="comments.pageSize"
:current-page="comments.pageNum"
:total="comments.total"
prev-text="上一页"
next-text="下一页"
@current-change="currentChange">
</el-pagination>
</div>
</el-col>
</el-col> </el-col>
</el-col> </el-col>
</el-row> </el-row>
@ -155,23 +168,19 @@ export default {
title: { title: {
type: String, type: String,
default: '' default: ''
},
postId: {
type: Number,
required: true
} }
}, },
fetch() { fetch() {
let {store} = this.$nuxt.context let {store, params, query} = this.$nuxt.context
return Promise.all([ return Promise.all([
store.dispatch('comment/fetchList', {post_id: this.postId}) store.dispatch('comment/fetchList', {post_id: params.article_id, page: query.page})
]) ])
}, },
computed: { computed: {
...mapState({ ...mapState({
comment: state => state.comment.data, comments: state => state.comment.list.data,
isFetchingComment: state => state.comment.fetching, isFetchingComment: state => state.comment.list.fetching,
isPostingComment: state => state.comment.posting, // isPostingComment: state => state.comment.posting,
constants: state => state.global.constants, constants: state => state.global.constants,
language: state => state.global.language, language: state => state.global.language,
isMobile: state => state.global.isMobile, isMobile: state => state.global.isMobile,
@ -200,7 +209,8 @@ export default {
loading: false, loading: false,
commentOriginalCommentId: 0, commentOriginalCommentId: 0,
commentAuthorAvatar: '', commentAuthorAvatar: '',
commentTitle: '' commentTitle: '',
postId: 0
} }
}, },
methods: { methods: {
@ -384,10 +394,19 @@ export default {
}, },
isAuthor(commentAuthorId) { isAuthor(commentAuthorId) {
return this.authorId === commentAuthorId; return this.authorId === commentAuthorId;
},
currentChange(page) {
this.$router.push({
path: `/article/${this.postId}`,
query: {
page: page
}
})
} }
}, },
async mounted() { async mounted() {
let _ts = this; let _ts = this;
_ts.$set(_ts, 'postId', _ts.$route.params.article_id);
_ts.$store.commit('setActiveMenu', 'post-article'); _ts.$store.commit('setActiveMenu', 'post-article');
if (_ts.loggedIn) { if (_ts.loggedIn) {
const responseData = await _ts.$axios.$get('/api/upload/token'); const responseData = await _ts.$axios.$get('/api/upload/token');
@ -450,6 +469,11 @@ export default {
if (isFetching) { if (isFetching) {
this.cancelCommentReply() this.cancelCommentReply()
} }
},
'$route'(to, from) {
if ((to.query.page || 1) !== (from.query.page || 1)) {
this.$fetch()
}
} }
}, },
destroyed() { destroyed() {

View File

@ -145,7 +145,7 @@
</el-col> </el-col>
<el-col> <el-col>
<comment-box :fetching="isFetching" :title="article.articleTitle" <comment-box :fetching="isFetching" :title="article.articleTitle"
:post-id="routeArticleId" :authorId="article.articleAuthorId" @gotoLogin="gotoLogin"></comment-box> :authorId="article.articleAuthorId" @gotoLogin="gotoLogin"></comment-box>
</el-col> </el-col>
<el-col> <el-col>
<el-dialog :visible.sync="dialogVisible"> <el-dialog :visible.sync="dialogVisible">
@ -207,9 +207,6 @@ export default {
}, },
isAdmin() { isAdmin() {
return this.$auth.hasScope('admin') || this.$auth.hasScope('blog_admin'); return this.$auth.hasScope('admin') || this.$auth.hasScope('blog_admin');
},
routeArticleId() {
return Number(this.$route.params.article_id);
} }
}, },
head() { head() {

View File

@ -9,34 +9,30 @@ export const LIKE_COMMENT_API_PATH = '/like/comment'
const getDefaultListData = () => { const getDefaultListData = () => {
return { return {
data: [], comments: [],
pagination: {} pagination: {}
} }
} }
export const state = () => { export const state = () => {
return { return {
list: {
fetching: false, fetching: false,
posting: false,
data: getDefaultListData() data: getDefaultListData()
} }
} }
}
export const mutations = { export const mutations = {
// 请求列表 // 请求列表
updateListFetching(state, action) { updateListFetching(state, action) {
state.fetching = action state.list.fetching = action
}, },
updateListData(state, action) { updateListData(state, action) {
state.data = { state.list.data = action
data: action,
pagination: {
page: 1
}
}
}, },
clearListData(state) { clearListData(state) {
state.data = getDefaultListData() state.list.data = getDefaultListData()
}, },
// 发布评论 // 发布评论
@ -59,28 +55,23 @@ export const mutations = {
} }
export const actions = { export const actions = {
fetchList({ commit, rootState }, params = {}) { fetchList({ commit, state }, params = {}) {
// const { SortType } = rootState.global.constants // 清空已有数据
// 修正参数
// params = Object.assign(
// {
// page: 1,
// per_page: 88
// },
// params
// )
// const isRestart = params.page === 1
// const isDescSort = params.sort === SortType.Desc
// 清空数据
// isRestart && commit('updateListData', getDefaultListData())
// commit('updateListData', getDefaultListData())
commit('updateListFetching', true) commit('updateListFetching', true)
let currentData = JSON.parse(JSON.stringify(state)).list.data
if (Number(params.page) === currentData.pageNum) {
commit('updateListFetching', false)
return
}
let data = {
page: params.page || 1
}
commit('updateListData', getDefaultListData())
return this.$axios return this.$axios
.$get(COMMENT_API_PATH + `${params.post_id}/comments`) .$get(COMMENT_API_PATH + `${params.post_id}/comments`, {
params: data
})
.then(response => { .then(response => {
// isDescSort && response.result.data.reverse() // isDescSort && response.result.data.reverse()
commit('updateListData', response) commit('updateListData', response)