favorite-service.js
8.8 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
'use strict';
const Promise = require('bluebird');
const co = Promise.coroutine;
const helpers = global.yoho.helpers;
const path = require('path');
const pagerPath = path.join(global.appRoot, '/apps/product/models/public-handler.js');
const pager = require(pagerPath).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 favoriteProductList = (uid, page, limit, type, sort, subscribe, reduction, promotion) => {
return co(function*() {
let data = {};
let product = {};
let result = {
sort: {},
reduction: {},
filter: {},
goods: {},
pager: {}
};
product = yield favoriteApi.getFavoriteProductList(uid);
if (product.data.category_list) {
result.sort = getSortInfo(product.data.category_list, sort);
}
result.reduction = yield redutionCount(uid);
let productList = [];
if (product.data.product_list) {
product.data.product_list.forEach(function(product) {
if (
(reduction === 'Y' && promotion === 'Y' && product.is_price_down === 'Y' && promotion === 'Y') ||
(sort && product.category_id === sort) ||
(subscribe && product.is_subscribe_reduction === 'Y') ||
(reduction === 'Y' && product.is_price_down === 'Y') ||
(promotion === 'Y' && product.is_join_promotion === 'Y')
) {
productList.push(product);
}
});
productList = product.data.product_list;
}
if (reduction === 'N' && promotion === 'N') {
result.filter = {
reductionUrl: helpers.urlFormat('/home/favorite', {is_reduction: 'Y'}),
reductionChecked: '',
activityUrl: helpers.urlFormat('/home/favorite', {is_promotion: 'Y'}),
activityChecked: ''
};
} else if (reduction === 'N' && promotion === 'Y') {
result.filter = {
reductionUrl: helpers.urlFormat('/home/favorite', {is_reduction: 'Y', is_promotion: 'Y'}),
reductionChecked: '',
activityUrl: helpers.urlFormat('/home/favorite', {is_promotion: 'Y'}),
activityChecked: ''
};
} else if (reduction === 'Y' && promotion === 'N') {
result.filter = {
reductionUrl: helpers.urlFormat('/home/favorite', {is_reduction: 'Y', is_promotion: 'Y'}),
reductionChecked: '',
activityUrl: helpers.urlFormat('/home/favorite', {is_promotion: 'Y', is_promotion: 'Y'}),
activityChecked: ''
};
} else {
result.filter = {
reductionUrl: helpers.urlFormat('/home/favorite', {is_reduction: 'Y', is_promotion: 'Y'}),
reductionChecked: '',
activityUrl: helpers.urlFormat('/home/favorite', {is_promotion: 'Y', is_promotion: 'Y'}),
activityChecked: ''
};
}
let total = productList;
let pageTotal = Math.ceil(total / limit);
result.pager = getPager(page, total, pageTotal);
result.goods = getGoodsInfo(productList, page, limit);
return result;
})();
};
const favoriteBrandList = (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.page_total < page) {
page = brand.data.page_total;
brand = yield favoriteApi.favoriteBrandData(uid, page, limit);
}
if (!brand.data.brand_list) {
return result;
}
let brands = [];
brand.data.brand_list.forEach((item, i)=> {
brands.push({
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
});
});
result.brands = brands;
let total = brand.data.total || 0;
let pageTotal = brand.data.page_total || 0;
page = brand.data.page || 0;
result.pager = getPager(page, total, pageTotal);
return result;
})();
};
const favoriteArticleListAsync = (uid, udid, page, limit)=> {
return co(function*() {
let result = {articles: [], pager: {}};
let articles = yield favoriteApi.favoriteArticleData(uid, udid, page, limit);
if (!articles.data && !articles.data.data) {
articles.data.data.forEach((item)=> {
result.articles.push({
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;
let pageTotal = articles.data.totalPage || 0;
let pageNum = articles.data.page || 0;
result.pager = getPager(pageNum, total, pageTotal);
} else {
result.articles = {empty: '你尚未收藏任何文章!'};
}
return result;
})();
};
const getPager = (page, total, totalPage, size, type)=> {
let result = {};
if (page && total && totalPage) {
result = {
count: total,
curPage: page,
totalPages: totalPage,
hasCheckAll: true
};
}
return result;
};
const getGoodsInfo = (data, page, limit)=> {
let result = [];
let begin = (page - 1) * limit;
if (!data) {
data = data.slice(begin, limit);
data.forEach((item, i)=> {
let obj = {
skn: item.product_id,
img: helpers.img(item.image, 100, 100),
name: item.product_name,
url: helpers.getUrlBySkc(item.product_id, item.goodsId, item.cnAlphabet),
price: item.sales_price,
priceDown: item.price_down,
buyNow: helpers.getUrlBySkc(item.product_id, item.goodsId, item.cnAlphabet),
soldOut: item.storage === 0 ? true : '',
hadNoticed: item.is_subscribe_reduction === 'Y' ? true : '',
count: item.promotion_list ? item.promotion_list.length : 0
};
if (item.promotion_list) {
item.promotion_list.forEach(function(item1) {
obj.activites.list.push({
type: item1.promotion_type,
name: item1.promotion_title
});
});
}
result.push(obj);
});
} else {
result = {empty: '您没有收藏商品'};
}
return result;
};
const redutionCount = (uid)=> {
return co(function*() {
let result = {count: 0, url: '/home/favorite/reduction', phone: ''};
let data = yield favoriteApi.redutionCount(uid);
if (data.data.num) {
result.count = +data.data.num;
result.phone = data.data.mobile;
}
return result;
})();
};
const getSortInfo = (categoryList, sort)=> {
let result = {default: {}, all: []};
let defaultCategory = {name: '全部', url: helpers.urlFormat('/home/favorite'), count: 0, focus: ''};
categoryList.forEach(function(category) {
result.all.push({
name: category.category_name,
url: helpers.urlFormat('/home/favorite', {sort_id: category.category_id}),
count: category.num,
focus: category.category_id === sort ? true : ''
});
defaultCategory.count += category.num;
defaultCategory.focus = sort === 0 ? true : '';
});
result.all.unshift(defaultCategory);
result.default = result.all.slice(result.all, 0, 7);
return result;
};
module.exports = {
getFavoriteTabs,
favoriteProductList,
favoriteArticleListAsync
};