feat: upvote_time

This commit is contained in:
KUN1007 2023-10-02 14:56:21 +08:00
parent 233db9288d
commit 5324186cad
6 changed files with 38 additions and 6 deletions

View file

@ -40,6 +40,7 @@ export interface HomeTopic {
popularity: number
user: HomeUserInfo
status: number
upvote_time: number
}
// 首页响应数据的格式

View file

@ -30,6 +30,7 @@ export interface TopicDetail {
share: number[]
category: string[]
popularity: number
upvote_time: number
}
// 更新话题的请求数据

21
src/utils/time.ts Normal file
View file

@ -0,0 +1,21 @@
/**
*
*/
import dayjs from 'dayjs'
export const hourDiff = (upvote_time: number, hours: number) => {
// 检查 upvote_time 是否有效,因为后端初始化为 0
if (upvote_time === 0 || upvote_time === undefined) {
return false
}
// 获取当前时间
const currentTime = dayjs()
// 获取 upvote_time这里假设 upvote_time 是一个 UNIX 时间戳
const upvoteTime = dayjs(upvote_time)
// 计算时间差并返回比较结果
return currentTime.diff(upvoteTime, 'hour') <= hours
}

View file

@ -1,6 +1,8 @@
<script setup lang="ts">
import { ref, watch, onMounted, onBeforeUnmount, onBeforeMount } from 'vue'
import SingleTopic from './SingleTopic.vue'
//
import { hourDiff } from '@/utils/time'
import { HomeTopic } from '@/api'
@ -93,11 +95,13 @@ onBeforeUnmount(() => {
<template>
<div class="topic-container" ref="content">
<TransitionGroup name="list" tag="div">
<!-- 热度 > 100 则话题处于被推状态 -->
<!-- 被推时间在 10h 之内 -->
<div
v-for="topic in topics"
:key="topic.tid"
:class="topic.popularity >= 100 ? 'kungalgame-comet-surround' : ''"
:class="
hourDiff(topic.upvote_time, 10) ? 'kungalgame-comet-surround' : ''
"
>
<span></span>
<span></span>

View file

@ -42,7 +42,8 @@ onMounted(async () => {
<div class="title">{{ kun.title }}</div>
<div class="hot">
<Icon icon="bi:fire" />
<span>{{ kun.popularity }}</span>
<!-- 后端计算小数不精确一律 ceil -->
<span>{{ Math.ceil(kun.popularity) }}</span>
</div>
</div>
</router-link>

View file

@ -18,6 +18,9 @@ import Tags from '../components/Tags.vue'
import { TopicDetail } from '@/api/topic/types/topic'
import { computed } from 'vue'
//
import { hourDiff } from '@/utils/time'
const topicData = defineProps<{
topicData: TopicDetail
}>()
@ -38,13 +41,14 @@ const {
status,
share,
category,
popularity,
// popularity,
upvote_time,
} = topicData.topicData
//
const loliStatus = computed(() => {
// > 100
if (popularity >= 100) {
// 10h
if (hourDiff(upvote_time, 10)) {
return 'featured'
}