chat.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <template>
  2. <view class="tab-page">
  3. <scroll-view class="scroll-bar" scroll-with-animation="true" scroll-y="true">
  4. <view v-for="(chat,index) in $store.state.chatStore.chats" :key="index">
  5. <chat-item :chat="chat" :index="index" @longpress.native="onShowMenu($event,index)"></chat-item>
  6. </view>
  7. </scroll-view>
  8. <pop-menu v-show="menu.show" :menu-style="menu.style" :items="menu.items" @close="menu.show=false"
  9. @select="onSelectMenu"></pop-menu>
  10. </view>
  11. </template>
  12. <script>
  13. export default {
  14. data() {
  15. return {
  16. menu: {
  17. show: false,
  18. style: "",
  19. chatIdx: -1,
  20. items: [{
  21. key: 'DELETE',
  22. name: '删除',
  23. icon: 'trash'
  24. },
  25. {
  26. key: 'TOP',
  27. name: '置顶',
  28. icon: 'arrow-up'
  29. }
  30. ]
  31. }
  32. }
  33. },
  34. methods: {
  35. onSelectMenu(item) {
  36. switch (item.key) {
  37. case 'DELETE':
  38. this.removeChat(this.menu.chatIdx);
  39. case 'TOP':
  40. this.moveToTop(this.menu.chatIdx);
  41. default:
  42. break;
  43. }
  44. },
  45. onShowMenu(e,chatIdx) {
  46. uni.getSystemInfo({
  47. success: (res) => {
  48. let touches = e.touches[0];
  49. let style = "";
  50. /* 因 非H5端不兼容 style 属性绑定 Object ,所以拼接字符 */
  51. if (touches.clientY > (res.windowHeight / 2)) {
  52. style = `bottom:${res.windowHeight-touches.clientY}px;`;
  53. } else {
  54. style = `top:${touches.clientY}px;`;
  55. }
  56. if (touches.clientX > (res.windowWidth / 2)) {
  57. style += `right:${res.windowWidth-touches.clientX}px;`;
  58. } else {
  59. style += `left:${touches.clientX}px;`;
  60. }
  61. this.menu.style = style;
  62. this.menu.chatIdx = chatIdx;
  63. //
  64. this.$nextTick(() => {
  65. this.menu.show = true;
  66. });
  67. }
  68. })
  69. },
  70. removeChat(chatIdx) {
  71. this.$store.commit("removeChat", chatIdx);
  72. },
  73. moveToTop(chatIdx) {
  74. this.$store.commit("moveTop", chatIdx);
  75. },
  76. refreshUnreadBadge() {
  77. if (this.unreadCount > 0) {
  78. uni.setTabBarBadge({
  79. index: 0,
  80. text: this.unreadCount + ""
  81. })
  82. } else {
  83. uni.removeTabBarBadge({
  84. index: 0
  85. })
  86. }
  87. }
  88. },
  89. computed: {
  90. unreadCount() {
  91. let count = 0;
  92. this.$store.state.chatStore.chats.forEach(chat => {
  93. count += chat.unreadCount;
  94. })
  95. return count;
  96. }
  97. },
  98. watch:{
  99. unreadCount(newCount,oldCount){
  100. this.refreshUnreadBadge();
  101. }
  102. },
  103. onLoad() {
  104. this.refreshUnreadBadge();
  105. }
  106. }
  107. </script>
  108. <style>
  109. </style>