|
|
@@ -2,14 +2,21 @@
|
|
|
<div class="discussion-area">
|
|
|
<h1 class="title">讨论 & 问答</h1>
|
|
|
<div class="discussion-area-content">
|
|
|
- <write-new-comment ref="writeComment" class="mb-24" @comment-submit="handleCommentSubmit" :placeholder="writingAreaPlaceholder"></write-new-comment>
|
|
|
+ <write-new-comment ref="writeComment" class="mb-24" @comment-submit="handleCommentSubmit"
|
|
|
+ :placeholder="writingAreaPlaceholder"></write-new-comment>
|
|
|
<div class="sub-title mb-8">本页讨论区</div>
|
|
|
<div class="no-content-warning" v-if="comments === null">
|
|
|
<div class="no-content-warning-tip">加载中...</div>
|
|
|
</div>
|
|
|
- <div class="no-content-warning" v-else-if="comments.length === 0">
|
|
|
+ <div class="no-content-warning" v-else-if="comments.length === 0 && wsComments.length === 0">
|
|
|
本页还没有讨论
|
|
|
</div>
|
|
|
+ <template v-for="comment of wsComments">
|
|
|
+ <c-comment v-if="comment.pageNumber === currentPage" :key="comment.id" :comment="comment" is-new="true"
|
|
|
+ @need-refresh="updateComments"
|
|
|
+ >
|
|
|
+ </c-comment>
|
|
|
+ </template>
|
|
|
<template v-for="comment of comments">
|
|
|
<c-comment :key="comment.id" :comment="comment"
|
|
|
@need-refresh="updateComments"
|
|
|
@@ -25,12 +32,15 @@ import CComment from '@/components/CComment'
|
|
|
import WriteNewComment from '@/components/WriteNewComment'
|
|
|
import { mapState } from 'vuex'
|
|
|
import { createComment, getComments } from '@/server/comment'
|
|
|
+import WebsocketPublisher from '@/server/websocket/ws'
|
|
|
+import { useCommentWsServer } from '@/config/server-info'
|
|
|
|
|
|
export default {
|
|
|
components: { WriteNewComment, CComment },
|
|
|
data () {
|
|
|
return {
|
|
|
- comments: null
|
|
|
+ comments: null,
|
|
|
+ wsComments: []
|
|
|
}
|
|
|
},
|
|
|
mounted () {
|
|
|
@@ -55,9 +65,25 @@ export default {
|
|
|
this.updateComments()
|
|
|
}
|
|
|
},
|
|
|
+ created () {
|
|
|
+ this._ws = new WebsocketPublisher(useCommentWsServer('/slide/') + this.slide.id)
|
|
|
+ this._ws.subscribe({
|
|
|
+ onNext: data => {
|
|
|
+ this.wsComments.unshift(data)
|
|
|
+ },
|
|
|
+ onError: err => {
|
|
|
+ this.$setErrorMessage(err)
|
|
|
+ },
|
|
|
+ onClose: () => {}
|
|
|
+ })
|
|
|
+ },
|
|
|
+ beforeDestroy () {
|
|
|
+ this._ws.close()
|
|
|
+ },
|
|
|
methods: {
|
|
|
updateComments () {
|
|
|
- getComments({ slideId: this.slide.id, pageNumber: this.currentPage }).then(res => {
|
|
|
+ getComments({ slideId: this.slide.id, pageNumber: this.currentPage, sort: 'createAt,desc' }).then(res => {
|
|
|
+ this.wsComments = []
|
|
|
this.comments = res.data
|
|
|
}).catch(err => {
|
|
|
this.$setErrorMessage('获取本页评论区的过程中:' + err.response.data.msg)
|
|
|
@@ -72,7 +98,6 @@ export default {
|
|
|
}).then(res => {
|
|
|
this.$refs.writeComment.resetContent()
|
|
|
this.$setSuccessMessage('您的讨论帖发表成功')
|
|
|
- this.updateComments()
|
|
|
}).catch(err => {
|
|
|
this.$setErrorMessage(err.response.data.msg)
|
|
|
})
|