83 lines
2.2 KiB
JavaScript
83 lines
2.2 KiB
JavaScript
import { ref } from 'vue'
|
||
import { getActivityListApi, getMintingActivitiesApi } from '@/utils/api.js'
|
||
import { getPreloadApi } from '@/utils/preloadApi/index'
|
||
|
||
/**
|
||
* 模块级共享状态(单例)
|
||
*/
|
||
const bannerActivities = ref([])
|
||
const banners = ref([])
|
||
// 加载态:用于骨架屏展示(任一 fetch 未结束均为 true)
|
||
const loading = ref(false)
|
||
|
||
// 内部:优先走预拉缓存,未命中/过期则手动 fetch
|
||
async function _tryPreloadThenFetch(key, fallbackFetch, processFn) {
|
||
const api = getPreloadApi()
|
||
if (api) {
|
||
try {
|
||
const cached = await api.get(key)
|
||
if (cached && cached.code === 0) {
|
||
processFn(cached)
|
||
return
|
||
}
|
||
} catch (e) { /* 未命中/过期,走手动 fetch */ }
|
||
}
|
||
const res = await fallbackFetch()
|
||
if (res && res.code === 0) processFn(res)
|
||
}
|
||
|
||
const loadBannerActivities = async () => {
|
||
loading.value = true
|
||
try {
|
||
await _tryPreloadThenFetch('banner.activities',
|
||
() => getActivityListApi(uni.getStorageSync('star_id') || null, 1, 10),
|
||
(res) => {
|
||
if (res.data?.activities) {
|
||
bannerActivities.value = res.data.activities.filter(item => item.status !== 'expired')
|
||
}
|
||
}
|
||
)
|
||
} catch (e) {
|
||
console.error('[useBanner] 加载 banner 活动失败', e?.message ?? e)
|
||
} finally {
|
||
loading.value = false
|
||
}
|
||
}
|
||
|
||
const loadBanners = async () => {
|
||
loading.value = true
|
||
try {
|
||
await _tryPreloadThenFetch('banner.minting',
|
||
() => getMintingActivitiesApi(null, 1, 10),
|
||
(res) => {
|
||
if (res.data?.activities) {
|
||
banners.value = res.data.activities.map(activity => ({
|
||
id: activity.id,
|
||
image_url: activity.cover_image,
|
||
title: activity.title,
|
||
link_type: 'activity',
|
||
link_value: String(activity.id),
|
||
description: activity.description,
|
||
route: activity.route,
|
||
params: activity.params
|
||
}))
|
||
}
|
||
}
|
||
)
|
||
} catch (e) {
|
||
console.error('[useBanner] 加载运营 banner 失败', e?.message ?? e)
|
||
} finally {
|
||
loading.value = false
|
||
}
|
||
}
|
||
|
||
export function useBanner() {
|
||
return {
|
||
bannerActivities,
|
||
banners,
|
||
loading,
|
||
loadBannerActivities,
|
||
loadBanners
|
||
}
|
||
}
|