uni-im-conversation.vue 2.9 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"
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
7
    :red-note="redNote"
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
3.4.31  
DCloud_JSON 已提交
14
    :is_star="conversation.is_star"
DCloud_JSON's avatar
DCloud_JSON 已提交
15
    link
16 17 18 19 20 21 22 23 24 25
  >
    <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 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
</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: () => {}
      }
    },
41
    emits: ['click'],
DCloud_JSON's avatar
DCloud_JSON 已提交
42
    computed: {
DCloud_JSON's avatar
3.4.31  
DCloud_JSON 已提交
43 44 45 46 47 48 49 50
      redNote() {
        if (this.conversation.hasDraft){
          return '[草稿]'
        }else if (this.conversation.call_list.length){
          return '[@我]'
        }
        return ''
      },
51 52 53 54 55 56 57 58 59 60
      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 已提交
61
        }
62
        return friendlyTime
DCloud_JSON's avatar
DCloud_JSON 已提交
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
      }
    },
    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>