courseComment.vue 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <template>
  2. <view class="tabs-content" :style="{ paddingBottom : `${props.paddingBottom || 0}rpx` }">
  3. <view v-for="(comment, index) in sortedCommentList" :key="index" class="comment-list-item">
  4. <view class="comment-list-left">
  5. <image :src="`${FILE_URL}/weixin.png`" class="comment-list-avator"></image>
  6. </view>
  7. <view class="comment-list-right">
  8. <view style="margin-bottom: 15rpx;">
  9. <text class="comment-list-username">{{ comment.username }}</text>
  10. <text class="comment-list-moment">{{ formatTime(comment.commentTime) }}</text>
  11. </view>
  12. <view>{{ comment.content }}</view>
  13. </view>
  14. </view>
  15. </view>
  16. </template>
  17. <script setup>
  18. import {
  19. ref,
  20. defineProps,
  21. computed
  22. } from 'vue'
  23. import configService from '@/utils/baseurl.js'
  24. const FILE_URL = configService.FILE_URL;
  25. const props = defineProps({
  26. paddingBottom: {
  27. type: Number,
  28. required: false,
  29. default: 10
  30. },
  31. commentList: {
  32. type: Array,
  33. required: false,
  34. default: () => []
  35. },
  36. });
  37. function formatDate(dateStr) {
  38. return dateStr.replace(" ", "T");
  39. }
  40. function formatTime(timeString) {
  41. const commentDate = new Date(formatDate(timeString));
  42. const now = new Date();
  43. const diff = now - commentDate;
  44. const minutes = Math.floor(diff / 60000);
  45. const hours = Math.floor(diff / 3600000);
  46. const days = Math.floor(diff / 86400000);
  47. if (minutes < 1) { // 修改这里以处理0分钟
  48. return "刚刚";
  49. } else if (minutes < 60) {
  50. return `${minutes}分钟前`;
  51. } else if (hours < 24) {
  52. return `${hours}小时前`;
  53. } else {
  54. return commentDate.toISOString().split('T')[0];
  55. }
  56. }
  57. const sortedCommentList = computed(() => {
  58. return props.commentList.sort((a, b) =>
  59. new Date(formatDate(b.commentTime)) - new Date(formatDate(a.commentTime))
  60. );
  61. });
  62. </script>
  63. <style lang="scss" scoped>
  64. .tabs-content{
  65. padding-top: 10rpx;
  66. }
  67. .comment-list-item {
  68. display: flex;
  69. padding: 20rpx 0;
  70. font-size: 28rpx;
  71. .comment-list-left{
  72. flex: 0 0 auto;
  73. padding-right: 20rpx;
  74. padding-left: 10rpx;
  75. .comment-list-avator{
  76. width: 100rpx;
  77. height: 100rpx;
  78. border-radius: 50%;
  79. }
  80. }
  81. .comment-list-right{
  82. flex: 1;
  83. .comment-list-username{
  84. padding-right: 25rpx;
  85. font-size: 32rpx;
  86. font-weight: bold;
  87. }
  88. }
  89. }
  90. </style>