index.js
13.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/**
* guang controller
* @author: lcy<chuanyang.liu@yoho.cn>
* @date: 2016/08/31
*/
'use strict';
const _ = require('lodash');
const guangModel = require('../models/index');
const headerModel = require('../../../doraemon/models/header');
const ghelper = require('../models/guang-helper');
const urlHelper = require('../models/url-helper');
const querystring = require('querystring');
const helpers = global.yoho.helpers;
/**
* 首页文章列表 类型列表
*/
exports.index = (req, res, next) => {
let channel = req.yoho.channel;
let page = req.query.page || 1;
let pageSize = req.query.pageSize || 20;
let type = req.query.type || 0;
let pathNav = guangModel.getPathNav(channel);
let uid = req.user.uid;
let udid = ghelper.getUdid(req, res);
let gender = ghelper.getGenderByCookie(req);
let isHotDegrade = _.get(req.app.locals.pc, 'guang.removeHotTag', false);
let isAdDegrade = _.get(req.app.locals.pc, 'guang.removeAd', false);
Promise.all([
guangModel.getBanner(channel),
guangModel.getCategory(type, channel),
guangModel.getArticleList(gender, type, uid, udid, page, '', '', pageSize, channel, true),
guangModel.getHotTags(page, pageSize, channel, isHotDegrade),
guangModel.getAds(channel, isAdDegrade),
guangModel.getRecoArticles(gender, 1, 10, channel),
headerModel.requestHeaderData(channel)
]).then(ret => {
if (_.isEmpty(ret[2]) || _.isEmpty(ret[2].msgs)) {
res.set('Cache-Control', 'no-cache');
}
res.render('guang/index', {
title: '逛' + (res.locals.title || ''),
guang: {
slider: ret[0],
msgTypes: ret[1],
msgs: ret[2] && ret[2].msgs,
hotTags: ret[3],
ads: ret[4],
exRecos: ret[5],
guangIndexPage: true,
pageSize: pageSize,
type: type,
pathNav: pathNav,
baseUrl: urlHelper.listUrl(channel, type),
page: page,
total: (ret[2] && ret[2].total) || 0
},
module: 'guang',
page: 'guang',
headerData: ret[6] && ret[6].headerData
});
}).catch(next);
};
/**
* 指定标签的文章列表
*/
exports.tags = (req, res, next) => {
let query = req.query.query;
let page = req.query.page || 1;
let pageSize = req.query.pageSize || 20;
let type = req.query.type || 0;
let uid = req.user.uid;
let udid = ghelper.getUdid(req, res);
let gender = ghelper.getGenderByCookie(req);
let channel = req.yoho.channel;
let pathNav = guangModel.getPathNav(channel, query);
let isHotDegrade = _.get(req.app.locals.pc, 'guang.removeHotTag', false);
let isAdDegrade = _.get(req.app.locals.pc, 'guang.removeAd', false);
Promise.all([
guangModel.getBanner(channel),
guangModel.getArticleList(gender, 0, uid, udid, page, query, '', pageSize, channel, true),
guangModel.getHotTags(1, 20, channel, isHotDegrade),
guangModel.getAds(channel, isAdDegrade),
guangModel.getRecoArticles(gender, 1, 10, channel),
headerModel.requestHeaderData(channel)
]).then(ret => {
if (_.isEmpty(ret[1]) || _.isEmpty(ret[1].msgs)) {
res.set('Cache-Control', 'no-cache');
}
res.render('guang/tag', {
title: query + (res.locals.title || ''),
guang: {
slider: ret[0],
msgs: ret[1] && ret[1].msgs,
hotTags: ret[2],
ads: ret[3],
exRecos: ret[4],
tag: query,
guangIndexPage: true,
baseUrl: `?${querystring.stringify(req.query)}`,
pageSize: pageSize,
type: type,
pathNav: pathNav,
page: page,
total: (ret[1] && ret[1].total) || 0
},
module: 'guang',
page: 'list',
headerData: ret[5] && ret[5].headerData
});
}).catch(next);
};
/**
* 作者文章列表
*/
exports.editor = (req, res, next) => {
let authorId = req.query.author_id;
let channel = req.yoho.channel;
let gender = ghelper.getGenderByCookie(req);
let isHotDegrade = _.get(req.app.locals.pc, 'guang.removeHotTag', false);
let isAdDegrade = _.get(req.app.locals.pc, 'guang.removeAd', false);
let uid = req.user.uid;
let udid = ghelper.getUdid(req, res);
let pathNav = guangModel.getPathNav(channel);
let page = req.query.page || 1;
let pageSize = req.query.pageSize || 20;
Promise.all([
guangModel.getAuthor(channel, authorId),
guangModel.getArticleList(gender, null, uid, udid, page, '', authorId, pageSize, channel, true),
guangModel.getHotTags(1, 20, channel, isHotDegrade),
guangModel.getAds(channel, isAdDegrade),
guangModel.getRecoArticles(gender, 1, 10, channel),
headerModel.requestHeaderData(channel)
]).then(ret => {
if (_.isEmpty(ret[1]) || _.isEmpty(ret[1].msgs)) {
res.set('Cache-Control', 'no-cache');
}
res.render('guang/editor', {
title: _.get(ret[0], 'name', '') + (res.locals.title || ''),
guang: {
editor: ret[0],
msgs: ret[1] && ret[1].msgs,
hotTags: ret[2],
ads: ret[3],
exRecos: ret[4],
gender: gender,
baseUrl: urlHelper.editorUrl(channel, authorId),
pageSize: pageSize,
pathNav: pathNav,
page: page,
total: (ret[1] && ret[1].total) || 0
},
module: 'guang',
page: 'list',
headerData: ret[5] && ret[5].headerData
});
}).catch(next);
};
/**
* 文章详情
*/
exports.detail = (req, res, next) => {
let id = req.params.id || req.query.id;
let page = req.query.page || 1;
let pageSize = req.query.pageSize || 10;
let col = req.query.col || 0;
let pjax = req.query._pjax;
let uid = req.user.uid;
let udid = ghelper.getUdid(req, res);
let gender = ghelper.getGenderByCookie(req);
let channel = req.yoho.channel;
let isHotDegrade = _.get(req.app.locals.pc, 'guang.removeHotTag', false);
let isAdDegrade = _.get(req.app.locals.pc, 'guang.removeAd', false);
if (!_.isNumber(id)) {
id = _.parseInt(id);
}
if (pjax) {
guangModel.getArticleComments(id, page, pageSize).then(ret => {
res.set('Cache-Control', 'no-cache');
res.render('guang/detail-comment', {
layout: false,
comment: ret
}, function(err, html) {
if (err) {
return res.send('');
}
return res.status(200).send(html);
});
});
return;
}
// 登陆后自动收藏
if (col === 1 && uid > 0) {
guangModel.collectArticle(id, uid);
}
guangModel.getArticleInfo(id, channel).then(info => {
// 判断参数是否有效, 无效会跳转到错误页面
if (!info || !info.title) {
return new Error('文章不存在');
}
let promises = [
headerModel.requestHeaderData(channel),
guangModel.getArticleContent(id),
guangModel.getHotTags(1, 20, channel, isHotDegrade),
guangModel.getArticleComments(id, page, pageSize),
guangModel.getArticleBaseInfo(id, uid, udid),
guangModel.getArticleRelateBrand(id),
guangModel.getRecoArticles(gender, 1, 10, channel),
guangModel.getAds(channel, isAdDegrade)
];
if (info.authorId) {
promises.push(guangModel.getAuthor(channel, info.authorId));
} else {
promises.push({});
}
if (info.tag) {
promises.push(guangModel.getRelateArticleList(id, info.tag, 2, channel));
} else {
promises.push({});
}
let pathNav = guangModel.getPathNav(channel, info.title);
Promise.all(promises).then(ret => {
if (_.isEmpty(ret[1])) {
res.set('Cache-Control', 'no-cache');
}
res.render('guang/detail', Object.assign({
module: 'guang',
page: 'detail',
headerData: ret[0] && ret[0].headerData,
guang: {
pathNav: pathNav,
id: id,
header: Object.assign({}, info, ret[8] || {},
{ commentNum: _.get(ret[3], 'commentNum', 0) }),
content: ret[1],
hotTags: ret[2],
comment: ret[3],
commentInfo: req.session['comment_' + udid],
userInfo: ret[4],
brands: ret[5],
tag: info.tags,
relatedPost: (ret[9] && ret[9].length) ? ret[9] : false,
exRecos: ret[6],
ads: ret[7],
// 分享
shareImg: info.shareImg,
sharedTitle: info.title,
shareDesc: info.desc,
weixinUrl: info.weixinUrl,
commentFirstPageUrl: helpers.urlFormat('/info/index', {
id: id,
pageSize: 10
}, 'guang')
}
}, {
title: `${info.title} | YOHO!BUY有货 | 年轻人潮流购物中心,中国潮流购物风向标,官方授权正品保证`,
keywords: ['Yoho! 有货,潮流,时尚,流行,购物,B2C,正品,购物网站,网上购物,货到付款,品牌服饰,男士护肤,',
'黑框眼镜,匡威,板鞋,i.t,izzue,5cm,eastpak,vans,lylescott,g-shock,new balance,lacoste,melissa,',
'casio,卡西欧手表,舒雅,jasonwood,odm,AAAA,香港购物,日本潮流'].join(''),
description: '潮流商品搜索,上衣,衬衫,TEE,卫衣,冲锋衣,风衣,羽绒服,裤子,休闲鞋,板鞋,配饰,复古眼镜',
webNavHeader: channel
}));
}).catch(next);
});
};
/**
* 列表页(列表首页、标签列表页、作者列表页)动态数据,如:查看数,点赞数,评论数,是否点赞,是否回复
* @param req
* @param res
*/
exports.listDynamicData = (req, res) => {
let ids = req.query.ids;
let udid = ghelper.getUdid(req, res);
let other = {};
let query = req.query.query,
type = req.query.type;
if (req.user.uid) {
other.uid = req.user.uid;
}
if (query) {
other.query = query;
}
if (type) {
other.type = type;
}
guangModel.getDynamicDataByIds(ids, udid, other).then(ret => {
res.send(ret);
});
};
/**
* 详情页动态数据,如:评论数,回复数,是否点赞,是否收藏
* @param req
* @param res
*/
exports.detailDynamicData = (req, res) => {
let id = req.query.id,
uid = req.user.uid,
udid = ghelper.getUdid(req, res);
guangModel.getDynamicDataById(id, uid, udid).then((ret) => {
res.status(200).send(ret);
}).catch(() => {
res.status(400);
});
};
/**
* 添加评论
*/
exports.comment = (req, res) => {
let id = req.body.id || req.query.id;
let uid = req.user.uid;
let udid = ghelper.getUdid(req, res);
let comment = req.body.comment;
// let pageSize = req.query.pageSize || 20;
if (!uid) {
req.session['comment_' + udid] = comment;
return res.send({
code: 401
});
}
guangModel.addComment(id, uid, comment).then(() => {
if (req.session['comment_' + udid]) {
req.session['comment_' + udid] = null;
}
return guangModel.getArticleComments(id, 1, 20);
}).then(commentInfo => {
res.render('guang/detail-comment', {
comment: commentInfo,
layout: false
}, (err, content) => {
if (err) {
return res.send({
code: 401
});
}
return res.send({
code: 200,
data: {
count: commentInfo.commentNum,
content: content
}
});
});
}).catch(() => {
return res.send({
code: 400,
message: '评论失败'
});
});
};
/**
* 赞
*/
exports.praise = (req, res) => {
let id = req.body.id || req.query.id;
// let uid = req.user.uid;
let udid = ghelper.getUdid(req, res);
guangModel.praise(id, udid).then(ret => {
res.send(ret);
}).catch(() => {
res.send({
code: 400
});
});
};
/**
* 取消赞
*/
exports.cancelPraise = (req, res) =>{
let id = req.body.id || req.query.id;
let udid = ghelper.getUdid(req, res);
guangModel.cancelPraise(id, udid).then(ret => {
res.send(ret);
}).catch(() => {
res.send({
code: 400
});
});
};
/**
* 收藏
*/
exports.collect = (req, res) =>{
let id = req.body.id || req.query.id;
let uid = req.user.uid;
if (!uid) {
return res.send({
code: 401
});
}
guangModel.collect(id, uid).then(ret => {
res.send(ret);
}).catch(() => {
res.send({
code: 400,
message: '收藏失败'
});
});
};
/**
* 取消收藏
*/
exports.cancelCollect = (req, res) =>{
let id = req.body.id || req.query.id;
let uid = req.user.uid;
if (!uid) {
return res.send({
code: 401
});
}
guangModel.cancelCollect(id, uid).then(ret => {
res.send(ret);
}).catch(() => {
res.send({
code: 400,
message: '取消收藏失败'
});
});
};