fix(pool): 搞了个缓存

This commit is contained in:
BTMuli
2023-04-03 13:28:53 +08:00
parent 25efdb398e
commit cd54e4a80e
3 changed files with 56 additions and 13 deletions

View File

@@ -56,17 +56,22 @@
// vue
import { ref, onMounted } from "vue";
import { useRouter } from "vue-router";
// store
import useHomeStore from "../store/modules/home";
// utils
import { createTGWindow } from "../utils/TGWindow";
// plugins
import MysOper from "../plugins/Mys";
// interface
import { GachaCard } from "../plugins/Mys/interface/gacha";
import { GachaCard, GachaData } from "../plugins/Mys/interface/gacha";
import { Map } from "../interface/Base";
// vue
const router = useRouter();
// store
const homeStore = useHomeStore();
// loading
const loading = ref(true as boolean);
@@ -87,7 +92,16 @@ onMounted(async () => {
await console.error("获取限时祈愿数据失败");
return;
}
poolCards.value = await MysOper.Gacha.card(gachaData);
if (!checkCover(gachaData)) {
poolCards.value = await MysOper.Gacha.card(gachaData);
let coverData: Map<string> = {};
poolCards.value.map(pool => {
coverData[pool.post_id] = pool.cover;
});
homeStore.poolCover = coverData;
} else {
poolCards.value = await MysOper.Gacha.card(gachaData, homeStore.poolCover);
}
poolCards.value.map(pool => {
poolTimeGet.value[pool.post_id] = getLastPoolTime(pool.time.end_stamp - Date.now());
poolTimePass.value[pool.post_id] = pool.time.end_stamp - Date.now();
@@ -102,6 +116,25 @@ onMounted(async () => {
loading.value = false;
});
// 检测是否有新的限时祈愿
function checkCover(data: GachaData[]) {
// 如果没有缓存
if (!homeStore.poolCover || Object.keys(homeStore.poolCover).length === 0) {
return false;
}
// 获取缓存
const cover = homeStore.poolCover;
return data.every(item => {
const post_id = item.activity_url.split("/").pop();
if (!post_id || isNaN(Number(post_id))) {
return false;
}
return (
cover[Number(post_id)] !== undefined && cover[Number(post_id)] !== "/source/UI/empty.webp"
);
});
}
function toOuter(url: string, title: string) {
createTGWindow(url, "祈愿", title, 1200, 800, true);
}
@@ -114,14 +147,12 @@ function getLastPoolTime(time: number) {
}
async function toPost(pool: GachaCard) {
// 获取路由路径
const path = router.resolve({
name: "帖子详情",
params: {
post_id: pool.post_id.toString(),
},
}).href;
// 打开新窗口
createTGWindow(path, "限时祈愿", pool.title, 960, 720, false);
}
</script>

View File

@@ -7,27 +7,37 @@
import { getPostData } from "../request/post";
import { GachaCard, GachaData } from "../interface/gacha";
import { Map } from "../../../interface/Base";
/**
* @description 根据卡池信息转为渲染用的卡池信息
* @since Alpha v0.1.2
* @param {GachaData[]} gachaData 卡池信息
* @param {Map<string>} poolCover 卡池封面
* @return {Promise<GachaCard[]>}
*/
export async function getGachaCard(gachaData: GachaData[]): Promise<GachaCard[]> {
export async function getGachaCard(
gachaData: GachaData[],
poolCover: Map<string> | undefined = undefined
): Promise<GachaCard[]> {
const gachaCard: GachaCard[] = [];
await Promise.allSettled(
gachaData.map(async (data: GachaData) => {
let cover = "/source/UI/empty.webp";
const post_id: number | undefined = Number(data.activity_url.split("/").pop()) || undefined;
if (post_id === undefined) {
if (post_id === undefined || isNaN(post_id)) {
throw new Error("无法获取帖子 ID");
}
let cover = "/source/UI/empty.webp";
try {
const post = await getPostData(post_id);
cover = post.cover?.url || post.post.images[0];
} catch (error) {
await console.error(error);
if (poolCover !== undefined) {
cover = poolCover[post_id];
} else {
try {
await console.log("调用 getPostData");
const post = await getPostData(post_id);
cover = post.cover?.url || post.post.images[0];
} catch (error) {
await console.error(error);
}
}
return gachaCard.push({
title: data.title,

View File

@@ -6,6 +6,7 @@
*/
import { defineStore } from "pinia";
import { Map } from "../../interface/Base";
const useHomeStore = defineStore({
id: "home",
@@ -23,6 +24,7 @@ const useHomeStore = defineStore({
show: true,
order: 2,
},
poolCover: {} as Map<string>,
};
},
actions: {
@@ -40,6 +42,7 @@ const useHomeStore = defineStore({
show: true,
order: 2,
},
poolCover: {},
};
},
getShowItem() {
@@ -47,7 +50,6 @@ const useHomeStore = defineStore({
defaultList.sort((a, b) => {
return this.getItemOrder(a) - this.getItemOrder(b);
});
console.info("getShowItem", defaultList);
return defaultList;
},
getShowValue() {