sale.js
9.36 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
/**
* sale controller
* @author: wsl<shuiling.wang@yoho.cn>
* @date: 2016/5/17
*/
'use strict';
const mRoot = '../models';
const utils = '../../../utils';
const headerModel = require('../../../doraemon/models/header');
const saleModel = require(`${mRoot}/sale`);
const _ = require('lodash');
const listParamsProcess = require(`${utils}/list-params-process`);
const yasProcess = require(`${utils}/yas-process`);
const channelToAppChannel = (channel) => {
return {
boys: '1',
girls: '2',
kids: '3',
lifestyle: '4'
}[channel] || '1';
};
// const queryParam = {
// brand: '0',
// gender: '1,2,3',
// sort: '0',
// size: '0',
// price: '0',
// pD: '0.1,0.9'
// };
/**
* 公共数据处理
* @param {[object]} req
* @param {[string]} title 标题
* @param {[string]} page js文件page名称
* @return {[type]}
*/
const _processPublicData = (req, title, page, backUrl) => {
return {
channel: req.yoho.channel,
renderData: {
module: 'product',
page: page,
// title: title,
saleNav: true,
pageHeader: headerModel.setNav({
navTitle: title,
navBtn: false,
backUrl: backUrl || ''
}),
pageFooter: true
}
};
};
/**
* sale 首页 资源位获取
* @param {[object]} req
* @param {[object]} res
* @return {[type]}
*/
let index = (req, res, next) => {
let refer = req.get('Referer') || `${global.yoho.config.siteUrl}/${req.cookies._Channel || ''}`;
let reqPath = req.baseUrl + req.path;
let backUrl = refer.indexOf(reqPath) === -1 && refer.indexOf('-sale') === -1 ? refer : '';
if (['boys', 'girls', 'kids', 'lifestyle'].indexOf(req.yoho.channel) === -1) {
return next();
}
backUrl && res.cookie('saleRefer', backUrl, {
domain: 'm.yohobuy.com'
});
backUrl = backUrl || req.cookies.saleRefer;
let params = _processPublicData(req, 'SALE', 'sale', backUrl);
params.channel = req.query.channel || params.channel;
let appParams = Object.assign({}, req.query, {
channel: channelToAppChannel(params.channel),
uid: undefined // eslint-disable-line
});
// 唤起 APP 的路径
res.locals.appPath = `yohobuy://yohobuy.com/goapp?openby:yohobuy={"action":"go.sale","params":${JSON.stringify(appParams)}}`;
saleModel.getSaleData(params.channel).then((result) => {
// 扩展头部频道选择数据
Object.assign(params.renderData.pageHeader, {
saleNav: saleModel.saleNav(params.channel)
});
// 此处 channel 需要读取 cookies 的 channel
if (req.cookies._Channel) {
res.locals.pageChannel = {};
res.locals.pageChannel[req.cookies._Channel] = true;
}
if (!result.length) {
res.set('Cache-Control', 'no-cache');
}
res.render('sale/index', Object.assign(params.renderData, {
content: result,
localCss: true,
floorHeader: {
title: {
title: '最新降价'
}
}
}));
}).catch(next);
};
// 301到新路由
let indexRedirect = (req, res) => {
let pathParamsUrl = listParamsProcess.generatePathUrl(req.query, req.path);
let urlPath = _.replace(pathParamsUrl, '//m.yohobuy.com/sale', `//m.yohobuy.com/${req.yoho.channel}-sale`);
return res.redirect(301, urlPath);
};
/**
* 断码区
* @param {[object]} req
* @param {[object]} res
* @return {[type]}
*/
let breakingYards = (req, res, next) => {
let params = _processPublicData(req, '断码区', 'break-code');
// 唤起 APP 的路径
res.locals.appPath = 'yohobuy://yohobuy.com/goapp?openby:yohobuy={"action":"go.shortsize"}';
saleModel.getBreakCodeData({
yhChannel: params.channel
}).then((result) => {
res.render('sale/break-code', Object.assign(params.renderData, result, {
title: '断码商品|断码折扣商品|YOHO!BUY有货',
keywords: '断码商品、断码专区、断码折扣',
description: '有货网为您带断码专区精选商品,大量断码商品,价格优惠幅度大,支持货到付款,让您全面了解潮流断码商品价格、品牌、图片、折扣等方面信息,想网购潮流断码区商品就上有货网!',
localCss: true
}));
}).catch(next);
};
/**
* 折扣专场列表
* @param {[object]} req
* @param {[object]} res
* @return {[type]}
*/
let discount = (req, res, next) => {
let params = _processPublicData(req, '折扣专场', 'discount');
saleModel.getDiscountData(params.channel).then((result) => {
res.render('sale/discount', Object.assign(params.renderData, {
data: result,
localCss: true
}));
}).catch(next);
};
/**
* 折扣专场专题详情
* @param {[object]} req
* @param {[object]} res
* @return {[type]}
*/
let discountDetail = (req, res, next) => {
let id = req.query.id;
let params = _processPublicData(req, '', 'discount-detail');
saleModel.getDiscountDetailData(id, req.yoho.channel).then((result) => {
params.renderData.pageHeader.navTitle = result.title;
// 唤起 APP 的路径
res.locals.appPath = result.activity && result.activity.cover_url ? `yohobuy://yohobuy.com/goapp?openby:yohobuy={"action":"go.discountmarketpro","params":{"id":"${id}","cover_url":"${result.activity.cover_url.split('?')[0]}","title":"${result.title}"}}` : '';
res.render('sale/discount-detail', Object.assign(params.renderData, result, {
title: `【${result.title}】潮流折扣商品|YOHO!BUY 有货`,
keywords: `${result.title}`,
description: `有货网为您提供大量${result.title}商品,支持货到付款,质量保证,让您全面了解${result.title}商品价格、品牌、图片、折扣等方面信息,想网购${result.title}商品就上有货网!`, // eslint-disable-line
localCss: true
}));
}).catch(next);
};
let getTime = (req, res, next) => {
let id = req.query.id;
saleModel.getDiscountTime(id, req.yoho.channel).then((time) => {
res.json(time);
}).catch(next);
};
/**
* vip 会员专享
* @param {[object]} req
* @param {[object]} res
* @return {[type]}
*/
let vip = (req, res, next) => {
let params = _processPublicData(req, '会员专享', 'vip');
saleModel.getVipData(params.channel).then((result) => {
res.render('sale/vip', Object.assign(params.renderData, {
title: 'VIP会员专享|会员专享|YOHO!BUY有货',
keywords: '会员专享,VIP专享,专享商品',
description: '有货网为您带VIP会员专享精选商品,大量潮流品牌商品,VIP价格优惠幅度大,支持货到付款,让您全面了解VIP会员专享商品价格、品牌、图片、折扣等方面信息,想网购VIP会员专享商品就上有货网!', // eslint-disable-line
content: result,
localCss: true
}));
}).catch(next);
};
/**
* 筛选
* @param {[object]} req
* @param {[object]} res
* @return {[type]}
*/
let filter = (req, res, next) => {
let params = Object.assign({}, req.query);
saleModel.getFilterData(params).then((result) => {
res.render('sale/filter', {
layout: false,
params: params,
filter: result
});
}).catch(next);
};
/**
* 商品搜索
* @param {[object]} req
* @param {[object]} res
* @return {[type]}
*/
let search = (req, res, next) => {
let params = req.query;
let uid = req.user.uid || 0;
if (params.from === 'seckill') {
uid = 0; // 秒杀无须用户信息
}
// 断码区域必须传尺码
if (params.saleType === '1' && (!params.breakSort || !params.breakSize)) {
return res.render('sale/product', {
layout: false,
params: params
});
}
if (uid) {
params.uid = uid;
}
saleModel.getSearchData(params, uid).then((result) => {
let vipLevel = result[1].cur_level;
let vipObj = {};
if (req.query.saleType === '2') {
vipObj = {
saleVip: (req.query.saleType === '2' && (!uid || vipLevel === '0')),
vipLevel: vipLevel,
saleViplogin: vipLevel >= 1 ? true : false,
vipPrice1: vipLevel === '1',
vipPrice2: vipLevel === '2',
vipPrice3: vipLevel === '3'
};
}
if (params.from === 'seckill' && req.yoho.isApp && _.isArray(result[0])) {
_.forEach(result[0], goods => {
goods.url = yasProcess.addParamsToGoodsHref(goods.url, req);
});
}
_.forEach(result[0], goods => {
goods.url = yasProcess.addParamsToGoodsHref({
href: goods.url,
fromPageName: yasProcess.getPname(req),
fromPageParam: req.get('referer')
});
});
res.render('sale/product', Object.assign({
layout: false,
params: params,
goods: result[0],
_noLazy: params._noLazy || false
}, vipObj));
}).catch(next);
};
module.exports = {
index,
breakingYards,
discount,
discountDetail,
vip,
filter,
search,
getTime,
indexRedirect
};