feat: 添加mock数据
This commit is contained in:
159
src/mocks/handlers/concept.js
Normal file
159
src/mocks/handlers/concept.js
Normal file
@@ -0,0 +1,159 @@
|
|||||||
|
// src/mocks/handlers/concept.js
|
||||||
|
// 概念相关的 Mock Handlers
|
||||||
|
|
||||||
|
import { http, HttpResponse } from 'msw';
|
||||||
|
|
||||||
|
// 模拟延迟
|
||||||
|
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
||||||
|
|
||||||
|
// 生成热门概念数据
|
||||||
|
const generatePopularConcepts = (size = 20) => {
|
||||||
|
const concepts = [
|
||||||
|
'人工智能', '新能源汽车', '半导体', '光伏', '锂电池',
|
||||||
|
'储能', '氢能源', '风电', '特高压', '工业母机',
|
||||||
|
'军工', '航空航天', '卫星导航', '量子科技', '数字货币',
|
||||||
|
'云计算', '大数据', '物联网', '5G', '6G',
|
||||||
|
'元宇宙', '虚拟现实', 'AIGC', 'ChatGPT', '算力',
|
||||||
|
'芯片设计', '芯片制造', '半导体设备', '半导体材料', 'EDA',
|
||||||
|
'新能源', '风光储', '充电桩', '智能电网', '特斯拉',
|
||||||
|
'比亚迪', '宁德时代', '华为', '苹果产业链', '鸿蒙',
|
||||||
|
'国产软件', '信创', '网络安全', '数据安全', '量子通信',
|
||||||
|
'医疗器械', '创新药', '医美', 'CXO', '生物医药',
|
||||||
|
'疫苗', '中药', '医疗信息化', '智慧医疗', '基因测序'
|
||||||
|
];
|
||||||
|
|
||||||
|
const results = [];
|
||||||
|
for (let i = 0; i < Math.min(size, concepts.length); i++) {
|
||||||
|
const changePct = (Math.random() * 12 - 2).toFixed(2); // -2% 到 +10%
|
||||||
|
const stockCount = Math.floor(Math.random() * 50) + 10; // 10-60 只股票
|
||||||
|
|
||||||
|
results.push({
|
||||||
|
concept: concepts[i],
|
||||||
|
concept_id: `CONCEPT_${1000 + i}`,
|
||||||
|
stock_count: stockCount,
|
||||||
|
price_info: {
|
||||||
|
avg_change_pct: parseFloat(changePct),
|
||||||
|
avg_price: (Math.random() * 100 + 10).toFixed(2),
|
||||||
|
total_market_cap: (Math.random() * 1000 + 100).toFixed(2)
|
||||||
|
},
|
||||||
|
description: `${concepts[i]}相关概念股`,
|
||||||
|
hot_score: Math.floor(Math.random() * 100)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 按涨跌幅降序排序
|
||||||
|
results.sort((a, b) => b.price_info.avg_change_pct - a.price_info.avg_change_pct);
|
||||||
|
|
||||||
|
return results;
|
||||||
|
};
|
||||||
|
|
||||||
|
// 概念相关的 Handlers
|
||||||
|
export const conceptHandlers = [
|
||||||
|
// 搜索概念(热门概念)
|
||||||
|
http.post('/concept-api/search', async ({ request }) => {
|
||||||
|
await delay(300);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const body = await request.json();
|
||||||
|
const { query = '', size = 20, page = 1, sort_by = 'change_pct' } = body;
|
||||||
|
|
||||||
|
console.log('[Mock Concept] 搜索概念:', { query, size, page, sort_by });
|
||||||
|
|
||||||
|
// 生成数据
|
||||||
|
let results = generatePopularConcepts(size);
|
||||||
|
|
||||||
|
// 如果有查询关键词,过滤结果
|
||||||
|
if (query) {
|
||||||
|
results = results.filter(item =>
|
||||||
|
item.concept.toLowerCase().includes(query.toLowerCase())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 根据排序字段排序
|
||||||
|
if (sort_by === 'change_pct') {
|
||||||
|
results.sort((a, b) => b.price_info.avg_change_pct - a.price_info.avg_change_pct);
|
||||||
|
} else if (sort_by === 'stock_count') {
|
||||||
|
results.sort((a, b) => b.stock_count - a.stock_count);
|
||||||
|
} else if (sort_by === 'hot_score') {
|
||||||
|
results.sort((a, b) => b.hot_score - a.hot_score);
|
||||||
|
}
|
||||||
|
|
||||||
|
return HttpResponse.json({
|
||||||
|
results,
|
||||||
|
total: results.length,
|
||||||
|
page,
|
||||||
|
size,
|
||||||
|
message: '搜索成功'
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error('[Mock Concept] 搜索概念失败:', error);
|
||||||
|
return HttpResponse.json(
|
||||||
|
{
|
||||||
|
results: [],
|
||||||
|
total: 0,
|
||||||
|
error: '搜索失败'
|
||||||
|
},
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
|
||||||
|
// 获取单个概念详情
|
||||||
|
http.get('/concept-api/concepts/:conceptId', async ({ params }) => {
|
||||||
|
await delay(300);
|
||||||
|
|
||||||
|
const { conceptId } = params;
|
||||||
|
console.log('[Mock Concept] 获取概念详情:', conceptId);
|
||||||
|
|
||||||
|
const concepts = generatePopularConcepts(50);
|
||||||
|
const concept = concepts.find(c => c.concept_id === conceptId || c.concept === conceptId);
|
||||||
|
|
||||||
|
if (concept) {
|
||||||
|
return HttpResponse.json({
|
||||||
|
...concept,
|
||||||
|
related_stocks: [
|
||||||
|
{ stock_code: '600519', stock_name: '贵州茅台', change_pct: 2.34 },
|
||||||
|
{ stock_code: '000858', stock_name: '五粮液', change_pct: 1.89 },
|
||||||
|
{ stock_code: '000568', stock_name: '泸州老窖', change_pct: 3.12 }
|
||||||
|
],
|
||||||
|
news: [
|
||||||
|
{ title: `${concept.concept}板块异动`, date: '2024-10-24', source: '财经新闻' }
|
||||||
|
]
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
return HttpResponse.json(
|
||||||
|
{ error: '概念不存在' },
|
||||||
|
{ status: 404 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
|
||||||
|
// 获取概念相关股票
|
||||||
|
http.get('/concept-api/concepts/:conceptId/stocks', async ({ params, request }) => {
|
||||||
|
await delay(300);
|
||||||
|
|
||||||
|
const { conceptId } = params;
|
||||||
|
const url = new URL(request.url);
|
||||||
|
const limit = parseInt(url.searchParams.get('limit') || '20');
|
||||||
|
|
||||||
|
console.log('[Mock Concept] 获取概念相关股票:', { conceptId, limit });
|
||||||
|
|
||||||
|
// 生成模拟股票数据
|
||||||
|
const stocks = [];
|
||||||
|
for (let i = 0; i < limit; i++) {
|
||||||
|
stocks.push({
|
||||||
|
stock_code: `${600000 + i}`,
|
||||||
|
stock_name: `股票${i + 1}`,
|
||||||
|
change_pct: (Math.random() * 10 - 2).toFixed(2),
|
||||||
|
price: (Math.random() * 100 + 10).toFixed(2),
|
||||||
|
market_cap: (Math.random() * 1000 + 100).toFixed(2)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return HttpResponse.json({
|
||||||
|
stocks,
|
||||||
|
total: stocks.length,
|
||||||
|
concept_id: conceptId
|
||||||
|
});
|
||||||
|
})
|
||||||
|
];
|
||||||
@@ -7,6 +7,7 @@ import { simulationHandlers } from './simulation';
|
|||||||
import { eventHandlers } from './event';
|
import { eventHandlers } from './event';
|
||||||
import { paymentHandlers } from './payment';
|
import { paymentHandlers } from './payment';
|
||||||
import { industryHandlers } from './industry';
|
import { industryHandlers } from './industry';
|
||||||
|
import { conceptHandlers } from './concept';
|
||||||
|
|
||||||
// 可以在这里添加更多的 handlers
|
// 可以在这里添加更多的 handlers
|
||||||
// import { userHandlers } from './user';
|
// import { userHandlers } from './user';
|
||||||
@@ -18,5 +19,6 @@ export const handlers = [
|
|||||||
...eventHandlers,
|
...eventHandlers,
|
||||||
...paymentHandlers,
|
...paymentHandlers,
|
||||||
...industryHandlers,
|
...industryHandlers,
|
||||||
|
...conceptHandlers,
|
||||||
// ...userHandlers,
|
// ...userHandlers,
|
||||||
];
|
];
|
||||||
|
|||||||
Reference in New Issue
Block a user