feat: upvote_time
This commit is contained in:
parent
2fdc26d241
commit
232dc1e0bf
|
@ -40,6 +40,7 @@ export interface HomeTopic {
|
|||
popularity: number
|
||||
user: HomeUserInfo
|
||||
status: number
|
||||
upvote_time: number
|
||||
}
|
||||
|
||||
// 首页响应数据的格式
|
||||
|
|
|
@ -30,6 +30,7 @@ export interface TopicDetail {
|
|||
share: number[]
|
||||
category: string[]
|
||||
popularity: number
|
||||
upvote_time: number
|
||||
}
|
||||
|
||||
// 更新话题的请求数据
|
||||
|
|
21
src/utils/time.ts
Normal file
21
src/utils/time.ts
Normal 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
|
||||
}
|
|
@ -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>
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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'
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue