feat: 添加MSW模拟服务和数据源集成
refactor: 重构页面组件移除冗余Layout组件 feat: 实现WebSocket和事件总线系统 feat: 添加队列和调度系统 docs: 更新架构文档和服务映射 style: 清理重复接口定义使用数据源 chore: 更新依赖项配置 feat: 添加运行时系统和领域引导 ci: 配置ESLint边界检查规则 build: 添加Redis和WebSocket依赖 test: 添加MSW浏览器环境入口 perf: 优化数据获取逻辑使用统一数据源 fix: 修复类型定义和状态管理问题
This commit is contained in:
217
dashboard/src/mock/msw.ts
Normal file
217
dashboard/src/mock/msw.ts
Normal file
@@ -0,0 +1,217 @@
|
||||
/**
|
||||
* [MOCK-003] MSW Mock配置
|
||||
* 拦截HTTP请求,返回Mock数据
|
||||
* AI注意: 这是网络层拦截,业务代码完全无感知
|
||||
*
|
||||
* @module mock/msw
|
||||
* @author AI-Frontend-Team
|
||||
* @created 2026-03-19
|
||||
*/
|
||||
|
||||
import { setupWorker, rest } from 'msw';
|
||||
import {
|
||||
mockCertificates,
|
||||
getMockCertificates,
|
||||
getMockCertificateById,
|
||||
createMockCertificate,
|
||||
updateMockCertificate,
|
||||
deleteMockCertificate,
|
||||
} from './data/certificate.mock';
|
||||
|
||||
/**
|
||||
* MSW请求处理器
|
||||
* 拦截证书相关API请求
|
||||
*/
|
||||
export const handlers = [
|
||||
// ============================================
|
||||
// 证书列表查询
|
||||
// ============================================
|
||||
rest.get('/api/v1/certificate/certificates', (req, res, ctx) => {
|
||||
const status = req.url.searchParams.get('status');
|
||||
const type = req.url.searchParams.get('type');
|
||||
const keyword = req.url.searchParams.get('keyword');
|
||||
const page = parseInt(req.url.searchParams.get('page') || '1');
|
||||
const pageSize = parseInt(req.url.searchParams.get('pageSize') || '10');
|
||||
|
||||
console.log('[MSW] GET /api/v1/certificate/certificates', { status, type, keyword, page, pageSize });
|
||||
|
||||
let data = getMockCertificates({ status: status || undefined, type: type || undefined, keyword: keyword || undefined });
|
||||
|
||||
// 分页
|
||||
const start = (page - 1) * pageSize;
|
||||
const end = start + pageSize;
|
||||
const paginatedData = data.slice(start, end);
|
||||
|
||||
return res(
|
||||
ctx.delay(300),
|
||||
ctx.status(200),
|
||||
ctx.json({
|
||||
success: true,
|
||||
data: paginatedData,
|
||||
total: data.length,
|
||||
page,
|
||||
pageSize,
|
||||
})
|
||||
);
|
||||
}),
|
||||
|
||||
// ============================================
|
||||
// 证书详情
|
||||
// ============================================
|
||||
rest.get('/api/v1/certificate/certificates/:id', (req, res, ctx) => {
|
||||
const { id } = req.params;
|
||||
const cert = getMockCertificateById(id as string);
|
||||
|
||||
console.log('[MSW] GET /api/v1/certificate/certificates/:id', { id });
|
||||
|
||||
if (!cert) {
|
||||
return res(
|
||||
ctx.delay(200),
|
||||
ctx.status(404),
|
||||
ctx.json({ success: false, error: '证书不存在' })
|
||||
);
|
||||
}
|
||||
|
||||
return res(
|
||||
ctx.delay(200),
|
||||
ctx.status(200),
|
||||
ctx.json({ success: true, data: cert })
|
||||
);
|
||||
}),
|
||||
|
||||
// ============================================
|
||||
// 创建证书
|
||||
// ============================================
|
||||
rest.post('/api/v1/certificate/certificates', async (req, res, ctx) => {
|
||||
const body = await req.json();
|
||||
|
||||
console.log('[MSW] POST /api/v1/certificate/certificates', body);
|
||||
|
||||
const newCert = createMockCertificate(body);
|
||||
|
||||
return res(
|
||||
ctx.delay(500),
|
||||
ctx.status(200),
|
||||
ctx.json({
|
||||
success: true,
|
||||
data: { id: newCert.id },
|
||||
})
|
||||
);
|
||||
}),
|
||||
|
||||
// ============================================
|
||||
// 更新证书
|
||||
// ============================================
|
||||
rest.put('/api/v1/certificate/certificates/:id', async (req, res, ctx) => {
|
||||
const { id } = req.params;
|
||||
const body = await req.json();
|
||||
|
||||
console.log('[MSW] PUT /api/v1/certificate/certificates/:id', { id, body });
|
||||
|
||||
const updated = updateMockCertificate(id as string, body);
|
||||
|
||||
if (!updated) {
|
||||
return res(
|
||||
ctx.delay(200),
|
||||
ctx.status(404),
|
||||
ctx.json({ success: false, error: '证书不存在' })
|
||||
);
|
||||
}
|
||||
|
||||
return res(
|
||||
ctx.delay(300),
|
||||
ctx.status(200),
|
||||
ctx.json({ success: true, data: updated })
|
||||
);
|
||||
}),
|
||||
|
||||
// ============================================
|
||||
// 更新证书状态(审核)
|
||||
// ============================================
|
||||
rest.put('/api/v1/certificate/certificates/:id/status', async (req, res, ctx) => {
|
||||
const { id } = req.params;
|
||||
const { status, approvedBy } = await req.json();
|
||||
|
||||
console.log('[MSW] PUT /api/v1/certificate/certificates/:id/status', { id, status, approvedBy });
|
||||
|
||||
const updates: any = { status };
|
||||
if (status === 'APPROVED') {
|
||||
updates.approvedBy = approvedBy || 'admin';
|
||||
updates.approvedDate = new Date().toISOString().split('T')[0];
|
||||
}
|
||||
|
||||
const updated = updateMockCertificate(id as string, updates);
|
||||
|
||||
if (!updated) {
|
||||
return res(
|
||||
ctx.delay(200),
|
||||
ctx.status(404),
|
||||
ctx.json({ success: false, error: '证书不存在' })
|
||||
);
|
||||
}
|
||||
|
||||
return res(
|
||||
ctx.delay(300),
|
||||
ctx.status(200),
|
||||
ctx.json({ success: true })
|
||||
);
|
||||
}),
|
||||
|
||||
// ============================================
|
||||
// 删除证书
|
||||
// ============================================
|
||||
rest.delete('/api/v1/certificate/certificates/:id', (req, res, ctx) => {
|
||||
const { id } = req.params;
|
||||
|
||||
console.log('[MSW] DELETE /api/v1/certificate/certificates/:id', { id });
|
||||
|
||||
const deleted = deleteMockCertificate(id as string);
|
||||
|
||||
if (!deleted) {
|
||||
return res(
|
||||
ctx.delay(200),
|
||||
ctx.status(404),
|
||||
ctx.json({ success: false, error: '证书不存在' })
|
||||
);
|
||||
}
|
||||
|
||||
return res(
|
||||
ctx.delay(200),
|
||||
ctx.status(200),
|
||||
ctx.json({ success: true })
|
||||
);
|
||||
}),
|
||||
];
|
||||
|
||||
/**
|
||||
* MSW Worker实例
|
||||
* 在浏览器环境中使用
|
||||
*/
|
||||
export const worker = setupWorker(...handlers);
|
||||
|
||||
/**
|
||||
* 启动MSW
|
||||
* @param options 启动选项
|
||||
*/
|
||||
export async function startMSW(options?: { onUnhandledRequest?: 'bypass' | 'warn' | 'error' }) {
|
||||
if (process.env.REACT_APP_USE_MOCK !== 'true') {
|
||||
console.log('[MSW] Mock mode is disabled');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('[MSW] Starting Mock Service Worker...');
|
||||
|
||||
await worker.start({
|
||||
onUnhandledRequest: options?.onUnhandledRequest || 'bypass',
|
||||
});
|
||||
|
||||
console.log('[MSW] Mock Service Worker started');
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止MSW
|
||||
*/
|
||||
export function stopMSW() {
|
||||
worker.stop();
|
||||
console.log('[MSW] Mock Service Worker stopped');
|
||||
}
|
||||
Reference in New Issue
Block a user