mockData.js
17.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
/**
* @Description: Mock 数据生成工具 - 用于测试分页加载功能
* @Date: 2026-02-08
*
* 支持的 API Mock:
* - weekHotAPI: 周热门资料
* - fileListAPI: 资料列表
* - listAPI: 产品列表
* - searchAPI: 搜索(产品+资料)
* - myListAPI: 消息列表
*/
// ============================================================================
// 工具函数
// ============================================================================
/**
* 生成随机文件大小
* @returns {string} 文件大小(如 "2.5MB")
*/
function generateRandomSize() {
const sizeInMB = (Math.random() * 10 + 0.5).toFixed(1)
return `${sizeInMB}MB`
}
/**
* 生成随机学习人数
* @returns {number} 学习人数(100-5000之间)
*/
function generateRandomReadCount() {
return Math.floor(Math.random() * 4900) + 100
}
/**
* 生成随机学习百分比
* @returns {number} 学习百分比(0-100之间)
*/
function generateRandomReadPercent() {
return Math.floor(Math.random() * 100)
}
/**
* 生成随机收藏状态
* @returns {string} '1' 或 '0'
*/
function generateRandomFavorite() {
return Math.random() > 0.7 ? '1' : '0'
}
/**
* 模拟网络延迟
* @param {number} min 最小延迟(ms)
* @param {number} max 最大延迟(ms)
* @returns {Promise}
*/
function mockDelay(min = 100, max = 300) {
const delay = Math.random() * (max - min) + min
return new Promise(resolve => setTimeout(resolve, delay))
}
// ============================================================================
// 1. 周热门资料 Mock (weekHotAPI)
// ============================================================================
const WEEK_HOT_MATERIALS = [
'财富管理基础知识指南',
'保险产品销售技巧',
'客户关系管理实战',
'家庭资产配置方案',
'税务筹划实用手册',
'退休规划完整教程',
'投资组合管理策略',
'风险控制与合规要求',
'高净值客户开发指南',
'理财产品营销话术',
'基金定投实战技巧',
'保单整理服务流程',
'传承规划案例分析',
'健康险产品对比分析',
'年金保险销售指南',
'重疾险核保知识',
'教育金规划方案',
'房贷规划实务操作',
'家族信托业务介绍',
'私募股权投资指南'
]
const FILE_TYPES = [
{ extension: 'pdf', name: 'PDF文档' },
{ extension: 'doc', name: 'Word文档' },
{ extension: 'docx', name: 'Word文档' },
{ extension: 'xls', name: 'Excel表格' },
{ extension: 'xlsx', name: 'Excel表格' },
{ extension: 'ppt', name: 'PPT演示文稿' },
{ extension: 'pptx', name: 'PPT演示文稿' },
{ extension: 'txt', name: '文本文件' },
{ extension: 'jpg', name: '图片' },
{ extension: 'png', name: '图片' }
]
/**
* 生成周热门资料数据
*/
function generateWeekHotItem(id) {
const fileType = FILE_TYPES[Math.floor(Math.random() * FILE_TYPES.length)]
const materialName = WEEK_HOT_MATERIALS[Math.floor(Math.random() * WEEK_HOT_MATERIALS.length)]
return {
meta_id: id,
name: `${materialName} ${fileType.name.toUpperCase()}`,
src: `https://placehold.co/100x100/e2e8f0/475569?text=${fileType.extension.toUpperCase()}`,
size: generateRandomSize(),
read_people_count: generateRandomReadCount(),
read_people_percent: generateRandomReadPercent(),
is_favorite: generateRandomFavorite(),
extension: fileType.extension
}
}
/**
* Mock: weekHotAPI
*/
export async function mockWeekHotAPI(params) {
await mockDelay()
const { page = 0, limit = 20 } = params
const totalPages = 5
if (page >= totalPages) {
return { code: 1, msg: 'success', data: { list: [] } }
}
const list = []
const startIndex = page * limit
for (let i = 0; i < limit; i++) {
list.push(generateWeekHotItem(startIndex + i + 1))
}
console.log(`[Mock] weekHotAPI - 第${page}页,共${list.length}条`)
return { code: 1, msg: 'success', data: { list } }
}
// ============================================================================
// 2. 资料列表 Mock (fileListAPI)
// ============================================================================
const MATERIAL_NAMES = [
'2024年保险行业发展趋势报告',
'高净值客户开发实战手册',
'家庭保障需求分析模板',
'养老规划产品对比表',
'教育金储备方案',
'重疾险条款解读',
'百万医疗险销售指南',
'年金险产品培训资料',
'终身寿险销售技巧',
'车险理赔流程说明',
'企业财产险基础知识',
'责任险产品介绍',
'意外险保障方案',
'健康险核保手册',
'投保实务操作指南',
'客户异议处理话术',
'保单托管服务流程',
'理赔案例分析',
'保险法律法规汇编',
'行业合规要求解读'
]
/**
* 生成资料列表项
*/
function generateMaterialItem(id) {
const fileType = FILE_TYPES[Math.floor(Math.random() * FILE_TYPES.length)]
const materialName = MATERIAL_NAMES[Math.floor(Math.random() * MATERIAL_NAMES.length)]
return {
id: id,
meta_id: id,
name: materialName,
title: materialName,
fileName: `${materialName}.${fileType.extension}`,
desc: '这是一份详细的培训资料,包含丰富的案例和实战技巧...',
size: generateRandomSize(),
extension: fileType.extension,
collected: generateRandomFavorite() === '1',
src: `https://placehold.co/100x100/e2e8f0/475569?text=${fileType.extension.toUpperCase()}`,
downloadUrl: `https://placehold.co/100x100/e2e8f0/475569?text=${fileType.extension.toUpperCase()}`,
post_date: new Date().toISOString(),
value: `https://placehold.co/100x100/e2e8f0/475569?text=${fileType.extension.toUpperCase()}`
}
}
/**
* Mock: fileListAPI
*/
export async function mockFileListAPI(params) {
await mockDelay()
const { page = 0, limit = 20, cid, keyword, child_id } = params
const totalPages = 8
if (page >= totalPages) {
return { code: 1, msg: 'success', data: { list: [], total: totalPages * limit } }
}
const list = []
const startIndex = page * limit
for (let i = 0; i < limit; i++) {
const item = generateMaterialItem(startIndex + i + 1)
// 如果有关键词搜索,过滤数据
if (keyword && !item.name.includes(keyword)) {
continue
}
list.push(item)
}
console.log(`[Mock] fileListAPI - 第${page}页,共${list.length}条`)
return {
code: 1,
msg: 'success',
data: {
list,
total: totalPages * limit,
max_level: 2,
cate: {
id: parseInt(cid) || 1,
category_name: '培训资料',
category_parent: 0,
category_description: null
}
}
}
}
// ============================================================================
// 3. 产品列表 Mock (listAPI)
// ============================================================================
const PRODUCT_NAMES = [
'百万年金保险计划',
'终身寿险至尊版',
'重疾险保障计划',
'百万医疗险',
'意外伤害保险',
'教育金保险计划',
'养老理财保险',
'高端医疗险',
'定期寿险',
'终身寿险',
'企业年金保险',
'团体意外险',
'家庭财产保险',
'责任保险系列',
'旅游保险',
'留学保险',
'健康保险计划',
'车辆保险',
'财产一切险',
'工程保险'
]
const PRODUCT_TAGS = [
{ id: 1, name: '热销', bg_color: '#FEE2E2', text_color: '#DC2626' },
{ id: 2, name: '新品', bg_color: '#DBEAFE', text_color: '#2563EB' },
{ id: 3, name: '推荐', bg_color: '#D1FAE5', text_color: '#059669' },
{ id: 4, name: '限时', bg_color: '#FEF3C7', text_color: '#D97706' }
]
const PRODUCT_CATEGORIES = [
{ id: 1, name: '人寿保险' },
{ id: 2, name: '健康保险' },
{ id: 3, name: '意外保险' },
{ id: 4, name: '财产保险' }
]
/**
* 生成产品列表项
*/
function generateProductItem(id) {
const productName = PRODUCT_NAMES[Math.floor(Math.random() * PRODUCT_NAMES.length)]
const recommend = Math.random() > 0.7 ? 'hot' : ''
// 随机选择1-2个标签
const tags = []
const tagCount = Math.floor(Math.random() * 2) + 1
const availableTags = [...PRODUCT_TAGS].sort(() => Math.random() - 0.5)
for (let i = 0; i < tagCount; i++) {
tags.push(availableTags[i])
}
return {
id: id,
product_name: productName,
name: productName,
cover_image: `https://placehold.co/400x300/4caf50/ffffff?text=${encodeURIComponent(productName.substring(0, 6))}`,
recommend: recommend,
tags: tags,
description: '这是一款优质的保险产品,为您的家庭提供全面保障...',
premium: Math.floor(Math.random() * 10000 + 1000),
category_id: Math.floor(Math.random() * 4) + 1
}
}
/**
* Mock: listAPI (产品列表)
*/
export async function mockProductListAPI(params) {
await mockDelay()
const { page = 0, limit = 10, cid, keyword } = params
const totalPages = 10
if (page >= totalPages) {
return { code: 1, msg: 'success', data: { list: [], categories: [], total: 0 } }
}
const list = []
const startIndex = page * limit
for (let i = 0; i < limit; i++) {
const item = generateProductItem(startIndex + i + 1)
// 如果有分类过滤
if (cid && item.category_id !== parseInt(cid)) {
continue
}
// 如果有关键词搜索
if (keyword && !item.product_name.includes(keyword)) {
continue
}
list.push(item)
}
console.log(`[Mock] listAPI - 第${page}页,共${list.length}条`)
return {
code: 1,
msg: 'success',
data: {
list,
categories: PRODUCT_CATEGORIES,
total: totalPages * limit
}
}
}
// ============================================================================
// 4. 搜索 Mock (searchAPI)
// ============================================================================
/**
* Mock: searchAPI (支持产品和资料搜索)
*/
export async function mockSearchAPI(params) {
await mockDelay()
const { page = 0, limit = 20, keyword, type } = params
if (!keyword) {
// 🔧 优化:如果没有关键词,返回更多推荐数据用于测试
// 生成20个产品和20个资料作为默认搜索结果
const defaultProducts = []
const defaultFiles = []
for (let i = 0; i < 20; i++) {
const productItem = generateProductItem(i + 1)
defaultProducts.push({
...productItem,
id: i + 1,
cover_image: productItem.cover_image
})
const materialItem = generateMaterialItem(i + 1)
defaultFiles.push({
...materialItem,
id: i + 1,
fileName: materialItem.fileName,
fileSize: materialItem.size,
learners: `${materialItem.read_people_count}人学习`,
readPeoplePercent: materialItem.read_people_percent,
collected: materialItem.collected,
extension: materialItem.extension,
downloadUrl: materialItem.downloadUrl,
title: materialItem.title,
src: materialItem.src
})
}
console.log(`[Mock] searchAPI - 无关键词,返回默认数据:产品${defaultProducts.length}条,资料${defaultFiles.length}条`)
return {
code: 1,
msg: 'success',
data: {
products: { list: defaultProducts, total: defaultProducts.length * 5 },
files: { list: defaultFiles, total: defaultFiles.length * 5 }
}
}
}
const totalPages = 5
if (page >= totalPages) {
return {
code: 1,
msg: 'success',
data: {
products: { list: [], total: 0 },
files: { list: [], total: 0 }
}
}
}
const products = []
const files = []
const startIndex = page * limit
// 🔧 优化:每次循环都生成数据和尝试匹配,增加命中率
for (let i = 0; i < limit / 2; i++) {
// 产品
const productItem = generateProductItem(startIndex + i + 1)
const productName = productItem.product_name.toLowerCase()
const searchKeyword = keyword.toLowerCase()
// 🔧 优化:更宽松的搜索条件
// 1. 完全匹配
// 2. 拆分关键词,包含任意一个字符即可
// 3. 关键词长度 >= 2 时,只要产品名称包含任意连续2个字符
const keywords = searchKeyword.split('').filter(k => k.trim())
const hasAnyChar = keywords.length > 0 && keywords.some(k => productName.includes(k))
const hasBigram = searchKeyword.length >= 2 && keywords.slice(0, -1).some((k, idx) => productName.includes(k + keywords[idx + 1]))
if (productName.includes(searchKeyword) || hasAnyChar || hasBigram) {
products.push({
...productItem,
id: startIndex + i + 1,
cover_image: productItem.cover_image
})
}
// 资料
const materialItem = generateMaterialItem(startIndex + i + 100)
const materialName = materialItem.name.toLowerCase()
const hasAnyCharMaterial = keywords.length > 0 && keywords.some(k => materialName.includes(k))
const hasBigramMaterial = searchKeyword.length >= 2 && keywords.slice(0, -1).some((k, idx) => materialName.includes(k + keywords[idx + 1]))
if (materialName.includes(searchKeyword) || hasAnyCharMaterial || hasBigramMaterial) {
files.push({
...materialItem,
id: startIndex + i + 100,
fileName: materialItem.fileName,
fileSize: materialItem.size,
learners: `${materialItem.read_people_count}人学习`,
readPeoplePercent: materialItem.read_people_percent,
collected: materialItem.collected,
extension: materialItem.extension,
downloadUrl: materialItem.downloadUrl,
title: materialItem.title,
src: materialItem.src
})
}
}
// 🔧 优化:如果没有匹配到任何数据,返回一些推荐数据
if (products.length === 0 && files.length === 0) {
console.log(`[Mock] searchAPI - 无匹配结果,返回推荐数据`)
// 生成 5 个推荐产品
for (let i = 0; i < 5; i++) {
const productItem = generateProductItem(startIndex + i + 1)
products.push({
...productItem,
id: startIndex + i + 1,
cover_image: productItem.cover_image
})
const materialItem = generateMaterialItem(startIndex + i + 100)
files.push({
...materialItem,
id: startIndex + i + 100,
fileName: materialItem.fileName,
fileSize: materialItem.size,
learners: `${materialItem.read_people_count}人学习`,
readPeoplePercent: materialItem.read_people_percent,
collected: materialItem.collected,
extension: materialItem.extension,
downloadUrl: materialItem.downloadUrl,
title: materialItem.title,
src: materialItem.src
})
}
}
console.log(`[Mock] searchAPI - 第${page}页,关键词"${keyword}",产品${products.length}条,资料${files.length}条`)
return {
code: 1,
msg: 'success',
data: {
products: { list: products, total: products.length * totalPages },
files: { list: files, total: files.length * totalPages }
}
}
}
// ============================================================================
// 5. 消息列表 Mock (myListAPI)
// ============================================================================
const MESSAGE_TITLES = [
'关于2024年新产品上线通知',
'系统升级维护公告',
'您的保单已生效提醒',
'理赔进度更新通知',
'续费提醒',
'活动邀请:财富管理讲座',
'客户服务满意度调查',
'最新培训资料已上线',
'合规要求更新通知',
'节日问候与祝福',
'产品停售通知',
'核保政策调整',
'理赔流程优化说明',
'客户权益保障计划',
'数字化服务升级公告'
]
/**
* 生成消息列表项
*/
function generateMessageItem(id) {
const title = MESSAGE_TITLES[Math.floor(Math.random() * MESSAGE_TITLES.length)]
const now = new Date()
const createDate = new Date(now.getTime() - Math.random() * 30 * 24 * 60 * 60 * 1000)
return {
id: id,
title: title,
intro: `这是一条关于"${title}"的重要通知,请及时查看详情...`,
content: `这是一条关于"${title}"的重要通知,请及时查看详情。如需了解更多信息,请联系客服。`,
create_time: formatDate(createDate),
is_read: Math.random() > 0.5 ? 1 : 0,
type: Math.random() > 0.5 ? 'notice' : 'system'
}
}
/**
* 格式化日期
*/
function formatDate(date) {
const year = date.getFullYear()
const month = String(date.getMonth() + 1).padStart(2, '0')
const day = String(date.getDate()).padStart(2, '0')
return `${year}-${month}-${day}`
}
/**
* Mock: myListAPI (消息列表)
*/
export async function mockMessageListAPI(params) {
await mockDelay()
const { page = 1, limit = 10 } = params
const totalPages = 8
if (page > totalPages) {
return { code: 1, msg: 'success', data: { list: [] } }
}
const list = []
const startIndex = (page - 1) * limit
for (let i = 0; i < limit; i++) {
list.push(generateMessageItem(startIndex + i + 1))
}
console.log(`[Mock] myListAPI - 第${page}页,共${list.length}条`)
return {
code: 1,
msg: 'success',
data: { list }
}
}
// ============================================================================
// 导出统一 Mock API 调用器
// ============================================================================
/**
* Mock API 调用器
* @param {string} apiName - API 名称
* @param {Object} params - 请求参数
* @returns {Promise}
*/
export async function mockAPI(apiName, params) {
switch (apiName) {
case 'weekHotAPI':
return await mockWeekHotAPI(params)
case 'fileListAPI':
return await mockFileListAPI(params)
case 'listAPI':
return await mockProductListAPI(params)
case 'searchAPI':
return await mockSearchAPI(params)
case 'myListAPI':
return await mockMessageListAPI(params)
default:
console.warn(`[Mock] 未知的 API: ${apiName}`)
return { code: 0, msg: 'Unknown API', data: null }
}
}