comment-card.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <template>
  2. <div v-infinite-scroll="loadMore" infinite-scroll-disabled="true" infinite-scroll-distance="10">
  3. <v-row>
  4. <v-col>
  5. {{ video.commentCount }} 条评论
  6. </v-col>
  7. <v-col>
  8. <span @click="getNewestComments">最新</span>
  9. </v-col>
  10. <v-col>
  11. <span @click="getHotComments">最热</span>
  12. </v-col>
  13. </v-row>
  14. <v-divider />
  15. <v-row>
  16. <div ref="comment" :style="wrapStyle" class="comment-wrap" style="width: 720px; height: 1080px">
  17. <Comment
  18. v-model="videoComments"
  19. :user="currentUser"
  20. :props="props"
  21. :before-submit="submit"
  22. :before-like="like"
  23. :before-delete="deleteComment"
  24. :upload-img="uploadImg"
  25. />
  26. </div>
  27. </v-row>
  28. </div>
  29. </template>
  30. <script>
  31. import Comment from '@/components/comment'
  32. import { videoComment } from '@/api/comment/comment'
  33. export default {
  34. name: 'CommentCard',
  35. components: {
  36. Comment
  37. },
  38. props: {
  39. video: {
  40. type: Object,
  41. default: () => {}
  42. }
  43. },
  44. data() {
  45. return {
  46. commentCount: this.count,
  47. videoComments: [],
  48. wrapStyle: '',
  49. props: {
  50. id: 'commentId',
  51. content: 'content',
  52. imgSrc: 'imgSrc',
  53. children: 'childrenComments',
  54. likes: 'likes',
  55. liked: 'liked',
  56. reply: 'reply',
  57. createAt: 'createAt',
  58. user: 'visitor'
  59. },
  60. currentUser: null,
  61. busy: false,
  62. page: 1
  63. }
  64. },
  65. created() {
  66. const userInfo = this.$store.state.user.userInfo
  67. if (userInfo !== null) {
  68. this.currentUser = {
  69. name: userInfo.username,
  70. avatar: userInfo.avatarUrl
  71. }
  72. }
  73. this.getVideoComment(this.video.videoId, 1)
  74. },
  75. mounted() {
  76. // const header = this.$refs.header
  77. const header = 10
  78. this.wrapStyle = `height: calc(100vh - ${header.clientHeight + 20}px)`
  79. },
  80. methods: {
  81. loadMore: function() {
  82. this.busy = true
  83. setTimeout(() => {
  84. this.getVideoComment(this.video.videoId, this.page)
  85. }, 1000)
  86. },
  87. getHotComments() {
  88. console.log('获取热门评论')
  89. },
  90. getNewestComments() {
  91. console.log('获取最新评论')
  92. },
  93. getVideoComment(videoId, page) {
  94. videoComment(videoId, page).then(res => {
  95. if (res.code === 0) {
  96. this.page += 1
  97. this.busy = false
  98. for (const item of res.data.list) {
  99. this.videoComments.push(item)
  100. }
  101. console.log('已获取的评论数量: ' + this.videoComments.length)
  102. } else {
  103. console.error(res.msg)
  104. }
  105. })
  106. .catch(error => {
  107. console.error(error.message)
  108. })
  109. },
  110. // 评论相关方法
  111. async submit(newComment, parent, add) {
  112. const res = await new Promise((resolve) => {
  113. setTimeout(() => {
  114. resolve({ newComment, parent })
  115. }, 300)
  116. })
  117. add(Object.assign(res.newComment, { _id: new Date().getTime() }))
  118. console.log('addComment: ', res)
  119. },
  120. async like(comment) {
  121. const res = await new Promise((resolve) => {
  122. setTimeout(() => {
  123. resolve(comment)
  124. }, 0)
  125. })
  126. console.log('likeComment: ', res)
  127. },
  128. async uploadImg({ file, callback }) {
  129. const res = await new Promise((resolve, reject) => {
  130. const reader = new FileReader()
  131. reader.readAsDataURL(file)
  132. reader.onload = () => {
  133. resolve(reader.result)
  134. }
  135. reader.onerror = () => {
  136. reject(reader.error)
  137. }
  138. })
  139. callback(res)
  140. console.log('uploadImg: ', res)
  141. },
  142. async deleteComment(comment, parent) {
  143. const res = await new Promise((resolve) => {
  144. setTimeout(() => {
  145. resolve({ comment, parent })
  146. }, 300)
  147. })
  148. console.log('deleteComment: ', res)
  149. }
  150. }
  151. }
  152. </script>
  153. <style lang="scss">
  154. .category-link {
  155. color: #999;
  156. }
  157. a {
  158. text-decoration: none;
  159. }
  160. @mixin scroll-style(
  161. $thumb: rgba(255, 255, 255, 0.6),
  162. $track: rgba(255, 255, 255, 0)
  163. ) {
  164. &::-webkit-scrollbar,
  165. &::-webkit-scrollbar-thumb,
  166. &::-webkit-scrollbar-track {
  167. border: none;
  168. box-shadow: none;
  169. }
  170. &::-webkit-scrollbar {
  171. width: 4px;
  172. }
  173. &::-webkit-scrollbar-thumb {
  174. border-radius: 2px;
  175. background: $thumb;
  176. }
  177. &::-webkit-scrollbar-track {
  178. background: $track;
  179. }
  180. }
  181. * {
  182. margin: 0;
  183. padding: 0;
  184. box-sizing: border-box;
  185. }
  186. html {
  187. font-size: 14px;
  188. }
  189. html,
  190. body,
  191. #app {
  192. height: 100%;
  193. }
  194. @media screen and (min-width: 320px) {
  195. html {
  196. font-size: calc(14px + 4 * ((100vw - 320px) / (1200 - 320)));
  197. }
  198. }
  199. @media screen and (min-width: 1200px) {
  200. html {
  201. font-size: 18px;
  202. }
  203. }
  204. .change-role {
  205. background: #1c2433;
  206. color: #eee;
  207. padding: 1rem;
  208. display: flex;
  209. justify-content: center;
  210. align-content: center;
  211. .change {
  212. cursor: pointer;
  213. padding: 0.4rem;
  214. margin-right: 2rem;
  215. font-size: 0.9rem;
  216. border: 1px solid #e99210;
  217. border-radius: 5px;
  218. user-select: none;
  219. &:hover {
  220. opacity: 0.9;
  221. }
  222. }
  223. .current-role {
  224. min-width: 15rem;
  225. color: #e99210;
  226. display: flex;
  227. justify-content: center;
  228. align-items: center;
  229. border: 1px dashed #e99210;
  230. padding: 0 1rem;
  231. img {
  232. width: 1.5rem;
  233. height: 1.5rem;
  234. margin-right: 0.5rem;
  235. border: 1px solid #eee;
  236. border-radius: 50%;
  237. }
  238. }
  239. }
  240. .comment-wrap {
  241. overflow: auto;
  242. @include scroll-style(#db8f1c, #b9b9b9);
  243. }
  244. @media screen and (min-width: 760px) {
  245. body {
  246. margin: 0 10%;
  247. border: 1px dashed #eee;
  248. }
  249. }
  250. @media screen and (max-width: 500px) {
  251. .change-role .current-role {
  252. min-width: 5rem;
  253. padding: 0 0.5rem;
  254. }
  255. }
  256. </style>