chat.vue 3.4 KB

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