shop.js
11.9 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
'use strict';
const mRoot = '../models';
const utils = '../../../utils';
const shopModel = require(`${mRoot}/shop`);
const listModel = require(`${mRoot}/list`);
const _ = require('lodash');
const helpers = global.yoho.helpers;
const productProcess = require(`${utils}/product-process`);
const stringProcess = require(`${utils}/string-process`);
const shopPrcs = require(`${utils}/shop-process`);
const co = require('bluebird').coroutine;
const css = require('../css');
const camelCase = global.yoho.camelCase;
const shop = {
/**
* 解析店铺的参数
*/
resolveShopParams(req, res, next) {
if (req.params.shopParams) {
req.query.shop_id = _.last(_.split(req.params.shopParams, '-'));
req.query.domain = req.params.shopParams.replace(`-${req.query.shop_id}`, '');
}
return next();
},
/**
* 店铺统一入口
*/
entry(req, res, next) {
const shopId = _.parseInt(stringProcess.paramsFilter(req.params.shop_id || req.query.shop_id));
const domain = req.params.domain || req.query.domain;
const uid = req.user.uid;
req.shopInfo = {};
return co(function* () {
if (shopId) {
let shopInfoApi = (yield req.ctx(shopModel).getShopInfo(shopId, uid)) || {};
let shopInfo = _.get(shopInfoApi, 'data', {});
let apiDomain = _.get(shopInfo, 'shop_domain');
if (apiDomain && apiDomain !== domain) {
return res.redirect(301, `//m.yohobuy.com/mip/shop/${apiDomain}-${shopId}.html`);
}
// 红人店铺
if (shopInfo && shopInfo.is_red_shop === 1) {
shopInfo.isRedShop = true;
}
// 店铺,非红人店铺,跳转店铺列表
if (shopInfo && shopInfo.is_red_shop !== 1) {
shopInfo.isShop = true;
}
// 可能没有店铺信息,跳转店铺列表
req.shopInfo = shopInfo;
} else if (domain) {
let domainInfo = (yield req.ctx(shopModel).getBrandLogoByDomain(domain)) || {}; // 通过域名查询店铺类型,或者品牌信息
if (domainInfo.shopId && domainInfo.type === '2') { // 店铺
let shopInfoApi = yield req.ctx(shopModel).getShopInfo(domainInfo.shopId, uid);
let shopInfo = _.get(shopInfoApi, 'data', {});
// 红人店铺
if (shopInfo && shopInfo.is_red_shop === 1) {
shopInfo.isRedShop = true;
}
// 店铺,非红人店铺,跳转店铺列表
if (shopInfo && shopInfo.is_red_shop !== 1) {
shopInfo.isShop = true;
}
_.assign(req.shopInfo, domainInfo, shopInfo);
} else { // 品牌
req.shopInfo = domainInfo;
}
}
if (req.shopInfo.isRedShop) { // 店铺
return shop.redShop(req, res, next);
} else if (shopId || req.shopInfo.isShop) { // 非红人店铺的店铺
return shop.list(req, res, next);
} else { // 什么都没有,去首页
return res.redirect(helpers.urlFormat('', {go: 1}));
}
})().catch(next);
},
/**
* 红人店铺
*/
redShop(req, res, next) {
let shopId = req.shopInfo.shops_id;
let domain = req.shopInfo.shop_domain || req.shopInfo.brandDomain || 'shop';
let channel = req.yoho.channel || 'boys';
let uid = req.user.uid || 0;
let udid = req.cookies.udid || 'yoho';
let isMip = true;
if (!shopId) {
return next();
}
co(function* () {
let [bannerData, favCountData, decoratorsData, categoryData, defaultGoods, newGoods] = yield Promise.all([
req.ctx(shopModel).getBanner(shopId),
req.ctx(shopModel).favCount(shopId, uid, channel, udid),
req.ctx(shopModel).getShopsdecorator(shopId),
req.ctx(shopModel).getShopCategory(shopId, channel),
req.ctx(listModel).getShopGoods({shop_id: shopId, order: '0', type: 'default'}),
req.ctx(listModel).getShopGoods({shop_id: shopId, order: '0', type: 'new', end: false, page: 1})
]);
let banner = _.get(bannerData, 'data.banner');
let shopInfo = req.shopInfo;
let favCount = _.get(favCountData, 'data[0].approximateCount', '2.1w');
let decoratorsAll = shopPrcs.floor(_.get(decoratorsData, 'data.modules', []), isMip);
let category = shopPrcs.category(_.get(categoryData, 'data', []), {
shopId: shopId,
domain: domain
}, isMip);
let goodsListBySkn = yield req.ctx(listModel).searchProductBySkn(decoratorsAll.skns);
let decorators = shopPrcs.pushGoodsInfo(decoratorsAll.decorators, goodsListBySkn);
shopInfo.shop_intro_link = helpers.urlFormat('/product/index/intro', {
shop_id: shopId,
union_type: '100000000013130'
});
let AppRecommendProduct_0 = _.find(decorators, {module_type: 'AppRecommendProduct', type: 0 });
let AppRecommendProduct_1 = _.find(decorators, {module_type: 'AppRecommendProduct', type: 1 });
if (AppRecommendProduct_0) {
AppRecommendProduct_0 = yield req.ctx(listModel).getShopGoods({
shop_id: shopId,
order: AppRecommendProduct_0.order
});
AppRecommendProduct_0 = productProcess.processProductList(_.get(AppRecommendProduct_0, 'data.product_list', []));// eslint-disable-line
}
if (AppRecommendProduct_1) {
AppRecommendProduct_1 = yield req.ctx(listModel).searchProductBySkn(AppRecommendProduct_1.skns);
AppRecommendProduct_1 = productProcess.processProductList(AppRecommendProduct_1);
_.forEach(AppRecommendProduct_1, (value, key) => {
AppRecommendProduct_1[key].tags = {};
AppRecommendProduct_1[key].is_soon_sold_out = false;
AppRecommendProduct_1[key].tags.is_hot = true;
});
}
_.each(decorators, item => {
if (item.module_type === 'AppRecommendProduct' && item.type === 0) {
item.list = camelCase(AppRecommendProduct_0) || [];
}
if (item.module_type === 'AppRecommendProduct' && item.type === 1) {
item.list = camelCase(AppRecommendProduct_1) || [];
}
});
defaultGoods = productProcess.processProductList(_.get(defaultGoods, 'data.product_list', []));
newGoods = productProcess.processProductList(_.get(newGoods, 'data.product_list', []));
let shopRedsCss = yield css('shop/shop-reds.css');
let chanpinCss = yield css('chanpin.css');
let commonCss = yield css('common.css');
res.render('shop/shop-reds', {
css: shopRedsCss + chanpinCss + commonCss,
mipTab: true,
mipFooter: true,
pageHead: {
text: '分类',
url: helpers.urlFormat('/product/index/category', {
shop_id: shopId,
domain: domain,
union_type: '100000000013130'
})
},
title: `${shopInfo.shop_name}_${shopInfo.shop_name}品牌官方旗舰店-YOHO!BUY 有货官网`,
pageTitle: shopInfo.shop_name || '',
allGoodsUrl: helpers.urlFormat('', {
shop_id: shopId,
title: '全部商品',
union_type: '100000000013130'
}, 'list'),
canonical: {
currentHref: `https://www.yohobuy.com${req.originalUrl}`
},
appPath: 'yohobuy://yohobuy.com/goapp?openby:yohobuy={"action":"go.shop","params":{"shop_template_type":"1","is_red_shop":"1","shop_id":"' +
shopId + '","shop_name":"' + shopInfo.shop_name + '"}}',
defaultGoods: {list: camelCase(defaultGoods) || []},
newGoods: {list: camelCase(newGoods) || []},
shopId,
banner, shopInfo, favCount, decorators, category
});
})().catch(next);
},
/**
* 非红人店铺的店铺页面
*/
list(req, res, next) {
co(function* () {
let shopId = _.parseInt(stringProcess.paramsFilter(req.query.shop_id)) || req.shopInfo.shops_id;
let shopInfo = req.shopInfo || {};
let title = shopInfo.shop_name || '店铺商品列表';
let searchParam = {
isApp: req.yoho.isApp,
shop_id: shopId,
app_type: req.query.app_type,
type: 'default',
order: '0',
page: 1
};
let [defaultGoods, newGoods] = yield Promise.all([
req.ctx(listModel).getShopGoods(searchParam),
req.ctx(listModel).getShopGoods(_.assign(searchParam, {type: 'new'}))
]);
defaultGoods = productProcess.processProductList(_.get(defaultGoods, 'data.product_list', []));
newGoods = productProcess.processProductList(_.get(newGoods, 'data.product_list', []));
if (shopInfo.isShop) {
let shopDecorator = yield req.ctx(shopModel).getShopDecorator(shopId);
let shopDecoratorList = _.get(shopDecorator, 'list', []);
if (shopDecoratorList) {
shopInfo.banner = shopPrcs.getShopBanner(shopDecoratorList);
}
}
let commonCss = yield css('common.css');
let shopListCss = yield css('/shop/shop-list.css');
let chanpinCss = yield css('chanpin.css');
res.render('shop/shop-list', _.assign(
{
css: shopListCss + chanpinCss + commonCss,
title: `${shopInfo.shop_name}_${shopInfo.shop_name}品牌官方旗舰店-YOHO!BUY 有货官网`,
pageTitle: title,
mipFooter: true,
mipTab: true,
mipLightBox: true,
shopId: shopId,
shopInfo: shopInfo,
defaultGoods: camelCase(defaultGoods),
newGoods: camelCase(newGoods),
allGoodsUrl: helpers.urlFormat('', {
shop_id: shopId,
title: '全部商品',
union_type: '100000000013130'
}, 'list'),
appPath: 'yohobuy://yohobuy.com/goapp?openby:yohobuy={"action":"go.shop","params":{"shop_template_type":"1","shop_id":"' +
shopId + '","shop_name":"' + shopInfo.shop_name + '"}}',
canonical: {
currentHref: `https://www.yohobuy.com${req.originalUrl}`
}
}
));
})().catch(next);
},
_formatJumpUrl(curUrl) {
let str = '';
if (curUrl.indexOf('?') > -1) {
str = '&';
} else {
str = '?';
}
return {
default: `//m.yohobuy.com${curUrl}${str}type=default`,
new: `//m.yohobuy.com${curUrl}${str}type=new`,
popularity: `//m.yohobuy.com${curUrl}${str}type=popularity`,
price: `//m.yohobuy.com${curUrl}${str}type=popularity&order=1`,
discount0: `//m.yohobuy.com${curUrl}${str}type=discount&order=0`,
discount1: `//m.yohobuy.com${curUrl}${str}type=discount&order=0`
};
}
};
module.exports = shop;