shop.js
7.04 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
/**
* 店铺相关页面
*
* 首页、列表页
*
* @author: jiangfeng<jeff.jiang@yoho.cn>
*/
'use strict';
const _ = require('lodash');
const Search = require('../models/search');
const DataHelper = require('../models/helpers');
const ShopData = require('../models/shop-service');
const Editorial = require('../models/editorial');
const shop = {
// 店铺首页
index(req, res, next) {
let domain = req.params.domain;
let uid = req.user.uid;
let shopId = req.query.shopId;
let channel = req.query._Channel || req.cookies._Channel || 'men';
let data = {
module: 'product',
page: 'shop',
title: domain
};
ShopData.getShopHeadData(domain, shopId, uid).then(result => {
if (result.shopId) {
let nav = [DataHelper.getChannelNav(channel)];
nav.push({
name: result.name
});
data.navPath = {
nav: nav
};
data.banner = result;
data.title = result.name;
data.shopId = result.shopId;
data.mores = {};
result.menus.forEach(m => {
if (m.id) {
data.mores[m.id] = m;
}
});
return Promise.all([Search.queryProductOfBrand({
shop_id: result.shopId,
limit: 16,
page: 1
}), Editorial.getByBrand(result.brandId)]);
} else {
return false;
}
}).then(result => {
let productData = result[0];
let editorial = result[1];
data.showEditorial = editorial && editorial.length === 3;
data.editorial = editorial;
_.forEach(editorial, (e) => {
e.title = e.title.length > 40 ? (e.title.substring(0, 40) + '...') : e.title;
e.url = `/editorial/${e.id}.html`;
});
if (productData && productData.code === 200 && productData.data) {
let ret = productData.data;
data.paginationData = {
page: 1,
limit: 16,
total: ret.total,
pageTotal: ret.page_total,
queryParams: req.query
};
data.products = DataHelper.handleProductList(ret.product_list);
res.display('shop-index', data);
} else {
shop.list(req, res, next);
}
}).catch(next);
},
// 店铺列表页
list(req, res, next) {
let data = {
module: 'product',
page: 'shop-list',
title: '店铺列表'
};
let channel = req.query._Channel || req.cookies._Channel || 'men';
let nav = [DataHelper.getChannelNav(channel)];
let domain = req.params.domain;
let uid = req.user.uid;
let shopId = req.query.shopId;
let q = req.query;
// let hasBrand = !!q.brand;
q.order = q.order || 's_n_desc';
q.page = parseInt(q.page || 1, 10);
// q.yh_channel = channel;
ShopData.getShopHeadData(domain, shopId, uid).then(result => {
data.banner = result;
data.title = result.name;
if (data.banner.banner) {
data.banner.banner = data.banner.banner.split('?')[0];
}
if (result.brandId) {
q.brand = result.brandId;
q.shop_id = result.shopId;
nav.push({
name: result.name
});
}
return result.brandId;
}).then((brandId) => {
if (brandId) {
return Promise.all([Search.queryAllSort({
brand: q.brand,
shop: q.shop_id,
small_sort: 1
}), Search.queryProductOfBrand(q)]).then(allResult => {
let allSort = allResult[0];
let result = allResult[1];
if (result && result.code === 200 && result.data) {
let ret = result.data;
if (ret.filter) {
ret.filter.standard = ret.standard;
ret.filter.group_sort = DataHelper.sortConvert(allSort.data.sort);
ret.filter.singleBrand = true;
data.filter = DataHelper.filterHandle(ret.filter, q);
data.filter.showPrice = ret.total > 10;
data.filter.showInfo = (data.filter.style && data.filter.style.length > 0)
|| (ret.standard && ret.standard.length > 0); //eslint-disable-line
}
data.paginationData = {
page: q.page,
limit: ret.limit || 45,
total: ret.total,
pageTotal: ret.page_total,
queryParams: req.query
};
data.navPath = {
nav: nav
};
res.display('shop-list', _.assign(data, {
products: DataHelper.handleProductList(ret.product_list, q, false),
order: q.order
}));
} else {
return Promise.reject('query shop index error');
}
});
} else {
return next();
}
}).catch(next);
},
// 首页ajax加载所有商品
indexQuery(req, res, next) {
let shopId = req.query.shopId;
let page = parseInt(req.query.page || 1, 10);
let query = {
shop_id: shopId,
limit: 16,
page: page,
order: req.query.order || ''
};
let data = {};
return Search.queryProductOfBrand(query).then(result => {
if (result && result.code === 200 && result.data) {
let ret = result.data;
data.paginationData = {
page: page,
limit: 16,
total: ret.total,
pageTotal: ret.page_total,
queryParams: req.query
};
let products = DataHelper.handleProductList(ret.product_list);
products.forEach(p => {
p.default_images = global.yoho.helpers.image(p.default_images, 270, 366);
p.sales_price = global.yoho.helpers.round(p.sales_price, 2);
});
res.json(_.assign(data, {
products: products
}));
} else {
return Promise.reject('query shop index error');
}
}).catch(next);
}
};
module.exports = shop;