detail.js
5.58 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
/**
* 商品详情-controller
* @author: Tao.Huang
* @date: 2016/6/5
*/
'use strict';
const helpers = global.yoho.helpers;
const mRoot = '../models';
const service = require(`${mRoot}/detail-service`);
const detailHelper = require(`${mRoot}/detail-helper`);
const Actions = require('./lib/actions');
const YohoAction = require('./lib/yoho-action');
const moment = require('moment');
const DEFAULT_AVATAR_ICO = 'http://static.yohobuy.com/images/v3/boy.jpg';
const _ = require('lodash');
class DetailAction extends YohoAction {
/**
* 渲染商品详情
*/
render() {
const req = this.request;
let pid = req.params[0];
let gid = req.params[1];
let uid = req.user.uid || '';
let vipLevel = detailHelper.vipLevel(req.user.vip);
let saveCurrentGoodsInCookies = _.partial(service.saveRecentGoodInCookies,
req.cookies._browse,
req.cookies._browseskn,
this.response
);
return service.showMainAsync({
pid: pid,
gid: gid,
channel: this.getSessionChannel(),
gender: this.guessUserGender(),
uid: uid,
isStudent: req.user.isStudent,
vipLevel: vipLevel,
saveInCookies: saveCurrentGoodsInCookies
}).then(result=> {
const seo = result.seo;
this.setTitle(seo.title, true, '|');
this.setKeywords(seo.keywords);
this.setDescription(seo.description, true);
this.setEntry('product', 'detail');
this.renderTemplate('product/detail', result);
}).catch(err => {
if (err.code === 404) {
return this.next();
}
return this.next(err);
});
}
}
/**
* 获取热区图
*/
class HotAreaAction extends YohoAction {
render() {
let pid = this.request.query.productId || 0;
return service.indexHotAreaAsync(pid).then(result => {
this.renderTemplate('product/hotarea', {
hotArea: result,
layout: false
});
});
}
}
/**
* 获得评论列表
*/
class CommentAction extends YohoAction {
render() {
const req = this.request;
let pid = req.query.productId || 0;
let page = req.query.page || 1;
let size = req.query.size || 10;
return service.getShareOrderListAsync(pid, page, size).then((result) => {
let pageResponse = _.get(result, 'data.pageResponse', {});
this.response.json({
code: result.code,
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.color_name', ''),
size: _.get(item, 'goods.size_name', ''),
comment: item.content,
total: pageResponse.totalCount
};
})
});
});
}
}
/**
* 获得咨询列表
*/
class ConsultAction extends YohoAction {
render() {
const req = this.request;
let uid = req.user.uid || '';
let pid = req.query.productId || 0;
let page = req.query.page || 1;
let size = req.query.size || 10;
return service.indexConsultAsync(uid, pid, page, size).then(result => {
this.response.json({
code: 200,
data: result
});
});
}
}
/**
* 新建咨询
*/
class CreateConsultAction extends YohoAction {
render() {
const req = this.request, res = this.response;
let uid = req.user.uid || '';
let pid = req.body.productId || 0;
let content = req.body.content;
if (content && uid) {
return service.createConsultAsync(uid, pid, content).then(result => {
res.json(result);
});
} 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 detailHeader = (req, res, next) => {
let pid = req.query.productId || 0;
let uid = req.user.uid || '';
let vipLevel = detailHelper.vipLevel(req.user.vip);
let dataMd5 = req.query.md5 || 0;
service.getDetailHeader(pid, uid, req.user.isStudent, vipLevel, dataMd5).then((result) => {
if (result.code === 200) {
return res.render('product/detail-header', Object.assign({layout: false}, result));
} else {
return res.status(204).end();
}
}).catch(next);
};
module.exports = {
showMain: Actions.createAction(DetailAction),
indexHotArea: Actions.createAction(HotAreaAction),
indexComment: Actions.createAction(CommentAction),
indexConsult: Actions.createAction(ConsultAction),
createConsult: Actions.createAction(CreateConsultAction),
productHeader: detailHeader
};