uni-im-msg-list.vue 23.2 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1
<template>
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
2 3
  <view class="uni-im-msg-list-root" :class="{'in-topic':filterAsUid.length}">
    <filter-contorl ref="filter-contorl" class="filter-contorl" @change="updateFilterUids" :conversationId="conversationId"></filter-contorl>
4 5 6 7
    <uni-im-list class="uni-im-list" :scrollTop="scrollTop"
      :scroll-into-view="scrollIntoView" ref="uni-im-list"
      @scroll="onScroll" @scrolltolower="onScrollToLower"
    >
8
     <template v-for="(msg,index) in visibleMsgList" :key="msg.unique_id || msg._id">
9
       <view class="uni-im-list-item" :ref="'item-'+index">
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
10 11
         <view v-if="index === 0 && filterAsUid.length === 0" class="data-state-tip-box">
           <uni-im-load-state :status="loadStatus" 
12 13 14
              :contentText='{"contentrefresh": "正在加载历史消息","contentnomore": "没有更多历史消息"}'></uni-im-load-state>
         </view>
         <view :class="['item',msg.type]" :id="'item-'+index" @click="clickItem">
15
           <view class="msg-box" :class="{'active-msg':msg._id === activeMsgId || msg.unique_id === activeMsgId,'pointer':chooseMore}" @click="checkMsg(msg)">
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
16
             <template v-if="chooseMore && !msg.is_revoke">
17 18 19 20
              <checkbox :checked="checkedMsgList.find(i=>i._id == msg._id) != undefined" class="checkbox" />
              <view class="mask"></view>
             </template>
             <!-- <text style="width: 750rpx;text-align: center;border: 1px solid #000;">{{'item-'+index}}</text> -->
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
21
             <uni-im-msg :msg="msg" :id="msg._id" :self="currentUid == msg.from_uid" :index="index"
22
             @putChatInputContent="putChatInputContent" :equalPrevTime="equalPrevTime(index)" 
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
23 24 25
             @showMsgById="showMsgById" @showControl="showControl" @intoTopic="intoTopic"
             @longpressMsgAvatar="longpressMsgAvatar" @retriesSendMsg="retriesSendMsg" 
             @viewMsg="viewMsg" ref="uni-im-msg" class="uni-im-msg"
26 27
             >
             </uni-im-msg>
28
           </view>
29 30
         </view>
       </view>
DCloud_JSON's avatar
DCloud_JSON 已提交
31
     </template>
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
32
     <uni-im-load-state v-if="visibleMsgList.length === 0" :status="loadStatus" class="uni-im-list-item" :contentText='{"contentrefresh": "加载中","contentnomore": "- 没有聊天记录 -"}'></uni-im-load-state>
DCloud_JSON's avatar
DCloud_JSON 已提交
33
    </uni-im-list>
34 35 36 37 38 39 40 41 42 43 44 45 46
    
    <view v-if="hasNewMsg" class="new-msg-bar" @click="showLast">
      <uni-icons type="pulldown" size="18" color="#007fff"></uni-icons>
      <text>有新消息</text>
    </view>
    
    <view style="position: fixed;top: 100px;width: 500rpx;">
      <!-- hasNewMsg:{{hasNewMsg}} -->
      <!-- scrollTop:{{scrollTop}} -->
      <!-- scrollIntoView:{{scrollIntoView}}
			visibleMsgList.length:{{visibleMsgList.length}} -->
			<!-- <button @click="showLast">showLast</button> -->
		</view>
DCloud_JSON's avatar
DCloud_JSON 已提交
47 48 49

    <view v-if="call_list.length" class="showCallMe" @click="showCallMe">@回复我({{call_list.length}})</view>

50
    <uni-popup @change="$event.show?'':closeGroupNotification()" ref="group-notification-popup" type="center" class="group-notification-popup">
DCloud_JSON's avatar
DCloud_JSON 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
      <uni-im-group-notification ref="group-notification"></uni-im-group-notification>
    </uni-popup>

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

<script>
/**
 * uni-im-msg-list 组件,渲染一个会话列表。
 * 
 * 内部使用 {@link module:uni-im-list} 组件实现列表功能,使用 {@link module:uni-im-msg} 组件实现每条消息的渲染。
 * 
 * @module
 * @see module:chat
 * @see module:uni-im-list
 */
  import uniIm from '@/uni_modules/uni-im/sdk/index.js';
  import uniImList from './components/uni-im-list/uni-im-list';
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
70
  import filterContorl from './components/filter-contorl/filter-contorl';
DCloud_JSON's avatar
DCloud_JSON 已提交
71 72 73 74 75 76 77 78

  // 当前页面滚动条高度
  let currentScrollTop = 0
  
  let appearObj = {};

  export default {
    components: {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
79 80
      uniImList,
      filterContorl
DCloud_JSON's avatar
DCloud_JSON 已提交
81
    },
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
82
    emits:['checkedMsgList','update:checkedMsgList','longpressMsgAvatar','showControl','clickItem','retriesSendMsg','putChatInputContent','showMenberList'],
DCloud_JSON's avatar
DCloud_JSON 已提交
83 84
    computed: {
      ...uniIm.mapState(['systemInfo', 'isWidescreen']),
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
85 86
      loadStatus() {
        return this.conversation.msg.hasMore ? 'loading' : 'noMore'
DCloud_JSON's avatar
DCloud_JSON 已提交
87 88
      },
      visibleMsgList() {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
89 90
        const visibleMsgList = this.conversation.msg.visibleDataList()
        // console.log('visibleMsgList',visibleMsgList);
DCloud_JSON's avatar
DCloud_JSON 已提交
91
        this.$nextTick(() => {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
92
          uniIm.utils.throttle(this.setIntersectionObserver, 1000);
DCloud_JSON's avatar
DCloud_JSON 已提交
93
        })
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
94 95 96 97 98
        
        if(this.filterAsUid.length > 0){
          return visibleMsgList.filter(i=>this.filterAsUid.includes(i.from_uid))
        }
        // console.log('visibleMsgList.l',visibleMsgList.length);
DCloud_JSON's avatar
DCloud_JSON 已提交
99
        return visibleMsgList
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
100 101 102 103
      },
      //当前用户自己的uid
      currentUid() {
        return uniIm.currentUser._id;
DCloud_JSON's avatar
DCloud_JSON 已提交
104 105 106 107
      }
    },
    data() {
      return {
108
        conversation: {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
109 110 111 112
          msg: {
            hasMore: true,
            visibleDataList: () => []
          },
113 114 115 116 117
          has_unread_group_notification: false,
          group_info: {
            notification: false
          }
        },
DCloud_JSON's avatar
DCloud_JSON 已提交
118 119 120 121 122
        scrollIntoView: "",
        scrollTop: 0,
        hasNewMsg: false,
        call_list: [],
        activeMsgId: "",
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
123
        filterAsUid:[]
DCloud_JSON's avatar
DCloud_JSON 已提交
124 125 126 127 128 129
      }
    },
    watch: {
      'conversation.call_list'(call_list) {
        this.call_list = call_list
      },
130 131
      'conversation.has_unread_group_notification': {
        async handler(hasUnreadGroupNotification) {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
132
          const group_notification = this.conversation?.group?.notification
133
          const conversation_id = this.conversationId
DCloud_JSON's avatar
DCloud_JSON 已提交
134
          // 弹出群公告
135 136
          if (hasUnreadGroupNotification && group_notification && group_notification.content) {
            await uniIm.utils.sleep(1000)
137 138 139 140
            // TODO 临时解决,公告还没弹出来就切换会话,导致弹出多次
            if(conversation_id !== this.conversationId){
              return
            }
DCloud_JSON's avatar
DCloud_JSON 已提交
141 142 143
            // 判断列表中是否已经渲染了此群公告,是则 call 当前用户。否则弹框提示
            let groupNotificationMsg = [...this.visibleMsgList].reverse().find(msg => msg.action ===
              'update-group-info-notification')
144
            // console.log('groupNotificationMsg', groupNotificationMsg,this.visibleMsgList);
145
            
DCloud_JSON's avatar
DCloud_JSON 已提交
146 147
            if (groupNotificationMsg) {
              this.conversation.call_list.push(groupNotificationMsg._id)
148
              this.closeGroupNotification()
DCloud_JSON's avatar
DCloud_JSON 已提交
149 150
            } else {
              this.$refs["group-notification-popup"].open()
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
151
              setTimeout(() => {
152
                this.$refs["group-notification"].notification = group_notification
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
153
              },0)
DCloud_JSON's avatar
DCloud_JSON 已提交
154 155 156 157
            }
          }
        },
        immediate: true
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
158 159 160 161 162 163
      },
      filterAsUid(filterAsUid,old){
        this.$refs['filter-contorl'].userIdList = filterAsUid
        if(filterAsUid.length === 0 || old.length === 0){
          setTimeout(this.showLast,0)
        }
DCloud_JSON's avatar
DCloud_JSON 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
      }
    },
    props: {
      conversationId: {
        default () {
          return false
        }
      },
      chooseMore: {
        default: false
      },
      checkedMsgList: {
        default () {
          return []
        }
      }
    },
    async mounted() {
    },
    destroyed() {
      // console.log('destroyed')
185 186
      if (this.intersectionObserver){
        this.intersectionObserver.disconnect()
DCloud_JSON's avatar
DCloud_JSON 已提交
187 188 189 190
      }
    },
    methods: {
      async init() {
191 192
        if (this.intersectionObserver){
          this.intersectionObserver.disconnect()
DCloud_JSON's avatar
DCloud_JSON 已提交
193
        }
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
194 195
        this.conversation = uniIm.conversation.find(this.conversationId)
        
DCloud_JSON's avatar
DCloud_JSON 已提交
196 197 198 199 200
        // init data --start
        this.scrollIntoView = ''
        this.scrollTop = 0
        currentScrollTop = 0

DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
201 202 203 204 205
        const showLast = ()=>{
          // #ifdef H5
          // 非web-pc端,list是颠倒的,所以默认显示最后一条。而web-pc端是正常的,所以需要滚动到底部
          if(this.isWidescreen){
            this.showLast()
DCloud_JSON's avatar
DCloud_JSON 已提交
206
          }
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
207
          // #endif
DCloud_JSON's avatar
DCloud_JSON 已提交
208
        }
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
        
        if(this.visibleMsgList.length === 0){
          await this.loadMore(showLast)
        } else {
          showLast()
          setTimeout(async()=>{
            const msgIsOverFlow = await this.msgIsOverFlow()
            if(this.visibleMsgList.length === 0 || !msgIsOverFlow){
             await this.loadMore(showLast)
            }
          },0)
        }
      },
      async msgIsOverFlow(){
        if(this.visibleMsgList.length > 0){
          // 计时
          // console.time('getElInfo')
          let firstMsgTop = (await this.getElInfo('#item-0')).top
          let lastMsgBottom = (await this.getElInfo('#item-' + (this.visibleMsgList.length - 1) )).bottom
          const listElInfo = await this.getElInfo('.uni-im-list')
          // console.timeEnd('getElInfo')
          return firstMsgTop < listElInfo.top || lastMsgBottom > listElInfo.bottom
        }else{
          return true
233
        }
DCloud_JSON's avatar
DCloud_JSON 已提交
234 235 236
      },
      async loadMore(callback) {
        let datas = []
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
237 238 239
        datas = await this.conversation.msg.getMore()
        if(datas.length){
          await this.insertMsg(datas)
DCloud_JSON's avatar
DCloud_JSON 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
        }
        if (typeof callback === 'function') {
          // 为兼容 web pc端特殊场景 不能使用$nextTick
          setTimeout(() => {
            callback(datas)
          }, 0)
        }
        return datas
      },
      msgOnAppear(msgId){
        uniIm.utils.throttle(()=>{
          let index = this.visibleMsgList.findIndex(i => i._id == msgId)
          if (index == -1) {
            return //因为是异步的,可能已经被销毁了替换了新对象
          }
255
          // console.log('msgOnAppear',index);
DCloud_JSON's avatar
DCloud_JSON 已提交
256 257 258 259
        }, 1000);
      },
      async setIntersectionObserver() {
        // console.log('setIntersectionObserver');
260 261 262
        if (this.intersectionObserver) {
          // console.log('this.intersectionObserver存在','执行销毁');
          this.intersectionObserver.disconnect()
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
263
          await uniIm.utils.sleep(1000)
DCloud_JSON's avatar
DCloud_JSON 已提交
264
        }
265 266 267
        
        this.intersectionObserver = uni.createIntersectionObserver(this, { observeAll: true })
          .relativeTo('.uni-im-list', {})
DCloud_JSON's avatar
DCloud_JSON 已提交
268 269 270 271
          .observe('.uni-im-msg', (res) => {
            const msgId = res.id
            const msgRef = this.$refs['uni-im-msg'].find(item => item.msg._id == msgId)
            if (!msgRef) {
272
              // console.error('找不到msgRef,或会话已切走', msgId);
DCloud_JSON's avatar
DCloud_JSON 已提交
273 274
              return
            }
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
            
            // hasBeenDisplayed表示是否已经显示过了
            const hasBeenDisplayed = appearObj[msgId] || false;
            // 新显示的
            const isAppear = res.intersectionRatio > 0 && !hasBeenDisplayed
            
            
            // 是否为最后一条消息
            const isLastMsg = [...this.visibleMsgList].pop()?._id === msgId
            if(isLastMsg){
              this.lastMsgIsShow = isAppear
              if(this.lastMsgIsShow){
                this.hasNewMsg = false
              }
            }
            
            if (isAppear) {
DCloud_JSON's avatar
DCloud_JSON 已提交
292 293 294
              appearObj[msgId] = true;
              msgRef.onAppear()
              this.msgOnAppear(msgId)
295
              
DCloud_JSON's avatar
DCloud_JSON 已提交
296
              // console.error('出现了',msgRef.msg.body)
297 298 299 300
              const isFirstMsg = this.visibleMsgList[0]?._id === msgId
              // 是否为第一个消息
              if (isFirstMsg) {
                // console.log('第一个消息出现在视窗内,加载更多',this.visibleMsgList[0]?._id , msgId);
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
301
                this.loadMore()
302
              }
DCloud_JSON's avatar
DCloud_JSON 已提交
303
              // 调用扩展点,通知消息列表某条消息进入显示区。
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
304
              uniIm.extensions.invokeExts('msg-appear', msgRef.msg)
305
            } else if (!res.intersectionRatio > 0 && hasBeenDisplayed) {
DCloud_JSON's avatar
DCloud_JSON 已提交
306 307 308 309
              appearObj[msgId] = false;
              msgRef.onDisappear()
              // console.error('消失了',msgRef.msg.body)
              // 调用扩展点,通知消息列表某条消息离开显示区。
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
310
              uniIm.extensions.invokeExts('msg-disappear', msgRef.msg)
DCloud_JSON's avatar
DCloud_JSON 已提交
311 312 313 314 315 316 317
            }
          })
      },
      viewMsg(msgList) {
        this.$refs['view-msg'].open(msgList);
      },
      async onScroll(e) {
318 319 320 321 322
        // 记录当前滚动条高度
        currentScrollTop = e.detail.scrollTop
        // console.log('onScroll', e.detail.scrollTop)
        
        // TODO:滚动停止后,将end置为true
DCloud_JSON's avatar
DCloud_JSON 已提交
323 324 325 326 327 328 329 330 331 332 333
        this.onScroll.end = false
        if (this.onScroll.timeoutId) {
          clearTimeout(this.onScroll.timeoutId)
        }
        this.onScroll.timeoutId = setTimeout(() => {
          this.onScroll.end = true
        }, 500)
      },
      async onScrollToLower() {
      },
      async canHoldScrollDo(fn){
334 335 336 337 338
        /**
         * 解决web-pc端,部分情况下插入消息滚动内容会跳动的问题
         */
        // #ifdef H5
        if(this.isWidescreen){
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
339
          if(window.chrome){
340
            // console.error('currentScrollTop',currentScrollTop)
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
341 342 343
            // console.error('this.visibleMsgList.length',this.visibleMsgList.length)
            const msgIsOverFlow = await this.msgIsOverFlow()
            if(msgIsOverFlow && currentScrollTop === 0 && this.visibleMsgList.length != 0){
344
              this.scrollTop = currentScrollTop
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
345
              await this.$nextTick(()=> this.scrollTop = (currentScrollTop + 1) )
346 347 348 349 350
              if(!this.canHoldScrollDo.tryIndex){
                this.canHoldScrollDo.tryIndex = 1
              }else{
                this.canHoldScrollDo.tryIndex ++
              }
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
351 352 353
              if(this.canHoldScrollDo.tryIndex > 10){
                console.warn('canHoldScrollDo tryIndex > 10',this.canHoldScrollDo.tryIndex,currentScrollTop,this.visibleMsgList.length)
                this.canHoldScrollDo.tryIndex = 0
DCloud_JSON's avatar
DCloud_JSON 已提交
354
                fn()
355
              }else{
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
356 357 358
                setTimeout(()=>{
                  this.canHoldScrollDo(fn)
                },300)
DCloud_JSON's avatar
DCloud_JSON 已提交
359
              }
360 361
            }else{
              fn()
DCloud_JSON's avatar
DCloud_JSON 已提交
362
            }
363 364 365 366
          }else{
            const getScrollContentHeight = ()=>document.querySelector('.scroll-content').offsetHeight
            let scrollContent = getScrollContentHeight()
            fn()
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
367 368
            // 下一个事件循环中,重新计算滚动条的高度
            setTimeout(()=>{
369
              const diff = getScrollContentHeight() - scrollContent
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
370
              // console.error( 'diff', diff)
371
              this.scrollTop = currentScrollTop
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
372 373
              this.$nextTick(()=> this.scrollTop = currentScrollTop + diff)
            },0)
DCloud_JSON's avatar
DCloud_JSON 已提交
374
          }
375 376 377 378
          return
        }
        // #endif
        fn()
DCloud_JSON's avatar
DCloud_JSON 已提交
379 380
      },
      async insertMsg(data) {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
381 382 383 384 385 386 387
        return await new Promise((resolve) => {
          this.canHoldScrollDo(async ()=>{
            // 重新获取会话对象,防止web pc端 切换太快引起的会话对象指向错误
            const conversation = await uniIm.conversation.get(data[0].conversation_id)
            conversation.msg.add(data,{unshift:true})
            resolve()
          })
DCloud_JSON's avatar
DCloud_JSON 已提交
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
        })
      },
      equalPrevTime(index) {
        if (index === 0) {
          return false
        } else if (index == this.visibleMsgList.length - 1) {
          return false
        } else {
          const getFriendlyTime = (msg) => {
            return uniIm.utils.toFriendlyTime(msg.create_time || msg.client_create_time)
          }
          return getFriendlyTime(this.visibleMsgList[index]) == getFriendlyTime(this.visibleMsgList[index - 1])
        }
      },
      async showCallMe() {
        let msgId = this.conversation.call_list.pop()
        console.log('msgId', msgId)
        this.showMsgById(msgId)
      },
407
      showLast() {
DCloud_JSON's avatar
DCloud_JSON 已提交
408
        let mLength = this.visibleMsgList.length
409
        this.showMsgByIndex(mLength - 1)
DCloud_JSON's avatar
DCloud_JSON 已提交
410 411 412 413
        this.hasNewMsg = false
      },
      notifyNewMsg() {
        this.hasNewMsg = true
414 415
        // 如果当前在底部,则自动显示最新消息
        if (this.lastMsgIsShow) {
DCloud_JSON's avatar
DCloud_JSON 已提交
416 417 418
          this.showLast()
        }
      },
419
      async getElInfo(select){
DCloud_JSON's avatar
DCloud_JSON 已提交
420 421
        return await new Promise((resolve, rejece) => {
          const query = uni.createSelectorQuery().in(this);
422
          query.select(select).boundingClientRect(data => {
DCloud_JSON's avatar
DCloud_JSON 已提交
423
            if (!data) {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
424
              console.log('找不到 showMsgByIndex:' + select);
425
              return rejece(false)
DCloud_JSON's avatar
DCloud_JSON 已提交
426
            }
427
            resolve(data)
DCloud_JSON's avatar
DCloud_JSON 已提交
428 429 430
          }).exec()
        })
      },
431
      async showMsgByIndex(index) {
DCloud_JSON's avatar
DCloud_JSON 已提交
432 433 434
        if (index == -1) {
          return
        }
435 436 437 438 439 440 441 442 443
        const listHeight = (await this.getElInfo('.uni-im-list')).height
        const targetInfo = await this.getElInfo('#item-' + index)
        const itemScrollTop = targetInfo.top
        // console.error('currentScrollTop',currentScrollTop)
        // console.error('itemScrollTop',itemScrollTop,listHeight,currentScrollTop,index)
        let val = 0;
        let m = listHeight - targetInfo.height
        if(m < 0){
          m = 10
DCloud_JSON's avatar
DCloud_JSON 已提交
444
        }
445 446 447 448 449 450 451
         if(this.isWidescreen){
           val = itemScrollTop + currentScrollTop - 0.5 * m
         }else{
           val = itemScrollTop * -1 + currentScrollTop + 0.3 * m
         }
        
        // console.error('val',val)
DCloud_JSON's avatar
DCloud_JSON 已提交
452 453 454 455 456
        // 赋值为当前滚动条的高度
        this.scrollTop = currentScrollTop
        // 设置一个新值触发视图更新 -> 滚动
        this.$nextTick(async () => {
          this.scrollTop = val
457
          // console.error('currentScrollTop',currentScrollTop)
DCloud_JSON's avatar
DCloud_JSON 已提交
458 459
        })
      },
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
      // 进入话题
      intoTopic(msgId) {
        let currentMsg = this.conversation.msg.find(msgId)
        const getAboutUser = msg => {
          const aboutUid = [msg.from_uid];
          const about_msg_id = msg.about_msg_id
          if(!about_msg_id){
            return aboutUid
          }
          const aboutMsg = this.conversation.msg.find(about_msg_id)
          if(aboutMsg){
            aboutUid.push(...getAboutUser(aboutMsg))
          }
          // 去重
          return [...new Set(aboutUid)]
        }
        
        this.filterAsUid = getAboutUser(currentMsg)
      },
      showMenberList(){
        this.$emit('showMenberList',arguments[0])
      },
DCloud_JSON's avatar
DCloud_JSON 已提交
482 483 484
      async showMsgById(msgId) {
        // 找到消息的索引
        let index = this.visibleMsgList.findIndex(i => i._id == msgId)
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
485
        // 如果找不到,先加载更多,再找
DCloud_JSON's avatar
DCloud_JSON 已提交
486 487 488
        if (index === -1) {
          const {_findIndex} = this.showMsgById
          if (!_findIndex) {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
489
            uni.showLoading();
DCloud_JSON's avatar
DCloud_JSON 已提交
490
            this.showMsgById._findIndex = 0
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
491
          } else if (_findIndex > 9) {
DCloud_JSON's avatar
DCloud_JSON 已提交
492 493
            uni.hideLoading()
            this.showMsgById._findIndex = false
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
494 495 496 497 498
            console.error('防止特殊情况下死循环,不加载10屏以外的引用数据');
            return uni.showToast({
              title: '暂不支持,定向10屏以外的引用消息',
              icon: 'none'
            });
DCloud_JSON's avatar
DCloud_JSON 已提交
499 500
          }
          this.showMsgById._findIndex++
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
501
          console.log('this.showMsgById._findIndex', this.showMsgById._findIndex);
DCloud_JSON's avatar
DCloud_JSON 已提交
502 503 504
          await this.loadMore()
          return await this.showMsgById(msgId)
        }
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
505
        uni.hideLoading()
DCloud_JSON's avatar
DCloud_JSON 已提交
506
        this.activeMsgId = msgId
507 508 509 510
        if(this.showMsgActiveColorActionT){
          clearTimeout(this.showMsgActiveColorActionT)
        }
        this.showMsgActiveColorActionT = setTimeout(() => {
DCloud_JSON's avatar
DCloud_JSON 已提交
511 512 513 514 515 516
          this.activeMsgId = ''
        }, 2000);
        this.showMsgByIndex(index)

        // 如果是显示群公告,则设置未读的群公告内容为 false
        if (this.visibleMsgList[index].action === "update-group-info-notification") {
517
          this.closeGroupNotification()
DCloud_JSON's avatar
DCloud_JSON 已提交
518 519 520
        }

      },
521 522 523 524 525 526 527 528
      closeGroupNotification() {
        // console.log('######关闭群公告',this.conversationId)
        
        const db = uniCloud.database();
        
        db.collection('uni-im-conversation')
        .where({
          id:this.conversationId,
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
529
          user_id: this.currentUid
530 531 532 533 534 535 536 537 538
        })
        .update({
          has_unread_group_notification: false
        }).then(res => {
          this.conversation.has_unread_group_notification = false
          // console.log('关闭群公告成功', res)
        }).catch(err => {
          console.error('关闭群公告失败', err)
        })
DCloud_JSON's avatar
DCloud_JSON 已提交
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
      },
      isChecked(msg) {
        return this.checkedMsgList.some(i => i._id === msg._id)
      },
      checkMsg(msg) {
        if (!this.chooseMore) {
          return
        }
        let checkedMsgList = this.checkedMsgList
        if (this.isChecked(msg)) {
          checkedMsgList.splice(checkedMsgList.findIndex(i => i._id === msg._id), 1)
        } else {
          checkedMsgList.push(msg)
        }
        this.$emit('update:checkedMsgList', checkedMsgList)
      },
      showControl(e) {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
556 557 558 559 560
        if (this.isWidescreen || this.onScroll.end !== false){
          this.$emit('showControl', e)
        }else{
          // console.log('滚动中,不响应长按弹出菜单')
        }
DCloud_JSON's avatar
DCloud_JSON 已提交
561
      },
562 563
      longpressMsgAvatar(e) {
        this.$emit('longpressMsgAvatar', e)
DCloud_JSON's avatar
DCloud_JSON 已提交
564 565 566 567 568 569 570 571 572
      },
      retriesSendMsg(e) {
        this.$emit('retriesSendMsg', e)
      },
      clickItem() {
        this.$emit('clickItem')
      },
      putChatInputContent(msgBody) {
        this.$emit('putChatInputContent', msgBody)
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
573 574 575
      },
      updateFilterUids(uids){
        this.filterAsUid = uids
DCloud_JSON's avatar
DCloud_JSON 已提交
576 577 578 579 580 581
      }
    }
  }

</script>

582 583
<style lang="scss">
.uni-im-msg-list-root{
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
584 585 586 587
  &.in-topic {
    // 显示滚动条时,最外层增加描边
    border: 2px #bee1ff solid;
  }
588
  &,.uni-im-list {
DCloud_JSON's avatar
DCloud_JSON 已提交
589
    background-color: transparent;
590
    height: 100%;
DCloud_JSON's avatar
DCloud_JSON 已提交
591
  }
592 593
  .uni-im-list-item {
    transform: scale(1, -1);
DCloud_JSON's avatar
DCloud_JSON 已提交
594 595 596 597
    .uni-im-msg {
      width: 0;
      flex-grow: 1;
    }
598 599 600 601 602 603 604 605 606 607
  }
  /* #ifdef H5 */
  @media screen and (min-device-width:960px) {
    .uni-im-list-item {
       // 关闭上下翻转
       transform: scale(1, 1);
     }
  }
  /* #endif */
  
608
  
DCloud_JSON's avatar
DCloud_JSON 已提交
609
  .item {
610
    margin:10px 0;
DCloud_JSON's avatar
DCloud_JSON 已提交
611 612
    // border: solid 1px #0055ff;
  }
613 614
  .uni-im-list-item .system .checkbox {
    display: none;
DCloud_JSON's avatar
DCloud_JSON 已提交
615
  }
616
  
DCloud_JSON's avatar
DCloud_JSON 已提交
617 618 619 620 621 622 623 624 625
  .data-state-tip-box {
    // height: 35px;
    align-items: center;
    justify-content: center;
    flex-direction: row;
    color: #999999;
    margin-bottom: -5px;
    margin-top: 10px;
  }
626
  
DCloud_JSON's avatar
DCloud_JSON 已提交
627 628 629 630 631 632 633
  .data-state-tip-text {
    height: 36px;
    line-height: 36px;
    font-size: 12px;
    margin: 0 5px;
    color: #999999;
  }
634
  
DCloud_JSON's avatar
DCloud_JSON 已提交
635 636 637 638 639
  /* #ifdef H5 */
  .loadMore-btn {
    font-size: 14px;
    color: #666;
  }
640
  
DCloud_JSON's avatar
DCloud_JSON 已提交
641 642 643
  .loadMore-btn::after {
    display: none;
  }
644
  
DCloud_JSON's avatar
DCloud_JSON 已提交
645 646 647 648 649 650 651
  /* #endif */
  .msg-box {
    position: relative;
    transition-property: background-color;
    transition-duration: 2s;
    flex-direction: row;
  }
652
  
DCloud_JSON's avatar
DCloud_JSON 已提交
653 654 655 656
  .msg-box .checkbox {
    margin: 30px 0 0 10px;
    // transform: translateX(20px);
  }
657
  
DCloud_JSON's avatar
DCloud_JSON 已提交
658 659 660 661 662 663 664 665
  .msg-box .mask {
    position: absolute;
    top: 0;
    left: 0;
    z-index: 999;
    width: 100%;
    height: 100%;
  }
666
  
DCloud_JSON's avatar
DCloud_JSON 已提交
667 668 669
  .active-msg {
    background-color: #f9f9f9;
  }
670
  
DCloud_JSON's avatar
DCloud_JSON 已提交
671 672 673 674 675 676 677 678 679 680 681 682
  .showCallMe {
    background-color: #62caf8;
    border-radius: 50px;
    padding: 2px 15px;
    font-size: 12px;
    color: #FFF;
    position: fixed;
    right: 5px;
    top: 10px;
    /* #ifdef H5 */
    top: 55px;
    cursor: pointer;
683
  
DCloud_JSON's avatar
DCloud_JSON 已提交
684 685 686 687 688
    @media screen and (min-device-width:960px) {
      top: calc(7vh + 20px);
      right: 30px;
      font-size: 16px;
    }
689
  
DCloud_JSON's avatar
DCloud_JSON 已提交
690 691
    /* #endif */
  }
692
  
DCloud_JSON's avatar
DCloud_JSON 已提交
693 694 695
  .group-notification-popup {
    z-index: 9999;
  }
696
  
DCloud_JSON's avatar
DCloud_JSON 已提交
697 698 699 700
  .new-msg-bar {
    position: absolute;
    display: flex;
    flex-direction: row;
DCloud_JSON's avatar
DCloud_JSON 已提交
701
    align-items: flex-start;
702
    right: 40px;
DCloud_JSON's avatar
DCloud_JSON 已提交
703 704 705
    bottom: 10px;
    font-size: 12px;
    background-color: white;
706
    color: #007fff;
DCloud_JSON's avatar
DCloud_JSON 已提交
707
    padding: 5px 8px 5px 5px;
708
    border-radius: 15px 15px 15px 15px;
DCloud_JSON's avatar
DCloud_JSON 已提交
709 710 711 712 713
    /* #ifdef H5 */
    pointer-events: auto;
    cursor: pointer;
    /* #endif */
  }
714 715 716 717 718
  /* #ifdef H5 */
  .pointer {
    cursor: pointer;
  }
  /* #endif */
719
}
DCloud_JSON's avatar
DCloud_JSON 已提交
720
</style>