chat-filtered.vue 6.1 KB
Newer Older
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
1
  <template>
2
  <view class="chat-filtered">
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
3 4 5 6 7
    <!-- #ifdef H5 -->
    <!-- web-pc端会话标题 设置导航栏标题,如与谁对话,群人数为几人等 -->
    <text v-if="isWidescreen" id="web-pc-chat-title" :selectable="true">{{navTitle}}</text>
    <!-- #endif -->
    
8
    <view class="head">
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
9 10 11 12
      <view class="hint">
        <template v-if="count == 0 && loading">正在加载……</template>
        <template v-else>{{ count }} 条与“{{ keyword }}”相关的聊天记录</template>
      </view>
13 14 15 16 17 18 19 20 21
      <view
        v-if="conversation_id"
        class="enter-chat"
        @click="onEnterConversation(conversation_id)"
      >
        <uni-icons type="chatbubble" size="16"></uni-icons>
        进入会话
      </view>
    </view>
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
22
    
23 24 25 26 27 28
    <scroll-view
      class="message-list"
      scroll-y
      :scroll-into-view="autoScrollToEl"
      @scrolltoupper="onScrollToUpper"
    >
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
      <view>
        <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>
        <view id="bottom-el" style="height: 1px;"></view>
      </view>
46
    </scroll-view>
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
47
    
48 49 50 51 52
    <chat-fragment
      v-if="fragment"
      :entry="fragment"
      @close="onCloseFragment"
    />
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
53
</view>
54 55 56 57 58 59 60 61 62 63 64 65 66 67
</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'
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
68
  import ChatFragment from './components/chat-fragment'
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

  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) {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
96
          title += `(${this.conversation.group.member.count()})`;
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
        }
        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>

170 171 172
<style lang="scss">
@import "@/uni_modules/uni-im/common/baseStyle.scss";
.chat-filtered {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
173
  height: 0;
174
  flex: 1;
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
175
  flex-grow: 1 !important;
176
  background-color: #efefef;
177 178 179 180 181 182 183 184
  .head {
    flex-direction: row;
    justify-content: space-between;
    height: 30px;
    line-height: 30px;
    padding: 0px 15px;
    font-size: 12px;
    border-bottom: 1px solid #ddd;
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197
    .hint {
      color: #999;
    }
    .enter-chat {
      flex-direction: row;
      padding: 0 5px;
      /* #ifdef H5 */
      cursor: pointer;
      &:hover {
        background-color: #ddd;
      }
      /* #endif */
    }
198
  }
199
  
200
  .message-list {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
201 202
    height: 1px;
    flex: 1;
203
  }
204
  
205 206 207
  .uni-im-msg ::v-deep .msg-content {
    width: calc(95% - 40px);
  }
208
  
209 210 211 212 213 214 215 216 217
  .float-info {
    align-items: flex-end;
    position: absolute;
    top: 0;
    right: 0px;
    font-size: 12px;
    color: #999;
    padding: 10px;
  }
218
  
219 220 221 222 223 224 225 226 227 228 229
  .enter-fragment {
    /* #ifdef H5 */
    cursor: pointer;
    /* #endif */
    color: #576b95;
  }
  
  /* #ifdef H5 */
  .enter-fragment:hover {
    color: #7c8cae;
  }
230
  
231 232 233 234
  .uni-im-msg:hover .enter-fragment {
    display: block;
  }
  /* #endif */
235
  
236 237 238 239 240 241 242 243
  .chat-fragment {
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    background-color: white;
  }
244
}
245
</style>