Skip to content

Commit

Permalink
fix: 页面刷新请求问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Plumbiu committed Feb 22, 2023
1 parent 2aa2e4c commit 6a6bd1b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 37 deletions.
17 changes: 17 additions & 0 deletions frontend/components/ArticlesList/Item/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@ import type { Ref } from 'vue'
import type { IArticleItem } from '~~/types/IArticleItem'
const artlist = inject<Ref<IArticleItem[]>>('artlist')
const [parent] = useAutoAnimate()
const formatTime = (createdAt: string): string => {
const created = new Date(createdAt)
const now = new Date()
const duration = (now.getTime() - created.getTime()) / 1000 / 60
let ans = '刚刚'
if (duration > 0 && duration < 60) // 一小时内
ans = `${(duration).toFixed(0)}分钟前`
else if (duration < 60 * 24) // 一天内
ans = `${(duration / 60).toFixed(0)}小时前`
else if (duration < 60 * 24 * 30) // 一个月内
ans = `${(duration / 60 / 24).toFixed(0)}天前`
else if (duration < 60 * 24 * 30 * 365) // 一年内
ans = `${(duration / 60 / 24 / 30).toFixed(0)}月前`
else if (duration >= 60 * 24 * 30 * 365) // 超过一年
ans = `${(duration / 60 / 24 / 30 / 365).toFixed(0)}年前`
return ans
}
const hideHandler = (id: string) => {
artlist && (artlist.value = artlist.value.filter(item => item.id !== id))
}
Expand Down
20 changes: 11 additions & 9 deletions frontend/components/ArticlesList/index.vue
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
<script setup lang="ts">
import type { IArticleItem } from '~~/types/IArticleItem'
const artlist = useState<IArticleItem[]>('artlist', () => [])
const { data: articleAds } = (await useFetch('/api/global/ad'))
const isLoading = useState('isLoading', () => false)
const route = useRoute()
let pagenum = 1
const { data: artlist, pending } = await useFetch<IArticleItem[]>(() => `/api/articles/list?sort=${route.query?.sort || 'recommended'}&type=${route?.params?.type || ''}&pageNum=1&tag=${route?.params?.tag || ''}`)
const addArtListItem = useThrottle(async () => {
useScrollBottom() && artlist.value?.length && artlist.value.push(...(await useFetchPostData(route?.params, route.query?.sort, ++pagenum)))
if (useScrollBottom() && artlist.value != null) {
const { data } = await useFetch<IArticleItem[]>(`/api/articles/list?sort=${route.query?.sort || 'recommended'}&type=${route?.params?.type || ''}&pageNum=${++pagenum}&tag=${route?.params?.tag || ''}`)
if (!data.value)
return
artlist.value.push(...data.value)
}
})
watch(route, () => {
pagenum = 1
}, { immediate: true, deep: true })
provide('artlist', artlist)
provide('ads', articleAds)
watch([() => route.query, () => route.params], async () => {
isLoading.value = true
artlist.value = await useFetchPostData(route?.params, route.query?.sort, pagenum = 1)
isLoading.value = false
}, { deep: true, immediate: true })
onMounted(() => {
(window as any).addEventListener('scroll', addArtListItem)
})
Expand All @@ -30,7 +32,7 @@ onUnmounted(() => {
<template #fallback>
<ArticlesListUiSkeleton />
</template>
<ArticlesListUiSkeleton v-if="isLoading || !artlist?.length" />
<ArticlesListUiSkeleton v-if="pending || !artlist?.length" />
<ArticlesListItem v-else />
</ClientOnly>
</div>
Expand Down
28 changes: 0 additions & 28 deletions frontend/composables/Articlelist/useArtlistFn.ts

This file was deleted.

0 comments on commit 6a6bd1b

Please sign in to comment.