chat.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <template>
  2. <view class="tab-page">
  3. <nav-bar search @search="showSearch = !showSearch">消息</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 class="nav-bar" v-if="showSearch">
  10. <view class="nav-search">
  11. <uni-search-bar radius="100" v-model="searchText" cancelButton="none" placeholder="搜索"></uni-search-bar>
  12. </view>
  13. </view>
  14. <view class="chat-tip" v-if="!loading && chatStore.chats.length == 0">
  15. 温馨提示:您现在还没有任何聊天消息,快跟您的好友发起聊天吧~
  16. </view>
  17. <scroll-view class="scroll-bar" v-else scroll-with-animation="true" scroll-y="true">
  18. <view v-for="(chat, index) in chatStore.chats" :key="index">
  19. <long-press-menu v-if="isShowChat(chat)" :items="menu.items" @select="onSelectMenu($event, index)">
  20. <chat-item :chat="chat" :index="index" :active="menu.chatIdx == index"></chat-item>
  21. </long-press-menu>
  22. </view>
  23. </scroll-view>
  24. </view>
  25. </template>
  26. <script>
  27. export default {
  28. data() {
  29. return {
  30. showSearch: false,
  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.chatStore.removeChat(chatIdx);
  68. },
  69. moveToTop(chatIdx) {
  70. this.chatStore.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. unreadCount() {
  94. let count = 0;
  95. this.chatStore.chats.forEach(chat => {
  96. if (!chat.delete) {
  97. count += chat.unreadCount;
  98. }
  99. })
  100. return count;
  101. },
  102. loading() {
  103. return this.chatStore.loadingGroupMsg || this.chatStore.loadingPrivateMsg
  104. }
  105. },
  106. watch: {
  107. unreadCount(newCount, oldCount) {
  108. this.refreshUnreadBadge();
  109. }
  110. },
  111. onShow() {
  112. this.refreshUnreadBadge();
  113. }
  114. }
  115. </script>
  116. <style scoped lang="scss">
  117. .tab-page {
  118. position: relative;
  119. display: flex;
  120. flex-direction: column;
  121. .chat-tip {
  122. position: absolute;
  123. top: 400rpx;
  124. padding: 50rpx;
  125. line-height: 50rpx;
  126. text-align: center;
  127. color: $im-text-color-lighter;
  128. }
  129. .chat-loading {
  130. display: block;
  131. width: 100%;
  132. height: 120rpx;
  133. background: white;
  134. position: fixed;
  135. top: 0;
  136. z-index: 999;
  137. color: $im-text-color-lighter;
  138. .loading-box {
  139. position: relative;
  140. }
  141. }
  142. .scroll-bar {
  143. flex: 1;
  144. height: 100%;
  145. }
  146. }
  147. </style>