Commit 2d647f6d authored by 陈家荣's avatar 陈家荣

更新了查询文章的方法

parent 857b0774
...@@ -970,3 +970,115 @@ exports.remove = function(array,val) { ...@@ -970,3 +970,115 @@ exports.remove = function(array,val) {
} }
return array; return array;
}; };
//获取数量
function countAllByConditionsAndNickName(nickName, conditions, callback) {
ForumThread.find(conditions).populate('from').exec(function(err, results){
if (err) {
console.error(err);
callback(err, null);
} else {
if(results){
var count = 0;
//匹配 nickName
for(var i in results){
var flag = results[i].from.nickName.toString().indexOf(nickName);
if(flag >= 0){ // 如果有
count+=1;
}
}
callback(null, count);
}else{
callback(null, 0);
}
}
});
}
function getAllThreadByFidHelpAndNickName(nickName, conditions, pageNo, pageSize, sort, callback) {
countAllByConditionsAndNickName(nickName, conditions, function(err, count) {
if (err) {
console.error(err);
callback(err, null);
} else {
var skip = (pageNo - 1) * pageSize;
var limit = count - skip > pageSize ? pageSize : (count - skip);
var sortBy = '-topTime -tag_topTime -created';
if (sort) {
sortBy = sort;
}
ForumThread.find(conditions).populate('from').populate('info').populate({
path: 'tag'
}).populate({
path: 'comments',
options: {
where:{status:1},
limit: 5,
sort: '-created'
},
select: 'from to created content'
}).sort(sortBy).exec(function(err, docs) {
if (err) {
console.error(err);
callback(err, null);
} else {
//匹配 nickName
var new_threads = [];
for(var i in docs){
var flag = docs[i].from.nickName.toString().indexOf(nickName);
if(flag >= 0){ // 如果有
new_threads.push(docs[i]);
}
}
var return_threads = new_threads.slice(skip, skip + limit);
var obj = {};
obj.total = count;
obj.pageNo = pageNo;
obj.pageSize = pageSize;
obj.items = return_threads;
if (return_threads && return_threads.length > 0) {
var asyncTasks = [];
return_threads.forEach(function(doc) {
// console.log(doc);
if (doc.type === 1 || doc.level !== 1) { //非照片墙或文章时获取评论
asyncTasks.push(function(callback) {
populateComment(doc, callback);
});
} else {
asyncTasks.push(function(callback) {
getSubThreads(doc, null, callback);
});
}
});
async.parallel(asyncTasks, function(err, results) {
if (err) {
console.error(err);
callback(null, null);
} else {
obj.items = results;
callback(null, obj);
}
});
} else {
callback(null, obj);
}
}
});
}
});
}
//获取全部列表数据
exports.getAllThreadByFidAndNickName = function(nickName, conditions, pageNo, pageSize, sort, callback) {
getAllThreadByFidHelpAndNickName(nickName, conditions, pageNo, pageSize, sort, callback);
};
\ No newline at end of file
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