common.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <template>
  2. <view class="comment_item">
  3. <view class="top">
  4. <view class="top_left">
  5. <img class="user_avatar" :src="props.data.user_avatar" />
  6. <uni-tag v-if="props.data.author" class="tag" type="primary" :inverted="false" text="作者" size="mini" circle />
  7. <span class="user_name">{{ props.data.user_name }}</span>
  8. <span class="user_name">{{ cReplyName }}</span>
  9. </view>
  10. <view class="top_right" @tap="likeClick(props.data)">
  11. <span :class="[props.data.is_like ? 'active' : '', 'like_count']">{{ cLikeCount }}</span>
  12. <uni-icons v-show="props.data.is_like" type="hand-up-filled" size="24" color="#007aff"></uni-icons>
  13. <uni-icons v-show="!props.data.is_like" type="hand-up" size="24" color="#999"></uni-icons>
  14. </view>
  15. </view>
  16. <view class="content" @tap="replyClick(props.data)">
  17. {{ c_content }}
  18. <span class="shrink" v-if="isShrink" @tap.stop="expandContentFun(props.data?.user_content)">...展开</span>
  19. <span
  20. class="shrink"
  21. v-if="!isShrink && user_content?.length > contentShowLength"
  22. @tap.stop="shrinkContentFun(props.data?.user_content)"
  23. >
  24. 收起</span
  25. >
  26. </view>
  27. <view class="bottom">
  28. <span class="create_time">{{ props.data.create_time }}</span>
  29. <span v-if="props.data.owner" class="delete" @tap="deleteClick(props.data)">删除</span>
  30. <!-- <span v-else class="reply" @tap="replyClick(props.data)"
  31. >回复</span
  32. > -->
  33. </view>
  34. </view>
  35. </template>
  36. <script setup>
  37. import { reactive, ref, onMounted, computed, watch, watchEffect } from "vue";
  38. const props = defineProps({
  39. // 评论数据
  40. data: {
  41. type: Object,
  42. default: () => {},
  43. },
  44. // 父级评论数据
  45. pData: {
  46. type: Object,
  47. default: () => {},
  48. },
  49. });
  50. // 被回复人名称
  51. const cReplyName = computed(() => {
  52. return props.data?.reply_name ? ` ▸ ` + props.data?.reply_name : "";
  53. });
  54. // 点赞数显示
  55. const cLikeCount = computed(() => {
  56. return props.data.like_count === 0 ? "" : props.data.like_count > 99 ? `99+` : props.data.like_count;
  57. });
  58. // 评论过长处理
  59. let contentShowLength = 70; // 默认显示评论字符
  60. let user_content = props.data?.user_content;
  61. let isShrink = ref(user_content?.length > contentShowLength); // 是否收缩评论
  62. let c_content = ref("");
  63. watch(
  64. () => isShrink.value,
  65. (newVal) => {
  66. c_content.value = newVal ? user_content.slice(0, contentShowLength + 1) : user_content;
  67. },
  68. {
  69. immediate: true,
  70. }
  71. );
  72. // 删除变更显示定制
  73. watch(
  74. () => props.data?.user_content,
  75. (newVal, oldVal) => {
  76. if (newVal !== oldVal) {
  77. c_content.value = newVal;
  78. }
  79. }
  80. );
  81. // 展开文字
  82. function expandContentFun() {
  83. isShrink.value = false;
  84. }
  85. // 收起文字
  86. function shrinkContentFun() {
  87. isShrink.value = true;
  88. }
  89. const emit = defineEmits(["likeClick", "replyClick", "deleteClick"]);
  90. // 点赞
  91. function likeClick(item) {
  92. emit("likeClick", item);
  93. }
  94. // 回复
  95. function replyClick(item) {
  96. // 自己不能回复自己
  97. if (item.owner) return;
  98. emit("replyClick", item);
  99. }
  100. // 删除
  101. function deleteClick(item) {
  102. emit("deleteClick", item);
  103. }
  104. </script>
  105. <style lang="scss" scoped>
  106. ////////////////////////
  107. .center {
  108. display: flex;
  109. align-items: center;
  110. }
  111. .ellipsis {
  112. overflow: hidden; //超出文本隐藏
  113. white-space: nowrap; //溢出不换行
  114. text-overflow: ellipsis; //溢出省略号显示
  115. }
  116. ////////////////////////
  117. .comment_item {
  118. font-size: 28rpx;
  119. .top {
  120. @extend .center;
  121. justify-content: space-between;
  122. .top_left {
  123. display: flex;
  124. align-items: center;
  125. overflow: hidden;
  126. .user_avatar {
  127. width: 68rpx;
  128. height: 68rpx;
  129. border-radius: 50%;
  130. margin-right: 12rpx;
  131. }
  132. .tag {
  133. margin-right: 6rpx;
  134. }
  135. .user_name {
  136. @extend .ellipsis;
  137. max-width: 180rpx;
  138. color: $uni-text-color-grey;
  139. }
  140. }
  141. .top_right {
  142. @extend .center;
  143. .like_count {
  144. color: $uni-text-color-grey;
  145. &.active {
  146. color: $uni-color-primary;
  147. }
  148. }
  149. }
  150. }
  151. .content {
  152. padding: 10rpx;
  153. margin-left: 70rpx;
  154. color: $uni-text-color;
  155. &:active {
  156. background-color: $uni-bg-color-hover;
  157. }
  158. .shrink {
  159. padding: 20rpx 20rpx 20rpx 0rpx;
  160. color: $uni-color-primary;
  161. }
  162. }
  163. .bottom {
  164. padding-left: 80rpx;
  165. font-size: 24rpx;
  166. .create_time {
  167. color: $uni-text-color-grey;
  168. }
  169. .delete {
  170. padding: 20rpx 20rpx 0 20rpx;
  171. color: $uni-border-color;
  172. }
  173. .reply {
  174. color: $uni-color-primary;
  175. }
  176. }
  177. }
  178. </style>