优化:getSiteInfo、statistics使用了状态管理模式,提升性能。

This commit is contained in:
2025-07-31 11:08:08 +08:00
parent 75aeccbb1a
commit 6f4ff3f8fb
11 changed files with 1367 additions and 163 deletions

152
src/store/modules/site.ts Normal file
View File

@@ -0,0 +1,152 @@
/**
* 网站信息 store
*/
import { defineStore } from 'pinia';
import { getSiteInfo } from '@/api/layout';
import { CmsWebsite } from '@/api/cms/cmsWebsite/model';
export interface SiteState {
// 网站信息
siteInfo: CmsWebsite | null;
// 加载状态
loading: boolean;
// 最后更新时间
lastUpdateTime: number | null;
// 缓存有效期(毫秒)
cacheExpiry: number;
}
export const useSiteStore = defineStore({
id: 'site',
state: (): SiteState => ({
siteInfo: null,
loading: false,
lastUpdateTime: null,
// 默认缓存30分钟
cacheExpiry: 30 * 60 * 1000
}),
getters: {
/**
* 获取网站名称
*/
websiteName: (state): string => {
return state.siteInfo?.websiteName || '';
},
/**
* 获取网站Logo
*/
websiteLogo: (state): string => {
return state.siteInfo?.websiteLogo || '/logo.png';
},
/**
* 获取网站描述
*/
websiteComments: (state): string => {
return state.siteInfo?.comments || '';
},
/**
* 获取小程序码
*/
websiteDarkLogo: (state): string => {
return state.siteInfo?.websiteDarkLogo || '';
},
/**
* 获取网站域名
*/
websiteDomain: (state): string => {
return state.siteInfo?.domain || '';
},
/**
* 获取网站ID
*/
websiteId: (state): number | undefined => {
return state.siteInfo?.websiteId;
},
/**
* 计算系统运行天数
*/
runDays: (state): number => {
if (!state.siteInfo?.createTime) return 0;
const createTime = new Date(state.siteInfo.createTime).getTime();
const now = new Date().getTime();
return Math.floor((now - createTime) / (24 * 60 * 60 * 1000));
},
/**
* 检查缓存是否有效
*/
isCacheValid: (state): boolean => {
if (!state.lastUpdateTime) return false;
const now = Date.now();
return (now - state.lastUpdateTime) < state.cacheExpiry;
}
},
actions: {
/**
* 获取网站信息
* @param forceRefresh 是否强制刷新
*/
async fetchSiteInfo(forceRefresh = false) {
// 如果缓存有效且不强制刷新,直接返回缓存数据
if (!forceRefresh && this.isCacheValid && this.siteInfo) {
return this.siteInfo;
}
this.loading = true;
try {
const data = await getSiteInfo();
this.siteInfo = data;
this.lastUpdateTime = Date.now();
// 更新localStorage中的相关信息
if (data.websiteId) {
localStorage.setItem('WebsiteId', String(data.websiteId));
}
if (data.domain) {
localStorage.setItem('Domain', data.domain);
localStorage.setItem('SiteUrl', `${data.prefix || 'https://'}${data.domain}`);
}
return data;
} catch (error) {
console.error('获取网站信息失败:', error);
throw error;
} finally {
this.loading = false;
}
},
/**
* 更新网站信息
*/
updateSiteInfo(siteInfo: Partial<CmsWebsite>) {
if (this.siteInfo) {
this.siteInfo = { ...this.siteInfo, ...siteInfo };
this.lastUpdateTime = Date.now();
}
},
/**
* 清除缓存
*/
clearCache() {
this.siteInfo = null;
this.lastUpdateTime = null;
},
/**
* 设置缓存有效期
*/
setCacheExpiry(expiry: number) {
this.cacheExpiry = expiry;
}
}
});

View File

@@ -0,0 +1,222 @@
/**
* 统计数据 store
*/
import { defineStore } from 'pinia';
import { pageUsers } from '@/api/system/user';
import { pageShopOrder, shopOrderTotal } from '@/api/shop/shopOrder';
import { addCmsStatistics, listCmsStatistics, updateCmsStatistics } from '@/api/cms/cmsStatistics';
import { CmsStatistics } from '@/api/cms/cmsStatistics/model';
import { safeNumber, hasValidId, isValidApiResponse } from '@/utils/type-guards';
export interface StatisticsState {
// 统计数据
statistics: CmsStatistics | null;
// 加载状态
loading: boolean;
// 最后更新时间
lastUpdateTime: number | null;
// 缓存有效期(毫秒)- 统计数据缓存时间较短
cacheExpiry: number;
// 自动刷新定时器
refreshTimer: number | null;
}
export const useStatisticsStore = defineStore({
id: 'statistics',
state: (): StatisticsState => ({
statistics: null,
loading: false,
lastUpdateTime: null,
// 默认缓存5分钟
cacheExpiry: 5 * 60 * 1000,
refreshTimer: null
}),
getters: {
/**
* 获取用户总数
*/
userCount: (state): number => {
return safeNumber(state.statistics?.userCount);
},
/**
* 获取订单总数
*/
orderCount: (state): number => {
return safeNumber(state.statistics?.orderCount);
},
/**
* 获取总销售额
*/
totalSales: (state): number => {
return safeNumber(state.statistics?.totalSales);
},
/**
* 获取今日销售额
*/
todaySales: (state): number => {
return safeNumber(state.statistics?.todaySales);
},
/**
* 获取本月销售额
*/
monthSales: (state): number => {
return safeNumber(state.statistics?.monthSales);
},
/**
* 获取今日订单数
*/
todayOrders: (state): number => {
return safeNumber(state.statistics?.todayOrders);
},
/**
* 获取今日新增用户
*/
todayUsers: (state): number => {
return safeNumber(state.statistics?.todayUsers);
},
/**
* 检查缓存是否有效
*/
isCacheValid: (state): boolean => {
if (!state.lastUpdateTime) return false;
const now = Date.now();
return (now - state.lastUpdateTime) < state.cacheExpiry;
}
},
actions: {
/**
* 获取统计数据
* @param forceRefresh 是否强制刷新
*/
async fetchStatistics(forceRefresh = false) {
// 如果缓存有效且不强制刷新,直接返回缓存数据
if (!forceRefresh && this.isCacheValid && this.statistics) {
return this.statistics;
}
this.loading = true;
try {
// 并行获取各种统计数据
const [users, orders, total, statisticsData] = await Promise.all([
pageUsers({}),
pageShopOrder({}),
shopOrderTotal(),
listCmsStatistics({})
]);
let statistics: CmsStatistics;
if (statisticsData && statisticsData.length > 0) {
// 更新现有统计数据
const existingStatistics = statisticsData[0];
// 确保数据存在且有有效的 ID
if (hasValidId(existingStatistics)) {
const updateData: Partial<CmsStatistics> = {
id: existingStatistics.id,
userCount: safeNumber(isValidApiResponse(users) ? users.count : 0),
orderCount: safeNumber(isValidApiResponse(orders) ? orders.count : 0),
totalSales: safeNumber(total),
};
// 异步更新数据库
setTimeout(() => {
updateCmsStatistics(updateData).catch((error) => {
console.error('更新统计数据失败:', error);
});
}, 1000);
// 更新本地数据
statistics = { ...existingStatistics, ...updateData };
} else {
// 如果现有数据无效,使用基础数据
statistics = {
userCount: safeNumber(isValidApiResponse(users) ? users.count : 0),
orderCount: safeNumber(isValidApiResponse(orders) ? orders.count : 0),
totalSales: safeNumber(total),
};
}
} else {
// 创建新的统计数据
statistics = {
userCount: safeNumber(isValidApiResponse(users) ? users.count : 0),
orderCount: safeNumber(isValidApiResponse(orders) ? orders.count : 0),
totalSales: safeNumber(total),
};
// 异步保存到数据库
setTimeout(() => {
addCmsStatistics(statistics).catch((error) => {
console.error('保存统计数据失败:', error);
});
}, 1000);
}
this.statistics = statistics;
this.lastUpdateTime = Date.now();
return statistics;
} catch (error) {
console.error('获取统计数据失败:', error);
throw error;
} finally {
this.loading = false;
}
},
/**
* 更新统计数据
*/
updateStatistics(statistics: Partial<CmsStatistics>) {
if (this.statistics) {
this.statistics = { ...this.statistics, ...statistics };
this.lastUpdateTime = Date.now();
}
},
/**
* 清除缓存
*/
clearCache() {
this.statistics = null;
this.lastUpdateTime = null;
},
/**
* 设置缓存有效期
*/
setCacheExpiry(expiry: number) {
this.cacheExpiry = expiry;
},
/**
* 开始自动刷新
* @param interval 刷新间隔毫秒默认5分钟
*/
startAutoRefresh(interval = 5 * 60 * 1000) {
this.stopAutoRefresh();
this.refreshTimer = window.setInterval(() => {
this.fetchStatistics(true).catch(console.error);
}, interval);
},
/**
* 停止自动刷新
*/
stopAutoRefresh() {
if (this.refreshTimer) {
clearInterval(this.refreshTimer);
this.refreshTimer = null;
}
}
}
});