mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 23:49:19 +08:00
feat: 商品详情页功能完成
后端: - ProductOverviewController POST /ec/product/overview - ProductOverviewQueryDTO / ProductOverviewVO (productId + 7个统计字段) - ProductOverviewServiceImpl 前端: - ProductCard 添加统计入口,goToDetails 改用 path 导航 - /api/ecommerce/product-overview/ 接口定义 - views/ecommerce/product/details/ 商品详情页 + OverviewTab (7个ChartCard) 数据库: - sys_menu id=43 parent_id 从 8 改为 6
This commit is contained in:
@@ -38,13 +38,14 @@ export async function getImportTaskPageApi(
|
||||
}
|
||||
|
||||
/**
|
||||
* SSE连接管理器(使用fetch API支持自定义请求头)
|
||||
* SSE连接管理器(使用原生EventSource,支持自动重连)
|
||||
*/
|
||||
class SseConnection {
|
||||
private abortController: AbortController | null = null;
|
||||
private eventSource: EventSource | null = null;
|
||||
private accessStore: ReturnType<typeof useAccessStore>;
|
||||
private listeners: Map<string, Set<(event: MessageEvent) => void>> =
|
||||
new Map();
|
||||
// EventSource 原生状态常量
|
||||
private readyState: number = EventSource.CLOSED;
|
||||
|
||||
constructor() {
|
||||
@@ -52,20 +53,38 @@ class SseConnection {
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加事件监听器
|
||||
* 添加事件监听器(需在 connect 之前调用)
|
||||
*/
|
||||
addEventListener(eventType: string, handler: (event: MessageEvent) => void) {
|
||||
if (!this.listeners.has(eventType)) {
|
||||
this.listeners.set(eventType, new Set());
|
||||
}
|
||||
this.listeners.get(eventType)!.add(handler);
|
||||
// 如果 EventSource 已建立,额外绑定一次(防止在 connect 之后调用 listener 漏掉)
|
||||
if (this.eventSource) {
|
||||
this.eventSource.addEventListener(eventType, handler as EventListener);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 建立SSE连接(使用fetch API)
|
||||
* 移除事件监听器
|
||||
*/
|
||||
removeEventListener(eventType: string, handler: (event: MessageEvent) => void) {
|
||||
const handlers = this.listeners.get(eventType);
|
||||
if (handlers) {
|
||||
handlers.delete(handler);
|
||||
}
|
||||
if (this.eventSource) {
|
||||
this.eventSource.removeEventListener(eventType, handler as EventListener);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 建立SSE连接
|
||||
* EventSource 原生支持自动重连,无需手动实现
|
||||
*/
|
||||
connect(): void {
|
||||
if (this.abortController) {
|
||||
if (this.eventSource) {
|
||||
this.disconnect();
|
||||
}
|
||||
|
||||
@@ -75,79 +94,37 @@ class SseConnection {
|
||||
return;
|
||||
}
|
||||
|
||||
const url = `${apiURL}/ec/import-task/sse/connect`;
|
||||
this.abortController = new AbortController();
|
||||
const url = `${apiURL}/ec/import-task/sse/connect?token=${encodeURIComponent(token)}`;
|
||||
this.readyState = EventSource.CONNECTING;
|
||||
|
||||
fetch(url, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
Accept: 'text/event-stream',
|
||||
},
|
||||
signal: this.abortController.signal,
|
||||
})
|
||||
.then((response) => {
|
||||
if (!response.ok) {
|
||||
throw new Error(`SSE连接失败: ${response.status}`);
|
||||
}
|
||||
const es = new EventSource(url);
|
||||
this.eventSource = es;
|
||||
|
||||
this.readyState = EventSource.OPEN;
|
||||
es.onopen = () => {
|
||||
this.readyState = EventSource.OPEN;
|
||||
};
|
||||
|
||||
const reader = response.body?.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
es.onerror = (event) => {
|
||||
this.readyState = EventSource.CLOSED;
|
||||
this.dispatchEvent('error', new MessageEvent('error', { data: event }));
|
||||
// EventSource 会自动重连,无需手动处理
|
||||
};
|
||||
|
||||
if (!reader) {
|
||||
throw new Error('无法获取响应流');
|
||||
}
|
||||
|
||||
// 读取流数据
|
||||
const readStream = () => {
|
||||
reader
|
||||
.read()
|
||||
.then(({ done, value }) => {
|
||||
if (done) {
|
||||
this.readyState = EventSource.CLOSED;
|
||||
return;
|
||||
}
|
||||
|
||||
const chunk = decoder.decode(value, { stream: true });
|
||||
this.parseSSE(chunk);
|
||||
readStream();
|
||||
})
|
||||
.catch((error) => {
|
||||
if (error.name !== 'AbortError') {
|
||||
console.error('读取SSE流失败:', error);
|
||||
this.readyState = EventSource.CLOSED;
|
||||
this.dispatchEvent(
|
||||
'error',
|
||||
new MessageEvent('error', { data: error }),
|
||||
);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
readStream();
|
||||
})
|
||||
.catch((error) => {
|
||||
if (error.name !== 'AbortError') {
|
||||
console.error('建立SSE连接失败:', error);
|
||||
this.readyState = EventSource.CLOSED;
|
||||
this.dispatchEvent(
|
||||
'error',
|
||||
new MessageEvent('error', { data: error }),
|
||||
);
|
||||
}
|
||||
// 将所有自定义事件类型的 listener 注册到 EventSource 上
|
||||
this.listeners.forEach((handlers, eventType) => {
|
||||
handlers.forEach((handler) => {
|
||||
es.addEventListener(eventType, handler as EventListener);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 断开连接
|
||||
* 断开连接(手动断开后不会自动重连)
|
||||
*/
|
||||
disconnect() {
|
||||
if (this.abortController) {
|
||||
this.abortController.abort();
|
||||
this.abortController = null;
|
||||
if (this.eventSource) {
|
||||
this.eventSource.close();
|
||||
this.eventSource = null;
|
||||
this.readyState = EventSource.CLOSED;
|
||||
}
|
||||
}
|
||||
@@ -159,22 +136,6 @@ class SseConnection {
|
||||
return this.readyState;
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除事件监听器
|
||||
*/
|
||||
removeEventListener(
|
||||
eventType: string,
|
||||
handler: (event: MessageEvent) => void,
|
||||
) {
|
||||
const handlers = this.listeners.get(eventType);
|
||||
if (handlers) {
|
||||
handlers.delete(handler);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 分发事件
|
||||
*/
|
||||
private dispatchEvent(eventType: string, event: MessageEvent) {
|
||||
const handlers = this.listeners.get(eventType);
|
||||
if (handlers) {
|
||||
@@ -187,34 +148,6 @@ class SseConnection {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析SSE数据
|
||||
*/
|
||||
private parseSSE(chunk: string) {
|
||||
const lines = chunk.split('\n');
|
||||
let eventType = 'message';
|
||||
let data = '';
|
||||
|
||||
for (const line of lines) {
|
||||
if (line.startsWith('event:')) {
|
||||
eventType = line.slice(6).trim();
|
||||
} else if (line.startsWith('data:')) {
|
||||
data += `${line.slice(5).trim()}\n`;
|
||||
} else if (
|
||||
line === '' && // 空行表示一个完整的消息
|
||||
data
|
||||
) {
|
||||
const event = new MessageEvent(eventType, {
|
||||
data: data.trim(),
|
||||
});
|
||||
this.dispatchEvent(eventType, event);
|
||||
this.dispatchEvent('message', event);
|
||||
data = '';
|
||||
eventType = 'message';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
import type { ProductOverviewDTO, ProductOverviewVO } from './types';
|
||||
|
||||
import { requestClient } from '#/api/request';
|
||||
|
||||
/**
|
||||
* 商品概览管理API
|
||||
*/
|
||||
|
||||
/**
|
||||
* 获取商品总览统计数据
|
||||
*/
|
||||
export async function getProductOverviewApi(params: ProductOverviewDTO.Query) {
|
||||
return requestClient.post<ProductOverviewVO.Overview>(
|
||||
'/ec/product/overview',
|
||||
params,
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
export * from './apis';
|
||||
export * from './types';
|
||||
@@ -0,0 +1,2 @@
|
||||
export type { ProductOverviewDTO } from './types.dto';
|
||||
export type { ProductOverviewVO } from './types.vo';
|
||||
@@ -0,0 +1,11 @@
|
||||
export namespace ProductOverviewDTO {
|
||||
/** 商品概览查询参数 */
|
||||
export interface Query {
|
||||
/** 商品ID */
|
||||
productId: number;
|
||||
/** 开始日期 */
|
||||
startDate: string;
|
||||
/** 结束日期 */
|
||||
endDate: string;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
export namespace ProductOverviewVO {
|
||||
/** 每日数据项 */
|
||||
export interface DailyData {
|
||||
/** 日期 */
|
||||
date: string;
|
||||
/** 对应的值 */
|
||||
value: number;
|
||||
}
|
||||
|
||||
/** 每日统计数据 */
|
||||
export interface DailyStatistics {
|
||||
/** 时间范围的总值 */
|
||||
total: number;
|
||||
/** 每日数据列表 */
|
||||
dailyDataList: DailyData[];
|
||||
}
|
||||
|
||||
/** 商品总览 */
|
||||
export interface Overview {
|
||||
/** 商品ID */
|
||||
productId: number;
|
||||
/** 商品名称 */
|
||||
productName: string;
|
||||
/** 销售额统计 */
|
||||
salesAmount: DailyStatistics;
|
||||
/** 订单数统计 */
|
||||
orderCount: DailyStatistics;
|
||||
/** 客单价统计 */
|
||||
averageOrderValue: DailyStatistics;
|
||||
/** 成本统计 */
|
||||
costAmount: DailyStatistics;
|
||||
/** 退款金额统计 */
|
||||
refundAmount: DailyStatistics;
|
||||
/** 推广费统计 */
|
||||
promotionFee: DailyStatistics;
|
||||
/** 费比统计 */
|
||||
feeRatio: DailyStatistics;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user