list.js
10.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
* @Author: sefon
* @Date: 2016-07-24 11:40:21
*/
'use strict';
const mRoot = '../models';
const list = require(`${mRoot}/list`);
const listSeoMap = require(`${global.middleware}/seo/listSeoMap`);
const helpers = global.yoho.helpers;
const _ = require('lodash');
const tdk = require('../../../utils/getTDK');
// 搜索相关接口
const searchApi = require(`${mRoot}/search-api`);
// 店铺页
const shop = (shopId, req, res, next, brandInfo) => {
let domain = req.query.domain,
params = req.query;
_.unset(params, 'domain');
shopId = parseInt(shopId, 10);
Object.assign(params, {shopId: shopId});
tdk('shop', shopId, req).then(TDKObj => {
if (TDKObj[0]) {
req.tdk = {
title: TDKObj[1],
keywords: TDKObj[2],
description: TDKObj[3]
};
}
return list.getShopInfo(shopId, req.user.uid).then(shopInfo => {
let pjax = params._pjax;
// 获取不到店铺信息跳转至首页
if (!shopInfo || _.isEmpty(shopInfo)) {
return res.redirect(helpers.urlFormat('', null, ''));
}
// 比较品牌域名与店铺域名是否一致,不一致跳转至店铺域名
if (!pjax && shopInfo.domain && domain && domain !== _.toLower(shopInfo.domain)) {
res.redirect(helpers.urlFormat('', params, shopInfo.domain));
return;
}
if (+shopInfo.shopTemplateType === 2) { // 经典模板
if (pjax) {
list.getShopGoodsData(shopId, req.yoho.channel, params, shopInfo).then(result => {
Object.assign(result, {
shopId: shopId,
layout: false
});
res.render('list/goods-list', result);
}).catch(next);
return;
}
list.getShopData(shopId, req.yoho.channel, params, shopInfo).then(result => {
Object.assign(result, {
page: 'shop',
shopId: shopId
});
// 店铺装修为空则不cache
if (!result.shopTopBanner) {
res.set('Cache-Control', 'no-cache');
}
res.render('list/shop-index', result);
}).catch(next);
} else { // 基础模板
list.getBaseShopData(params, Object.assign({uid: req.user.uid}, brandInfo),
req.yoho.channel, shopId).then(result => {
Object.assign(result, {page: 'list'});
// 基础店铺装修为空则不cache
if (!result.brand || !result.brand.shopBanner) {
res.set('Cache-Control', 'no-cache');
}
res.render('list/brand', result);
}).catch(next);
}
});
}).catch(next);
};
/**
* 商品分类列表页
* @param {[type]} req [description]
* @param {[type]} res [description]
* @return {[type]} [description]
*/
exports.index = (req, res, next) => {
let resData = {};
let qs = decodeURIComponent(req._parsedOriginalUrl.query || '');
list.getListData(Object.assign(req.query, {uid: req.user.uid, prid: req.user.prid}),
req.yoho.channel).then(result => {
Object.assign(resData, result);
if (qs) {
Object.assign(resData, listSeoMap[qs] || {});
}
// 查询结果为空则不cache
if (_.isEmpty(_.get(resData, 'list.goods', []))) {
res.set('Cache-Control', 'no-cache');
}
// 商品详情页跳转过来带参数
if (req.query.phrase) {
try {
resData.list.phrase = decodeURIComponent(req.query.phrase);
} catch (e) {
resData.list.phrase = '';
}
}
res.render('list/index', resData);
}).catch(next);
};
/**
* 新品到着
* @param {[type]} req [description]
* @param {[type]} res [description]
* @return {[type]} [description]
*/
exports.new = (req, res, next) => {
let resData = {};
list.getListNewData(Object.assign({order: 's_t_desc'}, req.query), req.yoho.channel).then(result => {
Object.assign(resData, result, {
hideInfo: {from: 'newProduct'}
});
// 查询结果为空则不cache
if (_.isEmpty(_.get(resData, 'list.goods', []))) {
res.set('Cache-Control', 'no-cache');
}
res.render('list/index', resData);
}).catch(next);
};
/**
* 新品到着(带频道)
* @param {[type]} req [description]
* @param {[type]} res [description]
* @return {[type]} [description]
*/
exports.newWithChannel = (req, res, next) => {
let channel = req.params[0];
req.query = req.query || {};
// 根据 XXXX-new 中的频道处理查询参数
switch (channel) {
case 'boys':
req.yoho.channel = 'boys';
req.query = Object.assign({gender: '1,3', category_id: '1'}, req.query);
break;
case 'girls':
req.yoho.channel = 'girls';
req.query = Object.assign({gender: '2,3', category_id: '2'}, req.query);
break;
case 'kids':
req.yoho.channel = 'kids';
req.query = Object.assign({gender: '1,2,3', category_id: '3'}, req.query);
break;
case 'lifestyle':
req.yoho.channel = 'lifestyle';
req.query = Object.assign({gender: '1,2,3', category_id: '4'}, req.query);
break;
default:
break;
}
this.new(req, res, next);
};
/**
* 品牌页
* @param {[type]} req [description]
* @param {[type]} res [description]
* @return {[type]} [description]
*/
exports.brand = (req, res, next) => {
let brandDomain = req.query.domain;
let shopId = req.query.shopId;
// shopId存在,直接走店铺
if (shopId) {
return shop(shopId, req, res, next);
}
if (!brandDomain) {
return next();
}
// 获取品牌信息
list.getBrandInfo({domain: brandDomain}).then(brandInfo => {
if (!brandInfo.hasOwnProperty('type')) {
return res.redirect(helpers.urlFormat(''));
}
switch (parseInt(brandInfo.type, 10)) {
case 1: // 搜索
res.redirect(helpers.urlFormat('', {query: brandInfo.brandDomain}, 'search'));
break;
case 2: // 店铺
return shop(brandInfo.shopId, req, res, next, brandInfo);
default: // 品牌
res.redirect(helpers.urlFormat('', {
query: brandInfo.brandDomain,
brand: brandInfo.brandId
}, 'search'));
break;
}
}).catch(next);
};
/**
* 品牌介绍页
* @param {[type]} req [description]
* @param {[type]} res [description]
* @return {[type]} [description]
*/
exports.brandAbout = (req, res, next) => {
let brandDomain = req.query.domain;
let shopId = req.query.shopId;
if (!brandDomain) {
return res.redirect(helpers.urlFormat(''));
}
if (shopId) {
list.getShopAbout(shopId, req.user.uid).then(result => {
res.render('list/brand', Object.assign(result, {page: 'list'}));
}).catch(next);
} else {
list.getBrandAbout(brandDomain, req.user.uid, req.yoho.channel).then(result => {
res.render('list/brand', Object.assign(result, {page: 'list'}));
}).catch(next);
}
};
/**
* 店铺商品列表页
* @param {[type]} req [description]
* @param {[type]} res [description]
* @return {[type]} [description]
*/
exports.shopList = (req, res, next) => {
let shopId = req.query.shopId;
if (!shopId) {
return next();
}
list.getShopListData(req.yoho.channel, req.query, req.user.uid).then(result => {
Object.assign(result, {
page: 'shop',
shopId: shopId
});
if (req.query.query) {
result.shopKey = req.query.query;
}
// 店铺装修为空则不cache
if (!result.shopTopBanner) {
res.set('Cache-Control', 'no-cache');
}
res.render('list/shop-list', result);
}).catch(next);
};
/**
* ajax调用品牌页左侧水牌
* @param {[type]} req [description]
* @param {[type]} res [description]
* @return {[type]} [description]
*/
exports.getNodeContent = (req, res, next) => {
if (!req.xhr || !req.body.node) {
return next();
}
list.getNodeContentData(req.body).then(result => {
res.json(result);
}).catch(next);
};
/**
* ajax调用品牌页左侧series folder
* @param {[type]} req [description]
* @param {[type]} res [description]
* @return {[type]} [description]
*/
exports.getAdnav = (req, res, next) => {
if (!req.xhr) {
return next();
}
list.getAdnav(req.body).then(result => {
res.json(result);
}).catch(next);
};
/**
* 判断用户是否收藏品牌页
* @param {[type]} req [description]
* @param {[type]} res [description]
* @return {[type]} [description]
*/
exports.isFavoriteBrand = (req, res, next) => {
let uid = req.user.uid;
let brandId = req.body.brandId;
if (!req.xhr) {
return next();
}
if (!req.body.brandId || !uid) {
return res.json({code: 400, message: '用户未登录或缺少参数'});
}
searchApi.isFavoriteBrand(uid, brandId).then(result => {
res.json(result);
}).catch(next);
};
/**
* 用户店铺优惠券领取状态
* @param {[type]} req [description]
* @param {[type]} res [description]
* @return {[type]} [description]
*/
exports.shopCouponSync = (req, res, next) => {
let uid = req.user.uid;
let id = req.query.id;
if (!req.xhr) {
return next();
}
if (!id || !uid) {
return res.json({code: 400, message: '用户未登录或缺少参数'});
}
list.getUserCoupunStatus(id, uid, 'shop').then(result => {
res.json(result);
});
};
/**
* 用户品牌优惠券领取状态
* @param {[type]} req [description]
* @param {[type]} res [description]
* @return {[type]} [description]
*/
exports.brandCouponSync = (req, res, next) => {
let uid = req.user.uid;
let id = req.query.id;
if (!req.xhr) {
return next();
}
if (!id || !uid) {
return res.json({code: 400, message: '用户未登录或缺少参数'});
}
list.getUserCoupunStatus(id, uid, 'brand').then(result => {
res.json(result);
});
};