index.js
5.4 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
/**
* 频道页面
* @author: Bi Kai<kai.bi@yoho.cn>
* @date: 2016/05/09
*/
'use strict';
const _ = require('lodash');
const channelModel = require('../models/channel');
const homeModel = require('../../home/models/index');
const footerModel = require('../../../doraemon/models/footer_tab'); // 底部tab
const helpers = global.yoho.helpers;
let _renderData = {
module: 'channel',
page: 'home',
homeHeader: {
searchUrl: helpers.urlFormat('/search', null, 'search')
},
maybeLike: true,
showFooterTab: footerModel.getUrlData('home'),
pageFooter: true
};
/**
* 频道页生成函数
* @param {[object]} req
* @param {[object]} res
* @param {[object]} data 自定义数据
* @return {[type]}
*/
let _channelPage = (req, res, data) => {
return channelModel.getChannelData({
gender: data.gender,
uid: _.toString(req.user.uid),
limit: 6// 首屏先获取前6个楼层,其余用ajax获取
}).then(result => {
_renderData.homeHeader.defaultTerms = result.defaultTerms;
if (!result.content.length || !result.sideNav.length) {
res.set('Cache-Control', 'no-cache');
}
// result.content = [{
// seckill: true,
// data: {
// title: {
// name: '限时秒抢',
// title: '限时秒抢',
// moreUrl: 'http://m.yohobuy.com'
// }
// }
// }].concat(result.content);
// console.log(result.content[9]);
res.render('channel', Object.assign({}, _renderData, data, result, {
localCss: true
}));
});
};
/**
* 获取首页频道其余楼层
*/
let getResourceContent = (req, res, next) => {
return channelModel.getChannelResource({
gender: req.query.gender,
uid: _.toString(req.user.uid),
}).then(result => {
if (result.length) {
result = result.slice(6);
}
res.json(result);
}).catch(next);
};
/**
* 频道选择页
*/
let index = (req, res, next) => {
channelModel.getChannelSwitchData().then((result) => {
res.render('channel-index', {
module: 'channel',
page: 'index',
title: 'Yoho!Buy 有货',
searchUrl: helpers.urlFormat('/', null, 'search'),
pageFooter: true,
channelList: result[0].channelList,
yohood: result[0].yohood,
double11: result[0].double11,
background: result[1]
});
}).catch(next);
};
/**
* 频道页,根据查询字符串跳转频道中间件
* @param {object} req
* @param {object} res
* @param {Function} next
* @return {Function}
*/
let switchChannel = (req, res, next) => {
let channel = req.yoho.channel;
// 如果查询字符串设置了 go 参数,跳转到 cookie 中设置的频道页
if (req.query.go && channel) {
return res.redirect('/' + channel);
}
// 设置浏览器缓存5分钟 300000ms
// res.set('Expires', (new Date(_.now() + 300000)).toGMTString());
next();
};
/**
* 男生首页
*/
let boys = (req, res, next) => {
_channelPage(req, res, {
gender: 'boys',
title: '男生 | Yoho!Buy有货 | 潮流购物逛不停',
boysHomePage: true
}).catch(next); // TODO 我们在路由处理的最上层的方法处理catch
};
/**
* 女生首页
*/
let girls = (req, res, next) => {
_channelPage(req, res, {
gender: 'girls',
title: '女生 | Yoho!Buy有货 | 潮流购物逛不停',
girlsHomePage: true
}).catch(next);
};
/**
* 潮童首页
*/
let kids = (req, res, next) => {
_channelPage(req, res, {
gender: 'kids',
title: '潮童 | Yoho!Buy有货 | 潮流购物逛不停',
kidsHomePage: true
}).catch(next);
};
/**
* 创意生活首页
*/
let lifestyle = (req, res, next) => {
_channelPage(req, res, {
gender: 'lifestyle',
title: '创意生活 | Yoho!Buy有货 | 潮流购物逛不停',
lifestyleHomePage: true
}).catch(next);
};
/**
* 频道页底部 bannel
* @param {[object]} req
* @param {[object]} res
* @return {[type]}
*/
let bottomBanner = (req, res, next) => {
let gender = req.query.gender || 'boys';
channelModel.getBottomBannerData(gender).then(result => {
res.send(result);
}).catch(next);
};
/**
* 店铺推荐收藏状态人数
*/
let shopRecom = (req, res, next) => {
channelModel.shopRecom({
shopIds: req.body.shopIds || '',
uid: req.user.uid || 0,
}).then(result => {
res.send(result);
}).catch(next);
};
/**
* 获取用户vip信息
*/
let userVip = (req, res, next) => {
let uid = req.user.uid;
if (!uid) {
res.json({
code: 555,
msg: '未登录'
});
} else {
homeModel.getGradeGrade(uid, req.query.channel || 1).then(result => {
if (result.code === 200) {
res.json({
code: 200,
current_vip_level: result.data.current_vip_level,
});
} else {
res.json({
code: 500,
msg: '出错了',
});
}
}).catch(next);
}
};
module.exports = {
switchChannel,
index,
boys,
girls,
kids,
lifestyle,
bottomBanner,
shopRecom,
userVip,
getResourceContent
};