2026-03-19 01:39:34 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* [MOCK-003] MSW Mock配置
|
|
|
|
|
|
* 拦截HTTP请求,返回Mock数据
|
|
|
|
|
|
* AI注意: 这是网络层拦截,业务代码完全无感知
|
|
|
|
|
|
*
|
|
|
|
|
|
* @module mock/msw
|
|
|
|
|
|
* @author AI-Frontend-Team
|
|
|
|
|
|
* @created 2026-03-19
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2026-03-19 19:08:15 +08:00
|
|
|
|
import { http, HttpResponse } from 'msw';
|
|
|
|
|
|
import { setupWorker } from 'msw/browser';
|
2026-03-19 01:39:34 +08:00
|
|
|
|
import {
|
|
|
|
|
|
mockCertificates,
|
|
|
|
|
|
getMockCertificates,
|
|
|
|
|
|
getMockCertificateById,
|
|
|
|
|
|
createMockCertificate,
|
|
|
|
|
|
updateMockCertificate,
|
|
|
|
|
|
deleteMockCertificate,
|
|
|
|
|
|
} from './data/certificate.mock';
|
2026-03-21 15:04:06 +08:00
|
|
|
|
import type { Certificate } from '@/types/certificate';
|
2026-03-19 01:39:34 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* MSW请求处理器
|
|
|
|
|
|
* 拦截证书相关API请求
|
|
|
|
|
|
*/
|
|
|
|
|
|
export const handlers = [
|
|
|
|
|
|
// ============================================
|
|
|
|
|
|
// 证书列表查询
|
|
|
|
|
|
// ============================================
|
2026-03-19 19:08:15 +08:00
|
|
|
|
http.get('/api/v1/certificate/certificates', ({ request }) => {
|
|
|
|
|
|
const url = new URL(request.url);
|
|
|
|
|
|
const status = url.searchParams.get('status');
|
|
|
|
|
|
const type = url.searchParams.get('type');
|
|
|
|
|
|
const keyword = url.searchParams.get('keyword');
|
|
|
|
|
|
const page = parseInt(url.searchParams.get('page') || '1');
|
|
|
|
|
|
const pageSize = parseInt(url.searchParams.get('pageSize') || '10');
|
2026-03-19 01:39:34 +08:00
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
2026-03-19 19:08:15 +08:00
|
|
|
|
return HttpResponse.json({
|
|
|
|
|
|
success: true,
|
|
|
|
|
|
data: paginatedData,
|
|
|
|
|
|
total: data.length,
|
|
|
|
|
|
page,
|
|
|
|
|
|
pageSize,
|
|
|
|
|
|
}, { status: 200 });
|
2026-03-19 01:39:34 +08:00
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
|
|
// ============================================
|
|
|
|
|
|
// 证书详情
|
|
|
|
|
|
// ============================================
|
2026-03-19 19:08:15 +08:00
|
|
|
|
http.get('/api/v1/certificate/certificates/:id', ({ params }) => {
|
|
|
|
|
|
const { id } = params;
|
2026-03-19 01:39:34 +08:00
|
|
|
|
const cert = getMockCertificateById(id as string);
|
|
|
|
|
|
|
|
|
|
|
|
console.log('[MSW] GET /api/v1/certificate/certificates/:id', { id });
|
|
|
|
|
|
|
|
|
|
|
|
if (!cert) {
|
2026-03-19 19:08:15 +08:00
|
|
|
|
return HttpResponse.json(
|
|
|
|
|
|
{ success: false, error: '证书不存在' },
|
|
|
|
|
|
{ status: 404 }
|
2026-03-19 01:39:34 +08:00
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-19 19:08:15 +08:00
|
|
|
|
return HttpResponse.json(
|
|
|
|
|
|
{ success: true, data: cert },
|
|
|
|
|
|
{ status: 200 }
|
2026-03-19 01:39:34 +08:00
|
|
|
|
);
|
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
|
|
// ============================================
|
|
|
|
|
|
// 创建证书
|
|
|
|
|
|
// ============================================
|
2026-03-19 19:08:15 +08:00
|
|
|
|
http.post('/api/v1/certificate/certificates', async ({ request }) => {
|
2026-03-21 15:04:06 +08:00
|
|
|
|
const body = await request.json() as Partial<Certificate>;
|
2026-03-19 01:39:34 +08:00
|
|
|
|
|
|
|
|
|
|
console.log('[MSW] POST /api/v1/certificate/certificates', body);
|
|
|
|
|
|
|
|
|
|
|
|
const newCert = createMockCertificate(body);
|
|
|
|
|
|
|
2026-03-19 19:08:15 +08:00
|
|
|
|
return HttpResponse.json(
|
|
|
|
|
|
{
|
2026-03-19 01:39:34 +08:00
|
|
|
|
success: true,
|
|
|
|
|
|
data: { id: newCert.id },
|
2026-03-19 19:08:15 +08:00
|
|
|
|
},
|
|
|
|
|
|
{ status: 200 }
|
2026-03-19 01:39:34 +08:00
|
|
|
|
);
|
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
|
|
// ============================================
|
|
|
|
|
|
// 更新证书
|
|
|
|
|
|
// ============================================
|
2026-03-19 19:08:15 +08:00
|
|
|
|
http.put('/api/v1/certificate/certificates/:id', async ({ params, request }) => {
|
|
|
|
|
|
const { id } = params;
|
2026-03-21 15:04:06 +08:00
|
|
|
|
const body = await request.json() as Partial<Certificate>;
|
2026-03-19 01:39:34 +08:00
|
|
|
|
|
|
|
|
|
|
console.log('[MSW] PUT /api/v1/certificate/certificates/:id', { id, body });
|
|
|
|
|
|
|
|
|
|
|
|
const updated = updateMockCertificate(id as string, body);
|
|
|
|
|
|
|
|
|
|
|
|
if (!updated) {
|
2026-03-19 19:08:15 +08:00
|
|
|
|
return HttpResponse.json(
|
|
|
|
|
|
{ success: false, error: '证书不存在' },
|
|
|
|
|
|
{ status: 404 }
|
2026-03-19 01:39:34 +08:00
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-19 19:08:15 +08:00
|
|
|
|
return HttpResponse.json(
|
|
|
|
|
|
{ success: true, data: updated },
|
|
|
|
|
|
{ status: 200 }
|
2026-03-19 01:39:34 +08:00
|
|
|
|
);
|
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
|
|
// ============================================
|
|
|
|
|
|
// 更新证书状态(审核)
|
|
|
|
|
|
// ============================================
|
2026-03-19 19:08:15 +08:00
|
|
|
|
http.put('/api/v1/certificate/certificates/:id/status', async ({ params, request }) => {
|
|
|
|
|
|
const { id } = params;
|
2026-03-21 15:04:06 +08:00
|
|
|
|
const body = await request.json() as { status: string; approvedBy?: string };
|
|
|
|
|
|
const { status, approvedBy } = body;
|
2026-03-19 01:39:34 +08:00
|
|
|
|
|
|
|
|
|
|
console.log('[MSW] PUT /api/v1/certificate/certificates/:id/status', { id, status, approvedBy });
|
|
|
|
|
|
|
2026-03-21 15:04:06 +08:00
|
|
|
|
const updates: Partial<Certificate> = { status: status as Certificate['status'] };
|
2026-03-19 01:39:34 +08:00
|
|
|
|
if (status === 'APPROVED') {
|
|
|
|
|
|
updates.approvedBy = approvedBy || 'admin';
|
|
|
|
|
|
updates.approvedDate = new Date().toISOString().split('T')[0];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const updated = updateMockCertificate(id as string, updates);
|
|
|
|
|
|
|
|
|
|
|
|
if (!updated) {
|
2026-03-19 19:08:15 +08:00
|
|
|
|
return HttpResponse.json(
|
|
|
|
|
|
{ success: false, error: '证书不存在' },
|
|
|
|
|
|
{ status: 404 }
|
2026-03-19 01:39:34 +08:00
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-19 19:08:15 +08:00
|
|
|
|
return HttpResponse.json(
|
|
|
|
|
|
{ success: true },
|
|
|
|
|
|
{ status: 200 }
|
2026-03-19 01:39:34 +08:00
|
|
|
|
);
|
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
|
|
// ============================================
|
|
|
|
|
|
// 删除证书
|
|
|
|
|
|
// ============================================
|
2026-03-19 19:08:15 +08:00
|
|
|
|
http.delete('/api/v1/certificate/certificates/:id', ({ params }) => {
|
|
|
|
|
|
const { id } = params;
|
2026-03-19 01:39:34 +08:00
|
|
|
|
|
|
|
|
|
|
console.log('[MSW] DELETE /api/v1/certificate/certificates/:id', { id });
|
|
|
|
|
|
|
|
|
|
|
|
const deleted = deleteMockCertificate(id as string);
|
|
|
|
|
|
|
|
|
|
|
|
if (!deleted) {
|
2026-03-19 19:08:15 +08:00
|
|
|
|
return HttpResponse.json(
|
|
|
|
|
|
{ success: false, error: '证书不存在' },
|
|
|
|
|
|
{ status: 404 }
|
2026-03-19 01:39:34 +08:00
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-19 19:08:15 +08:00
|
|
|
|
return HttpResponse.json(
|
|
|
|
|
|
{ success: true },
|
|
|
|
|
|
{ status: 200 }
|
2026-03-19 01:39:34 +08:00
|
|
|
|
);
|
|
|
|
|
|
}),
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 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');
|
|
|
|
|
|
}
|