commentDialog.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <template>
  2. <Popup :title="t('评论')" isClose ref="popRef">
  3. <template #content>
  4. <List url="/discover/video/comments" :defaultParams="{ video_id: video_id }" @datas="getList" ref="listRef">
  5. <template #item="{ item }">
  6. <view class="comment-item">
  7. <image class="avatar" :src="item.avatar || '/static/tx.png'" mode="aspectFill"></image>
  8. <view class="content">
  9. <view class="username">{{ item.username }}</view>
  10. <view class="text">{{ item.content }}</view>
  11. <view class="footer">
  12. <text class="time">{{ fromTime(item.indate) }}</text>
  13. <view class="actions">
  14. <text class="reply" @click="reply(item)">{{ t('回复') }}</text>
  15. </view>
  16. </view>
  17. </view>
  18. </view>
  19. <view v-if="item.replay && item.replay.length" class="child-comments">
  20. <view class="comment-item" v-for="child in item.replay" :key="child.id">
  21. <image class="avatar" :src="child.avatar || '/static/tx.png'" mode="aspectFill"></image>
  22. <view class="content">
  23. <text class="username">{{ child.username }}</text>
  24. <view class="text">{{ child.content }}</view>
  25. <view class="footer">
  26. <text class="time">{{ fromTime(child.indate) }}</text>
  27. </view>
  28. </view>
  29. </view>
  30. </view>
  31. </template>
  32. </List>
  33. </template>
  34. <template #footer>
  35. <view class="comment-input">
  36. <Input class="input" :placeholder="replyTo ? t('回复') + '@' + replyTo.username : t('说点什么...')"
  37. @confirm="submitComment" :focus="showReplyInput" border="surround" v-model="commentText" />
  38. <view class="submit-btn" @click.stop="submitComment" :style="{ opacity: commentText ? 1 : .5 }">
  39. <trans _t="发送" />
  40. </view>
  41. </view>
  42. </template>
  43. </Popup>
  44. </template>
  45. <script setup>
  46. import { ref, nextTick } from 'vue'
  47. import Input from "./input";
  48. import { t } from "@/locale"
  49. import { DISCOVER_VIDEO_SUBCOMMENTS } from "@/api"
  50. import List from "@/components/list";
  51. import { Toast, fromTime } from "@/utils"
  52. import Popup from './popup.vue';
  53. const props = defineProps({
  54. video_id: {
  55. required: true
  56. }
  57. })
  58. const popRef = ref(null);
  59. const defaultParams = ref([])
  60. // 评论输入
  61. const commentText = ref('')
  62. const replyTo = ref(null)
  63. const listRef = ref(null)
  64. const showReplyInput = ref(false)
  65. const commentList = ref([])
  66. const getList = (list) => {
  67. commentList.value = list
  68. }
  69. const open = () => {
  70. clearInput()
  71. popRef.value && popRef.value.open()
  72. defaultParams.video_id = props.video_id
  73. nextTick(() => {
  74. listRef.value && listRef.value.handleRefresh();
  75. })
  76. }
  77. const close = () => {
  78. clearInput()
  79. popRef.value && popRef.value.close()
  80. }
  81. const submitComment = async () => {
  82. if (!commentText.value.trim()) return
  83. try {
  84. const result = await DISCOVER_VIDEO_SUBCOMMENTS({
  85. video_id: props.video_id,
  86. content: commentText.value,
  87. pid: replyTo.value ? replyTo.value.id : 0
  88. })
  89. // 只添加新评论而不刷新整个列表
  90. if (replyTo.value) {
  91. addReplyToCommentList(commentList.value, replyTo.value.id, result.data)
  92. } else {
  93. commentList.value.unshift(result.data)
  94. }
  95. listRef.value && listRef.value.setList(commentList.value);
  96. clearInput()
  97. } catch (error) {
  98. Toast(error.msg)
  99. }
  100. }
  101. // 辅助函数 - 添加回复到评论树
  102. function addReplyToCommentList(comments, parentId, newComment) {
  103. for (const comment of comments) {
  104. if (comment.id === parentId) {
  105. if (!comment.replay) comment.replay = []
  106. comment.replay.unshift(newComment)
  107. return true
  108. }
  109. if (comment.replay && addReplyToCommentList(comment.replay, parentId, newComment)) {
  110. return true
  111. }
  112. }
  113. return false
  114. }
  115. function clearInput() {
  116. commentText.value = ''
  117. replyTo.value = null
  118. showReplyInput.value = false
  119. }
  120. const reply = (comment) => {
  121. replyTo.value = comment
  122. showReplyInput.value = true
  123. }
  124. defineExpose({
  125. open,
  126. close
  127. })
  128. </script>
  129. <style lang="less" scoped>
  130. @import url('@/style.less');
  131. :deep(.u-list) {
  132. height: max-content !important;
  133. }
  134. .comment-item {
  135. display: flex;
  136. padding: 20rpx 0;
  137. // border-bottom: 1rpx solid var(--borderColor);
  138. .avatar {
  139. width: 80rpx;
  140. height: 80rpx;
  141. border-radius: 50%;
  142. margin-right: 20rpx;
  143. flex-shrink: 0;
  144. }
  145. .content {
  146. flex: 1;
  147. overflow: hidden;
  148. .username {
  149. .size(28rpx);
  150. color: var(--text);
  151. margin-right: 10rpx;
  152. }
  153. .text {
  154. .size(30rpx);
  155. margin: 10rpx 0;
  156. line-height: 1.5;
  157. color: var(--text);
  158. word-break: break-all;
  159. }
  160. .footer {
  161. display: flex;
  162. justify-content: space-between;
  163. margin-top: 10rpx;
  164. .time {
  165. font-size: 24rpx;
  166. color: var(--text);
  167. }
  168. .actions {
  169. display: flex;
  170. align-items: center;
  171. .like,
  172. .reply {
  173. font-size: 24rpx;
  174. color: var(--text);
  175. margin-left: 20rpx;
  176. display: flex;
  177. align-items: center;
  178. &:active {
  179. opacity: 0.7;
  180. }
  181. }
  182. }
  183. }
  184. }
  185. }
  186. .child-comments {
  187. margin-left: 80rpx;
  188. }
  189. .comment-input {
  190. .flex_center;
  191. background-color: var(--bg);
  192. .input {
  193. flex: 1;
  194. height: 70rpx;
  195. padding: 0 20rpx;
  196. background-color: var(--inputBg);
  197. border-radius: 35rpx;
  198. margin-right: 20rpx;
  199. font-size: 28rpx;
  200. color: var(--text);
  201. }
  202. .submit-btn {
  203. .size(28rpx);
  204. height: 70rpx;
  205. line-height: 70rpx;
  206. padding: 0 30rpx;
  207. background-color: var(--black);
  208. color: #fff;
  209. border-radius: 35rpx;
  210. border: none;
  211. &[disabled] {
  212. background-color: var(--text-02);
  213. }
  214. }
  215. }
  216. </style>