chat.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <template>
  2. <view class="tab-page">
  3. <view v-if="loading" class="chat-loading">
  4. <loading :size="50" :mask="false">
  5. <view>消息接收中...</view>
  6. </loading>
  7. </view>
  8. <view class="nav-bar">
  9. <view class="nav-search">
  10. <uni-search-bar radius="100" v-model="searchText" cancelButton="none" placeholder="搜索"></uni-search-bar>
  11. </view>
  12. </view>
  13. <view class="chat-tip" v-if="!loading && chatStore.chats.length==0">
  14. 温馨提示:您现在还没有任何聊天消息,快跟您的好友发起聊天吧~
  15. </view>
  16. <scroll-view class="scroll-bar" v-else scroll-with-animation="true" scroll-y="true">
  17. <view v-for="(chatPos,i) in chatsPos" :key="i">
  18. <pop-menu v-if="isShowChat(chatStore.chats[chatPos.idx])" :items="menu.items"
  19. @select="onSelectMenu($event,chatPos.idx)">
  20. <chat-item :chat="chatStore.chats[chatPos.idx]"
  21. :active="menu.chatIdx==chatPos.idx" :index="chatPos.idx"></chat-item>
  22. </pop-menu>
  23. </view>
  24. </scroll-view>
  25. </view>
  26. </template>
  27. <script>
  28. export default {
  29. data() {
  30. return {
  31. searchText: "",
  32. menu: {
  33. show: false,
  34. style: "",
  35. chatIdx: -1,
  36. isTouchMove: false,
  37. items: [{
  38. key: 'DELETE',
  39. name: '删除该聊天',
  40. icon: 'trash',
  41. color: '#e64e4e'
  42. },
  43. {
  44. key: 'TOP',
  45. name: '置顶该聊天',
  46. icon: 'arrow-up'
  47. }
  48. ]
  49. }
  50. }
  51. },
  52. methods: {
  53. onSelectMenu(item,chatIdx) {
  54. switch (item.key) {
  55. case 'DELETE':
  56. this.removeChat(chatIdx);
  57. break;
  58. case 'TOP':
  59. this.moveToTop(chatIdx);
  60. break;
  61. default:
  62. break;
  63. }
  64. this.menu.show = false;
  65. },
  66. removeChat(chatIdx) {
  67. this.$store.commit("removeChat", chatIdx);
  68. },
  69. moveToTop(chatIdx) {
  70. this.$store.commit("moveTop", chatIdx);
  71. },
  72. isShowChat(chat){
  73. if(chat.delete){
  74. return false;
  75. }
  76. return !this.searchText || chat.showName.includes(this.searchText)
  77. },
  78. refreshUnreadBadge() {
  79. if (this.unreadCount > 0) {
  80. uni.setTabBarBadge({
  81. index: 0,
  82. text: this.unreadCount + ""
  83. })
  84. } else {
  85. uni.removeTabBarBadge({
  86. index: 0,
  87. complete: () => {}
  88. })
  89. }
  90. }
  91. },
  92. computed: {
  93. chatsPos() {
  94. // 计算会话的顺序
  95. let chatsPos = [];
  96. let chats = this.chatStore.chats;
  97. chats.forEach((chat, idx) => {
  98. chatsPos.push({
  99. idx: idx,
  100. sendTime: chat.lastSendTime
  101. })
  102. })
  103. chatsPos.sort((chatPos1, chatPos2) => {
  104. return chatPos2.sendTime - chatPos1.sendTime;
  105. });
  106. return chatsPos;
  107. },
  108. chatStore() {
  109. return this.$store.state.chatStore;
  110. },
  111. unreadCount() {
  112. let count = 0;
  113. this.chatStore.chats.forEach(chat => {
  114. if (!chat.delete) {
  115. count += chat.unreadCount;
  116. }
  117. })
  118. return count;
  119. },
  120. loading() {
  121. return this.chatStore.loadingGroupMsg || this.chatStore.loadingPrivateMsg
  122. }
  123. },
  124. watch: {
  125. unreadCount(newCount, oldCount) {
  126. this.refreshUnreadBadge();
  127. }
  128. },
  129. onShow() {
  130. this.refreshUnreadBadge();
  131. }
  132. }
  133. </script>
  134. <style scoped lang="scss">
  135. .tab-page {
  136. position: relative;
  137. border: #dddddd solid 1px;
  138. display: flex;
  139. flex-direction: column;
  140. .nav-bar {
  141. padding: 2rpx 20rpx;
  142. display: flex;
  143. align-items: center;
  144. background-color: white;
  145. border-bottom: 1px solid #ddd;
  146. height: 110rpx;
  147. .nav-search {
  148. flex: 1;
  149. height: 110rpx;
  150. }
  151. }
  152. .chat-tip {
  153. position: absolute;
  154. top: 400rpx;
  155. padding: 50rpx;
  156. line-height: 50rpx;
  157. text-align: left;
  158. color: darkblue;
  159. font-size: 30rpx;
  160. }
  161. .chat-loading {
  162. display: block;
  163. width: 100%;
  164. height: 120rpx;
  165. background: white;
  166. position: fixed;
  167. top: 0;
  168. z-index: 999;
  169. color: blue;
  170. .loading-box {
  171. position: relative;
  172. }
  173. }
  174. .scroll-bar {
  175. flex: 1;
  176. height: 100%;
  177. }
  178. }
  179. </style>