detail.js
8.7 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
/**
*
* @author: Aiden Xu<aiden.xu@yoho.cn>
* @date: 2016/07/19
*/
'use strict';
const _ = require('lodash');
const model = require('../models/detail');
const searchModel = require('../models/search');
// 频道映射
const CHANNEL_MAP = {
men: 1,
women: 2,
lifestyle: 4
};
// 保存在 gids 和 skns ,最近流览功能
const saveRecentGoodInCookies = (oldSkns, res, addSkns) => {
// oldSkns = oldSkns ? oldSkns.split(',') : [];
oldSkns = oldSkns ? JSON.parse(oldSkns) : [];
oldSkns = _.reject(oldSkns, old => old.product_skn === addSkns ? true : false);
oldSkns.unshift({product_skn: addSkns, time: (+new Date())});
// res.cookie('_browseskn', oldSkns.splice(0, 30).join(','), {
res.cookie('_browseskn_h5', JSON.stringify(oldSkns.splice(0, 20)), {
maxAge: 2000000000,
domain: '.yohoblk.com'
});
};
const vipLe = (key) => {
return {
银卡: 1,
金卡: 2,
白金: 3,
}[key];
};
/**
* 商品详情
*/
const component = {
index(req, res, next) {
const pid = req.params[0],
goodsId = req.params[1];
let params = {
product_id: _.toString(pid),
uid: req.user.uid || 0
};
req.ctx(model).product(params).then(product => {
product = product.data || {};
product.formatPrice = product.format_sales_price !== '0' ? product.format_sales_price : product.format_market_price;//eslint-disable-line
product.isDiscount = product.market_price > product.sales_price;
product.coverImage = _.get(product, 'goods_list[0].images_list[0].image_url');
res.render('pdetail', {
module: 'product',
page: 'detail',
pid: pid,
goodsId: goodsId,
product: product
});
}).catch(next);
},
sknIndex(req, res, next) {
const skn = req.params[0];
let params = {
product_skn: _.toString(skn),
uid: req.user.uid || 0
};
req.ctx(model).product(params).then(product => {
product = product.data || {};
product.formatPrice = product.format_sales_price !== '0' ? product.format_sales_price : product.format_market_price;//eslint-disable-line
product.isDiscount = product.market_price > product.sales_price;
product.coverImage = _.get(product, 'goods_list[0].images_list[0].image_url');
res.render('pdetail', {
module: 'product',
page: 'detail',
pid: product.product_id,
product: product
});
}).catch(next);
},
product(req, res, next) {
const pid = req.params[0]; // , goodsId = req.params[1];
let params = {
product_id: _.toString(pid),
uid: req.user.uid || 0
};
const productPromise = req.ctx(model).product(params);
const vipPromise = req.user.uid ? req.ctx(model).getVipData(params) : Promise.resolve();
Promise.all([productPromise, vipPromise]).then((result) => {
let vipPrice = [];
// 最近浏览存储cookies
if (_.has(result[0].data, 'product_skn') && !req.user.uid) {
saveRecentGoodInCookies(req.cookies._browseskn_h5, res, result[0].data.product_skn);
}
if (result[0].data && result[0].data.vip) {
for (let val of result[0].data.vip) {
vipPrice.push({
level: vipLe(val.caption),
price: val.price,
currentVip: result[1] && parseInt(result[1].data.current_vip_level, 10) === vipLe(val.caption) ? 'active' : ''//eslint-disable-line
});
}
result[0].data.vipPrice = vipPrice;
}
res.json(result[0]);
}).catch(next);
},
intro(req, res, next) {
let params = {
productskn: req.query.skn,
udid: 'f528764d624db129b32c21fbca0cb8d6',
uid: req.user.uid || ''
};
req.ctx(model).intro(params).then(result => {
res.json(result);
}).catch(next);
},
/**
* 加入购物车接口
*
*/
addToCart(req, res, next) {
let params = {
product_sku: req.body.productSku, // 商品SKU
buy_number: req.body.buyNumber, // 购买数量
goods_type: req.body.goodsType || 0, // 商品类型,0表示普通商品,1表示加价购商品
edit_product_sku: req.body.isEdit || 0, // 是否是编辑商品SKU,0表示不是编辑
selected: 'Y',
promotion_id: req.body.promotionId || null, // 促销id,默认null(加价购有关)
uid: req.user.uid || 0,
shopping_key: global.yoho.cookie.getShoppingKey(req)
};
req.ctx(model).addToCart(params).then(result => {
if (result.code === 200) {
// 将 shopping_key 写入Cookie
res.cookie('_SPK', result.data.shopping_key, {
maxAge: 86400 * 360
});
}
res.json(result);
}).catch(next);
},
/**
* 添加收藏/取消收藏
*
* @param req
* @param res
* @param next
*/
favorite(req, res, next) {
let params = {
uid: req.user.uid || 0,
type: 'product'
};
if (req.body.operation === 'add') {
params.id = req.body.id;
params.method = 'app.favorite.add';
} else if (req.body.operation === 'remove') {
params.fav_id = req.body.id;
params.method = 'app.favorite.cancel';
}
if (!req.user.uid) {
// 未登陆
res.json({
code: 403
});
return;
}
req.ctx(model).favorite(params).then(result => {
res.json(result);
}).catch(next);
},
isFavorite(req, res, next) {
if (!req.user.uid) {
return res.json({
code: 401,
message: '用户未登录'
});
}
let params = {
id: req.params.id,
uid: req.user.uid
};
req.ctx(model).isFavorite(params).then(result => {
res.json(result);
}).catch(next);
},
/**
* 获取购物车数量
*
* @param req
* @param res
* @param next
*/
getCartCount: (req, res, next) => {
let params = {
method: 'app.Shopping.count',
shopping_key: global.yoho.cookie.getShoppingKey(req),
uid: req.user.uid || 0
};
this.get({
data: params
}).then(result => {
res.json(result);
}).catch(next);
},
/**
* 搜索产品
*
* @param req
* @param res
* @param next
*/
search: (req, res, next) => {
let params = {
query: req.query.ids,
limit: 4,
app_type: 1
};
req.ctx(searchModel).products(params).then(result => {
res.json(result);
}).catch(next);
},
/**
* 根据skn 搜索产品
*
* @param req
* @param res
* @param next
*/
searchSkn: (req, res, next) => {
req.ctx(searchModel).productInfoBySkns(req.query.ids).then(result => {
res.json(result);
}).catch(next);
},
/**
* 是否支持退换货
* @param req
* @param res
* @param next
*/
supportRefundExchange: (req, res, next) => {
let params = {
product_skn: req.params.skn
};
req.ctx(model).refundExchange(params).then(result => {
res.json(result);
}).catch(next);
},
/**
* 猜你喜欢
* @param req
* @param res
* @param next
*/
mightLike: (req, res, next) => {
const query = req.query;
const channel = query.channel || req.cookies._Channel || 'men';
const params = {
product_skn: query.skn,
shopId: query.shopId,
yh_channel: CHANNEL_MAP[channel] || 1
};
return req.ctx(model).mightLike(params).then(result => {
if (result && result.code === 200) {
res.json({
code: 200,
data: result.data.product_list || [],
message: result.message
});
} else {
res.json({
code: 400,
data: [],
message: ''
});
}
}).catch(next);
}
};
module.exports = component;