courseComment.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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="/static/images/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.yongHuMing }}</text>
  10. <text class="comment-list-moment">{{ formatTime(comment.pingLunShiJian) }}</text>
  11. </view>
  12. <view>{{ comment.pingLunNeiRong }}</view>
  13. </view>
  14. </view>
  15. </view>
  16. </template>
  17. <script setup>
  18. import {
  19. ref,
  20. defineProps,
  21. computed
  22. } from 'vue'
  23. const props = defineProps({
  24. paddingBottom: {
  25. type: Number,
  26. required: false,
  27. default: 10
  28. },
  29. commentList: {
  30. type: Array,
  31. required: false,
  32. default: () => []
  33. },
  34. });
  35. function formatDate(dateStr) {
  36. return dateStr.replace(" ", "T"); // Replace space with 'T' to make it ISO format
  37. }
  38. function formatTime(timeString) {
  39. const commentDate = new Date(formatDate(timeString));
  40. const now = new Date();
  41. const diff = now - commentDate; // Difference in milliseconds
  42. const minutes = Math.floor(diff / 60000); // Convert milliseconds to minutes
  43. const hours = Math.floor(diff / 3600000); // Convert milliseconds to hours
  44. const days = Math.floor(diff / 86400000); // Convert milliseconds to days
  45. if (minutes < 1) { // 修改这里以处理0分钟
  46. return "刚刚";
  47. } else if (minutes < 60) {
  48. return `${minutes}分钟前`;
  49. } else if (hours < 24) {
  50. return `${hours}小时前`;
  51. } else {
  52. return commentDate.toISOString().split('T')[0]; // Return date in yyyy-MM-dd format
  53. }
  54. }
  55. const sortedCommentList = computed(() => {
  56. return props.commentList.sort((a, b) =>
  57. new Date(formatDate(b.pingLunShiJian)) - new Date(formatDate(a.pingLunShiJian))
  58. );
  59. });
  60. </script>
  61. <style lang="scss" scoped>
  62. .tabs-content{
  63. padding-top: 10rpx;
  64. }
  65. .comment-list-item {
  66. display: flex;
  67. padding: 20rpx 0;
  68. font-size: 24rpx;
  69. .comment-list-left{
  70. flex: 0 0 auto;
  71. padding-right: 20rpx;
  72. padding-left: 10rpx;
  73. .comment-list-avator{
  74. width: 100rpx;
  75. height: 100rpx;
  76. border-radius: 50%;
  77. }
  78. }
  79. .comment-list-right{
  80. flex: 1;
  81. .comment-list-username{
  82. padding-right: 25rpx;
  83. font-size: 32rpx;
  84. font-weight: bold;
  85. }
  86. }
  87. }
  88. </style>