index.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.conversationName=this.getConversationName(conversation);
  47. this.setConversationAvatar=this.setConversationAvatarHandler(conversation);
  48. this.timeago=caculateTimeago(conversation.lastMessage.lastTime * 1000);
  49. // this.setData({
  50. // conversationName: this.getConversationName(conversation),
  51. // setConversationAvatar: this.setConversationAvatarHandler(conversation),
  52. // timeago: caculateTimeago(conversation.lastMessage.lastTime * 1000)
  53. // });
  54. this.$updateTimeago(conversation);
  55. },
  56. immediate: true,
  57. deep: true
  58. }
  59. },
  60. methods: {
  61. // 先查 remark;无 remark 查 (c2c)nick/(group)name;最后查 (c2c)userID/(group)groupID
  62. getConversationName(conversation) {
  63. if (conversation.type === '@TIM#SYSTEM') {
  64. return '系统通知';
  65. }
  66. if (conversation.type === 'C2C') {
  67. return conversation.remark || conversation.userProfile.nick || conversation.userProfile.userID;
  68. }
  69. if (conversation.type === 'GROUP') {
  70. return conversation.groupProfile.name || conversation.groupProfile.groupID;
  71. }
  72. },
  73. setConversationAvatarHandler(conversation) {
  74. if (conversation.type === '@TIM#SYSTEM') {
  75. return 'https://web.sdk.qcloud.com/component/TUIKit/assets/system.png';
  76. }
  77. if (conversation.type === 'C2C') {
  78. return conversation.userProfile.avatar || 'https://sdk-web-1252463788.cos.ap-hongkong.myqcloud.com/component/TUIKit/assets/avatar_21.png';
  79. }
  80. if (conversation.type === 'GROUP') {
  81. return conversation.groupProfile.avatar || '/static/static/assets/gruopavatar.svg';
  82. }
  83. },
  84. deleteConversation() {
  85. uni.showModal({
  86. content: '确认删除会话?',
  87. success: res => {
  88. if (res.confirm) {
  89. uni.$TUIKit.deleteConversation(this.conversation.conversationID);
  90. this.setData({
  91. conversation: {},
  92. xScale: 0
  93. });
  94. }
  95. }
  96. });
  97. },
  98. handleTouchMove(e) {
  99. if (!this.lock) {
  100. this.last = e.detail.x;
  101. this.lock = true;
  102. }
  103. if (this.lock && e.detail.x - this.last < -5) {
  104. this.setData({
  105. xScale: -75
  106. });
  107. setTimeout(() => {
  108. this.lock = false;
  109. }, 2000);
  110. } else if (this.lock && e.detail.x - this.last > 5) {
  111. this.setData({
  112. xScale: 0
  113. });
  114. setTimeout(() => {
  115. this.lock = false;
  116. }, 2000);
  117. }
  118. },
  119. $updateTimeago(conversation) {
  120. if (conversation.conversationID) {
  121. // conversation.lastMessage.timeago = caculateTimeago(conversation.lastMessage.lastTime * 1000);
  122. conversation.lastMessage.messageForShow = conversation.lastMessage.messageForShow.slice(0, 15);
  123. }
  124. this.setData({
  125. conversation
  126. });
  127. },
  128. handleImageError() {
  129. this.setData({
  130. setConversationAvatar: '/static/static/assets/gruopavatar.svg'
  131. });
  132. }
  133. }
  134. };
  135. </script>
  136. <style>
  137. @import './index.css';
  138. </style>