chat-filtered.nvue 7.0 KB
Newer Older
DCloud_JSON's avatar
DCloud_JSON 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
<template>
  <!-- 设置导航栏标题,如与谁对话,群人数为几人等 -->
  <!-- #ifndef H5 -->
  <page-meta>
    <navigation-bar :title="navTitle" background-color="#f8f8f8" front-color="#000000" />
  </page-meta>
  <!-- #endif -->
  
  <!-- #ifdef H5 -->
  <view v-if="isWidescreen">
    <text style="position: absolute;z-index: 999;top: -45px;left: 15px;" :selectable="true">{{ navTitle }}</text>
  </view>
  <page-meta v-else>
     <navigation-bar :title="navTitle" background-color="#f8f8f8" front-color="#000000" />
  </page-meta>
  <!-- #endif -->

  <view class="page">
    <view class="head">
      <view v-if="count == 0 && loading" class="hint">正在加载……</view>
      <view v-else class="hint">{{ count }} 条与“{{ keyword }}”相关的聊天记录</view>
      <view
        v-if="conversation_id"
        class="enter-chat"
        @click="onEnterConversation(conversation_id)"
      >
        <uni-icons type="chatbubble" size="16"></uni-icons>
        进入会话
      </view>
    </view>
    <scroll-view
      class="message-list"
      scroll-y
      :scroll-into-view="autoScrollToEl"
      @scrolltoupper="onScrollToUpper"
    >
      <uni-im-msg
        v-for="msg in msgList"
        :key="msg._id"
        :id="`msg-${msg._id}`"
        :msg="msg"
        no-time
        no-jump
        @loadMore="cb => cb()"
      >
        <view class="float-info">
          <view>{{ toFriendlyTime(msg) }}</view>
          <view class="enter-fragment" @click="onOpenFragment(msg)">查看上下文</view>
        </view>
      </uni-im-msg>
      <!-- nvue不支持id选择器,所以需要写两个 -->
      <view id="bottom-el" class="bottom-el"></view>
    </scroll-view>

    <chat-fragment
      v-if="fragment"
      :entry="fragment"
      @close="onCloseFragment"
    />
  </view>
</template>

<script>
/**
 * chat-filtered 组件,渲染一个会话中经过滤选择的消息列表,用于显示某个会话的消息搜索结果。
 * 
 * 点击某条消息的“查看上下文”按钮可以打开 {@link module:chat-fragment} 组件。
 * 
 * @module
 */
  const uniImCo = uniCloud.importObject("uni-im-co", {
    customUI: true
  })
  import uniIm from '@/uni_modules/uni-im/sdk/index.js'
  import ChatFragment from './cmp/chat-fragment'

  export default {
    components: {
      ChatFragment,
    },

    emits: ['to-chat'],

    data() {
      return {
        loading: true,
        count: 0,
        keyword: '',
        msgList: [],
        hasMore: true,
        skip: Number.MAX_SAFE_INTEGER,
        conversation_id: '',
        autoScrollToEl: '',

        // 当前会话对象
        conversation: {},

        // 聊天记录片段的入口消息
        fragment: null,
      };
    },

    computed: {
      ...uniIm.mapState(['isWidescreen']),
      navTitle() {
        let title = this.conversation.title
        if (this.conversation.group_id) {
          title += `(${Object.keys(this.conversation.group_member).length})`;
        }
        return title
      }
    },

    async onLoad(param) {
      // 调用load方法,因为pc宽屏时本页面是以组件形式展示,如 $refs.chatFiltered.load(conversation_id)
      await this.load(param);
    },

    methods: {
      async load({ keyword, count, conversation_id }) {
        // 根据入口参数进行初始化
        this.loading = true
        this.count = count
        this.keyword = keyword
        this.msgList = []
        this.hasMore = true
        this.skip = Number.MAX_SAFE_INTEGER
        this.conversation_id = conversation_id
        this.conversation = await uniIm.conversation.get(conversation_id)
        this.autoScrollToEl = ''
        this.fragment = null

        // 加载第一批匹配的聊天记录
        this.loadData(() => {
          // 自动滚动到底
          this.autoScrollToEl = 'bottom-el'
        })
      },

      async loadData(afterLoaded) {
        this.loading = true
        let result = await uniImCo.getFilteredMessageOfConversation({
          keyword: this.keyword,
          skip: this.skip,
          conversation_id: this.conversation_id,
        })
        this.msgList.unshift(...result.data.reverse())
        if (this.count < this.msgList.length) {
          // 计数以传入的 count 为准,除非实际查询到的更多
          this.count = this.msgList.length
        }
        this.hasMore = result.hasMore
        this.skip = result.skip
        this.loading = false
        this.$nextTick(afterLoaded)
      },

      onScrollToUpper(evt) {
        if (this.loading) return
        if (!this.hasMore) return
        let elId = 'bottom-el'
        if (this.msgList.length > 0) {
          elId = 'msg-' + this.msgList[0]._id
        }
        this.autoScrollToEl = ''
        this.loadData(() => {
          this.autoScrollToEl = elId
        })
      },

      onEnterConversation(conversation_id) {
        this.$emit('to-chat', { conversation_id })
      },

      onOpenFragment(msg) {
        this.fragment = msg
      },

      onCloseFragment() {
        this.fragment = null
      },

      toFriendlyTime(msg) {
        return uniIm.utils.toFriendlyTime(msg.create_time || msg.client_create_time)
      }
    }
  }
</script>

<style lang="scss" scoped>
  /* #ifndef APP-NVUE */
  view {
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
  }

  page {
    background-color: #efefef;
  }
  /* #endif */

  .page {
    /* #ifdef APP-NVUE */
    flex: 1;
    /* #endif */

    /* #ifndef APP-NVUE */
    height: calc(100vh - 45px);
    /* #endif */

    /* #ifndef APP-NVUE */
    background-color: #efefef;
    /* #endif */
    // border: solid 5px #2faf4c;
  }

  /* #ifdef H5 */
  .pc {
    // .pc内的元素只有pc端打开才显示,样式在index页面
    display: none;
  }
  /* #endif */

  .head {
    flex-direction: row;
    justify-content: space-between;
    height: 30px;
    line-height: 30px;
    padding: 0px 15px;
    font-size: 12px;
    border-bottom: 1px solid #ddd;
  }

  .hint {
    color: #999;
  }

  .enter-chat {
    /* #ifdef H5 */
    cursor: pointer;
    /* #endif */
    flex-direction: row;
    padding: 0 5px;
  }
  
  /* #ifdef H5 */
  .enter-chat:hover {
    background-color: #ddd;
  }
  /* #endif */

  .bottom-el {
    height: 1px;
  }

  .message-list {
    /* #ifndef APP-NVUE */
    height: calc(100% - 30px);
    /* #endif */
  }

  .uni-im-msg ::v-deep .msg-content {
    /* #ifndef APP-NVUE */
    width: calc(95% - 40px);
    /* #endif */
  }

  .float-info {
    align-items: flex-end;
    position: absolute;
    top: 0;
    right: 0px;
    font-size: 12px;
    color: #999;
    padding: 10px;
  }

  .enter-fragment {
    /* #ifdef APP-NVUE */
    // 因为nvue不支持 display:none;
    width: 0;
    height: 0;
    /* #endif */
    
    /* #ifdef H5 */
    cursor: pointer;
    /* #endif */
    color: #576b95;
  }
  
  /* #ifdef H5 */
  .enter-fragment:hover {
    color: #7c8cae;
  }

  .uni-im-msg:hover .enter-fragment {
    display: block;
  }
  /* #endif */

  .chat-fragment {
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    background-color: white;
  }
</style>