item.js
2.23 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
/**
* 商品详情页controller
* @author: yyq<yanqing.yang@yoho.cn>
* @date: 2016/7/4
*/
'use strict';
const _ = require('lodash');
const item = require('../models/item');
// 保存在 gids 和 skns ,最近流览功能
const saveRecentGoodInCookies = (oldSkns, res, addSkns) => {
oldSkns = oldSkns ? oldSkns.split(',') : [];
oldSkns = _.reject(oldSkns, old => old === String(addSkns) ? true : false);
oldSkns.unshift(addSkns);
res.cookie('_browseskn', oldSkns.splice(0, 30).join(','), {
maxAge: 2000000000,
domain: '.yohoblk.com'
});
};
/**
* 商品详情页
* @function index
*/
const index = (req, res, next) => {
req.params.channel = req.yoho.channel;
item.getProductItemData(req.params, req.url, req.user.uid).then(result => {
if (_.isEmpty(result)) {
return next();
}
// 最近浏览存储cookies 限量商品不加入到最近浏览
if (!_.has(result.content.goodInfo, 'fashionTopGoods')) {
saveRecentGoodInCookies(req.cookies._browseskn, res, result.content.goodInfo.productSkn);
}
result.page = 'item';
result.title += ` | ${res.locals.title}`;
res.display('item', result);
}).catch(next);
};
/**
* 获取商品信息接口
* @function getProductInfo
* @param { number } productId 商品id
* @param { number } skn 商品skn
* @return { Object } 商品信息
*/
const getProductInfo = (req, res, next) => {
const productId = req.query.productId;
const skn = req.query.skn;
let uid;
if (req.user && req.user.uid) {
uid = req.user.uid;
}
item.getProductInfo(productId, skn, uid).then(result => {
res.json(result);
}).catch(next);
};
/**
* 特殊商品退换货
*/
const fetchReturn = (req, res, next) => {
let skn = req.query.skn || 0;
if (!skn) {
return {
code: 400,
message: '商品数据出错'
};
}
return item.fetchReturn(skn).then(result => {
return res.json({
code: 200,
data: {
result: result
}
});
}).catch(next);
};
module.exports = {
index, // 商品详情页
getProductInfo, // 获取商品信息接口
fetchReturn
};