common.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <template>
  2. <view class="comment_item">
  3. <view class="top">
  4. <view class="top_left" @click="goToUserHomepage(data.user_id)">
  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="#8de10b"></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. goToUserHomepage(id) {
  100. uni.$emit('check_login', () => {
  101. if (!id) {
  102. return;
  103. }
  104. uni.navigateTo({
  105. url: "/pages/my/userHomepage?id=" + id,
  106. });
  107. })
  108. },
  109. },
  110. mounted() {
  111. this.user_content = this.data.user_content;
  112. this.isShrink = this.user_content.length > this.contentShowLength;
  113. this.c_content = this.isShrink ? this.user_content.slice(0, this.contentShowLength + 1) : this.user_content;
  114. },
  115. };
  116. </script>
  117. <style lang="scss" scoped>
  118. ////////////////////
  119. .center {
  120. display: flex;
  121. align-items: center;
  122. }
  123. .ellipsis {
  124. overflow: hidden;
  125. white-space: nowrap;
  126. text-overflow: ellipsis;
  127. }
  128. ////////////////////
  129. .comment_item {
  130. font-size: 28rpx;
  131. .top {
  132. @extend .center;
  133. justify-content: space-between;
  134. width: 100%;
  135. .top_left {
  136. display: flex;
  137. align-items: center;
  138. overflow: hidden;
  139. .user_avatar {
  140. width: 68rpx;
  141. height: 68rpx;
  142. border-radius: 50%;
  143. margin-right: 12rpx;
  144. }
  145. .tag {
  146. margin-right: 6rpx;
  147. }
  148. .user_name {
  149. @extend .ellipsis;
  150. max-width: 180rpx;
  151. color: #8c8c8c;
  152. }
  153. }
  154. .top_right {
  155. @extend .center;
  156. margin-left: auto;
  157. min-width: 60rpx;
  158. justify-content: flex-end;
  159. .like_count {
  160. color: #8c8c8c;
  161. &.active {
  162. color: #8de10b;
  163. }
  164. }
  165. }
  166. }
  167. .content {
  168. padding: 10rpx;
  169. margin-left: 70rpx;
  170. color: #333;
  171. &:active {
  172. background-color: #f2f2f2;
  173. }
  174. .shrink {
  175. padding: 20rpx 20rpx 20rpx 0rpx;
  176. color: #007aff;
  177. }
  178. }
  179. .bottom {
  180. padding-left: 80rpx;
  181. font-size: 24rpx;
  182. .create_time {
  183. color: #8c8c8c;
  184. }
  185. .delete {
  186. padding: 20rpx 20rpx 0 20rpx;
  187. color: #c0c0c0;
  188. }
  189. .reply {
  190. color: #007aff;
  191. }
  192. }
  193. }
  194. </style>