index.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <template>
  2. <!--TODO: 默认图片在 cos 上添加 -->
  3. <movable-area v-if="conversation.conversationID" class="t-conversation-item-container">
  4. <movable-view class="t-conversation-item" direction="horizontal" @change="handleTouchMove" damping="100" :x="xScale">
  5. <view class="avatar-box">
  6. <image class="t-conversation-item-avatar" :src="setConversationAvatar" @error="handleImageError"></image>
  7. <view class="unread" v-if="conversation.unreadCount !== 0">
  8. <view class="read-text" v-if="conversation.unreadCount > 99">99+</view>
  9. <view class="read-text" v-else>{{ conversation.unreadCount }}</view>
  10. </view>
  11. </view>
  12. <view class="t-conversation-item-content">
  13. <label class="tui-conversation-item-name">{{ conversationName }}</label>
  14. <view class="tui-conversation-lastMessage">
  15. <text>{{ conversation.lastMessage.messageForShow }}</text>
  16. </view>
  17. </view>
  18. <view class="t-conversation-item-info">{{ timeago }}</view>
  19. <!-- <view class="t-conversation-delete" @tap.stop="deleteConversation">删除</view>-->
  20. </movable-view>
  21. </movable-area>
  22. </template>
  23. <script>
  24. import { caculateTimeago } from '../../base/common';
  25. export default {
  26. data() {
  27. return {
  28. xScale: 0,
  29. conversationName: '',
  30. conversationAvatar: '',
  31. setConversationAvatar: '',
  32. timeago: ''
  33. };
  34. },
  35. components: {},
  36. props: {
  37. conversation: {
  38. type: Object,
  39. default: () => {}
  40. }
  41. },
  42. watch: {
  43. conversation: {
  44. handler: function(conversation) {
  45. // 计算时间戳
  46. this.setData({
  47. conversationName: this.getConversationName(conversation),
  48. setConversationAvatar: this.setConversationAvatarHandler(conversation),
  49. timeago: caculateTimeago(conversation.lastMessage.lastTime * 1000)
  50. });
  51. this.$updateTimeago(conversation);
  52. },
  53. immediate: true,
  54. deep: true
  55. }
  56. },
  57. methods: {
  58. // 先查 remark;无 remark 查 (c2c)nick/(group)name;最后查 (c2c)userID/(group)groupID
  59. getConversationName(conversation) {
  60. if (conversation.type === '@TIM#SYSTEM') {
  61. return '系统通知';
  62. }
  63. if (conversation.type === 'C2C') {
  64. return conversation.remark || conversation.userProfile.nick || conversation.userProfile.userID;
  65. }
  66. if (conversation.type === 'GROUP') {
  67. return conversation.groupProfile.name || conversation.groupProfile.groupID;
  68. }
  69. },
  70. setConversationAvatarHandler(conversation) {
  71. if (conversation.type === '@TIM#SYSTEM') {
  72. return 'https://web.sdk.qcloud.com/component/TUIKit/assets/system.png';
  73. }
  74. if (conversation.type === 'C2C') {
  75. return conversation.userProfile.avatar || 'https://sdk-web-1252463788.cos.ap-hongkong.myqcloud.com/component/TUIKit/assets/avatar_21.png';
  76. }
  77. if (conversation.type === 'GROUP') {
  78. return conversation.groupProfile.avatar || '/static/static/assets/gruopavatar.svg';
  79. }
  80. },
  81. deleteConversation() {
  82. uni.showModal({
  83. content: '确认删除会话?',
  84. success: res => {
  85. if (res.confirm) {
  86. uni.$TUIKit.deleteConversation(this.conversation.conversationID);
  87. this.setData({
  88. conversation: {},
  89. xScale: 0
  90. });
  91. }
  92. }
  93. });
  94. },
  95. handleTouchMove(e) {
  96. if (!this.lock) {
  97. this.last = e.detail.x;
  98. this.lock = true;
  99. }
  100. if (this.lock && e.detail.x - this.last < -5) {
  101. this.setData({
  102. xScale: -75
  103. });
  104. setTimeout(() => {
  105. this.lock = false;
  106. }, 2000);
  107. } else if (this.lock && e.detail.x - this.last > 5) {
  108. this.setData({
  109. xScale: 0
  110. });
  111. setTimeout(() => {
  112. this.lock = false;
  113. }, 2000);
  114. }
  115. },
  116. $updateTimeago(conversation) {
  117. if (conversation.conversationID) {
  118. // conversation.lastMessage.timeago = caculateTimeago(conversation.lastMessage.lastTime * 1000);
  119. conversation.lastMessage.messageForShow = conversation.lastMessage.messageForShow.slice(0, 15);
  120. }
  121. this.setData({
  122. conversation
  123. });
  124. },
  125. handleImageError() {
  126. this.setData({
  127. setConversationAvatar: '/static/static/assets/gruopavatar.svg'
  128. });
  129. }
  130. }
  131. };
  132. </script>
  133. <style>
  134. @import './index.css';
  135. </style>