feat(shop): 实时修复今日数据及优化商城设置与商品列表展示
- 修复 dashboard 今日数据统计,改为实时计算订单、销售额、用户数与优惠券使用量 - 统计数据写入 cmsStatistics 表,新增 couponUsedCount 状态与 getter - dashboard 页面移除原有按日期查询逻辑,优惠券使用数改为响应式读取 - 商城设置 Logo 上传改为直接调用 OSS 接口上传,移除图片库选择组件 - 新增商品列表中市场价和会员价列,数据为空时显示 "-" - 同步更新商品列表导出功能,包含新增的价格列与列宽配置 - 商品列表商品图片添加 OSS 压缩访问支持 - 调整商品分类搜索框与关键词输入框宽度以优化布局
This commit is contained in:
@@ -2,8 +2,9 @@
|
||||
* 统计数据 store
|
||||
*/
|
||||
import { defineStore } from 'pinia';
|
||||
import dayjs from 'dayjs';
|
||||
import { pageUsers } from '@/api/system/user';
|
||||
import { pageShopOrder, shopOrderTotal } from '@/api/shop/shopOrder';
|
||||
import { pageShopOrder, shopOrderTotal, listShopOrder } from '@/api/shop/shopOrder';
|
||||
import {
|
||||
addCmsStatistics,
|
||||
listCmsStatistics,
|
||||
@@ -23,6 +24,8 @@ export interface StatisticsState {
|
||||
cacheExpiry: number;
|
||||
// 自动刷新定时器
|
||||
refreshTimer: number | null;
|
||||
// 今日使用优惠券数量
|
||||
couponUsedCount: number;
|
||||
}
|
||||
|
||||
export const useStatisticsStore = defineStore('statistics', {
|
||||
@@ -32,7 +35,8 @@ export const useStatisticsStore = defineStore('statistics', {
|
||||
lastUpdateTime: null,
|
||||
// 默认缓存5分钟
|
||||
cacheExpiry: 5 * 60 * 1000,
|
||||
refreshTimer: null
|
||||
refreshTimer: null,
|
||||
couponUsedCount: 0
|
||||
}),
|
||||
|
||||
getters: {
|
||||
@@ -85,6 +89,13 @@ export const useStatisticsStore = defineStore('statistics', {
|
||||
return safeNumber(state.statistics?.todayUsers);
|
||||
},
|
||||
|
||||
/**
|
||||
* 获取今日使用优惠券数量
|
||||
*/
|
||||
couponUsedCount: (state): number => {
|
||||
return safeNumber(state.couponUsedCount);
|
||||
},
|
||||
|
||||
/**
|
||||
* 检查缓存是否有效
|
||||
*/
|
||||
@@ -183,38 +194,49 @@ export const useStatisticsStore = defineStore('statistics', {
|
||||
return 0;
|
||||
})();
|
||||
|
||||
// 安全获取今日销售额
|
||||
const todaySales = (() => {
|
||||
if (statisticsData && statisticsData.length > 0) {
|
||||
const stats = statisticsData[0];
|
||||
if (stats.todaySales !== undefined && stats.todaySales !== null) {
|
||||
return safeNumber(stats.todaySales);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
})();
|
||||
// 实时计算今日数据(不依赖可能未更新的统计表)
|
||||
const todayStart = dayjs().startOf('day').format('YYYY-MM-DD HH:mm:ss');
|
||||
const todayEnd = dayjs().endOf('day').format('YYYY-MM-DD HH:mm:ss');
|
||||
|
||||
// 安全获取今日订单数
|
||||
const todayOrders = (() => {
|
||||
if (statisticsData && statisticsData.length > 0) {
|
||||
const stats = statisticsData[0];
|
||||
if (stats.todayOrders !== undefined && stats.todayOrders !== null) {
|
||||
return safeNumber(stats.todayOrders);
|
||||
}
|
||||
// 安全获取今日订单列表、销售额和优惠券使用量
|
||||
let todayOrders = 0;
|
||||
let todaySales = 0;
|
||||
let couponUsedCount = 0;
|
||||
try {
|
||||
const todayOrderList = await listShopOrder({
|
||||
createTimeStart: todayStart,
|
||||
createTimeEnd: todayEnd
|
||||
});
|
||||
if (Array.isArray(todayOrderList)) {
|
||||
todayOrders = todayOrderList.length;
|
||||
todaySales = todayOrderList.reduce((acc, order) => {
|
||||
return acc + (order.payStatus ? safeNumber(order.payPrice) : 0);
|
||||
}, 0);
|
||||
couponUsedCount = todayOrderList.filter((order) => {
|
||||
const couponType = order.couponType;
|
||||
return couponType !== undefined && couponType !== null && couponType !== 0;
|
||||
}).length;
|
||||
}
|
||||
return 0;
|
||||
})();
|
||||
} catch (e) {
|
||||
console.warn('⚠️ 获取今日订单列表失败:', e);
|
||||
}
|
||||
|
||||
// 安全获取今日新增用户
|
||||
const todayUsers = (() => {
|
||||
if (statisticsData && statisticsData.length > 0) {
|
||||
const stats = statisticsData[0];
|
||||
if (stats.todayUsers !== undefined && stats.todayUsers !== null) {
|
||||
return safeNumber(stats.todayUsers);
|
||||
}
|
||||
let todayUsers = 0;
|
||||
try {
|
||||
const todayUsersResult = await pageUsers({
|
||||
page: 1,
|
||||
limit: 1,
|
||||
createTimeStart: todayStart,
|
||||
createTimeEnd: todayEnd
|
||||
});
|
||||
if (todayUsersResult && typeof todayUsersResult === 'object' && 'count' in todayUsersResult) {
|
||||
todayUsers = safeNumber(todayUsersResult.count);
|
||||
}
|
||||
return 0;
|
||||
})();
|
||||
} catch (e) {
|
||||
console.warn('⚠️ 获取今日新增用户失败:', e);
|
||||
}
|
||||
|
||||
const totalSales = (() => {
|
||||
if (!total) {
|
||||
console.warn('⚠️ 订单总额API返回空数据');
|
||||
@@ -244,7 +266,10 @@ export const useStatisticsStore = defineStore('statistics', {
|
||||
id: existingStatistics.id,
|
||||
userCount: userCount,
|
||||
orderCount: orderCount,
|
||||
totalSales: totalSales
|
||||
totalSales: totalSales,
|
||||
todaySales: todaySales,
|
||||
todayOrders: todayOrders,
|
||||
todayUsers: todayUsers
|
||||
};
|
||||
|
||||
// 异步更新数据库
|
||||
@@ -293,6 +318,7 @@ export const useStatisticsStore = defineStore('statistics', {
|
||||
}
|
||||
|
||||
this.statistics = statistics;
|
||||
this.couponUsedCount = couponUsedCount;
|
||||
this.lastUpdateTime = Date.now();
|
||||
|
||||
return statistics;
|
||||
|
||||
Reference in New Issue
Block a user