shop.js
5.65 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
/**
* 店铺相关页面
*
* 首页、列表页
*
* @author: jiangfeng<jeff.jiang@yoho.cn>
*/
'use strict';
const _ = require('lodash');
const camelCase = global.yoho.camelCase;
const cookie = global.yoho.cookie;
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 = cookie.getUid(req);
let data = {
module: 'product',
page: 'shop',
title: domain
};
ShopData.getShopHeadData(domain, uid).then(result => {
if (result.shopId) {
let nav = [DataHelper.getChannelNav()];
nav.push({
name: result.name
});
data.navPath = {
nav: nav
};
data.banner = result;
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;
if (productData && productData.code === 200 && productData.data) {
let ret = camelCase(productData.data);
data.paginationData = {
page: 1,
limit: 16,
total: ret.total,
pageTotal: ret.pageTotal,
queryParams: req.query
};
data.products = DataHelper.handleProductList(ret.productList);
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 nav = [DataHelper.getChannelNav()];
let domain = req.params.domain;
let uid = cookie.getUid(req);
let q = req.query;
q.page = parseInt(q.page || 1, 10);
ShopData.getShopHeadData(domain, uid).then(result => {
data.banner = result;
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
});
} else {
res.status(404);
return Promise.reject('brand not found');
}
}).then(() => {
return Search.queryProductOfBrand(q).then(result => {
if (result && result.code === 200 && result.data) {
let ret = camelCase(result.data);
if (ret.filter) {
delete q.brand;
data.filter = DataHelper.filterHandle(ret.filter, req.query);
}
data.paginationData = {
page: q.page,
limit: ret.limit || 45,
total: ret.total,
pageTotal: ret.pageTotal,
queryParams: req.query
};
data.navPath = {
nav: nav
};
res.display('shop-list', _.assign(data, {
products: DataHelper.handleProductList(ret.productList),
order: q.order
}));
} else {
return Promise.reject('query shop index error');
}
});
}).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 = camelCase(result.data);
data.paginationData = {
page: page,
limit: 16,
total: ret.total,
pageTotal: ret.pageTotal,
queryParams: req.query
};
let products = DataHelper.handleProductList(ret.productList);
products.forEach(p => {
p.defaultImages = global.yoho.helpers.image(p.defaultImages, 270, 366);
p.salesPrice = global.yoho.helpers.round(p.salesPrice, 2);
});
res.json(_.assign(data, {
products: products
}));
} else {
return Promise.reject('query shop index error');
}
}).catch(next);
}
};
module.exports = shop;