UserCommentCard.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. <template>
  2. <el-card class="comment-card shadow-box">
  3. <div class="comment-container">
  4. <div class="comment-header">
  5. <span class="total-count">{{ totalSize }} 评论</span>
  6. <div class="sort-options">
  7. <span :class="{ active: sortType === 'hot' }" @click="handleSort('hot')">最热</span>
  8. <el-divider direction="vertical" />
  9. <span :class="{ active: sortType === 'time' }" @click="handleSort('time')">最新</span>
  10. </div>
  11. </div>
  12. <div class="main-reply-box">
  13. <el-avatar
  14. :size="48"
  15. :src="currentUser.avatar || defaultAvatar"
  16. class="clickable-avatar"
  17. @click.native="goToUserHome(currentUser.userId)"
  18. />
  19. <div class="reply-input-wrapper">
  20. <el-input
  21. v-model="newComment"
  22. type="textarea"
  23. :rows="3"
  24. placeholder="发一条友善的评论吧"
  25. resize="none"
  26. />
  27. <el-button type="primary" class="post-btn" @click="postMainComment">发表评论</el-button>
  28. </div>
  29. </div>
  30. <div
  31. v-infinite-scroll="loadMoreMainComments"
  32. class="comment-list"
  33. :infinite-scroll-disabled="mainScrollDisabled"
  34. :infinite-scroll-distance="20"
  35. >
  36. <div v-for="item in dataList" :key="item.id" class="comment-item">
  37. <div class="comment-root">
  38. <el-avatar
  39. :size="48"
  40. :src="item.user.avatar || defaultAvatar"
  41. class="clickable-avatar"
  42. @click.native="goToUserHome(item.user.userId || item.user.id)"
  43. />
  44. <div class="comment-content">
  45. <div class="user-info">
  46. <span class="user-name clickable-link" @click="goToUserHome(item.user.userId || item.user.id)">
  47. {{ item.user.name }}
  48. </span>
  49. </div>
  50. <p class="text">{{ item.content }}</p>
  51. <div class="comment-footer">
  52. <span class="time">{{ formatDate(item.createAt) }}</span>
  53. <span class="action" @click="showReplyInput(item, item)">回复</span>
  54. </div>
  55. <div v-if="item.total > 0" class="reply-list">
  56. <div v-for="reply in item.children.slice(0, 3)" :key="reply.id" class="reply-item">
  57. <el-avatar
  58. :size="24"
  59. :src="reply.user.avatar || defaultAvatar"
  60. class="reply-avatar clickable-avatar"
  61. @click.native="goToUserHome(reply.user.userId || reply.user.id)"
  62. />
  63. <div class="reply-right">
  64. <span class="user-name clickable-link" @click="goToUserHome(reply.user.userId || reply.user.id)">
  65. {{ reply.user.name }}
  66. </span>
  67. <span v-if="reply.targetId && reply.targetId !== item.id">
  68. 回复
  69. <span class="target-name clickable-link" @click="goToUserHome(reply.targetUserId)">
  70. @{{ reply.targetUsername }}
  71. </span> :
  72. </span>
  73. <span class="reply-content">{{ reply.content }}</span>
  74. <span class="sub-action" @click="showReplyInput(item, reply)">回复</span>
  75. </div>
  76. </div>
  77. <div v-if="item.total > 3" class="view-more-bar">
  78. 共 {{ item.total }} 条回复,
  79. <span class="click-more" @click="openReplyDialog(item)">点击查看更多</span>
  80. </div>
  81. </div>
  82. <div v-if="activeRootId === item.id && !dialogVisible" class="inner-reply-box">
  83. <el-input v-model="replyText" size="small" :placeholder="replyPlaceholder" />
  84. <el-button type="primary" size="small" @click="postReply">发布</el-button>
  85. <el-button type="text" size="small" @click="activeRootId = null">取消</el-button>
  86. </div>
  87. </div>
  88. </div>
  89. <el-divider />
  90. </div>
  91. <p v-if="loading" class="list-status">加载中...</p>
  92. <p v-if="noMoreMain && dataList.length > 0" class="list-status">没有更多评论了</p>
  93. <el-empty v-if="!loading && dataList.length === 0" description="暂无评论,快来抢沙发" />
  94. </div>
  95. <el-dialog :visible.sync="dialogVisible" title="评论详情" width="600px" :append-to-body="true" custom-class="reply-dialog" @close="activeRootId = null">
  96. <div v-if="currentParent" class="dialog-scroll-area">
  97. <div class="parent-info">
  98. <el-avatar
  99. :size="40"
  100. :src="currentParent.user.avatar || defaultAvatar"
  101. class="clickable-avatar"
  102. @click.native="goToUserHome(currentParent.user.userId || currentParent.user.id)"
  103. />
  104. <div class="parent-text">
  105. <div class="user-name clickable-link" @click="goToUserHome(currentParent.user.userId || currentParent.user.id)">
  106. {{ currentParent.user.name }}
  107. </div>
  108. <div class="content">{{ currentParent.content }}</div>
  109. <div class="reply-footer">
  110. <span class="time">{{ formatDate(currentParent.createAt) }}</span>
  111. <span class="action" @click="showReplyInput(currentParent, currentParent)">回复</span>
  112. </div>
  113. </div>
  114. </div>
  115. <el-divider content-position="left">全部回复 ({{ currentParent.total }})</el-divider>
  116. <div class="dialog-reply-list">
  117. <div v-for="sub in dialogReplies" :key="sub.id" class="dialog-reply-item">
  118. <el-avatar
  119. :size="32"
  120. :src="sub.user.avatar || defaultAvatar"
  121. class="clickable-avatar"
  122. @click.native="goToUserHome(sub.user.userId || sub.user.id)"
  123. />
  124. <div class="reply-body">
  125. <span class="user-name clickable-link" @click="goToUserHome(sub.user.userId || sub.user.id)">
  126. {{ sub.user.name }}
  127. </span>
  128. <span v-if="sub.targetId && sub.targetId !== currentParent.id">
  129. 回复
  130. <span class="target-name clickable-link" @click="goToUserHome(sub.targetUserId)">
  131. @{{ sub.targetUsername }}
  132. </span>
  133. </span>: {{ sub.content }}
  134. <div class="reply-footer">
  135. {{ formatDate(sub.createAt) }}
  136. <span class="action" @click="showReplyInput(currentParent, sub)">回复</span>
  137. </div>
  138. </div>
  139. </div>
  140. </div>
  141. <div class="dialog-pagination">
  142. <el-pagination
  143. small
  144. background
  145. layout="prev, pager, next"
  146. :total="currentParent.total"
  147. :page-size="childPageSize"
  148. :current-page.sync="childCurrentPage"
  149. @current-change="handleChildPageChange"
  150. />
  151. </div>
  152. </div>
  153. <div v-if="dialogVisible && activeRootId === currentParent.id" class="dialog-inner-reply">
  154. <el-divider />
  155. <div class="inner-reply-box">
  156. <el-input v-model="replyText" size="small" :placeholder="replyPlaceholder" />
  157. <el-button type="primary" size="small" @click="postReply">发布</el-button>
  158. <el-button type="text" size="small" @click="activeRootId = null">取消</el-button>
  159. </div>
  160. </div>
  161. </el-dialog>
  162. </div>
  163. </el-card>
  164. </template>
  165. <script>
  166. import { getChildComment, getComment, publishComment } from '@/api/comment'
  167. export default {
  168. name: 'UserCommentCard',
  169. props: {
  170. videoId: { type: String, required: true },
  171. currentUser: { type: Object, default: () => ({ userId: -1, name: '游客', avatar: '' }) }
  172. },
  173. data() {
  174. return {
  175. dataList: [],
  176. totalSize: 0,
  177. currentPage: 0,
  178. pageSize: 10,
  179. loading: false,
  180. noMoreMain: false,
  181. sortType: 'hot',
  182. dialogVisible: false,
  183. currentParent: null,
  184. dialogReplies: [],
  185. childCurrentPage: 1,
  186. childPageSize: 10,
  187. newComment: '',
  188. replyText: '',
  189. activeRootId: null,
  190. replyTarget: null,
  191. replyPlaceholder: '',
  192. defaultAvatar: 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'
  193. }
  194. },
  195. computed: {
  196. mainScrollDisabled() {
  197. return this.loading || this.noMoreMain
  198. }
  199. },
  200. watch: {
  201. videoId: {
  202. immediate: true,
  203. handler(val) {
  204. if (val) this.initCommentList()
  205. }
  206. }
  207. },
  208. methods: {
  209. goToUserHome(userId) {
  210. this.$message.info('goto userhome ' + userId)
  211. },
  212. initCommentList() {
  213. this.dataList = []
  214. this.currentPage = 0
  215. this.noMoreMain = false
  216. this.loadMoreMainComments()
  217. },
  218. async loadMoreMainComments() {
  219. if (this.loading || this.noMoreMain) return
  220. this.loading = true
  221. this.currentPage++
  222. try {
  223. const resp = await getComment({
  224. videoId: this.videoId,
  225. pn: this.currentPage,
  226. pageSize: this.pageSize,
  227. sortBy: this.sortType
  228. })
  229. if (resp.code === 0) {
  230. const newList = resp.data.list || []
  231. this.dataList = [...this.dataList, ...newList]
  232. this.totalSize = resp.data.totalSize
  233. if (newList.length < this.pageSize || this.dataList.length >= this.totalSize) {
  234. this.noMoreMain = true
  235. }
  236. }
  237. } catch (e) {
  238. console.error('加载失败', e)
  239. } finally {
  240. this.loading = false
  241. }
  242. },
  243. handleSort(type) {
  244. this.sortType = type
  245. this.initCommentList()
  246. },
  247. showReplyInput(root, target) {
  248. this.activeRootId = root.id
  249. this.replyTarget = target
  250. this.replyPlaceholder = `回复 @${target.user.name} :`
  251. this.replyText = ''
  252. },
  253. postMainComment() {
  254. if (!this.newComment.trim()) return
  255. publishComment({
  256. videoId: this.videoId,
  257. content: this.newComment,
  258. parentId: 0,
  259. targetId: 0
  260. }).then(resp => {
  261. if (resp.code === 0) {
  262. this.$message.success('评论成功')
  263. this.newComment = ''
  264. this.initCommentList()
  265. }
  266. })
  267. },
  268. postReply() {
  269. if (!this.replyText.trim()) return
  270. publishComment({
  271. videoId: this.videoId,
  272. content: this.replyText,
  273. parentId: this.activeRootId,
  274. targetId: this.replyTarget.id,
  275. targetUsername: this.replyTarget.user.name
  276. }).then(resp => {
  277. if (resp.code === 0) {
  278. this.$message.success('回复成功')
  279. this.replyText = ''
  280. // 不再直接置 null,如果想让用户连续回复可以保持,但通常交互是关闭
  281. this.activeRootId = null
  282. if (this.dialogVisible) {
  283. this.fetchDialogReplies(this.currentParent.id, this.childCurrentPage)
  284. } else {
  285. // 如果在主列表回复,可能需要刷新子列表或重新获取
  286. this.initCommentList()
  287. }
  288. }
  289. })
  290. },
  291. async openReplyDialog(parentItem) {
  292. this.currentParent = parentItem
  293. this.childCurrentPage = 1
  294. this.dialogVisible = true
  295. this.activeRootId = null // 重置输入框状态
  296. this.fetchDialogReplies(parentItem.id, 1)
  297. },
  298. fetchDialogReplies(commentId, pageNumber) {
  299. getChildComment({ commentId, pn: pageNumber }).then(resp => {
  300. if (resp.code === 0) {
  301. this.dialogReplies = resp.data.list
  302. }
  303. })
  304. },
  305. handleChildPageChange(page) {
  306. this.fetchDialogReplies(this.currentParent.id, page)
  307. },
  308. formatDate(ts) {
  309. if (!ts) return ''
  310. const date = new Date(ts)
  311. return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`
  312. }
  313. }
  314. }
  315. </script>
  316. <style scoped lang="scss">
  317. .comment-card { margin-top: 20px; border: none; }
  318. .comment-container { color: #18191c; }
  319. .clickable-avatar {
  320. cursor: pointer;
  321. transition: opacity 0.2s;
  322. &:hover { opacity: 0.8; }
  323. }
  324. .clickable-link {
  325. cursor: pointer;
  326. &:hover { color: #00aeec !important; }
  327. }
  328. .comment-header {
  329. margin-bottom: 24px; display: flex; align-items: center;
  330. .total-count { font-size: 18px; font-weight: 500; margin-right: 20px; }
  331. .sort-options span { font-size: 14px; color: #9499a0; cursor: pointer; &.active { color: #18191c; font-weight: bold; } }
  332. }
  333. .main-reply-box {
  334. display: flex; gap: 15px; margin-bottom: 30px;
  335. .reply-input-wrapper { flex: 1; .post-btn { margin-top: 10px; float: right; } }
  336. }
  337. .comment-list { min-height: 200px; }
  338. .list-status { text-align: center; color: #9499a0; font-size: 13px; padding: 20px 0; }
  339. .comment-root {
  340. display: flex; gap: 15px;
  341. .comment-content {
  342. flex: 1;
  343. .user-name { font-size: 13px; font-weight: bold; color: #61666d; }
  344. .text { font-size: 15px; line-height: 1.6; margin: 8px 0; }
  345. .comment-footer {
  346. font-size: 13px; color: #9499a0; display: flex; gap: 20px; margin-bottom: 8px;
  347. .action { cursor: pointer; &:hover { color: #00aeec; } }
  348. }
  349. }
  350. }
  351. .reply-list {
  352. background: #f6f7f8; border-radius: 6px; padding: 12px;
  353. .reply-item {
  354. display: flex;
  355. gap: 8px;
  356. font-size: 14px; line-height: 22px; margin-bottom: 8px;
  357. .reply-avatar { flex-shrink: 0; margin-top: 2px; }
  358. .reply-right { flex: 1; }
  359. .user-name { color: #61666d; font-weight: bold; margin-right: 5px; }
  360. .target-name { color: #00aeec; font-weight: 500; margin: 0 4px; }
  361. .sub-action {
  362. margin-left: 10px; font-size: 12px; color: #9499a0; cursor: pointer;
  363. display: none;
  364. }
  365. &:hover .sub-action { display: inline-block; }
  366. }
  367. .view-more-bar {
  368. font-size: 13px; color: #9499a0; margin-top: 8px; padding-left: 32px;
  369. .click-more { color: #00aeec; cursor: pointer; font-weight: 500; }
  370. }
  371. }
  372. .inner-reply-box {
  373. margin-top: 10px; display: flex; gap: 8px;
  374. background: #fff; padding: 10px; border: 1px solid #e3e5e7; border-radius: 6px;
  375. }
  376. .dialog-scroll-area {
  377. max-height: 50vh; overflow-y: auto; padding-right: 8px;
  378. .parent-info {
  379. display: flex; gap: 12px;
  380. .parent-text {
  381. flex: 1;
  382. .user-name { font-weight: bold; color: #61666d; margin-bottom: 4px; }
  383. .content { font-size: 15px; line-height: 1.6; margin-bottom: 8px; }
  384. .reply-footer { font-size: 12px; color: #9499a0; .action { margin-left: 15px; cursor: pointer; &:hover { color: #00aeec; } } }
  385. }
  386. }
  387. .dialog-reply-item {
  388. display: flex; gap: 10px; margin-bottom: 15px;
  389. .reply-body {
  390. flex: 1; font-size: 14px;
  391. .user-name { font-weight: bold; color: #61666d; }
  392. .target-name { color: #00aeec; margin: 0 4px; }
  393. .reply-footer { font-size: 12px; color: #9499a0; margin-top: 5px;
  394. .action { margin-left: 10px; cursor: pointer; &:hover { color: #00aeec; } }
  395. }
  396. }
  397. }
  398. .dialog-pagination { display: flex; justify-content: center; margin-top: 20px; }
  399. }
  400. /* 弹窗专用回复框样式 */
  401. .dialog-inner-reply {
  402. padding-top: 0;
  403. .el-divider--horizontal { margin: 12px 0; }
  404. }
  405. </style>