Commit 7a3eb34a authored by 陈志良's avatar 陈志良

1

parent b50a286f
...@@ -25,6 +25,20 @@ module.exports = function(app) { ...@@ -25,6 +25,20 @@ module.exports = function(app) {
//---------------------文章表单操作--------------------------------- //---------------------文章表单操作---------------------------------
var getClientIP = function(req) {
var ipAddress;
var headers = req.headers;
var forwardedIpsStr = headers['x-real-ip'] || headers['x-forwarded-for'];
if (forwardedIpsStr) {
ipAddress = forwardedIpsStr;
} else {
ipAddress = null;
}
if (!ipAddress) {
ipAddress = req.connection.remoteAddress;
}
return ipAddress;
};
//新增论坛文章 //新增论坛文章
router.post('/thread/create', function(req, res, next) { router.post('/thread/create', function(req, res, next) {
...@@ -162,7 +176,6 @@ router.post('/thread/:tid/update', function(req, res, next) { ...@@ -162,7 +176,6 @@ router.post('/thread/:tid/update', function(req, res, next) {
} }
}); });
//删除文章 //删除文章
router.post('/thread/:tid/delete', function(req, res, next) { router.post('/thread/:tid/delete', function(req, res, next) {
var tid = req.params.tid; var tid = req.params.tid;
...@@ -212,6 +225,7 @@ router.post('/thread/:tid/:fid/unTop', function(req, res, next) { ...@@ -212,6 +225,7 @@ router.post('/thread/:tid/:fid/unTop', function(req, res, next) {
res.json(returnCode.WRONG_PARAM); res.json(returnCode.WRONG_PARAM);
} }
}); });
//文章推荐 //文章推荐
router.post('/thread/:tid/:fid/recommend', function(req, res, next) { router.post('/thread/:tid/:fid/recommend', function(req, res, next) {
var tid = req.params.tid, var tid = req.params.tid,
...@@ -250,13 +264,8 @@ router.post('/thread/:tid/:fid/unRecommend', function(req, res, next) { ...@@ -250,13 +264,8 @@ router.post('/thread/:tid/:fid/unRecommend', function(req, res, next) {
res.json(returnCode.WRONG_PARAM); res.json(returnCode.WRONG_PARAM);
} }
}); });
/**
* [description] //文章列表
* @param {[type]}
* @param {[type]}
* @param {[type]}
* @return {[type]}
*/
router.get('/threads/list', function(req, res, next) { router.get('/threads/list', function(req, res, next) {
var pageNo = req.query.pageNo || 1; var pageNo = req.query.pageNo || 1;
var pageSize = req.query.pageSize || 10; var pageSize = req.query.pageSize || 10;
...@@ -324,24 +333,88 @@ router.get('/thread/:tid/comment/list', function(req, res, next) { ...@@ -324,24 +333,88 @@ router.get('/thread/:tid/comment/list', function(req, res, next) {
var tid = req.params.tid || null; var tid = req.params.tid || null;
var pageNo = req.query.pageNo || 1; var pageNo = req.query.pageNo || 1;
var pageSize = req.query.pageSize || 10; var pageSize = req.query.pageSize || 10;
var content = req.query.content || '';
var conditions = { var conditions = {
ent_code: req.session.user.ent_code, ent_code: req.session.user.ent_code,
thread: tid, thread: tid,
level: '1', // level: '1',
status: { // status: {
$ne: 2 // $ne: 2
} //过滤被删除的数据,status 为 2 表示删除 // } //过滤被删除的数据,status 为 2 表示删除
}; };
if(content){
conditions.content = { $regex:content, $options: 'i' };
}
if (tid) { if (tid) {
//获取最新5条评论 //获取最新5条评论
forumCommentService.getAllComment(conditions, pageNo, pageSize, function(err, results) { forumCommentService.getCommentList(conditions, pageNo, pageSize, function(err, results) {
if (err) { if (err) {
console.log(err); console.log(err);
res.json(returnCode.BUSY); res.json(returnCode.BUSY);
} else { } else {
res.json(_.assign(results, returnCode.SUCCESS)); var asyncTasks = [];
_.forEach(results.items,function(e){
asyncTasks.push(function(cb){
if(e.level == 1){
cb(null,e);
}else{
forumCommentService.getCommentParent(e._id,function(err,p_comment){
var comment = e.toObject();
comment.floor = p_comment.floor;
cb(null,comment);
});
}
});
});
async.parallel(asyncTasks,function(err,items){
results.items = items;
res.json(_.assign(results, returnCode.SUCCESS));
});
}
});
} else {
res.json(returnCode.WRONG_PARAM);
}
});
//评论列表(根据楼层查询)
router.get('/thread/:tid/comment/list/:floor', function(req, res, next) {
var tid = req.params.tid || null,
floor = req.params.floor || null,
content = req.query.content || '';
var conditions = {
ent_code: req.session.user.ent_code,
thread: tid,
level: '1',
floor:floor
};
if (tid && floor) {
forumCommentService.getAllComment(conditions, 1, 1, function(err, results) {
if (err) {
console.log(err);
res.json(returnCode.BUSY);
} else {
var asyncTasks = [];
if(results.items && results.items[0] && results.items[0].comments.length>0){
var items = [results.items[0]];
_.forEach(results.items[0].comments,function(e){
var subComment = e.toObject();
subComment.floor = results.items[0].floor;
if(content){
if(subComment.content.indexOf(content) != -1){
items.push(subComment);
}
}else{
items.push(subComment);
}
});
results.items = items;
results.total = items.length;
res.json(_.assign(results, returnCode.SUCCESS));
}else{
res.json(_.assign(results, returnCode.SUCCESS));
}
} }
}); });
} else { } else {
...@@ -353,82 +426,63 @@ router.get('/thread/:tid/comment/list', function(req, res, next) { ...@@ -353,82 +426,63 @@ router.get('/thread/:tid/comment/list', function(req, res, next) {
router.post('/thread/comment/update/:cid', function(req, res, next) { router.post('/thread/comment/update/:cid', function(req, res, next) {
var cid = req.params.cid || null; var cid = req.params.cid || null;
var status = req.body.status; var status = req.body.status;
var tid = req.body.tid;
var comment_count = req.body.comment_count;
var level = req.body.level;
var parent_cid = req.body.parent_cid;
if (cid) { if (cid) {
forumCommentService.updateCommentStatusById(cid, status, function(err, result) { async.waterfall([
if (err) { function(callback){
res.json(returnCode.BUSY); //获取评论实例
} else { forumCommentService.getCommentById(cid,callback);
// 如果是删除 },
if (status == 2) { function(comment,callback){
if (level == 1) { // 如果删除的是一级评论 //更新评论状态
// 获取评论的评论列表,并移除cid评论 forumCommentService.updateCommentStatusById(cid, status,function(err,update){
forumThreadService.getById(tid, function(err, result) { callback(err,comment);
if (err) { });
console.error(err); },
res.json(returnCode.BUSY); function(comment,callback){
} else { //是否删除评论
// result.comment_count -= 1; if(status == 2){
// var commentList = result.comments; //一级评论
// result.comments = forumThreadService.remove(commentList, cid); if(comment.level == 1){
var commentList = result.comments;
var commentList = result.comments; var comments = forumThreadService.remove(commentList, cid);
var comments = forumThreadService.remove(commentList, cid); forumThreadService.updateThreadById(comment.thread, {
comments:comments,
forumThreadService.updateThreadById(tid, { $inc: { comment_count: -1 }
comments:comments, }, function(err, result) {
$inc: { comment_count: -1 } callback(err);
}, function(err, result) {
if (err) {
console.error(err);
res.json(returnCode.BUSY);
} else {
res.json(returnCode.SUCCESS);
}
});
}
}); });
} else { // 删除子评论 }else{
forumThreadService.updateThreadCommentCount(comment.thread, function(err, result) {
// 更新文章评论 - 1
forumThreadService.updateThreadCommentCount(tid, function(err, result) {
if (err) { if (err) {
res.json(returnCode.BUSY); callback(err);
}else{
// 更新评论的子评论列表
forumCommentService.getCommentParent(cid,function(err,p_comment){
if(err || !p_comment){
callback(err || 'comment not exist');
}else{
var commentList = p_comment.comments;
var comments = forumCommentService.remove(commentList, cid);
forumCommentService.updateCommentById(p_comment._id, {
comments:comments
}, function(err, update) {
callback(err);
});
}
});
} }
}); });
// 更新评论的子评论列表
if (parent_cid) {
forumCommentService.getCommentById(parent_cid, function(err, result) {
if (err) {
console.error(err);
res.json(returnCode.BUSY);
} else {
var commentList = result.comments;
var comments = forumCommentService.remove(commentList, cid);
forumCommentService.updateCommentById(parent_cid, {
comments:comments
}, function(err, result) {
if (err) {
res.json(returnCode.BUSY);
} else {
res.json(returnCode.SUCCESS);
}
});
}
})
};
} }
} else { }else{
res.json(returnCode.SUCCESS); callback();
} }
}
],function(err,restult){
if(err){
console.error(err);
res.json(returnCode.BUSY);
}else{
res.json(returnCode.SUCCESS);
} }
}); });
} else { } else {
...@@ -436,7 +490,6 @@ router.post('/thread/comment/update/:cid', function(req, res, next) { ...@@ -436,7 +490,6 @@ router.post('/thread/comment/update/:cid', function(req, res, next) {
} }
}); });
// 更新回复评论 // 更新回复评论
router.post('/thread/comment/update/:cid/comments', function(req, res, next) { router.post('/thread/comment/update/:cid/comments', function(req, res, next) {
var tid = req.body.tid || null; var tid = req.body.tid || null;
...@@ -514,103 +567,94 @@ router.post('/thread/comment/update/:cid/comments', function(req, res, next) { ...@@ -514,103 +567,94 @@ router.post('/thread/comment/update/:cid/comments', function(req, res, next) {
// } // }
// }); // });
}); });
//添加评论 //添加评论
router.post('/thread/comment/add', function(req, res, next) { router.post('/thread/comment/add', function(req, res, next) {
var tid = req.body.tid || null; var tid = req.body.tid || null;
var entity = req.body; var entity = req.body;
var ip = getClientIP(req);
entity.created = new Date(); entity.created = new Date();
entity.ent_code = req.session.user.ent_code; entity.ent_code = req.session.user.ent_code;
entity.ip = ip; entity.ip = getClientIP(req);
var uid = req.session.user.id; var uid = req.session.user.id;
// 正式环境
if (tid) { if (tid) {
async.waterfall([ async.waterfall([
function(callback) { function(callback) {
forumUserService.getUserByUid(uid, callback); forumUserService.getUserByUid(uid, callback);
} },
], function(err, user) { function(user,callback){
if (err) { //获取发表人
res.json(returnCode.BUSY); if(!user){
} else { var userentity = {
if (user) {
entity.from = user._id;
// 添加评论
forumCommentService.createComment(entity, function(err, result) {
if (err) {
res.json(returnCode.BUSY);
} else {
var returnData = {
comment:  result,
errorcode: 0,
errormsg: '请求成功'
}
res.json(returnData);
}
});
} else {
var userentity = {
uid: uid, uid: uid,
nickName: req.session.user.name, nickName: req.session.user.name,
icon: req.session.user.headPic icon: req.session.user.headPic
}; };
forumUserService.createUser(userentity, function(err, doc) { forumUserService.createUser(userentity, function(err, doc) {
if (err) { if (err) {
console.log(err); callback(err);
res.json(returnCode.BUSY);
} else { } else {
entity.from = doc._id; entity.from = doc._id;
// 添加评论 callback();
forumCommentService.createComment(entity, function(err, result) {
if (err) {
res.json(returnCode.BUSY);
} else {
var returnData = {
comment:  result,
errorcode: 0,
errormsg: '请求成功'
}
res.json(returnData);
}
});
} }
}); });
}else{
entity.from = user;
callback();
} }
},
function(callback){
//获取楼层
if(entity.level == 1){
var conditions = {
thread:entity.thread,
level:'1'
}
forumCommentService.count(conditions, function(err, count) {
if (err) {
callback(err);
} else {
if(count){
entity.floor = count + 1;
}else{
entity.floor = 1;
}
callback();
}
});
}else{
callback();
}
},
function(callback){
// 添加评论
forumCommentService.createComment(entity, function(err, result) {
if (err) {
callback(err);
} else {
var returnData = {
comment:  result,
errorcode: 0,
errormsg: '请求成功'
}
callback(null,returnData);
}
});
}
], function(err, returnData) {
if (err) {
console.error(err);
res.json(returnCode.BUSY);
} else {
res.json(returnData);
} }
}); });
} else { } else {
res.json(returnCode.WRONG_PARAM); res.json(returnCode.WRONG_PARAM);
} }
// 测试环境
// entity.from = '55015675868b65a028187c49';
// if (tid) {
// // 添加评论
// forumCommentService.createComment(entity, function(err, result) {
// if (err) {
// res.json(returnCode.BUSY);
// } else {
// var returnData = {
// comment:  result,
// errorcode: 0,
// errormsg: '请求成功'
// }
// res.json(returnData);
// }
// });
// }
}); });
// 更新文章评论 // 更新文章评论
router.post('/thread/:tid/updateComments', function(req, res, next) { router.post('/thread/:tid/updateComments', function(req, res, next) {
var tid = req.params.tid || null; var tid = req.params.tid || null;
...@@ -653,26 +697,10 @@ router.post('/thread/:tid/updateComments', function(req, res, next) { ...@@ -653,26 +697,10 @@ router.post('/thread/:tid/updateComments', function(req, res, next) {
// }); // });
// } // }
// }); // });
}); });
var getClientIP = function(req) {
var ipAddress;
var headers = req.headers;
var forwardedIpsStr = headers['x-real-ip'] || headers['x-forwarded-for'];
if (forwardedIpsStr) {
ipAddress = forwardedIpsStr;
} else {
ipAddress = null;
}
if (!ipAddress) {
ipAddress = req.connection.remoteAddress;
}
return ipAddress;
};
//给以前的已一级评论添加楼层 //给以前的已一级评论添加楼层
// router.get('/thread/updateConmentFloor', function(req, res, next) { // router.get('/thread/updateConmentFloor', function(req, res, next) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment