search.js
13.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/**
* search controller
* @author: wsl<shuiling.wang@yoho.cn>
* @date: 2016/7/21
*/
'use strict';
const mRoot = '../models';
const headerModel = require('../../../doraemon/models/header');
//const resourcesProcess = require(`${utils}/resources-process`);
const searchModel = require(`${mRoot}/search`);
const _ = require('lodash');
const helpers = global.yoho.helpers;
//const _processShopInfo = (list) => {
// let formatData = {
// logoImg: helpers.image(list.shopLogo, 500, 500),
// storeName: (list.isShowShopName === 'Y') ? list.shopName : '',
// collect: (list.isFavorite === 'Y') ? true : false
// };
//
// let allGoodsParam = {
// title: '全部商品'
// }
//
// return {};
//};
/**
* 组织店铺页面数据
* @param {array} data 接口返回的店铺页所需数据
* @param {int} shopId 店铺id
* @param {int} isApp app版本
* @return array
*/
const _formShopData = (data, shopId, isApp) => {
let formatData = {},
single = false,
shopIdObj = {
shop_id: shopId
};
let brandId;
//signboard
//oneRowTwoColImages
//allHotProducts
//shopTopBanner
//brandBrowse
//recommend
//shopTopBannerApp
//hotRecommend
//oneRowTwoColImagesApp
//recommendApp
//navigationBar
//newProducts
//goodsTabBar
//hotProductsApp
//largeSlideImg
//shopTopBannerBase
//hotProducts
//largeSlideImgApp
//allNewProducts
//allHotProductsApp
// 组织店铺装修
if (data.decorator) {
_.forEach(data.decorator.list, (floor) => {
floor[_.camelCase(floor.resourceName)] = true;
// 店铺banner
if (floor.shopTopBannerApp) {
formatData.branerImg = helpers.image(JSON.parse(floor.resourceData)[0].shopSrc, 640, 200, 1);
}
// 资源位小图 接口返回两组,取每组第一张
if (floor.oneRowTwoColImagesApp) {
let jsonData = JSON.parse(floor.resourceData);
_.forEach(jsonData, (key, item) => {
if (key === 0 && item.data) {
formatData.spring = {
url: item.data.url,
springType: helpers.image(item.data.src)
}
}
});
}
// 店铺品牌一览
if (floor.brandBrowse) {
}
});
}
// 店铺基本信息
if (data.shopInfo) {
let allGoodsParam = {
title: '全部商品'
};
formatData = {
logoImg: helpers.image(data.shopInfo.shopLogo, 500, 500),
storeName: (data.shopInfo.isShowShopName === 'Y') ? list.shopName : '',
collect: (data.shopInfo.isFavorite === 'Y') ? true : false,
url: helpers.urlFormat('', shopIdObj, 'search'), // 搜索链接
more_url: helpers.urlFormat('', {
shop_id: shopId,
order: 's_n_d',
title: '人气单品'
}, 'search') // 人气单品的链接
};
if (single) { // 单品点
allGoodsParam.brand = brandId;
} else { // 店铺id
formatData.shopId = shopId;
allGoodsParam.shopId = shopId;
}
if (!isApp) {
_.assign({
allGoods: helpers.urlFormat('', allGoodsParam, 'search'),
shopIntroHref: helpers.urlFormat('/product/index/intro', shopIdObj) // 店铺简介页地址
}, formatData);
} else {
_.assign({
allGoods: `${helpers.urlFormat('', allGoodsParam, 'search')}&openby:yohobuy={"action":"go.list","params":{"title":"全部商品", "actiontype":"0","shop_id":"' . self::$shopId . '","page":"1"}}`,
shopIntroHref: helpers.urlFormat('/product/index/intro', {
shop_id: shopId,
app_version: isApp
}),
more_url: `openby:yohobuy={"action":"go.list","params":{"shop_id":${shopId},"title":"人气单品"}}`
}, formatData);
}
}
return formatData;
};
/**
* 新店铺首页
* @param {object} req
* @param {int} shopId 店铺id
* @param {int} uid 用户id
* @param {string} isApp app版本
*/
const _getShopData = (req, shopId, uid, isApp) => {
let data = {};
let channel = req.yoho.channel;
isApp = isApp || '';
return Promise.all([searchModel.getShopDecorator(shopId), searchModel.getShopInfo(shopId, uid)]).then((result) => {
data = {
decorator: result[0], // 店铺装修资源数据
shopInfo: result[1] // 店铺信息
};
// 店铺使用基础模板,返回品牌页面
if (data.shopInfo.shopTemplateType && data.shopInfo.shopTemplateType === '1') {
return {
goBrand: data.shopInfo
}
}
// 店铺分类
return searchModel.getShopCategory(shopId, channel);
}).then(shopCategory => {
_.assign({
shopCategory: shopCategory
}, data);
return _formShopData(data, shopId, isApp); // 组织楼层数据
});
};
/**
* 店铺首页 || 若店铺使用基础模板跳转品牌页
* @return int
*/
const _shop = (req, res, shopId) => {
let isApp = req.query.app_version || req.query.appVersion || false;
let pageHeader = {};
let uid = 0;
if (!isApp) {
pageHeader = headerModel.setNav({
navTitle: ''
});
uid = req.user.uid;
} else {
uid = req.query.uid;
req.session.appUid = uid;
res.cookie('appUid', uid, {
domain: 'yohobuy.com',
expires: new Date(Date.now() + 2592000000) // 有效期一年
});
}
_getShopData(req, shopId, uid, isApp).then((result) => {
if (result.goBrand) {
// 跳转基础模板
//_baseShop(result.goBrand);
}
res.render('shop/index', _.assign({
shopIndex: result,
shopPage: {
text: '分类',
url: helpers.urlFormat('/product/index/category', {
shop_id: shopId
})
},
gender: req.query.gender,
channel: req.query.channel
}, pageHeader));
});
};
// 搜索落地页
const list = (req, res, next) => {
let params = Object.assign({}, req.query);
let title = '';
let query = req.query.query;
let isQueryFirstClass = false; // 标识用户搜的是不是一级品类
let isQuerySecondClass = false; // 标识用户搜的是不是二级品类
let domain = null;
params = _.assign({
isSearch: true,
cartUrl: helpers.urlFormat('/cart/index/index')
}, params);
/* 判断是不是品牌, 是品牌跳到品牌列表页(显示搜索框),判断是不是品类, 是品类加导航标题(不显示搜索框) */
Promise.all([searchModel.getAllBrandNames(), searchModel.getClassNames()]).then((result) => {
if (query) {
_.forEach(result[0], function(obj) {
if (query === obj.brandDomain) { // 精确查品牌域名
domain = query;
return false;
}
if (query === obj.brandName) { // 精确查品牌名称
domain = obj.brandName;
return false;
}
if (obj.brandDomain.indexOf(query) > 0) { // 模糊查品牌域名
domain = obj.brandDomain;
return false;
}
});
// 跳转到品牌商品列表页
if (domain !== null && !params.shop_id) {
let url = helpers.urlFormat('', {
from: 'search',
query: query
}, domain);
res.redirect(url);
}
// 品类名称为空时跳出
if (!result[1]) {
return;
}
_.forEach(result[1].first, (obj) => {
// 精确查一级品类
if (obj === query) {
isQueryFirstClass = true;
return false;
}
});
_.forEach(result[1].second, (obj) => {
// 精确查二级品类
if (obj === query) {
isQuerySecondClass = true;
return false;
}
});
} else {
params.query = '';
}
// 搜索是一级品类
if (isQueryFirstClass) {
title = '全部' + query;
} else if (isQuerySecondClass) { // 搜索是二级品类
title = query;
} else { // 搜索其它内容
if (query) {
params.search = {
default: query === '' ? false : query,
url: ''
};
}
title = '搜索';
}
res.render('search/list', {
module: 'product',
page: 'search-list',
pageHeader: headerModel.setNav({
navTitle: title
}),
goodList: params,
pageFooter: true
});
}).catch(next);
};
// 品类落地页
const category = (req, res) => {
let params = Object.assign({}, req.query);
res.render('search/goods-list', {
module: 'product',
page: 'search-list',
pageHeader: headerModel.setNav({
navTitle: req.query.title
}),
goodList: params,
showDownloadApp: true,
pageFooter: true
});
};
// 品牌落地页
const brand = (req, res, next) => {
let params = Object.assign({}, req.query);
let domain = 'colormad';
let uid = req.user.uid || 20000032;
let brandId = 0;
let brandLogo = {};
let title = '';
let brandHome = false;
if (!domain) {
res.redirect('/?go=1');
}
searchModel.getBrandLogoByDomain(domain).then((result) => {
brandLogo = result;
title = brandLogo.name;
//无店铺:0--->品牌页 无单品店有多品店:1--->搜索页 有单品店:2--->店铺页面
if (brandLogo.type === '2' && brandLogo.shopId) {
_shop(req, res, brandLogo.shopId);
return false
}
if (brandLogo && brandLogo.id) {
brandId = brandLogo.id;
}
params = _.assign({
brand: brandId
}, params);
//获取品牌店铺信息
return searchModel.getBrandShops(brandId);
}).then((brandShop) => {
if (brandId === 0) {
params.query = domain;
}
// 从搜索页过来的,显示搜索框, 和进入品牌引导信息 或者品牌关联多店铺
if (req.query.from === 'search' || brandShop.length > 0) {
params = {
brandWay: brandShop ? brandShop : brandLogo,
search: {
default: req.query.query,
url: helpers.urlFormat('', null, 'search')
}
};
} else if (brandId !== 0) { // 品牌一览过来的展示品牌介绍和LOGO
return Promise.all([searchModel.getBrandIntro(brandId, uid), searchModel.getBrandBanner(brandId)]).then((result) => {
title = result[0].title;
delete result[0].title;
return _.assign({
banner: result[1]
}, result[0]);
});
}
}).then((brandHome) => {
params.brandHome = brandHome;
res.render('search/goods-list', {
module: 'product',
page: 'search-list',
pageHeader: headerModel.setNav({
navTitle: title !== '' ? title : domain
}),
goodList: params,
showDownloadApp: true,
pageFooter: true
});
}).catch(next);
};
const search = (req, res, next) => {
let params = Object.assign({}, req.query);
searchModel.getSearchData(params).then((result) => {
res.render('search/page', {
layout: false,
new: result
});
}).catch(next);
};
/**
* 筛选
* @param {[object]} req
* @param {[object]} res
* @return {[type]}
*/
let filter = (req, res, next) => {
let params = Object.assign({}, req.query);
searchModel.getFilterData(params).then((result) => {
res.render('search/filter', {
layout: false,
params: params,
filter: result
});
}).catch(next);
};
/**
* 品牌[店铺]收藏/取消收藏
* id 品牌ID
* opt 操作标识("ok":表示收藏,"cancel":表示取消收藏)
*/
const favoriteBrand = (req, res, next) => {
let id = req.body.id;
let uid = req.user.uid || 20000032;
let opt = req.body.opt || 'ok';
if (_.isNumber(id)) {
res.json({
code: 401,
message: '参数不正确',
data: false
});
return false;
} else if (!uid) {
res.json({
code: 400,
message: '未登录',
data: helpers.urlFormat('/signin.html', {
refer: decodeURI(req.cookies.refer)
})
});
return false;
} else if (opt !== 'ok') { // 取消收藏
searchModel.setFavoriteCancel(id, uid).then((data) => {
res.json(data);
return false;
}).catch(next);
}
searchModel.setFavorite(id, uid).then((result) => {
if (!result.code) { // 收藏
result = {
code: 401,
message: '参数不正确',
data: false
};
}
res.json(result);
});
};
module.exports = {
list,
filter,
search,
category,
brand,
favoriteBrand
};