diff --git a/src/store/modules/statistics.ts b/src/store/modules/statistics.ts index 06102a9..c465bea 100644 --- a/src/store/modules/statistics.ts +++ b/src/store/modules/statistics.ts @@ -4,6 +4,7 @@ import { defineStore } from 'pinia'; import { pageUsers } from '@/api/system/user'; import { pageCmsArticle } from '@/api/cms/cmsArticle'; +import { pageFiles } from '@/api/system/file'; import { addCmsStatistics, listCmsStatistics, @@ -49,12 +50,19 @@ export const useStatisticsStore = defineStore({ }, /** - * 获取订单总数 + * 获取文章总数 */ - orderCount: (state): number => { + articleCount: (state): number => { return safeNumber(state.statistics?.orderCount); }, + /** + * 获取素材(文件)数 + */ + fileCount: (state): number => { + return safeNumber(state.statistics?.todayOrders); + }, + /** * 获取总销售额 */ @@ -114,14 +122,21 @@ export const useStatisticsStore = defineStore({ this.loading = true; try { // 并行获取各种统计数据 - const [users, orders, total, statisticsData] = await Promise.all([ - pageUsers({}), - pageCmsArticle({}), - listCmsStatistics({}) + const [users, allArticles, files, statisticsData] = await Promise.all([ + pageUsers({}).catch(() => null), + pageCmsArticle({}).catch(() => null), + pageFiles({}).catch(() => null), // 文件素材 + listCmsStatistics({}).catch(() => null) ]); let statistics: CmsStatistics; + // 计算统计数据 + const totalArticleCount = isValidApiResponse(allArticles) + ? allArticles.count + : 0; + const totalFileCount = isValidApiResponse(files) ? files.count : 0; + if (statisticsData && statisticsData.length > 0) { // 更新现有统计数据 const existingStatistics = statisticsData[0]; @@ -133,10 +148,8 @@ export const useStatisticsStore = defineStore({ userCount: safeNumber( isValidApiResponse(users) ? users.count : 0 ), - orderCount: safeNumber( - isValidApiResponse(orders) ? orders.count : 0 - ), - totalSales: safeNumber(total) + orderCount: safeNumber(totalArticleCount), // 使用文章总数 + todayOrders: safeNumber(totalFileCount) // 使用文件总数 }; // 异步更新数据库 @@ -154,20 +167,16 @@ export const useStatisticsStore = defineStore({ userCount: safeNumber( isValidApiResponse(users) ? users.count : 0 ), - orderCount: safeNumber( - isValidApiResponse(orders) ? orders.count : 0 - ), - totalSales: safeNumber(total) + orderCount: safeNumber(totalArticleCount), + todayOrders: safeNumber(totalFileCount) }; } } else { // 创建新的统计数据 statistics = { userCount: safeNumber(isValidApiResponse(users) ? users.count : 0), - orderCount: safeNumber( - isValidApiResponse(orders) ? orders.count : 0 - ), - totalSales: safeNumber(total) + orderCount: safeNumber(totalArticleCount), + todayOrders: safeNumber(totalFileCount) }; // 异步保存到数据库 @@ -184,7 +193,18 @@ export const useStatisticsStore = defineStore({ return statistics; } catch (error) { console.error('获取统计数据失败:', error); - throw error; + + // 即使出错也要提供基础的统计数据 + const fallbackStatistics: CmsStatistics = { + userCount: 0, + orderCount: 0, + todayOrders: 0 + }; + + this.statistics = fallbackStatistics; + this.lastUpdateTime = Date.now(); + + return fallbackStatistics; } finally { this.loading = false; } diff --git a/src/utils/statistics-test.ts b/src/utils/statistics-test.ts new file mode 100644 index 0000000..569ea56 --- /dev/null +++ b/src/utils/statistics-test.ts @@ -0,0 +1,90 @@ +/** + * 统计功能测试工具 + * 用于验证统计数据的正确性 + */ + +import { pageUsers } from '@/api/system/user'; +import { pageCmsArticle } from '@/api/cms/cmsArticle'; +import { pageFiles } from '@/api/system/file'; + +/** + * 测试统计数据获取 + */ +export async function testStatistics() { + console.log('开始测试统计数据获取...'); + + try { + // 测试用户数据获取 + console.log('测试用户数据获取...'); + const users = await pageUsers({}).catch(err => { + console.warn('用户数据获取失败:', err.message); + return null; + }); + console.log('用户数据:', users ? `共 ${users.count} 个用户` : '获取失败'); + + // 测试文章总数获取 + console.log('测试文章总数获取...'); + const allArticles = await pageCmsArticle({}).catch(err => { + console.warn('文章数据获取失败:', err.message); + return null; + }); + console.log('文章总数:', allArticles ? `共 ${allArticles.count} 篇文章` : '获取失败'); + + // 测试文件数获取 + console.log('测试文件数获取...'); + const files = await pageFiles({}).catch(err => { + console.warn('文件数据获取失败:', err.message); + return null; + }); + console.log('文件数:', files ? `共 ${files.count} 个文件` : '获取失败'); + + // 汇总结果 + const result = { + userCount: users?.count || 0, + articleCount: allArticles?.count || 0, + fileCount: files?.count || 0, + success: true + }; + + console.log('统计测试完成:', result); + return result; + + } catch (error) { + console.error('统计测试失败:', error); + return { + userCount: 0, + articleCount: 0, + fileCount: 0, + success: false, + error: error.message + }; + } +} + +/** + * 验证文章状态枚举 + */ +export const ARTICLE_STATUS = { + PUBLISHED: 0, // 已发布 + PENDING: 1, // 待审核 + REJECTED: 2, // 已驳回 + VIOLATION: 3 // 违规内容 +} as const; + +/** + * 获取文章状态描述 + */ +export function getArticleStatusText(status: number): string { + switch (status) { + case ARTICLE_STATUS.PUBLISHED: + return '已发布'; + case ARTICLE_STATUS.PENDING: + return '待审核'; + case ARTICLE_STATUS.REJECTED: + return '已驳回'; + case ARTICLE_STATUS.VIOLATION: + return '违规内容'; + default: + return '未知状态'; + } +} diff --git a/src/views/cms/cmsStatistics/index.vue b/src/views/cms/cmsStatistics/index.vue index 6e17619..ce35b43 100644 --- a/src/views/cms/cmsStatistics/index.vue +++ b/src/views/cms/cmsStatistics/index.vue @@ -20,11 +20,11 @@ @@ -33,12 +33,12 @@ @@ -73,6 +73,7 @@ SettingOutlined, AccountBookOutlined, FileTextOutlined, + FileOutlined, MoneyCollectOutlined } from '@ant-design/icons-vue'; import { getSiteInfo } from '@/api/layout'; @@ -81,6 +82,7 @@ import { getPageTitle } from '@/utils/common'; import { listCmsStatistics } from '@/api/cms/cmsStatistics'; import { pageCmsArticle } from '@/api/cms/cmsArticle'; + import { pageFiles } from '@/api/system/file'; // 当前小程序项目 const siteInfo = ref({}); @@ -102,8 +104,8 @@ // 统计数据 const statistics = ref({ userCount: 0, - orderCount: 0, - todayVisit: 0, + articleCount: 0, + fileCount: 0, runDays: 365 }); @@ -126,20 +128,30 @@ }; const loadStatistics = async () => { - // TODO: 调用API获取统计数据 - const users = await pageUsers({}); - const articles = await pageCmsArticle({}); - if (users) { - statistics.value.userCount = users.count; - } - if (articles) { - statistics.value.orderCount = articles.count; - } - // 获取统计表数据 - const data = await listCmsStatistics({}); - if (data) { - // 统计数据 - statistics.value.totalSales = data[0].totalSales; + try { + // 并行获取统计数据 + const [users, allArticles, files] = await Promise.all([ + pageUsers({}).catch(() => null), + pageCmsArticle({}).catch(() => null), + pageFiles({}).catch(() => null) // 文件素材 + ]); + + // 用户数统计 + if (users) { + statistics.value.userCount = users.count || 0; + } + + // 文章数统计 + if (allArticles) { + statistics.value.articleCount = allArticles.count || 0; + } + + // 文件数统计 + if (files) { + statistics.value.fileCount = files.count || 0; + } + } catch (error) { + console.error('加载统计数据失败:', error); } }; diff --git a/src/views/cms/dashboard/index.vue b/src/views/cms/dashboard/index.vue index 6668033..95820f0 100644 --- a/src/views/cms/dashboard/index.vue +++ b/src/views/cms/dashboard/index.vue @@ -51,7 +51,7 @@ @@ -81,13 +81,13 @@ @@ -96,7 +96,7 @@ siteStore.runDays); const userCount = computed(() => statisticsStore.userCount); - const orderCount = computed(() => statisticsStore.orderCount); + const orderCount = computed(() => statisticsStore.articleCount); + const fileCount = computed(() => statisticsStore.fileCount); const totalSales = computed(() => statisticsStore.totalSales); // 加载状态