Authored by 刘传洋

code

... ... @@ -10,7 +10,6 @@ const _ = require('lodash');
const guangModel = require('../models/index');
const headerModel = require('../../../doraemon/models/header');
const cookie = global.yoho.cookie;
const ghelper = require('../models/guang-helper');
/**
... ... @@ -24,8 +23,8 @@ exports.index = (req, res, next) => {
let type = req.query.type || 0;
let pathNav = guangModel.getPathNav(channel);
let uid = cookie.getUid(req);
let udid = ghelper.getUdid(req);
let uid = req.user.uid;
let udid = ghelper.getUdid(req, res);
let gender = ghelper.getGenderByCookie(req);
Object.assign(res.locals, {
... ... @@ -57,7 +56,7 @@ exports.index = (req, res, next) => {
page: page,
total: (ret[2] && ret[2].total) || 0
},
headerData: ret[6]['headerData']
headerData: ret[6].headerData
});
}).catch(next);
... ... @@ -73,8 +72,8 @@ exports.tags = (req, res, next) => {
let pageSize = req.query.pageSize || 20;
let type = req.query.type || 0;
let uid = cookie.getUid(req);
let udid = ghelper.getUdid(req);
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);
... ... @@ -106,7 +105,7 @@ exports.tags = (req, res, next) => {
page: page,
total: (ret[1] && ret[1].total) || 0
},
headerData: ret[5]['headerData']
headerData: ret[5].headerData
});
}).catch(next);
};
... ... @@ -120,8 +119,8 @@ exports.editor = (req, res, next) => {
let channel = req.yoho.channel;
let gender = ghelper.getGenderByCookie(req);
let uid = cookie.getUid(req);
let udid = ghelper.getUdid(req);
let uid = req.user.uid;
let udid = ghelper.getUdid(req, res);
let pathNav = guangModel.getPathNav(channel);
... ... @@ -141,12 +140,12 @@ exports.editor = (req, res, next) => {
headerModel.requestHeaderData(channel)
]).then(ret => {
res.render('editor', {
title: ret[0]['name'] + (res.locals.title || ''),
title: ret[0].name + (res.locals.title || ''),
guang: {
editor: {
avatar: ret[0] && ret[0]['avatar'],
name: ret[0] && ret[0]['name'],
intro: ret[0] && ret[0]['author_desc'],
avatar: ret[0] && ret[0].avatar,
name: ret[0] && ret[0].name,
intro: ret[0] && ret[0].author_desc,
info: '设计理念:时尚,线条流畅'
},
msgs: ret[1] && ret[1].msgs,
... ... @@ -162,7 +161,7 @@ exports.editor = (req, res, next) => {
},
guangList: true,
guangListPage: true,
headerData: ret[5]['headerData']
headerData: ret[5].headerData
});
}).catch(next);
};
... ... @@ -177,8 +176,8 @@ exports.detail = (req, res, next) => {
let pageSize = req.query.pageSize || 20;
let col = req.query.col || 0;
let pjax = req.query._pjax;
let uid = cookie.getUid(req);
let udid = ghelper.getUdid(req);
let uid = req.user.uid;
let udid = ghelper.getUdid(req, res);
let gender = ghelper.getGenderByCookie(req);
let channel = req.yoho.channel;
... ... @@ -187,13 +186,13 @@ exports.detail = (req, res, next) => {
}
if (pjax) {
return guangModel.getArticleComments(id, page, pageSize).then(res => {
return res.render('comment', res);
return guangModel.getArticleComments(id, page, pageSize).then(ret => {
return res.render('comment', ret);
});
}
// 登陆后自动收藏
if (col == 1 && uid > 0) {
if (col === 1 && uid > 0) {
guangModel.collectArticle(id, uid);
}
... ... @@ -233,12 +232,13 @@ exports.detail = (req, res, next) => {
res.render('detail', Object.assign({
guangDetailPage: true,
headerData: ret[0]['headerData'],
headerData: ret[0].headerData,
guang: {
pathNav: pathNav,
id: id,
header: Object.assign({}, info, info.authorId && ret[8] || {}, { commentNum: (ret[3] && ret[3].commentNum) || 0 }),
header: Object.assign({}, info, info.authorId && ret[8] || {},
{ commentNum: (ret[3] && ret[3].commentNum) || 0 }),
content: ret[1],
hotTags: ret[2],
comment: ret[3],
... ... @@ -272,13 +272,14 @@ exports.detail = (req, res, next) => {
/**
* 添加评论
*/
exports.comment = (req, res, next) => {
exports.comment = (req, res) => {
let id = req.body.id || req.query.id;
let uid = cookie.getUid(req); // test : 1000
let udid = ghelper.getUdid(req);
let uid = req.user.uid;
let udid = ghelper.getUdid(req, res);
let comment = req.body.comment;
let pageSize = req.query.pageSize || 20;
// let pageSize = req.query.pageSize || 20;
if (!uid) {
req.session['comment_' + udid] = comment;
... ... @@ -287,7 +288,7 @@ exports.comment = (req, res, next) => {
});
}
guangModel.addComment(id, uid, comment).then(ret => {
guangModel.addComment(id, uid, comment).then(() => {
if (req.session['comment_' + udid]) {
req.session['comment_' + udid] = null;
}
... ... @@ -300,6 +301,11 @@ exports.comment = (req, res, next) => {
layout: false
}, (err, content) => {
if(err) {
return res.send({
code: 401
});
}
return res.send({
code: 200,
data: {
... ... @@ -308,7 +314,7 @@ exports.comment = (req, res, next) => {
}
});
});
}).catch(err => {
}).catch(() => {
return res.send({
code: 400,
message: '评论失败'
... ... @@ -319,15 +325,16 @@ exports.comment = (req, res, next) => {
/**
* 赞
*/
exports.praise = (req, res, next) => {
exports.praise = (req, res) => {
let id = req.body.id || req.query.id;
let uid = cookie.getUid(req);
let udid = ghelper.getUdid(req);
// let uid = req.user.uid;
let udid = ghelper.getUdid(req, res);
guangModel.praise(id, udid).then(ret => {
res.send(ret);
}).catch(ret => {
}).catch(() => {
res.send({
code: 400
});
... ... @@ -337,13 +344,13 @@ exports.praise = (req, res, next) => {
/**
* 取消赞
*/
exports.cancelPraise = (req, res, next) =>{
exports.cancelPraise = (req, res) =>{
let id = req.body.id || req.query.id;
let udid = ghelper.getUdid(req);
let udid = ghelper.getUdid(req, res);
guangModel.cancelPraise(id, udid).then(ret => {
res.send(ret);
}).catch(ret => {
}).catch(() => {
res.send({
code: 400
});
... ... @@ -353,9 +360,9 @@ exports.cancelPraise = (req, res, next) =>{
/**
* 收藏
*/
exports.collect = (req, res, next) =>{
exports.collect = (req, res) =>{
let id = req.body.id || req.query.id;
let uid = cookie.getUid(req);
let uid = req.user.uid;
if (!uid) {
return res.send({
... ... @@ -365,7 +372,7 @@ exports.collect = (req, res, next) =>{
guangModel.collect(id, uid).then(ret => {
res.send(ret);
}).catch(ret => {
}).catch(() => {
res.send({
code: 400,
message: '收藏失败'
... ... @@ -376,9 +383,9 @@ exports.collect = (req, res, next) =>{
/**
* 取消收藏
*/
exports.cancelCollect = (req, res, next) =>{
exports.cancelCollect = (req, res) =>{
let id = req.body.id || req.query.id;
let uid = cookie.getUid(req);
let uid = req.user.uid;
if (!uid) {
return res.send({
... ... @@ -388,7 +395,7 @@ exports.cancelCollect = (req, res, next) =>{
guangModel.cancelCollect(id, uid).then(ret => {
res.send(ret);
}).catch(ret => {
}).catch(() => {
res.send({
code: 400,
message: '取消收藏失败'
... ...
... ... @@ -6,15 +6,14 @@
'use strict';
const moment = require('moment');
const md5 = require('md5');
const requestIp = require('request-ip');
const _ = require('lodash');
const helpers = global.yoho.helpers;
/* COOKIE标识访问的是男生频道 */
const COOKIE_NAME_BOYS = 'boys';
const COOKIE_DOMAIN = '.yohobuy.com';
// const COOKIE_NAME_BOYS = 'boys';
// const COOKIE_DOMAIN = '.yohobuy.com';
const getGenderByCookie = (req) => {
let gender = null;
... ... @@ -46,8 +45,18 @@ const getGenderByCookie = (req) => {
*
* @return string
*/
const getUdid = (req) => {
return requestIp.getClientIp(req);
const getUdid = (req, res) => {
let udid = req.cookies.udid;
if (!udid) {
udid = md5(req.ip || requestIp.getClientIp(req));
if (res && res.cookie) {
res.cookie('udid', udid);
}
}
return udid;
};
/**
... ... @@ -67,7 +76,7 @@ const getArticleUrl = (url, id) => {
}
} catch (e) {}*/
if(_.isString(url) && _.trim(url) !== '') {
if (_.isString(url) && _.trim(url) !== '') {
return url;
}
... ...