Commit 78d84bb0 authored by 陈家荣's avatar 陈家荣

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

Conflicts:
	app/controllers/admin/forumThread.js
parents 2f39ffa0 16beef1e
...@@ -14,45 +14,72 @@ var ForumPvLog = mongoose.model('ForumPVLog'), ...@@ -14,45 +14,72 @@ var ForumPvLog = mongoose.model('ForumPVLog'),
module.exports = function(app) { module.exports = function(app) {
app.use('/admin/forum', router); app.use('/admin/forum', router);
}; };
var aggregate= function(model,match,group_id,sort,callback){
model.aggregate(
{
$match: match
},
{
$group : {
_id : group_id,
count : { $sum : 1 }
}
},
{
$sort:sort
},
function (err, docs){
callback(err,docs);
});
}
//社区情况(首页) //社区情况(首页)
router.get('/statistic',function(req, res, next) { router.get('/statistic',function(req, res, next) {
var yesterday = moment(new Date().getTime() - 24*3600*1000).format('YYYY-MM-DD'), var yesterday = moment(new Date().getTime() - 24*3600*1000).format('YYYY-MM-DD'),
before_yesterday = moment(new Date().getTime() - 2*24*3600*1000).format('YYYY-MM-DD'); before_yesterday = moment(new Date().getTime() - 2*24*3600*1000).format('YYYY-MM-DD');
var y_begin = yesterday, var ent_code = req.session.user.ent_code,
y_end = yesterday+' 23:59:59', y_begin = new Date(yesterday),//昨天开始时间
by_begin = before_yesterday, y_end = new Date(yesterday+' 23:59:59'),//昨天结束时间
by_end = before_yesterday+' 23:59:59'; by_begin = new Date(before_yesterday),//前天开始时间
var q = {ent_code:req.session.user.ent_code}; by_end = new Date(before_yesterday+' 23:59:59');//前天结束时间
then.parallel([ then.parallel([
function(cont){ function(cont){
//昨天访问用户 //昨天访问用户
q.created = {$gte:y_begin,$lte:y_end}; var match = {ent_code:ent_code,created:{$gte:y_begin,$lte:y_end}};
ForumUvLog.count(q,cont); aggregate(ForumUvLog,match,{mid:'$mid'},{'count':-1},function(err,docs){
cont(err,docs.length);
});
},function(cont){ },function(cont){
//前天访问用户 //前天访问用户
q.created = {$gte:by_begin,$lte:by_end}; var match = {ent_code:ent_code,created:{$gte:by_begin,$lte:by_end}};
ForumUvLog.count(q,cont); aggregate(ForumUvLog,match,{mid:'$mid'},{'count':-1},function(err,docs){
cont(err,docs.length);
});
}, },
function(cont){ function(cont){
//昨天发帖数 //昨天发帖数
q.created = {$gte:y_begin,$lte:y_end}; var match = {ent_code:ent_code,created:{$gte:y_begin,$lte:y_end}};
ForumThread.count(q,cont); aggregate(ForumThread,match,{mid:'$mid'},{'count':-1},function(err,docs){
cont(err,docs.length);
});
}, },
function(cont){ function(cont){
//前天发帖数 //前天发帖数
q.created = {$gte:by_begin,$lte:by_end}; var match = {ent_code:ent_code,created:{$gte:by_begin,$lte:by_end}};
ForumThread.count(q,cont); aggregate(ForumThread,match,{mid:'$mid'},{'count':-1},function(err,docs){
cont(err,docs.length);
});
} }
]).then(function(cont,datas){ ]).then(function(cont,datas){
var y_user = datas[0], var y_user = datas[0],
by_user = datas[1], by_user = datas[1],
y_thread = datas[2], y_thread = datas[2],
by_thread = datas[3]; by_thread = datas[3];
//计算昨天比前天多 多少 的百分比
var user_rate = (by_user === 0)?y_user:(y_user/by_user).toFixed(4)-1, var user_rate = (by_user === 0)?y_user:(y_user/by_user).toFixed(4)-1,
thread_rate = (by_thread === 0)?y_thread:(y_thread/by_thread).toFixed(4)-1; thread_rate = (by_thread === 0)?y_thread:(y_thread/by_thread).toFixed(4)-1;
cont(null,y_user,user_rate,y_thread,thread_rate); cont(null,y_user,user_rate,y_thread,thread_rate);
}).then(function(cont,y_user,user_rate,y_thread,thread_rate){ }).then(function(cont,y_user,user_rate,y_thread,thread_rate){
//返回数据
res.json({result:true,data:{ res.json({result:true,data:{
user:y_user, user:y_user,
user_rate:Math.round(user_rate>1?100:user_rate<0?0:user_rate*100), user_rate:Math.round(user_rate>1?100:user_rate<0?0:user_rate*100),
......
...@@ -226,6 +226,8 @@ router.get('/threads/list', function(req, res, next) { ...@@ -226,6 +226,8 @@ router.get('/threads/list', function(req, res, next) {
var tagId = req.query.tagId; var tagId = req.query.tagId;
var pid = req.query.pid; var pid = req.query.pid;
var content = req.query.content; var content = req.query.content;
var type = req.query.type;
var status = req.query.status;
var conditions = { var conditions = {
ent_code: req.session.user.ent_code, ent_code: req.session.user.ent_code,
level: 1 level: 1
...@@ -250,15 +252,21 @@ router.get('/threads/list', function(req, res, next) { ...@@ -250,15 +252,21 @@ router.get('/threads/list', function(req, res, next) {
$options: 'i' $options: 'i'
} }
} }
if(type){
conditions.type = type;
}
if(status){
conditions.status = status;
}
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 {
if (results.items.length > 0 && results.items[0].pid) { if (pid) {
forumThreadService.getById(results.items[0].pid, function(err, parentThread) { forumThreadService.getById(pid, function(err, parentThread) {
res.json(_.assign({ res.json(_.assign({
parentThread: parentThread parentThread: parentThread
}, results, returnCode.SUCCESS)); }, results, returnCode.SUCCESS));
...@@ -332,7 +340,7 @@ router.post('/thread/comment/update/:cid', function(req, res, next) { ...@@ -332,7 +340,7 @@ router.post('/thread/comment/update/:cid', function(req, res, next) {
var comments = forumThreadService.remove(commentList, cid); var comments = forumThreadService.remove(commentList, cid);
forumThreadService.updateThreadById(tid, { forumThreadService.updateThreadById(tid, {
comments : comments, comments:comments,
$inc: { comment_count: -1 } $inc: { comment_count: -1 }
}, function(err, result) { }, function(err, result) {
if (err) { if (err) {
...@@ -363,7 +371,7 @@ router.post('/thread/comment/update/:cid', function(req, res, next) { ...@@ -363,7 +371,7 @@ router.post('/thread/comment/update/:cid', function(req, res, next) {
var commentList = result.comments; var commentList = result.comments;
var comments = forumCommentService.remove(commentList, cid); var comments = forumCommentService.remove(commentList, cid);
forumCommentService.updateCommentById(parent_cid, { forumCommentService.updateCommentById(parent_cid, {
comments : comments, comments:comments
}, function(err, result) { }, function(err, result) {
if (err) { if (err) {
res.json(returnCode.BUSY); res.json(returnCode.BUSY);
......
...@@ -137,7 +137,8 @@ router.get('/info/:fid/threads', function(req, res, next) { ...@@ -137,7 +137,8 @@ router.get('/info/:fid/threads', function(req, res, next) {
var sort = '-top -topTime -created' var sort = '-top -topTime -created'
var conditions = { var conditions = {
ent_code: req.session.user.ent_code, ent_code: req.session.user.ent_code,
level: 1 level: 1,
status:1
}; };
if (fid) { if (fid) {
conditions.info = fid; conditions.info = fid;
......
...@@ -77,7 +77,7 @@ router.get('/:ent_code/share.js', function(req, res, next) { ...@@ -77,7 +77,7 @@ router.get('/:ent_code/share.js', function(req, res, next) {
type = req.query.type, type = req.query.type,
id = req.query.id, id = req.query.id,
uId= req.session.mobileForumUser.userId, uId= req.session.mobileForumUser.userId,
ent_code = req.session.user.ent_code, ent_code = req.params.ent_code,
mid = 0, mid = 0,
link = '/app/forum/' + ent_code + '/index?pageUrl=', link = '/app/forum/' + ent_code + '/index?pageUrl=',
share_log = {}; share_log = {};
......
...@@ -60,7 +60,9 @@ router.get('/tag/:tid/threads', function(req, res, next) { ...@@ -60,7 +60,9 @@ router.get('/tag/:tid/threads', function(req, res, next) {
var tid=req.params.tid; var tid=req.params.tid;
var sort = '-tag_top -tag_topTime -created'; var sort = '-tag_top -tag_topTime -created';
var conditions={ var conditions={
ent_code:req.session.user.ent_code ent_code:req.session.user.ent_code,
status:1,
level:1
}; };
if(tid){ if(tid){
conditions.tag={$in:[tid]}; conditions.tag={$in:[tid]};
......
...@@ -698,7 +698,8 @@ router.get('/thread/:tid/comment/list', function(req, res, next) { ...@@ -698,7 +698,8 @@ router.get('/thread/:tid/comment/list', function(req, res, next) {
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:1
}; };
if (tid) { if (tid) {
......
...@@ -142,12 +142,16 @@ function populateComment(doc, callback){ ...@@ -142,12 +142,16 @@ function populateComment(doc, callback){
var asyncTasks = []; var asyncTasks = [];
doc.comments.forEach(function(comment){ doc.comments.forEach(function(comment){
asyncTasks.push(function(callback) { asyncTasks.push(function(callback) {
ForumComment.findOne({_id: comment}).populate({path:'from to', select:'uid nickName icon'}).exec(function(err,c){ ForumComment.findOne({_id: comment,status:1}).populate({path:'from to', select:'uid nickName icon'}).exec(function(err,c){
if (err) { if (err) {
console.error(err); console.error(err);
callback(null, null); callback(null, null);
} else{ } else{
if(c){
callback(null ,c); callback(null ,c);
}else{
callback(null);
}
} }
}); });
}); });
...@@ -157,8 +161,13 @@ function populateComment(doc, callback){ ...@@ -157,8 +161,13 @@ function populateComment(doc, callback){
console.log(err); console.log(err);
callback(null, null); callback(null, null);
} else { } else {
// console.log(results); var comments = [];
callback(null, results); for(var i=0;i<results.length;i+=1){
if(results[i]){
comments.push(results[i]);
}
}
callback(null,comments);
} }
}); });
......
...@@ -129,25 +129,20 @@ exports.getThreadById = function(tid, callback) { ...@@ -129,25 +129,20 @@ exports.getThreadById = function(tid, callback) {
var conditions = { var conditions = {
_id: tid _id: tid
}; };
ForumThread.find(conditions).populate('from').populate('info').populate('share').exec(function(err, docs) { ForumThread.findOne(conditions).populate('from').populate('info').populate('share').exec(function(err, doc) {
if (err) { if (err) {
console.error(err); console.error(err);
callback(err, null); callback(err, null);
} else { } else {
var thread = {}; cb(null, doc);
if (docs && docs.length > 0) {
thread = docs[0];
cb(null, thread);
} else {
cb(null, thread);
}
} }
}); });
}, },
function(cb) { function(cb) {
var conditions = { var conditions = {
thread: tid, thread: tid,
level: '1' level: '1',
status:1
}; };
//获取最新10条评论 //获取最新10条评论
forumCommentService.getAllComment(conditions, 1, 10, function(err, results) { forumCommentService.getAllComment(conditions, 1, 10, function(err, results) {
...@@ -163,7 +158,8 @@ exports.getThreadById = function(tid, callback) { ...@@ -163,7 +158,8 @@ exports.getThreadById = function(tid, callback) {
//获取话题子文章列表 //获取话题子文章列表
var conditions = { var conditions = {
pid: tid, pid: tid,
type: 2 type: 2,
status:1
}; };
getAllThreadByFidHelp(conditions, 1, 10, null, function(err, threads) { getAllThreadByFidHelp(conditions, 1, 10, null, function(err, threads) {
if (err) { if (err) {
...@@ -180,6 +176,7 @@ exports.getThreadById = function(tid, callback) { ...@@ -180,6 +176,7 @@ exports.getThreadById = function(tid, callback) {
var conditions = { var conditions = {
pid: tid, pid: tid,
type: 3, type: 3,
status:1,
images: {$exists: true, $not: {$size: 0}} images: {$exists: true, $not: {$size: 0}}
}; };
getAllThreadByFidHelp(conditions, 1, 10, '-created', function(err, threads) { getAllThreadByFidHelp(conditions, 1, 10, '-created', function(err, threads) {
...@@ -196,6 +193,7 @@ exports.getThreadById = function(tid, callback) { ...@@ -196,6 +193,7 @@ exports.getThreadById = function(tid, callback) {
var conditions = { var conditions = {
pid: tid, pid: tid,
type: 3, type: 3,
status:1,
images: {$exists: true, $not: {$size: 0}} images: {$exists: true, $not: {$size: 0}}
}; };
getAllThreadByFidHelp(conditions, 1, 10, '-praise_count', function(err, threads) { getAllThreadByFidHelp(conditions, 1, 10, '-praise_count', function(err, threads) {
...@@ -487,6 +485,7 @@ function getAllThreadByFidHelp(conditions, pageNo, pageSize, sort, callback) { ...@@ -487,6 +485,7 @@ function getAllThreadByFidHelp(conditions, pageNo, pageSize, sort, callback) {
}).populate({ }).populate({
path: 'comments', path: 'comments',
options: { options: {
where:{status:1},
limit: 5, limit: 5,
sort: '-created' sort: '-created'
}, },
......
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