uni-im-conversation-list.vue 8.7 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1
<template>
2
<view class="uni-im-conversation-list">
3
  <scroll-view
DCloud_JSON's avatar
DCloud_JSON 已提交
4 5
    ref="conversation-list"
    class="conversation-list"
6
    :class="{canCheck}"
DCloud_JSON's avatar
DCloud_JSON 已提交
7 8 9 10 11 12
    :style="{'background-color': conversationList.length?'#FFF':''}"
    :scroll-top="listScrollTop"
    scroll-y="true"
    @scrolltolower="loadMore()"
    @scroll="onScroll"
  >
13
    <uni-im-conversation v-for="(item,index) in conversationList" :key="item.id"
14
      class="conversation-list-item" :class="{'activeConversation':activeConversationId == item.id,'focus':focusConversationId === item.id}"
15 16
      :conversation="item" :id="item.id"
      @click="clickItem(item)" @contextmenu.prevent="openConversationMenu($event,index)"
17
      @longpress="onScroll.isScrolling?'':openConversationMenu($event,index)"
DCloud_JSON's avatar
DCloud_JSON 已提交
18
    >
19 20 21 22 23 24 25 26
      <template v-if="canCheck" #left>
        <view style="justify-content: center;">
          <view class="check-box" :class="{checked:isCheck(item)}">
            <uni-icons
              v-if="isCheck(item)"
              color="#FFF"
              type="checkmarkempty"
            />
DCloud_JSON's avatar
DCloud_JSON 已提交
27
          </view>
28 29 30
        </view>
      </template>
    </uni-im-conversation>
DCloud_JSON's avatar
DCloud_JSON 已提交
31

32 33 34 35 36 37 38 39 40 41 42
    <view style="margin-top: 5px;backgroundColor:'#f5f5f5'">
      <template v-if="!keyword">
        <uni-im-load-state
          :content-text="loadMoreContentText"
          :status="conversationHasMore?'loading':'noMore'"
        />
      </template>
      <text v-else-if="conversationList.length === 0"
        style="text-align: center;flex: 1;margin: 8px;color: #aaa;"
      >没有相关数据</text>
    </view>
DCloud_JSON's avatar
DCloud_JSON 已提交
43 44 45 46 47 48 49 50 51 52 53 54

  </scroll-view>

  <uni-im-contextmenu ref="uni-im-contextmenu" />
</view>
</template>

<script>
import uniIm from '@/uni_modules/uni-im/sdk/index.js';
const db = uniCloud.database();
let currentScrollTop = 0;
export default {
55
  emits: ['change', 'clickItem', 'onScroll'],
DCloud_JSON's avatar
DCloud_JSON 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
  props: {
    keyword: {
      type: String,
      default: ''
    },
    activeConversationId: {
      type: [String, Boolean],
      default: ''
    },
    canCheck: {
      type: Boolean,
      default: false
    },
    checkedList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      listScrollTop: 0,
77
      focusConversationId: ''
DCloud_JSON's avatar
DCloud_JSON 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
    }
  },
  computed: {
    conversationHasMore() {
      return uniIm.conversation.hasMore
    },
    loadMoreContentText() {
      return {
        contentrefresh: "正在加载...",
        contentnomore: (this.conversationList.length ? "没有更多数据了" : "没有会话数据")
      }
    },
    conversationList() {
      return uniIm.conversation.dataList
        // #ifdef H5
        .filter(i => i.title.toLowerCase().includes(this.keyword.toLowerCase()))
        // #endif
        .filter(i => !i.hidden)
    }
  },
  watch: {
    conversationList: {
      handler() {
        // console.log('更新会话列表',this.conversationList)
        this.$emit('change', this.conversationList)
      },
      deep: true
    },
    activeConversationId(activeConversationId) {
      // #ifdef H5
      // 重试次数
      let tryIndex = 0
      const scrollToCurrentConversation = () => {
        if (tryIndex > 5) {
          return
        }
        const query = uni.createSelectorQuery().in(this);
        query.select('#' + activeConversationId).boundingClientRect(data => {
          if (!data) {
            // console.log('找不到 showMsgByIndex #'+activeConversationId,'tryIndex:'+tryIndex);
            tryIndex++
            setTimeout(() => scrollToCurrentConversation(), 300)
            return
          } else {
            // console.log(data);
          }
          let listHeight = document.querySelector('.conversation-list')?.clientHeight
          if (data.top - 70 < listHeight) {
            return
          }
          this.listScrollTop = ''
          this.$nextTick(() => {
            this.listScrollTop = currentScrollTop - listHeight + data.top + data.height
            // console.log('this.listScrollTop',this.listScrollTop);
          })
        }).exec()
      }
      scrollToCurrentConversation()
      // #endif
    }
  },
  mounted() {},
  methods: {
    onScroll(e) {
      currentScrollTop = e.detail.scrollTop
      this.$emit('onScroll', e)
144 145 146 147 148 149 150
      // 设置 this.onScroll.isScrolling 为 true
      this.onScroll.isScrolling = true
      clearTimeout(this.onScroll.timer)
      this.onScroll.timer = setTimeout(() => {
        // 设置 this.onScroll.isScrolling 为 false
        this.onScroll.isScrolling = false
      }, 500)
DCloud_JSON's avatar
DCloud_JSON 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    },
    isCheck(item) {
      return this.checkedList.some(i => i.id === item.id)
    },
    clickItem(item) {
      if (this.canCheck) {
        // 判断是否选中
        let checkedList = this.checkedList;
        if (this.isCheck(item)) {
          checkedList.splice(checkedList.findIndex(i => i.id === item.id), 1)
        } else {
          checkedList.push(item)
        }
        this.$emit('update:checkedList', checkedList)
      }
      this.$emit('clickItem', item)
    },
    openConversationMenu(e, index) {
      let conversation = this.conversationList[index]
170
      this.focusConversationId = conversation.id
DCloud_JSON's avatar
DCloud_JSON 已提交
171
      const myContextmenu = this.$refs['uni-im-contextmenu']
172 173 174 175
      
      const clientY = e.clientY || e.changedTouches[0].clientY
      const clientX = e.clientX || e.changedTouches[0].clientX
      
DCloud_JSON's avatar
DCloud_JSON 已提交
176
      const position = {
177 178
        "top": clientY + 35,
        "left": clientX
DCloud_JSON's avatar
DCloud_JSON 已提交
179 180
      }
      let menuList = [{
181
                        "title": conversation.pinned ? "取消置顶" : "置顶",
DCloud_JSON's avatar
DCloud_JSON 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
                        "action": () => {
                          conversation.pinned = !conversation.pinned
                          db.collection('uni-im-conversation')
                            .doc(conversation._id)
                            .update({
                              "pinned": conversation.pinned
                            })
                            .then((e) => {
                              console.log('updated 置顶', e.result.updated, conversation._id)
                            })
                            .catch(() => {
                              uni.showToast({
                                title: '服务端错误,置顶失败,请稍后重试',
                                icon: 'none'
                              });
                              conversation.pinned = !conversation.pinned
                            })
                        }
                      },
201 202 203 204 205 206
                      {
                        "title": "标为" + (conversation.unread_count ? "已读" : "未读"),
                        "action": () => {
                          conversation.setUnreadCount(conversation.unread_count ? 0 : 1)
                        }
                      },
DCloud_JSON's avatar
DCloud_JSON 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
                      {
                        "title": conversation.mute ? "允许消息通知" : "消息免打扰",
                        "action": () => {
                          console.log('mute 允许消息通知 / 消息免打扰')
                          conversation.changeMute()
                        }
                      },
                      // {
                      //   "title":"复制会话id",
                      //   "action":()=>{
                      //      uni.setClipboardData({
                      //        data:conversation.id,
                      //        showToast:false
                      //      })
                      //   }
                      // },
                      {
                        "title": "移除会话",
                        "action": () => conversation.hide()
                      }
      ]
      myContextmenu.show(position, menuList)
      myContextmenu.onClose(() => {
230
        this.focusConversationId = ''
DCloud_JSON's avatar
DCloud_JSON 已提交
231 232
      })
    },
233 234 235 236
    closeConversationMenu(){
      const myContextmenu = this.$refs['uni-im-contextmenu']
      myContextmenu.closeMe()
    },
DCloud_JSON's avatar
DCloud_JSON 已提交
237 238 239 240 241 242 243 244 245
    async loadMore() {
      let data = await uniIm.conversation.loadMore()
      // console.log('加载到新的会话数据',data);
      return data
    }
  }
}
</script>

246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
<style lang="scss">
  .uni-im-conversation-list{
    &,.conversation-list{
      height: 100%;
      flex: 1;
    }
    .tip {
      flex: 1;
    }
    
    .canCheck  {
      .note-box,
      .time,
      .state {
        display: none;
      }
      .title {
        font-size: 16px !important;
      }
    }
    
    .conversation-list .conversation-list-item.focus {
      border: 2px solid #1ab94e;
    }
    
    .conversation-list .conversation-list-item {
      border-radius: 5px;
    }
    
    .conversation-list .conversation-list-item.activeConversation {
      background-color: #f1f1f1;
    }
    
    .check-box {
      border: 1px solid #ccc;
      width: 20px;
      height: 20px;
      border-radius: 2px;
      margin-right: 10px;
    }
    
    .check-box.checked {
      background-color: #00a953;
      border-color: #00a953;
    }
DCloud_JSON's avatar
DCloud_JSON 已提交
291 292
  }
</style>