list.js
4.14 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
'use strict';
const css = require('../css');
const _ = require('lodash');
const co = require('bluebird').coroutine;
const mRoot = '../models';
const utils = '../../../utils';
const productProcess = require(`${utils}/product-process`);
const searchProcess = require(`${utils}/search-process`);
const stringProcess = require(`${utils}/string-process`);
const listParamsProcess = require(`${utils}/list-params-process`);
const listModel = require(`${mRoot}/list`);
const camelCase = global.yoho.camelCase;
/**
* 封面图
* @type {{boys: string, gilrs: string}}
*/
const _coverChannel = {
boys: '1,3',
gilrs: '2,3'
};
const _formatJumpUrl = (params) => {
let opt = _.merge({}, params);
let order = 1;
delete opt[0];
if (opt.type === 'price') {
order = opt.order === '0' ? 1 : 0;
}
opt.union_type = '100000000013130';
return {
default: listParamsProcess.generatePathUrl(_.merge({}, opt, {type: 'default'})),
new: listParamsProcess.generatePathUrl(_.merge({}, opt, {type: 'new'})),
popularity: listParamsProcess.generatePathUrl(_.merge({}, opt, {type: 'popularity'})),
price: listParamsProcess.generatePathUrl(_.merge({}, opt, {type: 'price', order: order})),
discount0: listParamsProcess.generatePathUrl(_.merge({}, opt, {type: 'discount', order: 0})),
discount1: listParamsProcess.generatePathUrl(_.merge({}, opt, {type: 'discount', order: 1})),
curUrl: listParamsProcess.generatePathUrl(opt)
};
};
const _formatFilterUrl = (params, filter) => {
let opt = _.merge({}, params);
delete opt[0];
delete opt.from;
opt.union_type = '100000000013130';
_.each(_.get(filter, 'classify', []), (item) => {
_.each(_.get(item, 'subs', {}), sub => {
sub.url = listParamsProcess.generatePathUrl(_.merge({}, opt, {[item.dataType]: sub.dataId}));
});
});
return filter;
};
const index = (req, res, next) => {
let params = _.assign({}, req.query);
let uid = req.user.uid;
// 获取第一页数据做服务端渲染
let initialData = _.assign({
gender: params.gender,
type: 'default',
order: '0',
page: 1,
limit: 24,
isApp: params.app_version
}, params);
if (uid) {
initialData.uid = uid;
}
co(function* () {
let result = yield req.ctx(listModel).getCategoryGoods(initialData);
let filter = yield req.ctx(listModel).getFilterData(params);
let responseResult = {
list: productProcess.processProductList(_.get(result, 'data.product_list', []), {
isApp: params.isApp || (params.appVersion && params.appVersion !== 'false'),
gender: _coverChannel[params.coverChannel],
showSimilar: params.shop_id || params.material === 'true' ? false : true
})
};
let seoParams = searchProcess.getFilterValueForSeo(initialData, _.get(result, 'data', {}));
let seoRenderData = searchProcess.getListSeoData(seoParams);
let seoTitle = _.get(seoParams, 'sort');
let paramsTitle = params.title || params.sort_name; // 可能会配置的标题,优先级最高
if (paramsTitle) {
seoTitle = stringProcess.decodeURIComponent(paramsTitle);
}
if (!params.type) {
params.type = 'default';
}
let jumpUrls = _formatJumpUrl(params);
let chanpinCss = yield css('chanpin.css');
let commonCss = yield css('common.css');
res.render('list', Object.assign({
css: chanpinCss + commonCss,
title: seoRenderData.title || '商品列表',
mipFooter: true,
mipTab: true,
canonical: {
currentHref: `https://www.yohobuy.com${req.url}`
},
goodList: params,
pageTitle: seoTitle || '商品列表',
appPath: `yohobuy://yohobuy.com/goapp?openby:yohobuy={"action":"go.list","params":${JSON.stringify(params)}}`,
jumpUrls: jumpUrls,
filter: _formatFilterUrl(params, filter)
}, camelCase(responseResult)));
})().catch(next);
};
module.exports = {
index
};