uni-im-conversation.vue 2.7 KB
Newer Older
1 2 3
<template>
  <uni-im-info-card
    :id="conversation.id"
DCloud_JSON's avatar
DCloud_JSON 已提交
4
    @click="handleClick"
5 6
    :title="conversation.title"
    :note="conversation.note"
7
    :red-note="conversation.hasDraft?'[草稿]&nbsp;':''"
8 9 10 11 12 13
    :tags="conversation.tags"
    :avatarUrl="avatarUrl"
    :time="friendlyTime"
    :badge="conversation.unread_count"
    :mute="conversation.mute"
    :pinned="conversation.pinned"
DCloud_JSON's avatar
DCloud_JSON 已提交
14
    link
15 16 17 18 19 20 21 22 23 24
  >
    <template #left>
      <slot name="left"></slot>
    </template>
    <template #avatar-overlay-list>
      <template v-for="overlay in avatarOverlayList" :key="overlay.component.name">
        <component :is="overlay.component" v-bind="overlay.props" cementing="ConversationAvatarOverlay" />
      </template>
    </template>
  </uni-im-info-card>
DCloud_JSON's avatar
DCloud_JSON 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
</template>

<script>
  import uniIm from '@/uni_modules/uni-im/sdk/index.js';
  import {
    markRaw
  } from "vue";
  export default {
    name: 'UniImConversation',
    props: {
      conversation: {
        type: Object,
        default: () => {}
      }
    },
40
    emits: ['click'],
DCloud_JSON's avatar
DCloud_JSON 已提交
41
    computed: {
42 43 44 45 46 47 48 49 50 51
      friendlyTime() {
        // 使得时间会随着心跳动态更新
        let timestamp = this.conversation.time + uniIm.heartbeat * 0
        let friendlyTime = uniIm.utils.toFriendlyTime(timestamp)
        // console.log('friendlyTime',friendlyTime);
        let friendlyTimeArr = friendlyTime.split(' ')
        let friendlyTimeArrL = friendlyTimeArr.length
        // 如果含 年/月(不在3天内,且不是同一周),去掉时间
        if (friendlyTimeArrL == 3 && friendlyTime.includes('/')) {
          friendlyTime = friendlyTimeArr[0]
DCloud_JSON's avatar
DCloud_JSON 已提交
52
        }
53
        return friendlyTime
DCloud_JSON's avatar
DCloud_JSON 已提交
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
      }
    },
    data() {
      // 调用扩展点,扩展程序可以为该会话增加覆盖的图标元素。
      let avatarOverlayList = uniIm.extensions
        .invokeExts("conversation-avatar-overlay", this.conversation)
        .filter((result) => result && result.component)
        .map((result) => {
          return {
            component: markRaw(result.component),
            props: result.props || {},
            handlers: result.handlers || {},
          };
        });
      return {
        avatarUrl: "/uni_modules/uni-im/static/avatarUrl.png",
        avatarOverlayList
      };
    },
    watch: {
      'conversation.avatar_file': {
        async handler(avatar_file) {
          if (typeof avatar_file == 'object' && avatar_file.url) {
            this.avatarUrl = await uniIm.utils.getTempFileURL(avatar_file.url);
          }
        },
        immediate: true,
      },
    },
    methods: {
      handleClick() {
        this.$emit('click', this.conversation)
      }
    }
  };
</script>