Commit c917666e authored by 陈家荣's avatar 陈家荣

Merge branch 'development' of git.wxpai.cn:scrmgroup/pisns-forum-api into development

Conflicts:
	app/controllers/mobile/forumThread.js
parents b1b7e6ad 21a75806
'use strict'; 'use strict';
var express = require('express'), var express = require('express'),
router = express.Router(), router = express.Router(),
returnCode = require('../../utils/returnCode'), returnCode = require('../../utils/returnCode'),
_ = require('lodash'); _ = require('lodash');
var mongoose = require('mongoose'); var mongoose = require('mongoose');
var ForumThread = mongoose.model('ForumThread'); var ForumThread = mongoose.model('ForumThread');
var forumThreadService=require('../../service/forumThreadService'); var forumThreadService = require('../../service/forumThreadService');
var forumRolePermissionService=require('../../service/forumRolePermissionService'); var forumRolePermissionService = require('../../service/forumRolePermissionService');
var forumPraiseLogService=require('../../service/forumPraiseLogService'); var forumPraiseLogService = require('../../service/forumPraiseLogService');
var forumShareLogService=require('../../service/forumShareLogService'); var forumShareLogService = require('../../service/forumShareLogService');
var forumCommentService=require('../../service/forumCommentService'); var forumCommentService = require('../../service/forumCommentService');
var forumTagService=require('../../service/forumTagService'); var forumTagService = require('../../service/forumTagService');
var forumUserService=require('../../service/forumUserService'); var forumUserService = require('../../service/forumUserService');
var userUtil=require('../../utils/user'); var httpService = require('../../service/httpService');
var async=require('async');
var userUtil = require('../../utils/user');
var async = require('async');
module.exports = function(app) { module.exports = function(app) {
app.use('/admin/forum', router); app.use('/admin/forum', router);
}; };
...@@ -25,169 +27,176 @@ module.exports = function(app) { ...@@ -25,169 +27,176 @@ module.exports = function(app) {
//新增论坛文章 //新增论坛文章
router.post('/thread/create', function(req, res, next) { router.post('/thread/create', function(req, res, next) {
var rs = {};
var rs = {}; req.body.ent_code = req.session.user.ent_code;
req.body.ent_code=req.session.user.ent_code;
var uid = req.session.user.id;
var uid=req.session.user.id; // var uid='12345';
// var uid='12345'; if (req.body.pid) {
req.body.level = 2;
async.waterfall([ }
function(callback){ async.waterfall([
forumUserService.getUserByUid(uid,callback); function(callback) {
} forumUserService.getUserByUid(uid, callback);
],function(err,user){ }
if(err){ ], function(err, user) {
res.json(returnCode.BUSY); if (err) {
}else{ res.json(returnCode.BUSY);
if(user){ } else {
req.body.from=user._id; if (user) {
forumThreadService.createThread(req.body,function(err,entity){ req.body.from = user._id;
if (err) { forumThreadService.createThread(req.body, function(err, entity) {
console.error(err); if (err) {
res.json(returnCode.BUSY); console.error(err);
} else { res.json(returnCode.BUSY);
rs.data = {'id':entity._id}; } else {
res.json(_.assign(rs, returnCode.SUCCESS)); rs.data = {
} 'id': entity._id
}); };
}else{ res.json(_.assign(rs, returnCode.SUCCESS));
var entity={ }
uid:uid, });
nickName:req.session.user.name, } else {
icon:req.session.user.headPic var entity = {
}; uid: uid,
forumUserService.createUser(entity,function(err,doc){ nickName: req.session.user.name,
if(err){ icon: req.session.user.headPic
res.json(returnCode.BUSY); };
}else{ forumUserService.createUser(entity, function(err, doc) {
if (err) {
req.body.from=doc._id; res.json(returnCode.BUSY);
forumThreadService.createThread(req.body,function(err,entity){ } else {
if (err) {
console.error(err); req.body.from = doc._id;
res.json(returnCode.BUSY); forumThreadService.createThread(req.body, function(err, entity) {
} else { if (err) {
rs.data = {'id':entity._id}; console.error(err);
res.json(_.assign(rs, returnCode.SUCCESS)); res.json(returnCode.BUSY);
} } else {
}); rs.data = {
} 'id': entity._id
}); };
} res.json(_.assign(rs, returnCode.SUCCESS));
} }
}); });
}
});
}
}
});
}); });
//获取目标论坛文章 //获取目标论坛文章
router.get('/thread/:tid/get', function(req, res, next) { router.get('/thread/:tid/get', function(req, res, next) {
var tid = req.params.tid || null; var tid = req.params.tid || null;
var rs = {}; var rs = {};
if (tid) { if (tid) {
async.parallel([ async.parallel([
function(callback){ function(callback) {
forumThreadService.getThreadById(tid,function(err,thread){ forumThreadService.getThreadById(tid, function(err, thread) {
if(err){ if (err) {
callback(err,null); callback(err, null);
}else{ } else {
callback(null,thread); callback(null, thread);
} }
}); });
}, },
function(callback){ function(callback) {
forumTagService.getAllTag(req.session.user.ent_code,1,100,function(err,results){ forumTagService.getAllTag(req.session.user.ent_code, 1, 100, function(err, results) {
if(err){ if (err) {
callback(err,null); callback(err, null);
}else{ } else {
callback(null,results); callback(null, results);
} }
}); });
} }
],function(err,results){ ], function(err, results) {
if(err){ if (err) {
res.json(returnCode.BUSY); res.json(returnCode.BUSY);
}else{ } else {
rs.data=results[0]; rs.data = results[0];
rs.tagList=results[1]; rs.tagList = results[1];
res.json(_.assign(rs, returnCode.SUCCESS)); res.json(_.assign(rs, returnCode.SUCCESS));
} }
}); });
} else { } else {
res.json(returnCode.WRONG_PARAM); res.json(returnCode.WRONG_PARAM);
} }
}); });
//更新文章状态、如:屏蔽 //更新文章状态、如:屏蔽
router.post('/thread/:tid/update', function(req, res, next) { router.post('/thread/:tid/update', function(req, res, next) {
var tid=req.params.tid; var tid = req.params.tid;
var fatherTitle = req.body.father; var fatherTitle = req.body.father;
delete req.body.father; delete req.body.father;
req.body.ent_code=req.session.user.ent_code; req.body.ent_code = req.session.user.ent_code;
if(tid){ if (tid) {
forumThreadService.updateThreadById(tid,req.body,function(err,thread){ forumThreadService.updateThreadById(tid, req.body, function(err, thread) {
if(err){ if (err) {
res.json(returnCode.BUSY); res.json(returnCode.BUSY);
}else{ } else {
console.log(thread); res.json({
console.log(fatherTitle); message: "success",
res.json({message:"success",data:thread,title:fatherTitle}); data: thread,
} title: fatherTitle
}); });
}else{ }
res.json(returnCode.WRONG_PARAM); });
} } else {
res.json(returnCode.WRONG_PARAM);
}
}); });
//删除文章 //删除文章
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;
if(tid){ if (tid) {
forumThreadService.deleteThreadById(tid,function(err,thread){ forumThreadService.deleteThreadById(tid, function(err, thread) {
if(err){ if (err) {
res.json(returnCode.BUSY); res.json(returnCode.BUSY);
}else{ } else {
res.json(returnCode.SUCCESS); res.json(returnCode.SUCCESS);
} }
}); });
}else{ } else {
res.json(returnCode.WRONG_PARAM); res.json(returnCode.WRONG_PARAM);
} }
}); });
//文章置顶 //文章置顶
router.post('/thread/:tid/:fid/top', function(req, res, next) { router.post('/thread/:tid/:fid/top', function(req, res, next) {
var tid=req.params.tid;//文章ID var tid = req.params.tid; //文章ID
var fid=req.params.fid;//板块ID var fid = req.params.fid; //板块ID
if(tid && fid){ if (tid && fid) {
forumThreadService.updateTopByThreadId(fid,tid,function(err,thread){ forumThreadService.updateTopByThreadId(fid, tid, function(err, thread) {
if(err){ if (err) {
res.json(returnCode.BUSY); res.json(returnCode.BUSY);
}else{ } else {
res.json(returnCode.SUCCESS); res.json(returnCode.SUCCESS);
} }
}); });
}else{ } else {
res.json(returnCode.WRONG_PARAM); res.json(returnCode.WRONG_PARAM);
} }
}); });
//文章取消置顶 //文章取消置顶
router.post('/thread/:tid/:fid/unTop', function(req, res, next) { router.post('/thread/:tid/:fid/unTop', function(req, res, next) {
var tid=req.params.tid;//文章ID var tid = req.params.tid; //文章ID
var fid=req.params.fid;//板块ID var fid = req.params.fid; //板块ID
if(tid && fid){ if (tid && fid) {
forumThreadService.updateUnTopByThreadId(fid,tid,function(err,thread){ forumThreadService.updateUnTopByThreadId(fid, tid, function(err, thread) {
if(err){ if (err) {
res.json(returnCode.BUSY); res.json(returnCode.BUSY);
}else{ } else {
res.json(returnCode.SUCCESS); res.json(returnCode.SUCCESS);
} }
}); });
}else{ } else {
res.json(returnCode.WRONG_PARAM); res.json(returnCode.WRONG_PARAM);
} }
}); });
/** /**
...@@ -198,145 +207,248 @@ router.post('/thread/:tid/:fid/unTop', function(req, res, next) { ...@@ -198,145 +207,248 @@ router.post('/thread/:tid/:fid/unTop', function(req, res, next) {
* @return {[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;
var infoId=req.query.infoId; var infoId = req.query.infoId;
var tagId=req.query.tagId; var tagId = req.query.tagId;
var pid=req.query.pid; var pid = req.query.pid;
var conditions={ var conditions = {
ent_code:req.session.user.ent_code, ent_code: req.session.user.ent_code,
level:1 level: 1
}; };
if(infoId){ if (infoId) {
conditions.info=infoId; conditions.info = infoId;
} }
if(tagId){ if (tagId) {
conditions.tag={$in:[tagId]}; conditions.tag = {
$in: [tagId]
};
} }
if(pid){ if (pid) {
conditions.pid = pid; conditions.pid = pid;
conditions.level = 2; conditions.level = 2;
} }
console.log(conditions); forumThreadService.getAllThreadByFid(conditions, pageNo, pageSize, null, function(err, results) {
forumThreadService.getAllThreadByFid(conditions,pageNo,pageSize,null,function(err,results){ if (err) {
if(err){ console.error(err);
console.error(err); res.json(returnCode.BUSY);
res.json(returnCode.BUSY); } else {
}else{ res.json(_.assign(results, returnCode.SUCCESS));
res.json(_.assign(results, returnCode.SUCCESS)); }
} });
});
}); });
//评论列表 //评论列表
router.get('/thread/:tid/comment/list', function(req, res, next) { 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 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:{$ne : 2} //过滤被删除的数据,status 为 2 表示删除 status: {
}; $ne: 2
} //过滤被删除的数据,status 为 2 表示删除
if(tid){ };
//获取最新5条评论
forumCommentService.getAllComment(conditions,pageNo,pageSize,function(err,results){ if (tid) {
if(err){ //获取最新5条评论
console.log(err); forumCommentService.getAllComment(conditions, pageNo, pageSize, function(err, results) {
res.json(returnCode.BUSY); if (err) {
}else{ console.log(err);
res.json(_.assign(results, returnCode.SUCCESS)); res.json(returnCode.BUSY);
} } else {
}); res.json(_.assign(results, returnCode.SUCCESS));
}else{ }
res.json(returnCode.WRONG_PARAM); });
} } else {
res.json(returnCode.WRONG_PARAM);
}
}); });
// 更新评论状态 // 更新评论状态
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;
if(cid){ var comment_count = req.body.comment_count;
forumCommentService.updateCommentStatusById(cid, status, function(err,result){ var level = req.body.level;
if(err){ var parent_cid = req.body.parent_cid;
res.json(returnCode.BUSY); if (cid) {
}else{ forumCommentService.updateCommentStatusById(cid, status, function(err, result) {
res.json(returnCode.SUCCESS); if (err) {
} res.json(returnCode.BUSY);
}); } else {
}else{ // 如果是删除
res.json(returnCode.WRONG_PARAM); if (status == 2) {
} if (level == 1) { // 如果删除的是一级评论
// 获取评论的评论列表,并移除cid评论
forumThreadService.getById(tid, function(err, result) {
if (err) {
console.error(err);
callback(err, null);
} else {
result.comment_count -= 1;
var commentList = result.comments;
result.comments = forumThreadService.remove(commentList, cid);
forumThreadService.updateThreadById(tid, result, function(err, result) {
if (err) {
console.error(err);
callback(err, null);
} else {
res.json(returnCode.SUCCESS);
}
});
}
});
} else { // 删除子评论
// 更新文章评论 - 1
forumThreadService.updateThreadCommentCount(tid, comment_count - 1, function(err, result) {
if (err) {
callback(err, null);
}
});
// 更新评论的子评论列表
if (parent_cid) {
forumCommentService.getCommentById(parent_cid, function(err, result) {
if (err) {
console.error(err);
callback(err, null);
} else {
var commentList = result.comments;
result.comments = forumCommentService.remove(commentList, cid);
forumCommentService.updateCommentById(parent_cid, result, function(err, result) {
if (err) {
res.json(returnCode.BUSY);
} else {
res.json(returnCode.SUCCESS);
}
});
}
})
};
}
}
}
});
} else {
res.json(returnCode.WRONG_PARAM);
}
}); });
// 更新回复评论 // 更新回复评论
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.params.tid || null; var tid = req.body.tid || null;
var cid = req.params.cid || null; var cid = req.params.cid || null;
var replayComment_id = req.body.replayComment_id; var replayComment_id = req.body.replayComment_id;
// 获取评论的子评论列表 // 获取评论的子评论列表
forumCommentService.getCommentById(cid,function(err, result){ forumCommentService.getCommentById(cid, function(err, result) {
if (err) { if (err) {
console.error(err); console.error(err);
callback(err,null); callback(err, null);
} else { } else {
// 更新文章对象 // 更新文章评论 + 1
forumThreadService.getThreadById(tid,function(err,thread){ forumThreadService.updateThreadCommentCountInc(tid, function(err, thread) {
if(err){ if (err) {
callback(err,null); callback(err, null);
}else{ }
thread.comment_count+=1; });
} // 更新评论对象
}); result.comments.push(replayComment_id); // 添加子评论ID
// result.comment_count+=1; // 回复评论+1
// 更新评论对象 forumCommentService.updateCommentById(cid, result, function(err, result) {
result.comments.push(replayComment_id); // 添加子评论ID if (err) {
result.comment_count+=1; // 回复评论+1 res.json(returnCode.BUSY);
} else {
forumCommentService.updateCommentById(cid, result, function(err,result){ res.json(returnCode.SUCCESS);
if(err){ }
res.json(returnCode.BUSY); });
}else{ }
res.json(returnCode.SUCCESS); });
}
});
}
});
}); });
//添加评论 //添加评论
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;
entity.created = new Date(); var ip = getClientIP(req);
entity.ent_code = req.session.user.ent_code; entity.created = new Date();
// entity.from = req.session.mobileForumUser.userId; // 正式环境 entity.ent_code = req.session.user.ent_code;
entity.from = '55015675868b65a028187c49'; // 测试环境 entity.ip = ip;
if(tid){ // entity.from = req.session.mobileForumUser.userId; // 正式环境
// 添加评论 entity.from = '55015675868b65a028187c49'; // 测试环境
forumCommentService.createComment(entity,function(err,result){ if (tid) {
if(err){ // 添加评论
res.json(returnCode.BUSY); forumCommentService.createComment(entity, function(err, result) {
}else{ if (err) {
var returnData = { res.json(returnCode.BUSY);
// returnCode.SUCCESS, } else {
comment : result var returnData = {
} comment:  result,
res.json(returnData); errorcode: 0,
} errormsg: '请求成功'
}); }
} res.json(returnData);
}); }
\ No newline at end of file });
}
});
// 更新文章评论
router.post('/thread/:tid/updateComments', function(req, res, next) {
var tid = req.params.tid || null;
var cid = req.body.cid || null;
// 获取评论的子评论列表
forumThreadService.getById(tid, function(err, result) {
if (err) {
console.error(err);
callback(err, null);
} else {
result.comments.push(cid);
result.comment_count += 1;
forumThreadService.updateThreadById(tid, result, function(err, result) {
if (err) {
console.error(err);
callback(err, null);
} else {
res.json(returnCode.SUCCESS);
}
});
}
});
});
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;
};
...@@ -253,10 +253,10 @@ exports.getThreadById = function(tid, callback) { ...@@ -253,10 +253,10 @@ exports.getThreadById = function(tid, callback) {
//根据ID更新文章 //根据ID更新文章
exports.updateThreadById = function(tid, entity, callback) { exports.updateThreadById = function(tid, entity, callback) {
var shareEntity = entity.share || {}; var shareEntity = entity.share;
if (entity.share && entity.share._id) { if (entity.share && entity.share._id) {
entity.share = entity.share._id; entity.share = entity.share._id;
}else{ } else {
entity.share = ''; entity.share = '';
} }
if (entity.share) { if (entity.share) {
...@@ -283,7 +283,7 @@ exports.updateThreadById = function(tid, entity, callback) { ...@@ -283,7 +283,7 @@ exports.updateThreadById = function(tid, entity, callback) {
}); });
} else { } else {
if (shareEntity.title || shareEntity.description || shareEntity.icon) { if (shareEntity && (shareEntity.title || shareEntity.description || shareEntity.icon)) {
shareEntity.ent_code = entity.ent_code; shareEntity.ent_code = entity.ent_code;
var forumShare = new ForumShare(shareEntity); var forumShare = new ForumShare(shareEntity);
...@@ -630,7 +630,7 @@ exports.updateThreadShareCount = function(threadId, callback) { ...@@ -630,7 +630,7 @@ exports.updateThreadShareCount = function(threadId, callback) {
}; };
//更新文章评论数 //更新文章评论数
exports.updateThreadCommentCount = function(threadId, callback) { exports.updateThreadCommentCountInc = function(threadId, callback) {
ForumThread.update({ ForumThread.update({
_id: threadId _id: threadId
}, { }, {
...@@ -651,6 +651,23 @@ exports.updateThreadCommentCount = function(threadId, callback) { ...@@ -651,6 +651,23 @@ exports.updateThreadCommentCount = function(threadId, callback) {
}); });
}; };
//更新文章评论数
exports.updateThreadCommentCount = function(threadId, comment_count, callback) {
ForumThread.update({
_id: threadId
}, {
'comment_count': comment_count
}, null,
function(err, result) {
if (err) {
console.error(err);
callback(err, null);
} else {
callback(null, null);
}
});
};
//更新文章浏览数 //更新文章浏览数
exports.updateThreadPvCount = function(threadId, callback) { exports.updateThreadPvCount = function(threadId, callback) {
ForumThread.update({ ForumThread.update({
...@@ -672,3 +689,20 @@ exports.updateThreadPvCount = function(threadId, callback) { ...@@ -672,3 +689,20 @@ exports.updateThreadPvCount = function(threadId, callback) {
} }
}); });
}; };
// 查找数组元素下标
function indexOf(array,val) {
for (var i = 0; i < array.length; i++) {
if (array[i] == val) return i;
}
return -1;
};
// 删除数组指定值
exports.remove = function(array,val) {
var index = indexOf(array,val);
if (index > -1) {
array.splice(index, 1);
}
return array;
};
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