common.vue 4.6 KB

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