list.js
16.3 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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/**
* 品类|品牌|店铺 controller
* @author: wsl<shuiling.wang@yoho.cn>
* @date: 2016/7/21
*/
'use strict';
const mRoot = '../models';
const headerModel = require('../../../doraemon/models/header');
const listModel = require(`${mRoot}/list`);
const _ = require('lodash');
const helpers = global.yoho.helpers;
/**
* 组织店铺页面数据
* @param {array} data 接口返回的店铺页所需数据
* @param {int} shopId 店铺id
* @param {int} isApp app版本
* @return array
*/
const _formShopData = (data, shopId, isApp) => {
let formatData = {
shopId: shopId,
appVersion: isApp
},
single = false;
let brandId;
// 组织店铺装修
if (data.decorator) {
_.forEach(data.decorator.list, (floor) => {
let resData = JSON.parse(floor.resourceData);
floor[_.camelCase(floor.resourceName)] = true;
// 店铺banner
if (floor.shopTopBannerApp) {
formatData.branerImg = resData[0].shopSrc;
}
// 资源位小图 接口返回两组,取每组第一张
if (floor.oneRowTwoColImagesApp) {
let spring = [];
_.forEach(resData, (item) => {
if (item.data) {
spring.push({
url: item.data[0].url,
springType: helpers.image(item.data[0].src)
});
}
});
formatData.spring = spring;
}
// 店铺品牌一览
if (floor.brandBrowse) {
let brandNumber = resData.length;
let brand = {
list: []
};
brandId = '';
// 少于2个不展示 单品店:单品店根据品牌id查询
if (brandNumber < 2) {
brandId = formatData.brand = resData[0].id;
single = true;
} else {
_.forEach(resData, (item) => {
if (item.brandDomain) {
brand.list.push({
url: helpers.urlFormat('', '', item.brandDomain) + (isApp ? `?openby:yohobuy={"action":"go.brand","params":{"shop_id":${shopId},"brand_id":${item.id}}}` : ''), //eslint-disable-line
img: helpers.image(item.brandIco, 640, 400),
brandName: item.brandName
});
brandId += item.id + ',';
}
});
formatData.brandList = brand;
// 大于5个返回更多品牌链接
if (brandNumber > 5) {
formatData.brandList.url = helpers.urlFormat('/product/index/allBrand', {
shop_id: shopId
});
}
}
}
// 资源位大图幻灯
if (floor.largeSlideImgApp) {
let bannerTop = {
data: []
};
_.forEach(resData, (item) => {
if (item.data[0]) {
bannerTop.data.push({
url: item.data[0].url,
src: item.data[0].src
});
}
});
formatData.bannerTop = bannerTop;
}
// 热门品类
if (floor.recommendApp) {
let hotCategory = {
data: {
title: {
title: '热门品类'
},
list: []
}
};
_.forEach(resData, (cate) => {
hotCategory.data.list.push({
url: cate.url,
src: helpers.image(cate.src)
});
});
formatData.hotCategory = hotCategory;
}
// 人气单品
if (floor.hotProductsApp) {
let goods = [];
_.forEach(resData, (item) => {
goods.push({
url: helpers.urlFormat(item.productId, item.goodsId, item.cnAlphabet) + (isApp ? `?openby:yohobuy={"action":"go.productDetail","params":{"product_skn":${item.productSkn}}` : ''),//eslint-disable-line
img: helpers.image(item.src, 235, 314),
productName: item.productName,
salesPrice: item.salesPrice,
presentPrice: item.salesPrice
});
});
formatData.hotList = goods;
}
});
}
// 店铺基本信息
if (data.shopInfo) {
let allGoodsParam = {
title: '全部商品'
};
formatData = _.assign({
logoImg: data.shopInfo.shopLogo,
storeName: (data.shopInfo.isShowShopName === 'Y') ? data.shopInfo.shopName : '',
collect: data.shopInfo.isFavorite === 'Y',
url: helpers.urlFormat('', {
shop_id: shopId
}, 'search'), // 搜索链接
more_url: helpers.urlFormat('', {
shop_id: shopId,
order: 's_n_d',
title: '人气单品'
}, 'search') // 人气单品的链接
}, formatData);
if (single) { // 单品点
allGoodsParam.brand = brandId;
} else { // 店铺id
formatData.shopId = shopId;
allGoodsParam.shopId = shopId;
}
if (!isApp) {
formatData = _.assign({
allGoods: helpers.urlFormat('', allGoodsParam, 'search'),
shopIntroHref: helpers.urlFormat('/product/index/intro', {
shop_id: shopId
}) // 店铺简介页地址
}, formatData);
} else {
formatData = _.assign({
allGoods: `${helpers.urlFormat('', allGoodsParam, 'search')}&openby:yohobuy={"action":"go.list","params":{"title":"全部商品", "actiontype":"0","shop_id":"' . self::$shopId . '","page":"1"}}`,//eslint-disable-line
shopIntroHref: helpers.urlFormat('/product/index/intro', {
shop_id: shopId,
app_version: isApp
}),
more_url: formatData.more_url + `?openby:yohobuy={"action":"go.list","params":{"shop_id":${shopId},"title":"人气单品"}}`//eslint-disable-line
}, formatData);
}
}
// 店铺下面的所有分类
if (data.shopCategory) {
console.log(data.shopCategory);
let total = data.shopCategory.length;
let shopCategory = {
list: []
};
if (total > 6) {
shopCategory.url = helpers.urlFormat('/product/index/category', {
shop_id: shopId
});
} else {
_.forEach(data.shopCategory, (item) => {
shopCategory.list.push({
url: helpers.urlFormat('', {
shop_id: shopId,
sort: item.relationParameter.sort
}, 'search'),
categoryId: item.categoryId,
name: item.categoryName
});
});
}
formatData.shopCategory = shopCategory;
}
if (single) {
formatData.favId = shopId;
formatData.shopId = '';
}
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;
return Promise.all([listModel.getShopDecorator(shopId), listModel.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 listModel.getShopCategory(shopId, channel).then((shopCategory) => {
data = _.assign({
shopCategory: shopCategory
}, data);
return _formShopData(data, shopId, isApp); // 组织楼层数据
});
});
};
/**
* 店铺基础模板
*/
const _baseShop = (req, res, data) => {
let params = Object.assign({
cartUrl: helpers.urlFormat('/cart/index/index')
}, req.query);
Promise.all([listModel.getShopDecorator(data.shopsId), listModel.getShopBrands(data.shopsId)]).then((result) => {
result[0] = result[0] || {};
console.log(result[0], result[1]);
_.forEach(result[0], (item) => {
if (item.resourceName === 'shopTopBanner_base') {
let banner = JSON.parse(item.resourceData);
params = _.assign({
shopBanner: {
banner: banner[0].shopSrc
}
}, params);
}
});
if (data.multBrandShopType === '2') {
// 转义店铺
params.shop_id = data.shopsId;
} else {
params.brand = result[1];
}
res.render('search/goods-list', {
module: 'product',
page: 'search-list',
pageHeader: headerModel.setNav({
navTitle: data.shopName
}),
title: data.shopName,
goodList: params,
showDownloadApp: true,
pageFooter: true
});
});
};
/**
* 店铺首页 || 若店铺使用基础模板跳转品牌页
* @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 = {
pageHeader: _.assign({
shopPage: {
text: '分类',
url: helpers.urlFormat('/product/index/category', {
shop_id: shopId
})
}
}, headerModel.setNav({
navTitle: false
}))
};
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(req, res, result.goBrand);
} else {
result = _.assign(result, pageHeader);
res.render('shop/index', {
module: 'product',
page: 'shop',
shopIndex: result,
shopHeadHide: true,
gender: req.query.gender,
channel: req.query.channel
});
}
});
};
// 品类落地页
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 = '';
if (!domain) {
res.redirect('/?go=1');
}
listModel.getBrandLogoByDomain(domain).then((result) => {
brandLogo = result;
title = brandLogo.name;
if (brandLogo && brandLogo.id) {
brandId = brandLogo.id;
}
params = _.assign({
brand: brandId
}, params);
// 无店铺:0--->品牌页 无单品店有多品店:1--->搜索页 有单品店:2--->店铺页面
if (brandLogo.type === '2' && brandLogo.shopId) {
_shop(req, res, brandLogo.shopId);
} else { // 获取品牌店铺信息
listModel.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([listModel.getBrandIntro(brandId, uid), listModel.getBrandBanner(brandId)]).then((resData) => { //eslint-disable-line
title = resData[0].title;
delete resData[0].title;
return _.assign({
banner: resData[1]
}, resData[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 shopIntro = (req, res, next) => {
let shopId = req.query.shop_id;
let appVersion = req.body.appVersion || false;
let params = {};
if (shopId) {
listModel.getShopIntro(shopId).then((result) => {
if (appVersion) {
params = {
title: '店铺简介'
};
} else {
params = {
pageHeader: headerModel.setNav({
navTitle: '店铺简介'
})
};
}
res.render('shop/intro', _.assign(params, result));
}).catch(next);
} else {
res.redirect('/?go=1');
}
};
/**
* 品牌[店铺]收藏/取消收藏
* 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';
let type = req.body.type || 'product';
let appVersion = req.body.appVersion || false;
if (appVersion === 'true') {
uid = req.cookies.appUid;
}
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') { // 取消收藏
listModel.setFavoriteCancel(id, uid, type).then((data) => {
res.json(data);
return false;
}).catch(next);
}
listModel.setFavorite(id, uid, type).then((result) => {
if (!result.code) { // 收藏
result = {
code: 401,
message: '参数不正确',
data: false
};
}
res.json(result);
});
};
module.exports = {
category,
brand,
favoriteBrand,
shopIntro
};