优化:getSiteInfo、statistics使用了状态管理模式,提升性能。
This commit is contained in:
263
src/views/test/store-test.vue
Normal file
263
src/views/test/store-test.vue
Normal file
@@ -0,0 +1,263 @@
|
||||
<template>
|
||||
<div class="store-test-page">
|
||||
<a-card title="状态管理测试页面" :bordered="false">
|
||||
<a-space direction="vertical" style="width: 100%">
|
||||
|
||||
<!-- 网站信息测试 -->
|
||||
<a-card title="网站信息 Store 测试" size="small">
|
||||
<a-spin :spinning="siteLoading">
|
||||
<a-descriptions :column="2" size="small">
|
||||
<a-descriptions-item label="网站名称">
|
||||
{{ websiteName || '暂无数据' }}
|
||||
</a-descriptions-item>
|
||||
<a-descriptions-item label="网站Logo">
|
||||
<img v-if="websiteLogo" :src="websiteLogo" alt="logo" style="width: 50px; height: 50px;" />
|
||||
<span v-else>暂无Logo</span>
|
||||
</a-descriptions-item>
|
||||
<a-descriptions-item label="网站描述">
|
||||
{{ websiteComments || '暂无描述' }}
|
||||
</a-descriptions-item>
|
||||
<a-descriptions-item label="运行天数">
|
||||
{{ runDays }} 天
|
||||
</a-descriptions-item>
|
||||
<a-descriptions-item label="网站域名">
|
||||
{{ websiteDomain || '暂无域名' }}
|
||||
</a-descriptions-item>
|
||||
<a-descriptions-item label="网站ID">
|
||||
{{ websiteId || '暂无ID' }}
|
||||
</a-descriptions-item>
|
||||
</a-descriptions>
|
||||
|
||||
<a-space style="margin-top: 16px">
|
||||
<a-button @click="refreshSiteInfo" :loading="siteLoading">
|
||||
刷新网站信息
|
||||
</a-button>
|
||||
<a-button @click="clearSiteCache">
|
||||
清除网站缓存
|
||||
</a-button>
|
||||
</a-space>
|
||||
</a-spin>
|
||||
</a-card>
|
||||
|
||||
<!-- 统计数据测试 -->
|
||||
<a-card title="统计数据 Store 测试" size="small">
|
||||
<a-spin :spinning="statisticsLoading">
|
||||
<a-row :gutter="16">
|
||||
<a-col :span="6">
|
||||
<a-statistic
|
||||
title="用户总数"
|
||||
:value="userCount"
|
||||
:value-style="{ color: '#3f8600' }"
|
||||
/>
|
||||
</a-col>
|
||||
<a-col :span="6">
|
||||
<a-statistic
|
||||
title="订单总数"
|
||||
:value="orderCount"
|
||||
:value-style="{ color: '#1890ff' }"
|
||||
/>
|
||||
</a-col>
|
||||
<a-col :span="6">
|
||||
<a-statistic
|
||||
title="总销售额"
|
||||
:value="totalSales"
|
||||
:value-style="{ color: '#cf1322' }"
|
||||
/>
|
||||
</a-col>
|
||||
<a-col :span="6">
|
||||
<a-statistic
|
||||
title="今日销售额"
|
||||
:value="todaySales"
|
||||
:value-style="{ color: '#722ed1' }"
|
||||
/>
|
||||
</a-col>
|
||||
</a-row>
|
||||
|
||||
<a-space style="margin-top: 16px">
|
||||
<a-button @click="refreshStatistics" :loading="statisticsLoading">
|
||||
刷新统计数据
|
||||
</a-button>
|
||||
<a-button @click="clearStatisticsCache">
|
||||
清除统计缓存
|
||||
</a-button>
|
||||
<a-button
|
||||
@click="toggleAutoRefresh"
|
||||
:type="autoRefreshEnabled ? 'primary' : 'default'"
|
||||
>
|
||||
{{ autoRefreshEnabled ? '停止自动刷新' : '开始自动刷新' }}
|
||||
</a-button>
|
||||
</a-space>
|
||||
</a-spin>
|
||||
</a-card>
|
||||
|
||||
<!-- 缓存状态信息 -->
|
||||
<a-card title="缓存状态信息" size="small">
|
||||
<a-descriptions :column="2" size="small">
|
||||
<a-descriptions-item label="网站信息缓存有效">
|
||||
<a-tag :color="siteStore.isCacheValid ? 'green' : 'red'">
|
||||
{{ siteStore.isCacheValid ? '有效' : '无效' }}
|
||||
</a-tag>
|
||||
</a-descriptions-item>
|
||||
<a-descriptions-item label="统计数据缓存有效">
|
||||
<a-tag :color="statisticsStore.isCacheValid ? 'green' : 'red'">
|
||||
{{ statisticsStore.isCacheValid ? '有效' : '无效' }}
|
||||
</a-tag>
|
||||
</a-descriptions-item>
|
||||
<a-descriptions-item label="网站信息最后更新">
|
||||
{{ siteStore.lastUpdateTime ? new Date(siteStore.lastUpdateTime).toLocaleString() : '从未更新' }}
|
||||
</a-descriptions-item>
|
||||
<a-descriptions-item label="统计数据最后更新">
|
||||
{{ statisticsStore.lastUpdateTime ? new Date(statisticsStore.lastUpdateTime).toLocaleString() : '从未更新' }}
|
||||
</a-descriptions-item>
|
||||
</a-descriptions>
|
||||
</a-card>
|
||||
|
||||
<!-- 操作日志 -->
|
||||
<a-card title="操作日志" size="small">
|
||||
<a-list
|
||||
:data-source="logs"
|
||||
size="small"
|
||||
:pagination="{ pageSize: 5 }"
|
||||
>
|
||||
<template #renderItem="{ item }">
|
||||
<a-list-item>
|
||||
<a-list-item-meta>
|
||||
<template #title>
|
||||
<span>{{ item.action }}</span>
|
||||
<a-tag size="small" style="margin-left: 8px">
|
||||
{{ item.timestamp }}
|
||||
</a-tag>
|
||||
</template>
|
||||
<template #description>
|
||||
{{ item.result }}
|
||||
</template>
|
||||
</a-list-item-meta>
|
||||
</a-list-item>
|
||||
</template>
|
||||
</a-list>
|
||||
</a-card>
|
||||
|
||||
</a-space>
|
||||
</a-card>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, onMounted, onUnmounted } from 'vue';
|
||||
import { useSiteData } from '@/composables/useSiteData';
|
||||
import { useSiteStore } from '@/store/modules/site';
|
||||
import { useStatisticsStore } from '@/store/modules/statistics';
|
||||
|
||||
// 使用组合式函数
|
||||
const {
|
||||
websiteName,
|
||||
websiteLogo,
|
||||
websiteComments,
|
||||
websiteDomain,
|
||||
websiteId,
|
||||
runDays,
|
||||
userCount,
|
||||
orderCount,
|
||||
totalSales,
|
||||
todaySales,
|
||||
siteLoading,
|
||||
statisticsLoading,
|
||||
fetchSiteInfo,
|
||||
fetchStatistics,
|
||||
clearCache
|
||||
} = useSiteData();
|
||||
|
||||
// 直接使用 store(用于访问更多详细信息)
|
||||
const siteStore = useSiteStore();
|
||||
const statisticsStore = useStatisticsStore();
|
||||
|
||||
// 本地状态
|
||||
const autoRefreshEnabled = ref(false);
|
||||
const logs = ref<Array<{ action: string; timestamp: string; result: string }>>([]);
|
||||
|
||||
// 添加日志
|
||||
const addLog = (action: string, result: string) => {
|
||||
logs.value.unshift({
|
||||
action,
|
||||
timestamp: new Date().toLocaleTimeString(),
|
||||
result
|
||||
});
|
||||
// 只保留最近20条日志
|
||||
if (logs.value.length > 20) {
|
||||
logs.value = logs.value.slice(0, 20);
|
||||
}
|
||||
};
|
||||
|
||||
// 刷新网站信息
|
||||
const refreshSiteInfo = async () => {
|
||||
try {
|
||||
await fetchSiteInfo(true);
|
||||
addLog('刷新网站信息', '成功');
|
||||
} catch (error) {
|
||||
addLog('刷新网站信息', `失败: ${error}`);
|
||||
}
|
||||
};
|
||||
|
||||
// 刷新统计数据
|
||||
const refreshStatistics = async () => {
|
||||
try {
|
||||
await fetchStatistics(true);
|
||||
addLog('刷新统计数据', '成功');
|
||||
} catch (error) {
|
||||
addLog('刷新统计数据', `失败: ${error}`);
|
||||
}
|
||||
};
|
||||
|
||||
// 清除网站缓存
|
||||
const clearSiteCache = () => {
|
||||
siteStore.clearCache();
|
||||
addLog('清除网站缓存', '成功');
|
||||
};
|
||||
|
||||
// 清除统计缓存
|
||||
const clearStatisticsCache = () => {
|
||||
statisticsStore.clearCache();
|
||||
addLog('清除统计缓存', '成功');
|
||||
};
|
||||
|
||||
// 切换自动刷新
|
||||
const toggleAutoRefresh = () => {
|
||||
if (autoRefreshEnabled.value) {
|
||||
statisticsStore.stopAutoRefresh();
|
||||
autoRefreshEnabled.value = false;
|
||||
addLog('停止自动刷新', '成功');
|
||||
} else {
|
||||
statisticsStore.startAutoRefresh(10000); // 10秒间隔用于测试
|
||||
autoRefreshEnabled.value = true;
|
||||
addLog('开始自动刷新', '10秒间隔');
|
||||
}
|
||||
};
|
||||
|
||||
onMounted(async () => {
|
||||
addLog('页面加载', '开始初始化');
|
||||
|
||||
try {
|
||||
await Promise.all([
|
||||
fetchSiteInfo(),
|
||||
fetchStatistics()
|
||||
]);
|
||||
addLog('初始化数据', '成功');
|
||||
} catch (error) {
|
||||
addLog('初始化数据', `失败: ${error}`);
|
||||
}
|
||||
});
|
||||
|
||||
onUnmounted(() => {
|
||||
// 清理自动刷新
|
||||
if (autoRefreshEnabled.value) {
|
||||
statisticsStore.stopAutoRefresh();
|
||||
}
|
||||
addLog('页面卸载', '清理完成');
|
||||
});
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.store-test-page {
|
||||
padding: 20px;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user