feat(mock): 优化产品列表 Mock 数据结构
- 修复 Mock 数据结构与真实 API 一致性 - 新增 form_sn 和 created_time 字段 - 修复 categories 为数组结构 - 添加测试商品置顶功能(savings-product-30b41aae) - 移除多余字段 Co-Authored-By: Claude Opus 4.6
Showing
1 changed file
with
48 additions
and
6 deletions
| ... | @@ -363,7 +363,14 @@ const PRODUCT_CATEGORIES = [ | ... | @@ -363,7 +363,14 @@ const PRODUCT_CATEGORIES = [ |
| 363 | /** | 363 | /** |
| 364 | * 生成产品列表项 | 364 | * 生成产品列表项 |
| 365 | */ | 365 | */ |
| 366 | -function generateProductItem(id) { | 366 | +/** |
| 367 | + * 生成产品列表项(符合真实 API 结构) | ||
| 368 | + * | ||
| 369 | + * @param {number} id - 产品 ID | ||
| 370 | + * @param {string} formSn - 表单标识(可选) | ||
| 371 | + * @returns {Object} 产品对象 | ||
| 372 | + */ | ||
| 373 | +function generateProductItem(id, formSn) { | ||
| 367 | const productName = PRODUCT_NAMES[Math.floor(Math.random() * PRODUCT_NAMES.length)] | 374 | const productName = PRODUCT_NAMES[Math.floor(Math.random() * PRODUCT_NAMES.length)] |
| 368 | const recommend = Math.random() > 0.7 ? 'hot' : '' | 375 | const recommend = Math.random() > 0.7 ? 'hot' : '' |
| 369 | 376 | ||
| ... | @@ -376,16 +383,19 @@ function generateProductItem(id) { | ... | @@ -376,16 +383,19 @@ function generateProductItem(id) { |
| 376 | tags.push(availableTags[i]) | 383 | tags.push(availableTags[i]) |
| 377 | } | 384 | } |
| 378 | 385 | ||
| 386 | + // 随机选择分类 | ||
| 387 | + const categoryId = Math.floor(Math.random() * 4) + 1 | ||
| 388 | + const category = PRODUCT_CATEGORIES.find(c => parseInt(c.id) === categoryId) | ||
| 389 | + | ||
| 379 | return { | 390 | return { |
| 380 | id: id, | 391 | id: id, |
| 381 | product_name: productName, | 392 | product_name: productName, |
| 382 | - name: productName, | ||
| 383 | cover_image: `https://picsum.photos/seed/product-${id}/400/300`, | 393 | cover_image: `https://picsum.photos/seed/product-${id}/400/300`, |
| 384 | recommend: recommend, | 394 | recommend: recommend, |
| 385 | - tags: tags, | 395 | + form_sn: formSn || `product-template-${categoryId}`, // 关键:对应模板的 form_sn |
| 386 | - description: '这是一款优质的保险产品,为您的家庭提供全面保障...', | 396 | + created_time: new Date().toISOString(), |
| 387 | - premium: Math.floor(Math.random() * 10000 + 1000), | 397 | + categories: [category], // ✅ 真实 API 结构:categories 是数组 |
| 388 | - category_id: Math.floor(Math.random() * 4) + 1 | 398 | + tags: tags |
| 389 | } | 399 | } |
| 390 | } | 400 | } |
| 391 | 401 | ||
| ... | @@ -405,6 +415,38 @@ export async function mockProductListAPI(params) { | ... | @@ -405,6 +415,38 @@ export async function mockProductListAPI(params) { |
| 405 | const list = [] | 415 | const list = [] |
| 406 | const startIndex = page * limit | 416 | const startIndex = page * limit |
| 407 | 417 | ||
| 418 | + // 🔧 测试商品:第一页第一位固定为储蓄产品(form_sn:savings-product-30b41aae) | ||
| 419 | + if (page === 0) { | ||
| 420 | + const testCategory = PRODUCT_CATEGORIES.find(c => parseInt(c.id) === 1) | ||
| 421 | + const testProduct = { | ||
| 422 | + id: 'savings-product-30b41aae', | ||
| 423 | + product_name: '储蓄产品测试(form_sn:savings-product-30b41aae)', | ||
| 424 | + cover_image: 'https://picsum.photos/seed/savings-product-30b41aae/400/300', | ||
| 425 | + recommend: 'hot', | ||
| 426 | + form_sn: 'savings-product-30b41aae', // ✅ 关键字段:对应真实 API 的 form_sn | ||
| 427 | + created_time: new Date().toISOString(), | ||
| 428 | + categories: [testCategory], // ✅ 符合真实 API 结构:categories 是数组 | ||
| 429 | + tags: [{ id: '1', name: '热销', bg_color: '#FEE2E2', text_color: '#DC2626' }], | ||
| 430 | + // 测试标识(不影响业务逻辑) | ||
| 431 | + _test: true, | ||
| 432 | + _test_note: 'form_sn:savings-product-30b41aae' | ||
| 433 | + } | ||
| 434 | + | ||
| 435 | + // 检查分类和关键词过滤 | ||
| 436 | + let shouldInclude = true | ||
| 437 | + if (cid && !testProduct.categories.some(c => parseInt(c.id) === parseInt(cid))) { | ||
| 438 | + shouldInclude = false | ||
| 439 | + } | ||
| 440 | + if (keyword && !testProduct.product_name.includes(keyword)) { | ||
| 441 | + shouldInclude = false | ||
| 442 | + } | ||
| 443 | + | ||
| 444 | + if (shouldInclude) { | ||
| 445 | + list.push(testProduct) | ||
| 446 | + console.log('[Mock] listAPI - 测试商品已置顶: form_sn=savings-product-30b41aae') | ||
| 447 | + } | ||
| 448 | + } | ||
| 449 | + | ||
| 408 | for (let i = 0; i < limit; i++) { | 450 | for (let i = 0; i < limit; i++) { |
| 409 | const item = generateProductItem(startIndex + i + 1) | 451 | const item = generateProductItem(startIndex + i + 1) |
| 410 | 452 | ... | ... |
-
Please register or login to post a comment