uni-im-friendly-time.vue 1.4 KB
Newer Older
DCloud_JSON's avatar
3.4.31  
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
<template>
  <view class="format-time-text" @click="onclick">
    <text class="text">{{friendlyTime}}</text>
    <text class="text detail" v-if="showDetail">({{timeString}})</text>
  </view>
</template>

<script>
  import uniIm from '@/uni_modules/uni-im/sdk/index.js';
  export default {
    props: {
      time: {
        type: Number,
        default: 0
      },
    },
    computed: {
      friendlyTime() {
        let time = this.time
        // 使得时间会随着心跳动态更新
        time = time + uniIm.heartbeat * 0
        return uniIm.utils.toFriendlyTime(time)
      },
    },
    data() {
      return {
        timeString: new Date(this.time).toLocaleString(),
        showDetail: false
      }
    },
    mounted() {
      // #ifdef H5
      // 鼠标移入移出事件 设置显示时间
      this.$el.addEventListener('mouseenter', () => {
        this.showDetail = true
      })
      this.$el.addEventListener('mouseleave', () => {
        this.showDetail = false
      })
      // #endif
    },
    methods: {
      onclick() {
        this.showDetail = true
        setTimeout(() => {
          this.showDetail = false
        }, 2000)
      }
    }
  }
</script>

<style lang="scss">
.format-time-text {
  flex-direction: row !important;
  justify-content: center;
  align-items: center;
  .text {
    font-size: 12px;
    text-align: center;
    color: #999999;
    line-height: 22px;
  }
  .detail {
    margin-left: 5px;
  }
}
</style>