chat.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. <view class="tip-icon">
  22. <text class="iconfont icon-chat"></text>
  23. </view>
  24. <view class="tip-title">还没有聊天</view>
  25. <view class="tip-content">添加好友或创建群聊,开始精彩的对话吧</view>
  26. </view>
  27. <scroll-view class="scroll-bar" v-else scroll-with-animation="true" scroll-y="true">
  28. <view v-for="(chat, index) in chatStore.chats" :key="index">
  29. <long-press-menu v-if="isShowChat(chat)" :items="menu.items" @select="onSelectMenu($event, index)">
  30. <chat-item :chat="chat" :index="index" :active="menu.chatIdx == index"></chat-item>
  31. </long-press-menu>
  32. </view>
  33. </scroll-view>
  34. </view>
  35. </template>
  36. <script>
  37. export default {
  38. data() {
  39. return {
  40. showSearch: false,
  41. searchText: "",
  42. menu: {
  43. show: false,
  44. style: "",
  45. chatIdx: -1,
  46. isTouchMove: false,
  47. items: [{
  48. key: 'DELETE',
  49. name: '删除该聊天',
  50. icon: 'trash',
  51. color: '#e64e4e'
  52. },
  53. {
  54. key: 'TOP',
  55. name: '置顶该聊天',
  56. icon: 'arrow-up'
  57. }
  58. ]
  59. }
  60. }
  61. },
  62. methods: {
  63. onSelectMenu(item, chatIdx) {
  64. switch (item.key) {
  65. case 'DELETE':
  66. this.removeChat(chatIdx);
  67. break;
  68. case 'TOP':
  69. this.moveToTop(chatIdx);
  70. break;
  71. default:
  72. break;
  73. }
  74. this.menu.show = false;
  75. },
  76. removeChat(chatIdx) {
  77. this.chatStore.removeChat(chatIdx);
  78. },
  79. moveToTop(chatIdx) {
  80. this.chatStore.moveTop(chatIdx);
  81. },
  82. isShowChat(chat) {
  83. if (chat.delete) {
  84. return false;
  85. }
  86. return !this.searchText || chat.showName.includes(this.searchText)
  87. },
  88. onSearch() {
  89. this.showSearch = !this.showSearch;
  90. this.searchText = "";
  91. },
  92. refreshUnreadBadge() {
  93. if (this.unreadCount > 0) {
  94. uni.setTabBarBadge({
  95. index: 0,
  96. text: this.unreadCount + ""
  97. })
  98. } else {
  99. uni.removeTabBarBadge({
  100. index: 0,
  101. complete: () => {}
  102. })
  103. }
  104. }
  105. },
  106. computed: {
  107. unreadCount() {
  108. let count = 0;
  109. this.chatStore.chats.forEach(chat => {
  110. if (!chat.isDnd && !chat.delete) {
  111. count += chat.unreadCount;
  112. }
  113. })
  114. return count;
  115. },
  116. loading() {
  117. return this.chatStore.loading;
  118. },
  119. initializing() {
  120. return !this.configStore.appInit;
  121. },
  122. showChats() {
  123. this.chatStore.chats.filter((chat) => !chat.delete && chat.showName && chat.showName.includes(this
  124. .searchText))
  125. }
  126. },
  127. watch: {
  128. unreadCount(newCount, oldCount) {
  129. this.refreshUnreadBadge();
  130. }
  131. },
  132. onShow() {
  133. this.refreshUnreadBadge();
  134. }
  135. }
  136. </script>
  137. <style lang="scss">
  138. .tab-page {
  139. position: relative;
  140. display: flex;
  141. flex-direction: column;
  142. .chat-tip {
  143. position: absolute;
  144. top: 50%;
  145. left: 50%;
  146. transform: translate(-50%, -50%);
  147. display: flex;
  148. flex-direction: column;
  149. align-items: center;
  150. padding: 40rpx;
  151. text-align: center;
  152. width: 80%;
  153. .tip-icon {
  154. width: 120rpx;
  155. height: 120rpx;
  156. background: linear-gradient(135deg, #f8f9fa, #e9ecef);
  157. border-radius: 50%;
  158. display: flex;
  159. align-items: center;
  160. justify-content: center;
  161. margin-bottom: 40rpx;
  162. box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.08);
  163. border: 1rpx solid $im-bg-active;
  164. .iconfont {
  165. font-size: 60rpx;
  166. color: $im-text-color-lighter;
  167. }
  168. }
  169. .tip-title {
  170. font-size: $im-font-size-large;
  171. color: $im-text-color;
  172. font-weight: 500;
  173. margin-bottom: 20rpx;
  174. }
  175. .tip-content {
  176. font-size: $im-font-size-smaller;
  177. color: $im-text-color-lighter;
  178. line-height: 1.6;
  179. margin-bottom: 50rpx;
  180. }
  181. }
  182. .chat-loading {
  183. display: block;
  184. width: 100%;
  185. height: 120rpx;
  186. background: white;
  187. color: $im-text-color-lighter;
  188. .loading-box {
  189. position: relative;
  190. }
  191. }
  192. .scroll-bar {
  193. flex: 1;
  194. height: 100%;
  195. }
  196. }
  197. </style>