product.js
5.25 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
/**
* @description Mock 数据 - 产品中心
* @note 用于测试滚动加载更多功能
*/
// Mock 分类数据
export const mockCategories = [
{ id: 1, name: '寿险' },
{ id: 2, name: '健康险' },
{ id: 3, name: '意外险' },
{ id: 4, name: '年金险' },
{ id: 5, name: '重疾险' }
]
// Mock 标签数据
const mockTags = [
{ id: 1, name: '热销', bg_color: '#FEF3C7', text_color: '#92400E' },
{ id: 2, name: '新品', bg_color: '#DBEAFE', text_color: '#1E40AF' },
{ id: 3, name: '推荐', bg_color: '#D1FAE5', text_color: '#065F46' },
{ id: 4, name: '限时', bg_color: '#FEE2E2', text_color: '#991B1B' }
]
// 生成单个产品数据
const generateProduct = (id) => {
const recommendTypes = ['normal', 'hot']
const recommend = recommendTypes[Math.floor(Math.random() * recommendTypes.length)]
// 随机选择 1-2 个标签
const tagCount = Math.floor(Math.random() * 2) + 1
const tags = []
for (let i = 0; i < tagCount; i++) {
const tag = mockTags[Math.floor(Math.random() * mockTags.length)]
if (!tags.find(t => t.id === tag.id)) {
tags.push(tag)
}
}
// 随机选择 1-2 个分类
const categoryCount = Math.floor(Math.random() * 2) + 1
const categories = []
for (let i = 0; i < categoryCount; i++) {
const category = mockCategories[Math.floor(Math.random() * mockCategories.length)]
if (!categories.find(c => c.id === category.id)) {
categories.push(category)
}
}
return {
id: id,
product_name: `测试产品 ${id} - ${categories.map(c => c.name).join('+')}`,
recommend: recommend,
form_sn: `product_form_${id}`,
created_time: '2025-12-01 12:00:00',
categories: categories,
tags: tags,
// 使用 Lorem Picsum 随机图片服务(基于产品 ID 确保图片固定)
cover_image: `https://picsum.photos/300/200?random=${id}`
}
}
// 生成产品列表
const generateProductList = (page, limit, cid = null) => {
const start = page * limit
const end = start + limit
// 如果指定了分类,只返回该分类的产品
let filteredProducts = []
if (cid) {
// 为每个分类生成固定数量的产品
const categoryProducts = []
for (let i = 1; i <= 50; i++) {
const product = generateProduct(i)
// 强制该产品属于指定分类
product.categories = mockCategories.find(c => String(c.id) === String(cid))
? [mockCategories.find(c => String(c.id) === String(cid))]
: [{ id: parseInt(cid), name: '测试分类' }]
categoryProducts.push(product)
}
filteredProducts = categoryProducts
} else {
// 全部产品
for (let i = 1; i <= 100; i++) {
filteredProducts.push(generateProduct(i))
}
}
const total = filteredProducts.length
const list = filteredProducts.slice(start, end)
return {
list,
total,
hasMore: end < total
}
}
/**
* Mock 产品列表 API
* @param {Object} params 请求参数
* @param {string} params.page 页码(从 0 开始)
* @param {string} params.limit 每页数量
* @param {string} params.cid 分类 ID(可选)
* @param {string} params.keyword 搜索关键词(可选)
* @returns {Promise} 模拟 API 响应
*/
export const mockListAPI = (params) => {
return new Promise((resolve) => {
// 模拟网络延迟(300-800ms)
const delay = Math.floor(Math.random() * 500) + 300
setTimeout(() => {
const page = parseInt(params.page) || 0
const limit = parseInt(params.limit) || 10
const cid = params.cid || null
const keyword = params.keyword || ''
let result = generateProductList(page, limit, cid)
// 如果有搜索关键词,过滤产品
if (keyword) {
result.list = result.list.filter(p =>
p.product_name.includes(keyword)
)
// 搜索时重新计算总数
result.total = result.list.length + Math.floor(Math.random() * 20)
}
resolve({
code: 1,
msg: 'success',
data: {
categories: mockCategories,
list: result.list,
total: result.total
}
})
}, delay)
})
}
/**
* Mock 产品详情 API
* @param {Object} params 请求参数
* @param {string} params.i 产品 ID
* @returns {Promise} 模拟 API 响应
*/
export const mockDetailAPI = (params) => {
return new Promise((resolve) => {
const delay = Math.floor(Math.random() * 500) + 300
setTimeout(() => {
const id = parseInt(params.i) || 1
const product = generateProduct(id)
// 添加额外的详情字段
product.product_description = `这是产品 ${id} 的详细描述。\n\n产品特点:\n1. 保障全面\n2. 灵活配置\n3. 理赔便捷`
product.documents = [
{
file_url: 'https://example.com/file1.pdf',
file_name: '产品条款.pdf',
file_size: '1024000',
file_size_formatted: '1.0 MB'
},
{
file_url: 'https://example.com/file2.pdf',
file_name: '产品说明.pdf',
file_size: '512000',
file_size_formatted: '512 KB'
}
]
product.status = 'active'
product.created_by = 1
product.updated_by = 1
product.updated_time = '2025-12-01 12:00:00'
resolve({
code: 1,
msg: 'success',
data: product
})
}, delay)
})
}