mirror of
https://git.fightbot.fun/hxuanyu/BingPaper.git
synced 2026-02-15 16:59:32 +08:00
增加多地区每日图片抓取能力
This commit is contained in:
@@ -2,11 +2,12 @@ import { ref, onMounted, watch } from 'vue'
|
||||
import type { Ref } from 'vue'
|
||||
import { bingPaperApi } from '@/lib/api-service'
|
||||
import type { ImageMeta } from '@/lib/api-types'
|
||||
import { getDefaultMkt } from '@/lib/mkt-utils'
|
||||
|
||||
/**
|
||||
* 获取今日图片
|
||||
*/
|
||||
export function useTodayImage() {
|
||||
export function useTodayImage(mkt?: string) {
|
||||
const image = ref<ImageMeta | null>(null)
|
||||
const loading = ref(false)
|
||||
const error = ref<Error | null>(null)
|
||||
@@ -15,7 +16,7 @@ export function useTodayImage() {
|
||||
loading.value = true
|
||||
error.value = null
|
||||
try {
|
||||
image.value = await bingPaperApi.getTodayImageMeta()
|
||||
image.value = await bingPaperApi.getTodayImageMeta(mkt || getDefaultMkt())
|
||||
} catch (e) {
|
||||
error.value = e as Error
|
||||
console.error('Failed to fetch today image:', e)
|
||||
@@ -46,8 +47,9 @@ export function useImageList(pageSize = 30) {
|
||||
const hasMore = ref(true)
|
||||
const currentPage = ref(1)
|
||||
const currentMonth = ref<string | undefined>(undefined)
|
||||
const currentMkt = ref<string | undefined>(getDefaultMkt())
|
||||
|
||||
const fetchImages = async (page = 1, month?: string) => {
|
||||
const fetchImages = async (page = 1, month?: string, mkt?: string) => {
|
||||
if (loading.value) return
|
||||
|
||||
loading.value = true
|
||||
@@ -55,7 +57,8 @@ export function useImageList(pageSize = 30) {
|
||||
try {
|
||||
const params: any = {
|
||||
page,
|
||||
page_size: pageSize
|
||||
page_size: pageSize,
|
||||
mkt: mkt || currentMkt.value || getDefaultMkt()
|
||||
}
|
||||
if (month) {
|
||||
params.month = month
|
||||
@@ -84,7 +87,7 @@ export function useImageList(pageSize = 30) {
|
||||
|
||||
const loadMore = () => {
|
||||
if (!loading.value && hasMore.value) {
|
||||
fetchImages(currentPage.value + 1, currentMonth.value)
|
||||
fetchImages(currentPage.value + 1, currentMonth.value, currentMkt.value)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,7 +95,14 @@ export function useImageList(pageSize = 30) {
|
||||
currentMonth.value = month
|
||||
currentPage.value = 1
|
||||
hasMore.value = true
|
||||
fetchImages(1, month)
|
||||
fetchImages(1, month, currentMkt.value)
|
||||
}
|
||||
|
||||
const filterByMkt = (mkt?: string) => {
|
||||
currentMkt.value = mkt
|
||||
currentPage.value = 1
|
||||
hasMore.value = true
|
||||
fetchImages(1, currentMonth.value, mkt)
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
@@ -106,10 +116,11 @@ export function useImageList(pageSize = 30) {
|
||||
hasMore,
|
||||
loadMore,
|
||||
filterByMonth,
|
||||
filterByMkt,
|
||||
refetch: () => {
|
||||
currentPage.value = 1
|
||||
hasMore.value = true
|
||||
fetchImages(1, currentMonth.value)
|
||||
fetchImages(1, currentMonth.value, currentMkt.value)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -117,7 +128,7 @@ export function useImageList(pageSize = 30) {
|
||||
/**
|
||||
* 获取指定日期的图片
|
||||
*/
|
||||
export function useImageByDate(dateRef: Ref<string>) {
|
||||
export function useImageByDate(dateRef: Ref<string>, mktRef?: Ref<string | undefined>) {
|
||||
const image = ref<ImageMeta | null>(null)
|
||||
const loading = ref(false)
|
||||
const error = ref<Error | null>(null)
|
||||
@@ -126,7 +137,7 @@ export function useImageByDate(dateRef: Ref<string>) {
|
||||
loading.value = true
|
||||
error.value = null
|
||||
try {
|
||||
image.value = await bingPaperApi.getImageMetaByDate(dateRef.value)
|
||||
image.value = await bingPaperApi.getImageMetaByDate(dateRef.value, mktRef?.value || getDefaultMkt())
|
||||
} catch (e) {
|
||||
error.value = e as Error
|
||||
console.error(`Failed to fetch image for date ${dateRef.value}:`, e)
|
||||
@@ -135,10 +146,16 @@ export function useImageByDate(dateRef: Ref<string>) {
|
||||
}
|
||||
}
|
||||
|
||||
// 监听日期变化,自动重新获取
|
||||
watch(dateRef, () => {
|
||||
fetchImage()
|
||||
}, { immediate: true })
|
||||
// 监听日期和地区变化,自动重新获取
|
||||
if (mktRef) {
|
||||
watch([dateRef, mktRef], () => {
|
||||
fetchImage()
|
||||
}, { immediate: true })
|
||||
} else {
|
||||
watch(dateRef, () => {
|
||||
fetchImage()
|
||||
}, { immediate: true })
|
||||
}
|
||||
|
||||
return {
|
||||
image,
|
||||
|
||||
Reference in New Issue
Block a user