detail.js
7.34 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
/**
* 商品详情-controller
* @author: Tao.Huang
* @date: 2016/6/5
*/
'use strict';
const mRoot = '../models';
const service = require(`${mRoot}/detail-service`);
const detailHelper = require(`${mRoot}/detail-helper`);
const moment = require('moment');
const _ = require('lodash');
const qs = require('querystring');
const helpers = global.yoho.helpers;
const DEFAULT_AVATAR_ICO = 'https://static.yohobuy.com/images/v3/boy.jpg';
/**
* 首屏渲染商品详情
*/
const showMain = (req, res, next) => {
let skn = req.params[0];
let channel = req.yoho.channel;
let gender = req.yoho.gender;
const nullUserInfo = {
uid: null,
isStudent: null,
vipLevel: null,
saveInCookies: null
};
return req.ctx(service).showMainAsync(req, Object.assign({
skn: skn,
channel: channel,
gender: gender
}, nullUserInfo)).then((result)=> {
return res.render('product/detail', Object.assign({
module: 'product',
page: 'detail'
}, result.seo, result));
}).catch(()=> next());
};
const showMainBack = (req, res) => {
let pid = req.params[0];
return req.ctx(service).showMainBackAsync({
pid: pid
}).then((result)=> {
if (_.get(result, 'data.product_skn')) {
let query = !_.isEmpty(req.query) ? `?${qs.stringify(req.query)}` : '';
res.redirect(301, `${helpers.getUrlBySkc(result.data.product_skn)}${query}`);
}
res.end();
}).catch();
};
/**
* 商品价格相关的数据
*/
const detailHeader = (req, res, next) => {
let skn = req.query.productSkn;
let uid = req.user.uid || 0;
let vipLevel = detailHelper.vipLevel(req.user.vip);
let dataMd5 = req.query.md5 || 0;
let saveCurrentGoodsInCookies = _.partial(req.ctx(service).saveRecentGoodInCookies,
req.cookies._browseskn,
res
);
if (!skn) {
return res.status(204).end();
}
return req.ctx(service).getDetailHeader(
{skn: skn}, uid, req.user.isStudent, vipLevel, dataMd5, saveCurrentGoodsInCookies
) .then((result) => {
if (result.code === 200) {
return res.render('product/detail-header', Object.assign({layout: false}, result.data));
} else {
res.status(204).end();
}
}).catch(next);
};
/**
* 特殊商品退换货
*/
const detailReturn = (req, res, next) => {
let skn = req.query.skn || 0;
if (!skn) {
return {
code: 400,
message: '商品数据出错'
};
}
return req.ctx(service).saleReturn(skn).then(result => {
return res.json({
code: 200,
data: result
});
}).catch(next);
};
/**
* 获取热区图
*/
const indexHotArea = (req, res, next) => {
let pid = req.query.productId || 0;
return req.ctx(service).indexHotAreaAsync(pid).then((result) => {
res.render('product/hotarea', {
hotArea: result,
layout: false
});
}).catch(next);
};
/**
* 获得评论列表
*/
const indexComment = (req, res, next) => {
let pid = req.query.productId || 0;
let page = req.query.page || 1;
let size = req.query.size || 10;
let filterId = req.query.filterId || 7; // 7全部,6有图
return req.ctx(service).getShareOrderListAsync(pid, page, size, filterId).then((result) => {
let pageResponse = _.get(result, 'data.pageResponse', {});
return res.json({
code: result.code,
filter: _.get(result, 'data.fileter', []),
data: _.get(pageResponse, 'list', []).map((item)=> {
return {
avatar: _.get(item, 'userInfo.headIco', '') ?
helpers.image(item.userInfo.headIco, 30, 30) :
DEFAULT_AVATAR_ICO,
userName: _.get(item, 'userInfo.nickName', ''),
date: moment(item.createTime, 'X').format('YYYY-MM-DD HH:mm:ss'),
color: _.get(item, 'goods.factory_goods_name', ''),
size: _.get(item, 'goods.size_name', ''),
comment: item.content,
total: pageResponse.totalCount,
url: _.get(item, 'url', ''),
star: _.get(item, 'satisfied', 1),
comfort: _.get(item, 'size', 'MIDDLE'),
height: _.get(item, 'height', ''),
weight: _.get(item, 'weight', '')
};
})
});
}).catch(next);
};
/**
* 获得咨询列表
*/
const indexConsult = (req, res, next) => {
let uid = req.user.uid || '';
let pid = req.query.productId || 0;
let page = req.query.page || 1;
let size = req.query.size || 10;
return req.ctx(service).indexConsultAsync(uid, pid, page, size).then(result => {
return res.json({
code: 200,
data: result
});
}).catch(next);
};
/**
* 新建咨询
*/
const createConsult = (req, res, next) => {
let uid = req.user.uid || '';
let pid = req.body.productId || 0;
let content = req.body.content;
if (content && uid) {
return req.ctx(service).createConsultAsync(uid, pid, content).then(result => {
res.json(result);
}).catch(next);
} else if (!content) {
res.json({
code: 400,
message: '请输入咨询内容'
});
} else if (!uid) {
res.json({
code: 403,
message: '用户没有登录',
data: {
url: helpers.urlFormat('/signin.html')
}
});
} else {
res.json({
code: 400,
message: '请输入咨询内容'
});
}
};
const likeConsult = (req, res, next) => {
let uid = req.user.uid;
let id = req.params.id || 0;
if (!id) {
return res.json({
code: 401,
message: '咨询无效'
});
}
req.ctx(service).likeAsync(uid, id).then((result) => {
return res.json(result);
}).catch(next);
};
const usefulConsult = (req, res, next) => {
let uid = req.user.uid;
let id = req.params.id || 0;
if (!id) {
return res.json({
code: 401,
message: '咨询无效'
});
}
req.ctx(service).usefulAsync(uid, id).then((result) => {
return res.json(result);
}).catch(next);
};
const recommend = (req, res, next) => {
let skn = req.query.skn || '';
let pageSize = req.query.size || 5;
let pageNum = req.query.num || 1;
if (!skn) {
return res.json({
code: 400,
message: '服务器错误'
});
}
req.ctx(service).recommendAsync(skn, pageNum, pageSize)
.then(result => res.json(result))
.catch(next);
};
/**
* 获取套餐
*/
const getPackage = (req, res, next) => {
let skn = req.query.skn || '';
if (!skn) {
return res.json({
code: 400,
message: '服务器错误'
});
}
req.ctx(service).getPackage(skn)
.then(result => res.json(result))
.catch(next);
};
module.exports = {
showMain,
showMainBack,
indexHotArea,
indexComment,
indexConsult,
createConsult,
productHeader: detailHeader,
detailReturn,
recommend,
getPackage,
likeConsult,
usefulConsult
};