uni-im-conversation-list.vue 11.4 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1
<template>
2
<view class="uni-im-conversation-list">
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
3 4 5 6 7
  <view class="conversation-type">
    <view class="item" v-for="item in conversation.typeList" :key="item.value" @click="changeType(item.value)" :class="{'active':activeTypeId === item.value}">
      {{item.title}}
    </view>
  </view>
8
  <scroll-view
DCloud_JSON's avatar
DCloud_JSON 已提交
9 10
    ref="conversation-list"
    class="conversation-list"
11
    :class="{canCheck}"
DCloud_JSON's avatar
DCloud_JSON 已提交
12 13
    :scroll-top="listScrollTop"
    scroll-y="true"
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
14
    @scrolltolower="conversation.loading ? '' : loadMore()"
DCloud_JSON's avatar
DCloud_JSON 已提交
15 16
    @scroll="onScroll"
  >
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
17
    <view v-for="(item,index) in conversationList" :key="item.id"
18
      class="conversation-list-item" :class="{'activeConversation':activeConversationId == item.id,'focus':focusConversationId === item.id}"
DCloud_JSON's avatar
DCloud_JSON 已提交
19
    >
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33
      <uni-im-conversation
        :conversation="item" :id="item.id"
        @click="clickItem(item)" @contextmenu.prevent="openConversationMenu($event,index)"
        @longpress="onScroll.isScrolling?'':openConversationMenu($event,index)"
      >
        <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"
              />
            </view>
DCloud_JSON's avatar
DCloud_JSON 已提交
34
          </view>
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
35 36 37
        </template>
      </uni-im-conversation>
    </view>
38 39 40 41
    <view style="margin-top: 5px;backgroundColor:'#f5f5f5'">
      <template v-if="!keyword">
        <uni-im-load-state
          :content-text="loadMoreContentText"
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
42
          :status="conversation.hasMore?'loading':'noMore'"
43 44 45 46 47 48
        />
      </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 已提交
49 50 51 52 53 54 55 56 57 58 59 60

  </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 {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
61
  emits: ['change', 'clickItem', 'onScroll', 'update:checkedList'],
DCloud_JSON's avatar
DCloud_JSON 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
  props: {
    keyword: {
      type: String,
      default: ''
    },
    activeConversationId: {
      type: [String, Boolean],
      default: ''
    },
    canCheck: {
      type: Boolean,
      default: false
    },
    checkedList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      listScrollTop: 0,
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
83 84
      focusConversationId: '',
      activeTypeId: 'all'
DCloud_JSON's avatar
DCloud_JSON 已提交
85 86 87
    }
  },
  computed: {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
88
    conversation:()=>uniIm.conversation,
DCloud_JSON's avatar
DCloud_JSON 已提交
89 90 91 92 93 94 95
    loadMoreContentText() {
      return {
        contentrefresh: "正在加载...",
        contentnomore: (this.conversationList.length ? "没有更多数据了" : "没有会话数据")
      }
    },
    conversationList() {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
96
      const conversationList = uniIm.conversation.dataList
DCloud_JSON's avatar
DCloud_JSON 已提交
97 98 99 100
        // #ifdef H5
        .filter(i => i.title.toLowerCase().includes(this.keyword.toLowerCase()))
        // #endif
        .filter(i => !i.hidden)
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
        .filter(conversation=>{
          if(typeof this.activeTypeId === 'object' && Object.keys(this.activeTypeId)[0] === 'group_type'){
            return conversation.group?.type === this.activeTypeId.group_type
          }else{
            const action = {
              all:()=>true,
              unread:()=>conversation.unread_count > 0 || this.oldUnreadCid?.includes(conversation.id),
              group:()=>conversation.group_id,
              single:()=>!conversation.group_id,
              is_star:()=>conversation.is_star
            }
            return action[this.activeTypeId]?.()
          }
        })
        if(this.activeTypeId === 'unread'){
          this.oldUnreadCid = conversationList?.map(i=>i.id)
        }else{
          this.oldUnreadCid = []
        }
        return conversationList
DCloud_JSON's avatar
DCloud_JSON 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    }
  },
  watch: {
    conversationList: {
      handler() {
        // console.log('更新会话列表',this.conversationList)
        this.$emit('change', this.conversationList)
      },
      deep: true
    },
    activeConversationId(activeConversationId) {
      // #ifdef H5
      // 重试次数
      let tryIndex = 0
      const scrollToCurrentConversation = () => {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
136
        if (!activeConversationId || tryIndex > 5) {
DCloud_JSON's avatar
DCloud_JSON 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
          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()
      }
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
160 161 162
      setTimeout(() => {
        scrollToCurrentConversation()
      },300)
DCloud_JSON's avatar
DCloud_JSON 已提交
163 164 165
      // #endif
    }
  },
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
  async mounted() {
    // 监听鼠标移入conversation-type 后滚动事件,并设置滚动条位置
    // #ifdef H5
    const tagBox = this.$el.querySelector('.conversation-type')
    function scrollFunc(e) {
      tagBox.scrollLeft += e.deltaY/5
    }
    tagBox.addEventListener('mouseover', (e) => {
      tagBox.addEventListener('wheel', scrollFunc)
    })
    tagBox.addEventListener('mouseout', (e) => {
      tagBox.removeEventListener('wheel', scrollFunc)
    })
    // #endif
  },
DCloud_JSON's avatar
DCloud_JSON 已提交
181
  methods: {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
182 183 184 185 186 187 188 189 190 191
    changeType(tagId){
      this.activeTypeId = tagId
      uniIm.conversation.hasMore = true
      uniIm.conversation.loadMore.type = tagId
      this.listScrollTop = ''
      this.$nextTick(()=>{
        this.listScrollTop = 0
      })
      this.loadMore()
    },
DCloud_JSON's avatar
DCloud_JSON 已提交
192 193 194
    onScroll(e) {
      currentScrollTop = e.detail.scrollTop
      this.$emit('onScroll', e)
195 196 197 198 199 200 201
      // 设置 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 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
    },
    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]
221
      this.focusConversationId = conversation.id
DCloud_JSON's avatar
DCloud_JSON 已提交
222
      const myContextmenu = this.$refs['uni-im-contextmenu']
223 224 225 226
      
      const clientY = e.clientY || e.changedTouches[0].clientY
      const clientX = e.clientX || e.changedTouches[0].clientX
      
DCloud_JSON's avatar
DCloud_JSON 已提交
227
      const position = {
228 229
        "top": clientY + 35,
        "left": clientX
DCloud_JSON's avatar
DCloud_JSON 已提交
230 231
      }
      let menuList = [{
232
                        "title": conversation.pinned ? "取消置顶" : "置顶",
DCloud_JSON's avatar
DCloud_JSON 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
                        "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
                            })
                        }
                      },
252 253 254 255 256 257
                      {
                        "title": "标为" + (conversation.unread_count ? "已读" : "未读"),
                        "action": () => {
                          conversation.setUnreadCount(conversation.unread_count ? 0 : 1)
                        }
                      },
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
258 259 260 261 262 263
                      {
                        "title": (conversation.is_star ? "取消" : "设为") + "星标",
                        "action": () => {
                          conversation.changeIsStar()
                        }
                      },
DCloud_JSON's avatar
DCloud_JSON 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
                      {
                        "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(() => {
287
        this.focusConversationId = ''
DCloud_JSON's avatar
DCloud_JSON 已提交
288 289
      })
    },
290 291 292 293
    closeConversationMenu(){
      const myContextmenu = this.$refs['uni-im-contextmenu']
      myContextmenu.closeMe()
    },
DCloud_JSON's avatar
DCloud_JSON 已提交
294 295 296 297 298 299 300 301 302
    async loadMore() {
      let data = await uniIm.conversation.loadMore()
      // console.log('加载到新的会话数据',data);
      return data
    }
  }
}
</script>

303 304
<style lang="scss">
  .uni-im-conversation-list{
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
    height: 0;
    flex: 1;
    overflow: hidden;
    /* #ifdef MP-WEIXIN */
    height: 100%;
    /* #endif */
    .conversation-type {
      margin: 0 10px;
      flex-direction: row;
      overflow: scroll;
      // 滚动条样式,高度设置
      &::-webkit-scrollbar {
        width: 0;
        height: 0;
      }
      .item {
        color: #333;
        text-align: center;
        padding: 5px 0;
        margin:0 15px;
        cursor: pointer;
        &.active {
          color: #1ab94e;
          border-bottom: 2px solid #1ab94e;
        }
      }
    }
    .conversation-list{
      height:0;
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
      flex: 1;
    }
    .tip {
      flex: 1;
    }
    
    .canCheck  {
      .note-box,
      .time,
      .state {
        display: none;
      }
      .title {
        font-size: 16px !important;
      }
    }
    
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
351 352 353 354 355 356 357 358 359 360 361 362
    .conversation-list {
      .conversation-list-item {
        border-radius: 5px;
        overflow: hidden;
        &.focus 
        {
          border: 2px solid #1ab94e;
        }
        &.activeConversation {
          background-color: #f1f1f1;
        }
      }
363 364 365 366 367 368 369 370 371 372 373 374 375 376
    }
    
    .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 已提交
377 378
  }
</style>