shop.js
4.58 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
'use strict';
const _ = require('lodash');
const helpers = global.yoho.helpers;
const mRoot = '../models';
const shopModel = require(`${mRoot}/shop-service`);
const tdk = require('../../../utils/getTDK');
// 店铺首页(经典&基础)
exports.index = (req, res, next) => {
let domain = _.toLower(req.query.domain);
let shopId = req.query.shopId;
let shopInfo = req.params.shopInfo;
if (shopInfo) { // 新路由 /shop/:domain-:id.html
shopId = _.last(_.split(shopInfo, '-'));
domain = shopInfo.replace(`-${shopId}`, '');
req.query.domain = domain;
req.query.shopId = shopId;
} else if (req.shopRedirect && domain && shopId) { // subdomain方式
_.unset(req.query, 'shopId');
return res.redirect(301, helpers.urlFormat(`/shop/${domain}-${shopId}.html`,
_.isEmpty(req.query) ? null : req.query));
}
if (isNaN(+shopId)) {
return res.redirect(301, '/404');
}
if (req.xhr && req.query._pjax && shopId) {
return req.ctx(shopModel).getShopGoodsData(shopId, req.yoho.channel, req.query).then(result => {
Object.assign(result, {
shopId: shopId,
layout: false
});
res.render('list/goods-list', result);
});
}
return Promise.all([
tdk('shop', shopId, req),
req.ctx(shopModel).getShopInfoAsync(domain, req.yoho.channel, req.query)
]).then(result => {
let TDKObj = result[0],
shopObj = result[1];
if (TDKObj && TDKObj[0]) {
req.tdk = {
title: TDKObj[1],
keywords: TDKObj[2],
description: TDKObj[3]
};
}
// 数据异常,重定向
if (shopObj.redirect) {
if (shopObj.redirectType) {
return res.redirect(shopObj.redirectType, shopObj.redirect);
}
return res.redirect(shopObj.redirect);
}
if (shopObj.templateType === 2) { // 经典模板
Object.assign(shopObj, {page: 'shop'});
// 店铺装修为空则不cache
if (!shopObj.shopTopBanner) {
res.set('Cache-Control', 'no-cache');
}
res.render('shop/index', shopObj);
} else { // 基础模版
Object.assign(shopObj, {
page: 'list',
pageClass: 'static-list-page'
});
// 基础店铺装修为空则不cache
if (!shopObj.brand || !shopObj.brand.shopBanner) {
res.set('Cache-Control', 'no-cache');
}
res.render('list/brand', shopObj);
}
}).catch(next);
};
// 经典店铺列表
exports.list = (req, res, next) => {
let shopInfo = req.params.shopInfo;
if (shopInfo && !req.query.shopId) {
const shopId = _.last(_.split(shopInfo, '-'));
req.query.domain = shopInfo.replace(`-${shopId}`, '');
req.query.shopId = shopId;
}
if (!req.query.shopId) {
return next();
}
return req.ctx(shopModel).getShopListAsync(req.yoho.channel, req.query).then(shopObj => {
// 数据异常,重定向
if (shopObj.redirect) {
if (shopObj.redirectType) {
return res.redirect(shopObj.redirectType, shopObj.redirect);
}
return res.redirect(shopObj.redirect);
}
if (shopObj.templateType === 2) { // 经典模板
Object.assign(shopObj, {
page: 'shop',
pageClass: 'static-list-page',
shopId: req.query.shopId,
shopKey: req.query.query || ''
});
// 店铺装修为空则不cache
if (!shopObj.shopTopBanner) {
res.set('Cache-Control', 'no-cache');
}
res.render('shop/list', shopObj);
} else { // 基础模版
Object.assign(shopObj, {
page: 'list',
pageClass: 'static-list-page'
});
// 基础店铺装修为空则不cache
if (!shopObj.brand || !shopObj.brand.shopBanner) {
res.set('Cache-Control', 'no-cache');
}
res.render('list/brand', shopObj);
}
}).catch(next);
};
// 经典店铺推荐文章
exports.article = (req, res, next) => {
let brands = req.query.brands;
if (!brands) {
return res.send('');
}
return req.ctx(shopModel).getShopArticleByBrandsAsync(brands).then(result => {
res.render('shop/article', Object.assign(result, {layout: false}));
}).catch(next);
};