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