chat.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <template>
  2. <view class="tab-page">
  3. <view class="chat-tip" v-if="$store.state.chatStore.chats.length==0">
  4. 温馨提示:您现在还没有任何聊天消息,快跟您的好友发起聊天吧~
  5. </view>
  6. <scroll-view class="scroll-bar" v-else scroll-with-animation="true" scroll-y="true">
  7. <view v-for="(chat,index) in $store.state.chatStore.chats" :key="index">
  8. <chat-item :chat="chat" :index="index" @longpress.native="onShowMenu($event,index)"></chat-item>
  9. </view>
  10. </scroll-view>
  11. <pop-menu v-show="menu.show" :menu-style="menu.style" :items="menu.items" @close="menu.show=false"
  12. @select="onSelectMenu"></pop-menu>
  13. </view>
  14. </template>
  15. <script>
  16. export default {
  17. data() {
  18. return {
  19. menu: {
  20. show: false,
  21. style: "",
  22. chatIdx: -1,
  23. items: [{
  24. key: 'DELETE',
  25. name: '删除',
  26. icon: 'trash'
  27. },
  28. {
  29. key: 'TOP',
  30. name: '置顶',
  31. icon: 'arrow-up'
  32. }
  33. ]
  34. }
  35. }
  36. },
  37. methods: {
  38. onSelectMenu(item) {
  39. switch (item.key) {
  40. case 'DELETE':
  41. this.removeChat(this.menu.chatIdx);
  42. break;
  43. case 'TOP':
  44. this.moveToTop(this.menu.chatIdx);
  45. break;
  46. default:
  47. break;
  48. }
  49. this.menu.show = false;
  50. },
  51. onShowMenu(e, chatIdx) {
  52. uni.getSystemInfo({
  53. success: (res) => {
  54. let touches = e.touches[0];
  55. let style = "";
  56. /* 因 非H5端不兼容 style 属性绑定 Object ,所以拼接字符 */
  57. if (touches.clientY > (res.windowHeight / 2)) {
  58. style = `bottom:${res.windowHeight-touches.clientY}px;`;
  59. } else {
  60. style = `top:${touches.clientY}px;`;
  61. }
  62. if (touches.clientX > (res.windowWidth / 2)) {
  63. style += `right:${res.windowWidth-touches.clientX}px;`;
  64. } else {
  65. style += `left:${touches.clientX}px;`;
  66. }
  67. this.menu.style = style;
  68. this.menu.chatIdx = chatIdx;
  69. //
  70. this.$nextTick(() => {
  71. this.menu.show = true;
  72. });
  73. }
  74. })
  75. },
  76. removeChat(chatIdx) {
  77. this.$store.commit("removeChat", chatIdx);
  78. },
  79. moveToTop(chatIdx) {
  80. this.$store.commit("moveTop", chatIdx);
  81. },
  82. refreshUnreadBadge() {
  83. if (this.unreadCount > 0) {
  84. uni.setTabBarBadge({
  85. index: 0,
  86. text: this.unreadCount + ""
  87. })
  88. } else {
  89. uni.removeTabBarBadge({
  90. index: 0,
  91. complete: () => {}
  92. })
  93. }
  94. }
  95. },
  96. computed: {
  97. unreadCount() {
  98. let count = 0;
  99. this.$store.state.chatStore.chats.forEach(chat => {
  100. count += chat.unreadCount;
  101. })
  102. return count;
  103. }
  104. },
  105. watch: {
  106. unreadCount(newCount, oldCount) {
  107. this.refreshUnreadBadge();
  108. }
  109. },
  110. onShow() {
  111. this.refreshUnreadBadge();
  112. }
  113. }
  114. </script>
  115. <style scoped lang="scss">
  116. .chat-tip {
  117. position: absolute;
  118. top: 400rpx;
  119. padding: 50rpx;
  120. line-height: 50rpx;
  121. text-align: left;
  122. color: darkblue;
  123. font-size: 30rpx;
  124. }
  125. </style>