mirror of
https://github.com/BTMuli/TeyvatGuide.git
synced 2025-12-19 10:23:21 +08:00
♻️ 首页组件加载逻辑重构
This commit is contained in:
@@ -17,8 +17,8 @@
|
|||||||
<div class="home-select">
|
<div class="home-select">
|
||||||
<v-select
|
<v-select
|
||||||
variant="outlined"
|
variant="outlined"
|
||||||
v-model="showHome"
|
v-model="showItems"
|
||||||
:items="homeStore.getShowItems()"
|
:items="showItemsAll"
|
||||||
:hide-details="true"
|
:hide-details="true"
|
||||||
:multiple="true"
|
:multiple="true"
|
||||||
:chips="true"
|
:chips="true"
|
||||||
@@ -32,7 +32,7 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import { onMounted, onUnmounted, ref, shallowRef, toRaw } from "vue";
|
import { type Component, computed, onMounted, ref, shallowRef, watch } from "vue";
|
||||||
|
|
||||||
import TGameNav from "../../components/app/t-gamenav.vue";
|
import TGameNav from "../../components/app/t-gamenav.vue";
|
||||||
import showLoading from "../../components/func/loading.js";
|
import showLoading from "../../components/func/loading.js";
|
||||||
@@ -41,33 +41,55 @@ import PhCompCalendar from "../../components/pageHome/ph-comp-calendar.vue";
|
|||||||
import PhCompPool from "../../components/pageHome/ph-comp-pool.vue";
|
import PhCompPool from "../../components/pageHome/ph-comp-pool.vue";
|
||||||
import PhCompPosition from "../../components/pageHome/ph-comp-position.vue";
|
import PhCompPosition from "../../components/pageHome/ph-comp-position.vue";
|
||||||
import { useAppStore } from "../../store/modules/app.js";
|
import { useAppStore } from "../../store/modules/app.js";
|
||||||
import { useHomeStore } from "../../store/modules/home.js";
|
import { ShowItemEnum, useHomeStore } from "../../store/modules/home.js";
|
||||||
import TGLogger from "../../utils/TGLogger.js";
|
import TGLogger from "../../utils/TGLogger.js";
|
||||||
import TGConstant from "../../web/constant/TGConstant.js";
|
import TGConstant from "../../web/constant/TGConstant.js";
|
||||||
|
|
||||||
// store
|
type SFComp = Component & {
|
||||||
|
__file?: string;
|
||||||
|
__hmrId?: string;
|
||||||
|
__name?: string;
|
||||||
|
__scopeId?: string;
|
||||||
|
};
|
||||||
|
|
||||||
const appStore = useAppStore();
|
const appStore = useAppStore();
|
||||||
const homeStore = useHomeStore();
|
const homeStore = useHomeStore();
|
||||||
|
|
||||||
// data
|
const showItemsAll: Array<ShowItemEnum> = [
|
||||||
const endNum = ref<number>(0);
|
ShowItemEnum.calendar,
|
||||||
const components = shallowRef<any[]>([]);
|
ShowItemEnum.pool,
|
||||||
const showHome = ref<string[]>(homeStore.getShowValue());
|
ShowItemEnum.position,
|
||||||
const loadComp = ref<string[]>(toRaw(showHome.value));
|
];
|
||||||
|
const showItems = computed<ShowItemEnum[]>({
|
||||||
|
get: () => homeStore.getShowItems(),
|
||||||
|
set: (v: ShowItemEnum[]) => homeStore.setShowItems(v),
|
||||||
|
});
|
||||||
|
const loadItems = shallowRef<ShowItemEnum[]>([]);
|
||||||
|
const components = shallowRef<SFComp[]>([]);
|
||||||
|
|
||||||
const gameSelectList = TGConstant.BBS.CHANNELS;
|
const gameSelectList = TGConstant.BBS.CHANNELS;
|
||||||
const curGid = ref<string>(gameSelectList[0].gid);
|
const curGid = ref<string>(gameSelectList[0].gid);
|
||||||
|
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
showLoading.start("正在加载首页...");
|
showLoading.start("正在加载首页...");
|
||||||
// @ts-expect-error-next-line
|
// @ts-expect-error-next-line The import.meta meta-property is not allowed in files which will build into CommonJS output.
|
||||||
const isProdEnv = import.meta.env.MODE === "production";
|
const isProdEnv = import.meta.env.MODE === "production";
|
||||||
// 获取当前环境
|
if (isProdEnv && appStore.devMode) appStore.devMode = false;
|
||||||
if (isProdEnv && appStore.devMode) {
|
await loadComp();
|
||||||
appStore.devMode = false;
|
});
|
||||||
}
|
|
||||||
const temp = [];
|
watch(
|
||||||
for (const item of showHome.value) {
|
() => components.value,
|
||||||
|
(cur, old) => {
|
||||||
|
const newComp = cur.filter((i) => !old.includes(i));
|
||||||
|
if (newComp.length === 0) showLoading.end();
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
async function loadComp(): Promise<void> {
|
||||||
|
showLoading.start("正在加载首页...");
|
||||||
|
const temp: SFComp[] = [];
|
||||||
|
for (const item of showItems.value) {
|
||||||
switch (item) {
|
switch (item) {
|
||||||
case "限时祈愿":
|
case "限时祈愿":
|
||||||
temp.push(PhCompPool);
|
temp.push(PhCompPool);
|
||||||
@@ -78,54 +100,46 @@ onMounted(async () => {
|
|||||||
case "素材日历":
|
case "素材日历":
|
||||||
temp.push(PhCompCalendar);
|
temp.push(PhCompCalendar);
|
||||||
break;
|
break;
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const items = showHome.value.join("、");
|
showLoading.update("正在加载首页...", `正在加载:${showItems.value.join("、")}`);
|
||||||
showLoading.update("正在加载首页...", `正在加载:${items}`);
|
|
||||||
components.value = temp;
|
components.value = temp;
|
||||||
await TGLogger.Info(`[Home][onMounted] 打开首页,当前显示:${items}`);
|
await TGLogger.Info(`[Home][loadComp] 打开首页,当前显示:${showItems.value.join("、")}`);
|
||||||
});
|
}
|
||||||
|
|
||||||
async function submitHome(): Promise<void> {
|
async function submitHome(): Promise<void> {
|
||||||
const show = showHome.value;
|
if (showItems.value.length === 0) {
|
||||||
if (show.length < 1) {
|
|
||||||
showSnackbar.warn("请至少选择一个!");
|
showSnackbar.warn("请至少选择一个!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
homeStore.setShowValue(show);
|
|
||||||
showSnackbar.success("设置成功!");
|
showSnackbar.success("设置成功!");
|
||||||
await TGLogger.Info("[Home][submitHome] 首页设置成功,当前显示:" + show.join("、"));
|
await TGLogger.Info("[Home][submitHome] 首页设置成功,当前显示:" + showItems.value.join("、"));
|
||||||
setTimeout(() => window.location.reload(), 1000);
|
loadItems.value = showItems.value.filter((i) => loadItems.value.includes(i));
|
||||||
|
await loadComp();
|
||||||
}
|
}
|
||||||
|
|
||||||
function getName(name: string): string {
|
function getName(name: string): ShowItemEnum | undefined {
|
||||||
switch (name) {
|
switch (name) {
|
||||||
case "ph-comp-pool":
|
case "ph-comp-pool":
|
||||||
return "限时祈愿";
|
return ShowItemEnum.pool;
|
||||||
case "ph-comp-position":
|
case "ph-comp-position":
|
||||||
return "近期活动";
|
return ShowItemEnum.position;
|
||||||
case "ph-comp-calendar":
|
case "ph-comp-calendar":
|
||||||
return "素材日历";
|
return ShowItemEnum.calendar;
|
||||||
default:
|
default:
|
||||||
return "";
|
return undefined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 组件加载完成
|
async function loadEnd(item: SFComp): Promise<void> {
|
||||||
async function loadEnd(item: any): Promise<void> {
|
const compName = getName(item.__name ?? "");
|
||||||
await TGLogger.Info(`[Home][loadEnd] ${item.__name} 加载完成`);
|
if (!compName) return;
|
||||||
loadComp.value = loadComp.value.filter((v) => v !== getName(item.__name));
|
await TGLogger.Info(`[Home][loadEnd] ${compName} 加载完成`);
|
||||||
endNum.value++;
|
showLoading.update("正在加载首页...", `${compName} 加载完成`);
|
||||||
if (endNum.value === components.value.length) {
|
if (!loadItems.value.includes(compName)) loadItems.value.push(compName);
|
||||||
showLoading.end();
|
else showSnackbar.warn(`${compName} 已加载`);
|
||||||
return;
|
if (loadItems.value.length === components.value.length) showLoading.end();
|
||||||
}
|
|
||||||
showLoading.update("正在加载首页...", `正在加载:${loadComp.value.join("、")}`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
onUnmounted(() => (components.value = []));
|
|
||||||
</script>
|
</script>
|
||||||
<style lang="css" scoped>
|
<style lang="css" scoped>
|
||||||
.home-container {
|
.home-container {
|
||||||
|
|||||||
@@ -7,119 +7,45 @@
|
|||||||
import { defineStore } from "pinia";
|
import { defineStore } from "pinia";
|
||||||
import { ref } from "vue";
|
import { ref } from "vue";
|
||||||
|
|
||||||
export const useHomeStore = defineStore(
|
export const enum ShowItemEnum {
|
||||||
"home",
|
calendar = "素材日历",
|
||||||
() => {
|
pool = "限时祈愿",
|
||||||
const calendarShow = ref({
|
position = "近期活动",
|
||||||
show: true,
|
}
|
||||||
order: 3,
|
|
||||||
});
|
|
||||||
const poolShow = ref({
|
|
||||||
show: true,
|
|
||||||
order: 1,
|
|
||||||
});
|
|
||||||
const positionShow = ref({
|
|
||||||
show: true,
|
|
||||||
order: 2,
|
|
||||||
});
|
|
||||||
const homeShow = ref({
|
|
||||||
calendarShow,
|
|
||||||
poolShow,
|
|
||||||
positionShow,
|
|
||||||
});
|
|
||||||
const poolCover = ref({} satisfies Record<number, string>);
|
|
||||||
|
|
||||||
function init(): void {
|
export type ShowItem = { show: boolean; order: number; label: ShowItemEnum };
|
||||||
calendarShow.value = {
|
|
||||||
show: true,
|
export const useHomeStore = defineStore("home", () => {
|
||||||
order: 3,
|
const homeShow = ref<Array<ShowItem>>([
|
||||||
};
|
{ show: true, order: 1, label: ShowItemEnum.pool },
|
||||||
poolShow.value = {
|
{ show: true, order: 2, label: ShowItemEnum.position },
|
||||||
show: true,
|
{ show: true, order: 3, label: ShowItemEnum.calendar },
|
||||||
order: 1,
|
]);
|
||||||
};
|
const poolCover = ref<Record<number, string>>();
|
||||||
positionShow.value = {
|
|
||||||
show: true,
|
function getShowItems(): Array<ShowItemEnum> {
|
||||||
order: 2,
|
const homeShowLocal = localStorage.getItem("homeShow");
|
||||||
};
|
if (homeShowLocal === null || !Array.isArray(JSON.parse(homeShowLocal))) {
|
||||||
poolCover.value = {};
|
localStorage.setItem("homeShow", JSON.stringify(homeShow.value));
|
||||||
|
}
|
||||||
|
homeShow.value = JSON.parse(localStorage.getItem("homeShow")!);
|
||||||
|
return homeShow.value
|
||||||
|
.filter((item) => item.show)
|
||||||
|
.sort((a, b) => a.order - b.order)
|
||||||
|
.map((item) => item.label);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getShowItems(): string[] {
|
function setShowItems(items: Array<ShowItemEnum>): void {
|
||||||
const defaultList = ["素材日历", "限时祈愿", "近期活动"];
|
|
||||||
defaultList.sort((a, b) => {
|
|
||||||
return getItemOrder(a) - getItemOrder(b);
|
|
||||||
});
|
|
||||||
return defaultList;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getShowValue(): string[] {
|
|
||||||
const showValue = [];
|
|
||||||
if (calendarShow.value.show) showValue.push("素材日历");
|
|
||||||
if (poolShow.value.show) showValue.push("限时祈愿");
|
|
||||||
if (positionShow.value.show) showValue.push("近期活动");
|
|
||||||
showValue.sort((a, b) => {
|
|
||||||
return getItemOrder(a) - getItemOrder(b);
|
|
||||||
});
|
|
||||||
return showValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getItemOrder(item: string): number {
|
|
||||||
if (item === "素材日历") return calendarShow.value.order;
|
|
||||||
if (item === "限时祈愿") return poolShow.value.order;
|
|
||||||
if (item === "近期活动") return positionShow.value.order;
|
|
||||||
return 4;
|
|
||||||
}
|
|
||||||
|
|
||||||
function setShowValue(value: string[]): void {
|
|
||||||
let order = 1;
|
let order = 1;
|
||||||
// 遍历 value
|
for (const item of items) {
|
||||||
value.forEach((item) => {
|
const findIdx = homeShow.value.findIndex((i) => i.label === item);
|
||||||
if (!getShowItems().includes(item)) {
|
if (findIdx === -1) continue;
|
||||||
throw new Error("传入的值不在可选范围内");
|
homeShow.value[findIdx].show = true;
|
||||||
|
homeShow.value[findIdx].order = order++;
|
||||||
}
|
}
|
||||||
if (item === "素材日历") {
|
for (const item of homeShow.value) if (!items.includes(item.label)) item.show = false;
|
||||||
calendarShow.value.order = order;
|
localStorage.setItem("homeShow", JSON.stringify(homeShow.value));
|
||||||
calendarShow.value.show = true;
|
|
||||||
order++;
|
|
||||||
}
|
|
||||||
if (item === "限时祈愿") {
|
|
||||||
poolShow.value.order = order;
|
|
||||||
poolShow.value.show = true;
|
|
||||||
order++;
|
|
||||||
}
|
|
||||||
if (item === "近期活动") {
|
|
||||||
positionShow.value.order = order;
|
|
||||||
positionShow.value.show = true;
|
|
||||||
order++;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
// 遍历 getShowItems()
|
|
||||||
getShowItems().forEach((item) => {
|
|
||||||
if (!value.includes(item)) {
|
|
||||||
if (item === "素材日历") {
|
|
||||||
calendarShow.value.show = false;
|
|
||||||
}
|
|
||||||
if (item === "限时祈愿") {
|
|
||||||
poolShow.value.show = false;
|
|
||||||
}
|
|
||||||
if (item === "近期活动") {
|
|
||||||
positionShow.value.show = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return { poolCover, getShowItems, setShowItems };
|
||||||
homeShow,
|
});
|
||||||
poolCover,
|
|
||||||
init,
|
|
||||||
getShowItems,
|
|
||||||
getShowValue,
|
|
||||||
setShowValue,
|
|
||||||
};
|
|
||||||
},
|
|
||||||
{
|
|
||||||
persist: true,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|||||||
Reference in New Issue
Block a user