chat-item.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <template>
  2. <view class="chat-item" :class="active ? 'active' : ''">
  3. <!--rich-text中的表情包会屏蔽事件,所以这里用一个遮罩层捕获点击事件 -->
  4. <view class="mask" @tap="showChatBox()"></view>
  5. <view class="left">
  6. <head-image :url="chat.headImage" :name="chat.showName"></head-image>
  7. </view>
  8. <view class="chat-right">
  9. <view class="chat-name">
  10. <view class="chat-name-text">
  11. <view>{{ chat.showName }}</view>
  12. </view>
  13. <view class="chat-time">{{ $date.toTimeText(chat.lastSendTime, true) }}</view>
  14. </view>
  15. <view class="chat-content">
  16. <view class="chat-at-text">{{ atText }}</view>
  17. <view class="chat-send-name" v-if="isShowSendName">{{ chat.sendNickName + ':&nbsp;' }}</view>
  18. <view v-if="!isTextMessage" class="chat-content-text">{{chat.lastContent}}</view>
  19. <rich-text v-else class="chat-content-text" :nodes="nodesText"></rich-text>
  20. <view v-if="chat.isDnd" class="icon iconfont icon-dnd"></view>
  21. <uni-badge v-else-if="chat.unreadCount > 0" :max-num="99" :text="chat.unreadCount" />
  22. </view>
  23. </view>
  24. </view>
  25. </template>
  26. <script>
  27. export default {
  28. name: "chatItem",
  29. data() {
  30. return {}
  31. },
  32. props: {
  33. chat: {
  34. type: Object
  35. },
  36. index: {
  37. type: Number
  38. },
  39. active: {
  40. type: Boolean,
  41. default: false
  42. }
  43. },
  44. methods: {
  45. showChatBox() {
  46. // 初始化期间进入会话会导致消息不刷新
  47. if (!getApp().$vm.isInit || this.chatStore.isLoading()) {
  48. uni.showToast({
  49. title: "正在初始化页面,请稍后...",
  50. icon: 'none'
  51. })
  52. return;
  53. }
  54. uni.navigateTo({
  55. url: "/pages/chat/chat-box?chatIdx=" + this.index
  56. })
  57. }
  58. },
  59. computed: {
  60. isShowSendName() {
  61. if (!this.chat.sendNickName) {
  62. return false;
  63. }
  64. let size = this.chat.messages.length;
  65. if (size == 0) {
  66. return false;
  67. }
  68. // 只有群聊的普通消息需要显示名称
  69. let lastMsg = this.chat.messages[size - 1];
  70. return this.$msgType.isNormal(lastMsg.type)
  71. },
  72. atText() {
  73. if (this.chat.atMe) {
  74. return "[有人@我]"
  75. } else if (this.chat.atAll) {
  76. return "[@全体成员]"
  77. }
  78. return "";
  79. },
  80. isTextMessage() {
  81. let idx = this.chat.messages.length - 1;
  82. let messageType = this.chat.messages[idx].type;
  83. return messageType == this.$enums.MESSAGE_TYPE.TEXT;
  84. },
  85. nodesText() {
  86. let text = this.$str.html2Escape(this.chat.lastContent);
  87. return this.$emo.transform(text, 'emoji-small')
  88. }
  89. }
  90. }
  91. </script>
  92. <style scoped lang="scss">
  93. .chat-item {
  94. height: 96rpx;
  95. display: flex;
  96. margin-bottom: 2rpx;
  97. position: relative;
  98. padding: 18rpx 20rpx;
  99. align-items: center;
  100. background-color: white;
  101. white-space: nowrap;
  102. &:hover {
  103. background-color: $im-bg-active;
  104. }
  105. &.active {
  106. background-color: $im-bg-active;
  107. }
  108. .mask {
  109. position: absolute;
  110. width: 100%;
  111. height: 100%;
  112. left: 0;
  113. right: 0;
  114. z-index: 99;
  115. }
  116. .left {
  117. position: relative;
  118. display: flex;
  119. justify-content: center;
  120. align-items: center;
  121. width: 100rpx;
  122. height: 100rpx;
  123. }
  124. .chat-right {
  125. height: 100%;
  126. flex: 1;
  127. display: flex;
  128. flex-direction: column;
  129. justify-content: center;
  130. padding-left: 20rpx;
  131. text-align: left;
  132. overflow: hidden;
  133. .chat-name {
  134. display: flex;
  135. .chat-name-text {
  136. flex: 1;
  137. font-size: $im-font-size-large;
  138. white-space: nowrap;
  139. overflow: hidden;
  140. display: flex;
  141. align-items: center;
  142. }
  143. .chat-time {
  144. font-size: $im-font-size-smaller-extra;
  145. color: $im-text-color-lighter;
  146. text-align: right;
  147. white-space: nowrap;
  148. overflow: hidden;
  149. }
  150. }
  151. .chat-content {
  152. display: flex;
  153. font-size: $im-font-size-smaller;
  154. color: $im-text-color-lighter;
  155. padding-top: 8rpx;
  156. align-items: center;
  157. .chat-at-text {
  158. color: $im-color-danger;
  159. }
  160. .chat-send-name {
  161. font-size: $im-font-size-smaller;
  162. }
  163. .chat-content-text {
  164. flex: 1;
  165. white-space: nowrap;
  166. overflow: hidden;
  167. text-overflow: ellipsis;
  168. }
  169. .icon {
  170. font-size: $im-font-size;
  171. }
  172. }
  173. }
  174. }
  175. </style>