sale.js
8.19 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
/**
* 频道页面 model
* @author: wsl<shuiling.wang@yoho.cn>
* @date: 2016/05/17
*/
'use strict';
const library = '../../../library';
const utils = '../../../utils';
const API = require(`${library}/api`).API;
const ServiceAPI = require(`${library}/api`).ServiceAPI;
const sign = require(`${library}/sign`);
const logger = require(`${library}/logger`);
const helpers = require(`${library}/helpers`);
const camelCase = require(`${library}/camel-case`);
const resourcesProcess = require(`${utils}/resources-process`);
const productProcess = require(`${utils}/product-process`);
const _ = require('lodash');
const api = new API();
const serviceAPI = new ServiceAPI();
const anHour = 3600000;
const aDay = anHour * 24;
/**
* 需要格式化的时间格式
*/
const timeFormat = {
d: '剩{d}天',
h: '剩{h}小时',
m: '剩{m}分钟',
s: '剩{s}秒',
dh: '剩{d}天{h}小时',
dhms: '剩{d}天{h}小时{m}分钟{s}秒',
hms: '剩{h}小时{m}分钟{s}秒',
ms: '剩{m}分钟{s}秒'
};
/**
* 排序转换
*/
const typeCont = {
price: ['s_p_desc', 's_p_asc'],
discount: ['p_d_desc', 'p_d_asc'],
sale: ['s_n_desc', 's_n_asc'],
newest: ['s_t_desc', 's_t_asc'],
stock: ['s_s_desc', 's_s_asc']
};
/**
* 折扣专场专题列表过期时间处理
* @param {[string]} time
* @return {[object]}
*/
const processTime = (time) => {
let data = {};
let type = '';
if (time < anHour) {
data.warnColor = true;
data.time = '低于1小时';
} else {
if (time > aDay) {
type = 'dh';
} else {
type = 'h';
}
data.time = helpers.dateDiffFormat(timeFormat[type], time, 'ms');
}
return data;
};
/**
* 折扣专场列表数据处理
* @param {[array]} list
* @return {[array]}
*/
const processDiscount = (list) => {
const formatData = [];
let flag = true;
list = list || [];
list = camelCase(list);
if (list.length === 1) {
flag = false;
}
_.forEach(list, (data) => {
if (flag === true) {
data.specialUrl = `/product/sale/discount/detail?id=${data.id}`;
_.merge(data, processTime(data.leftTime));
}
formatData.push(data);
});
return formatData;
};
/**
* 折扣专场接口调用
* @param {[object]} params
* @return {[array]}
*/
const discount = (params) => {
params = params || {};
return api.get('', sign.apiSign(Object.assign({
method: 'app.activity.get',
sort: 2,
plateform: 3
}, params)));
};
/**
* 断码区分类数据处理
* @param {[array]} list
* @return {[array]}
*/
const processBreakingSort = (list) => {
const formatData = {};
const sort = [];
const sub = [];
list = list || [];
list = camelCase(list);
_.forEach(list, (data, index) => {
sort.push({
sortName: data.sortName,
sortId: data.sortId
});
data.sub.key = index;
sub.push(data.sub);
});
formatData.sortData = sort;
formatData.sub = sub;
return formatData;
};
/**
* 商品搜索接口请求
* @param {[object]} params
* @return {[array]}
*/
const searchSales = (params) => {
// 排除基本筛选项默认值为0的对象
for (let str in params) {
if (str !== 'order' && params[str] === '0' || params[str] === null) {
delete params[str];
}
}
params = Object.assign({
limit: '50'
}, params);
if (typeCont[params.type]) {
params.order = typeCont[params.type][params.order];
}
return api.get('', sign.apiSign(Object.assign({
method: 'app.search.sales'
}, params)), true);
};
/**
* 获取用户数据信息
* @param {[string]} uid
* @return {[array]}
*/
const getUserProfile = (uid) => {
if (!uid) {
return Promise.resolve({
code: 200,
data: {}
});
}
return api.get('', sign.apiSign({
method: 'app.passport.profile',
uid: uid
}), true);
};
/**
* 获取商品数据
*/
exports.getSearchData = (params, uid) => {
return Promise.all([
searchSales(params).then((result) => {
if (result && result.code === 200) {
return productProcess.processProductList(result.data.product_list || []);
} else {
logger.error('SALE 商品搜索返回 code 不是 200');
return [];
}
}),
getUserProfile(uid).then((result) => {
if (result && result.code === 200) {
return result.data.vip_info ? camelCase(result.data.vip_info) : {};
} else {
logger.error('获取用户信息返回 code 不是 200');
return {};
}
})
]);
};
/**
* 获取资源位数据
* @param {[string]} page
* @return {[array]}
*/
const getResources = (page) => {
const contentCode = {
sale: '7c2b77093421efa8ae9302c91460db73',
breakCode: '7c2b77093421efa8ae9302c91460db73',
vip: '7c2b77093421efa8ae9302c91460db73'
};
return serviceAPI.get('operations/api/v5/resource/get', sign.apiSign({
content_code: contentCode[page]
})).then((result) => {
if (result && result.code === 200) {
return resourcesProcess(result.data);
} else {
logger.error('SALE 页面资源位返回 code 不是 200');
return [];
}
});
};
/**
* 获取断码区分类数据
* @param {[string]} yhChannel
* @return {[object]}
*/
const getBreakingSort = (yhChannel) => {
return api.get('', sign.apiSign({
method: 'app.sale.getBreakingSort',
yh_channel: yhChannel || '1'
})).then((result) => {
if (result && result.code === 200) {
return processBreakingSort(result.data);
} else {
logger.error('断码区分类接口返回 code 不是 200');
return {};
}
});
};
/**
* 获取筛选数据
* @param {[object]} params
* @return {[array]}
*/
exports.getFilterData = (params) => {
return searchSales(params).then((result) => {
if (result && result.code === 200) {
return productProcess.processFilter(result.data.filter || [], {
hideSize: params.saleType === '1',
hideSort: params.saleType === '1'
});
} else {
logger.error('SALE 商品搜索返回 code 不是 200');
return [];
}
});
};
/**
* 获取sale首页数据
* @return {[array]}
*/
exports.getSaleData = () => {
return getResources('sale');
};
/**
* 获取会员享数据
* @return {[array]}
*/
exports.getVipData = () => {
return getResources('vip');
};
/**
* 获取断码区数据
* @param {[object]} params
* @return {[object]}
*/
exports.getBreakCodeData = (params) => {
params = params || {};
return Promise.all([getResources('breakCode'), getBreakingSort(params.yhChannel)]).then((result) => {
return {
content: result[0],
nav: result[1]
};
});
};
/**
* 获取折扣专场专题列表数据
* @param {[object]} params
* @return {[object]}
*/
exports.getDiscountData = (params) => {
const discountData = {};
return discount(params).then((result) => {
if (result && result.code === 200) {
discountData.data = processDiscount(result.data);
return discountData;
} else {
logger.error('折扣专场专题列表返回 code 不是 200');
return {};
}
});
};
/**
* 获取折扣专场专题详情数据
* @param {[string]} id
* @return {[object]}
*/
exports.getDiscountDetailData = (id) => {
let res = {};
let param = {
id: id
};
return discount(param).then((result) => {
if (result && result.code === 200) {
res = processDiscount(result.data);
return {
title: res[0].title,
activity: {
coverUrl: res[0].coverUrl,
time: res[0].time,
leftTime: res[0].leftTime
}
};
} else {
logger.error('折扣专场专题详情返回 code 不是 200');
return {};
}
});
};