chat.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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="(chat,index) in chatStore.chats" :key="index">
  18. <pop-menu v-if="isShowChat(chat)" :items="menu.items"
  19. @select="onSelectMenu($event,index)">
  20. <chat-item :chat="chat" :index="index"
  21. :active="menu.chatIdx==index"></chat-item>
  22. </pop-menu>
  23. </view>
  24. </scroll-view>
  25. </view>
  26. </template>
  27. <script>
  28. import useChatStore from '@/store/chatStore.js'
  29. export default {
  30. data() {
  31. return {
  32. searchText: "",
  33. menu: {
  34. show: false,
  35. style: "",
  36. chatIdx: -1,
  37. isTouchMove: false,
  38. items: [{
  39. key: 'DELETE',
  40. name: '删除该聊天',
  41. icon: 'trash',
  42. color: '#e64e4e'
  43. },
  44. {
  45. key: 'TOP',
  46. name: '置顶该聊天',
  47. icon: 'arrow-up'
  48. }
  49. ]
  50. }
  51. }
  52. },
  53. methods: {
  54. onSelectMenu(item,chatIdx) {
  55. switch (item.key) {
  56. case 'DELETE':
  57. this.removeChat(chatIdx);
  58. break;
  59. case 'TOP':
  60. this.moveToTop(chatIdx);
  61. break;
  62. default:
  63. break;
  64. }
  65. this.menu.show = false;
  66. },
  67. removeChat(chatIdx) {
  68. this.chatStore.removeChat(chatIdx);
  69. },
  70. moveToTop(chatIdx) {
  71. this.chatStore.moveTop(chatIdx);
  72. },
  73. isShowChat(chat){
  74. if(chat.delete){
  75. return false;
  76. }
  77. return !this.searchText || chat.showName.includes(this.searchText)
  78. },
  79. refreshUnreadBadge() {
  80. if (this.unreadCount > 0) {
  81. uni.setTabBarBadge({
  82. index: 0,
  83. text: this.unreadCount + ""
  84. })
  85. } else {
  86. uni.removeTabBarBadge({
  87. index: 0,
  88. complete: () => {}
  89. })
  90. }
  91. }
  92. },
  93. computed: {
  94. unreadCount() {
  95. let count = 0;
  96. this.chatStore.chats.forEach(chat => {
  97. if (!chat.delete) {
  98. count += chat.unreadCount;
  99. }
  100. })
  101. return count;
  102. },
  103. loading() {
  104. return this.chatStore.loadingGroupMsg || this.chatStore.loadingPrivateMsg
  105. }
  106. },
  107. watch: {
  108. unreadCount(newCount, oldCount) {
  109. this.refreshUnreadBadge();
  110. }
  111. },
  112. onShow() {
  113. this.refreshUnreadBadge();
  114. }
  115. }
  116. </script>
  117. <style scoped lang="scss">
  118. .tab-page {
  119. position: relative;
  120. border: #dddddd solid 1px;
  121. display: flex;
  122. flex-direction: column;
  123. .nav-bar {
  124. padding: 2rpx 20rpx;
  125. display: flex;
  126. align-items: center;
  127. background-color: white;
  128. border-bottom: 1px solid #ddd;
  129. height: 110rpx;
  130. .nav-search {
  131. flex: 1;
  132. height: 110rpx;
  133. }
  134. }
  135. .chat-tip {
  136. position: absolute;
  137. top: 400rpx;
  138. padding: 50rpx;
  139. line-height: 50rpx;
  140. text-align: left;
  141. color: darkblue;
  142. font-size: 30rpx;
  143. }
  144. .chat-loading {
  145. display: block;
  146. width: 100%;
  147. height: 120rpx;
  148. background: white;
  149. position: fixed;
  150. top: 0;
  151. z-index: 999;
  152. color: blue;
  153. .loading-box {
  154. position: relative;
  155. }
  156. }
  157. .scroll-bar {
  158. flex: 1;
  159. height: 100%;
  160. }
  161. }
  162. </style>