favorite-service.js
11.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
'use strict';
const Promise = require('bluebird');
const co = Promise.coroutine;
const _ = require('lodash');
const helpers = global.yoho.helpers;
const pager = require('./pager').handlePagerData;
const favoriteApi = require('./favorite-api');
const TABS = [
{type: 'product', name: '商品收藏'},
{type: 'brand', name: '品牌收藏'},
{type: 'article', name: '文章收藏'}
];
const getFavoriteTabs = (type) => {
type = type || 'product';
return TABS.map((item) => {
item.active = item.type === type;
item.url = helpers.urlFormat('/home/favorite', {type: item.type});
return item;
});
};
const _getSortInfo = (categoryList, sort)=> {
if (_.isEmpty(categoryList)) {
return false;
}
let result = {};
result.all = categoryList.map((category) => {
return {
name: category.category_name,
url: helpers.urlFormat('/home/favorite', {sort_id: category.category_id}),
count: category.num,
focus: category.category_id === sort
};
});
let defaultCategory = {
name: '全部',
url: helpers.urlFormat('/home/favorite'),
count: _.sumBy(categoryList, category => category.num),
focus: sort === 0
};
result.all.unshift(defaultCategory);
result.default = result.all.slice(0, 7);
return result;
};
const getGoodsInfo = (data, page, limit)=> {
let result = data.slice((page - 1) * limit, page * limit).map((item) => {
return {
skn: item.product_id,
img: helpers.image(item.image, 100, 100),
name: item.product_name,
url: helpers.getUrlBySkc(item.product_skn),
price: item.sales_price,
priceDown: item.price_down,
buyNow: helpers.getUrlBySkc(item.product_skn),
soldOut: item.storage === 0 ? true : '',
hadNoticed: item.is_subscribe_reduction === 'Y' ? true : '',
activites: {
count: item.promotion_list ? item.promotion_list.length : 0,
list: _.get(item, 'promotion_list', []).map((val) => {
return {
type: val.promotion_type,
name: val.promotion_title
};
})
}
};
});
if (_.isEmpty(result)) {
return {
empty: '您没有收藏商品'
};
}
return result;
};
/**
* 降价提醒
*/
const _redutionCount = (uid)=> {
return co(function*() {
let data = yield favoriteApi.redutionCount(uid);
let result = {
count: 0,
phone: '',
url: '/home/favorite/reduction'
};
if (data.data.num) {
result.count = +data.data.num;
result.phone = data.data.mobile;
}
return result;
})();
};
const favoriteProductListAsync = (uid, page, limit, selectedSort, subscribe, reduction, promotion, query) => {
return co(function*() {
let result = {
sort: {},
reduction: {},
filter: {},
goods: {},
pager: {}
};
let product = yield favoriteApi.getFavoriteProductList(uid, 1, 500);
result.sort = _getSortInfo(_.get(product, 'data.category_list'), selectedSort);
result.reduction = yield _redutionCount(uid);
let productList = (function() {
let products = _.get(product, 'data.product_list', []);
if (reduction === 'Y' && promotion === 'Y') {
// 参加活动的降价商品
return products.filter(pro => pro.is_price_down === 'Y' && pro.is_join_promotion === 'Y');
} else if (selectedSort) {
// 商品分类过滤
return products.filter(pro => pro.category_id === selectedSort);
} else if (subscribe === 'Y') {
// 订阅降价通知过滤
return products.filter(pro => pro.is_subscribe_reduction === 'Y');
} else if (reduction === 'Y') {
// 降价商品过滤
return products.filter(pro => pro.is_price_down === 'Y');
} else if (promotion === 'Y') {
// 参加活动商品过滤
return products.filter(pro => pro.is_join_promotion === 'Y');
} else {
return products;
}
}());
result.filter = (function() {
if (reduction === 'N' && promotion === 'N') {
return {
reductionUrl: helpers.urlFormat('/home/favorite', {is_reduction: 'Y'}),
reductionChecked: '',
activityUrl: helpers.urlFormat('/home/favorite', {is_promotion: 'Y'}),
activityChecked: ''
};
} else if (reduction === 'N' && promotion === 'Y') {
return {
reductionUrl: helpers.urlFormat('/home/favorite', {is_reduction: 'Y', is_promotion: 'Y'}),
reductionChecked: '',
activityUrl: helpers.urlFormat('/home/favorite'),
activityChecked: ''
};
} else if (reduction === 'Y' && promotion === 'N') {
return {
reductionUrl: helpers.urlFormat('/home/favorite'),
reductionChecked: '',
activityUrl: helpers.urlFormat('/home/favorite', {is_reduction: 'Y', is_promotion: 'Y'}),
activityChecked: ''
};
} else {
return {
reductionUrl: helpers.urlFormat('/home/favorite', {is_promotion: 'Y'}),
reductionChecked: '',
activityUrl: helpers.urlFormat('/home/favorite', {is_reduction: 'Y'}),
activityChecked: ''
};
}
}());
let total = productList.length;
let pageTotal = Math.ceil(total / limit);
page = page > pageTotal ? pageTotal : page;
result.goods = getGoodsInfo(productList, page, limit);
result.pager = pager(total, Object.assign(query, {hasCheckAll: true}));
return result;
})();
};
const favoriteBrandListAsync = (uid, page, limit, type)=> {
return co(function*() {
let result = {
brands: {
empty: '您没有收藏品牌',
pager: {}
}
};
let brand = yield favoriteApi.favoriteBrandData(uid, page, limit);
if (!brand.data || !brand.data.page_total) {
return result;
}
if (!brand.data.brand_list) {
return result;
}
result.brands = _.get(brand, 'data.brand_list', []).map((item) => {
return {
id: item.brand_id,
brandOrShopType: item.brandOrShopType || '',
shop_id: item.shop_id || '',
img: helpers.image(item.brand_ico, 100, 100),
url: helpers.urlFormat('', {shopId: item.shop_id || ''}, item.brand_domain),
name: item.brand_name,
naCount: item.new_product_num,
colCount: item.brand_favorite_num
};
});
let total = brand.data.total || 0;
page = brand.data.page || 0;
result.pager = pager(total, {page, limit, type, hasCheckAll: true});
return result;
})();
};
const favoriteArticleListAsync = (uid, udid, page, limit, type)=> {
return co(function*() {
let result = {};
let articles = yield favoriteApi.favoriteArticleData(uid, udid, page, limit);
result.articles = _.get(articles, 'data.data', []).map((item) => {
return {
id: item.id,
name: item.title,
img: helpers.image(item.src, 146, 96),
desc: item.intro,
url: helpers.urlFormat('/' + item.id + '.html', '', 'guang')
};
});
let total = articles.data.total || 0;
result.pager = pager(total, {page, limit, type, hasCheckAll: true});
if (_.isEmpty(result.articles)) {
result.articles = {
empty: '你尚未收藏任何文章!'
};
}
return result;
})();
};
const newProductAsync = (uid, page, limit, id) => {
return co(function * () {
let products = yield favoriteApi.favoriteBrandData(uid, page, limit);
return _.get(products, 'data.brand_list', []).reduce((total, cur) => {
if (id !== cur.brand_id) {
return total;
}
if (cur.new_product_num === 0) {
return total;
}
total = _.concat(total, _.take(_.get(cur, 'new_product', []), 20).map((pro) => {
return {
img: pro.default_images,
url: helpers.getUrlBySkc(pro.product_skn),
name: pro.product_name,
salePrice: pro.sales_price === pro.market_price ? '' : pro.sales_price,
marketPrice: pro.market_price
};
}));
return total;
}, []);
})();
};
const reductionAsync = (uid, page, limit) =>{
let result = {};
result.tabs = getFavoriteTabs('product');
return favoriteProductListAsync(uid, page, limit, 0, 'Y').then((products) => {
result.goods = products.goods;
result.reductionUrl = helpers.urlFormat('/home/fovorite');
return {
meFavoritePage: true,
meFavorite: result
};
});
};
const enableNoticeAsync = (uid, mobile, id) => {
return co(function *() {
let result = {
code: 400,
message: '订阅失败'
};
if (!uid || !mobile || !id) {
return result;
}
let data = yield favoriteApi.redutionAdd(uid, mobile, id);
if (data.code === 200) {
result.code = 200;
return data;
}
if (data.code === 500) {
result.code = 500;
switch (data.message) {
case 'count must be lt 5':
result.message = '您的订阅数已经到达上限';
break;
case 'mobile must bu not null':
result.message = '请填写手机号';
break;
default:
result.message = '订阅失败';
break;
}
}
return result;
})();
};
const disableNoticeAsync = (uid, id) => {
return co(function * () {
if (!uid || !id) {
return {
code: 400,
message: '取消失败'
};
}
return yield favoriteApi.redutionCancel(uid, id);
})();
};
/**
* ids 是 group,品牌删除
*/
const cancelBrandAsync = co(function * (uid, ids, group) {
let result;
if (group.bid) {
result = yield favoriteApi.cancel.brand(uid, group.bid);
}
if (group.sid) {
result = yield favoriteApi.cancel.shop(uid, group.sid);
}
if (!group) {
result = yield favoriteApi.cancel.brand(uid, ids);
}
return result;
});
/**
* ids 可能是 group
*/
const cancelAsync = (uid, ids, group, type) => {
if (type === 'brand') {
return cancelBrandAsync(uid, ids, group);
}
return favoriteApi.cancel[type](uid, ids);
};
module.exports = {
getFavoriteTabs,
favoriteBrandListAsync,
favoriteProductListAsync,
favoriteArticleListAsync,
newProductAsync,
reductionAsync,
enableNoticeAsync,
disableNoticeAsync,
cancelBrandAsync,
cancelAsync
};