Commit 585ec733 authored by strong's avatar strong

Merge branch 'remould_1214_api_optimize' into SANDBOX

parents c75e8d9d c76dbf7f
...@@ -17,6 +17,8 @@ router.get('/commentTips', function(req, res, next) { ...@@ -17,6 +17,8 @@ router.get('/commentTips', function(req, res, next) {
if(err){ if(err){
res.json(returnCode.UNCHECK_ERROR(err)); res.json(returnCode.UNCHECK_ERROR(err));
}else{ }else{
res.setHeader('Cache-Control', 'private, max-age=600'); // 私有缓存一个10分钟
res.setHeader('Expires', new Date(Date.now() + 600000).toUTCString());
res.json(_.assign({data:result}, returnCode.SUCCESS)); res.json(_.assign({data:result}, returnCode.SUCCESS));
} }
}); });
......
...@@ -37,6 +37,8 @@ router.get('/info/list/all', function(req, res, next) { ...@@ -37,6 +37,8 @@ router.get('/info/list/all', function(req, res, next) {
res.json(returnCode.BUSY); res.json(returnCode.BUSY);
} else { } else {
rs.data = datas.items; rs.data = datas.items;
res.setHeader('Cache-Control', 'public, max-age=3600'); // 公共缓存一个小时
res.setHeader('Expires', new Date(Date.now() + 3600000).toUTCString());
res.json(_.assign(rs, returnCode.SUCCESS)); res.json(_.assign(rs, returnCode.SUCCESS));
} }
}); });
...@@ -66,18 +68,13 @@ router.get('/info/:fid/get', function(req, res, next) { ...@@ -66,18 +68,13 @@ router.get('/info/:fid/get', function(req, res, next) {
if (fid) { if (fid) {
var source = req.session.mobileForumUser.source; var source = req.session.mobileForumUser.source;
httpService.createLog(req, source, fid, 2); httpService.createLog(req, source, fid, 2);
async.waterfall([
function(callback) {
//更新浏览数
forumInfoService.updateInfoPvCount(fid, function(err, result) { forumInfoService.updateInfoPvCount(fid, function(err, result) {
if (err) { if (err) {
callback(err, null); console.error(err);
} else {
callback(null, null);
} }
}); });
}, async.parallel([
function(data, callback) { function(callback) {
forumInfoService.getInfoById(fid, function(err, info) { forumInfoService.getInfoById(fid, function(err, info) {
if (err) { if (err) {
callback(err, null); callback(err, null);
...@@ -86,25 +83,25 @@ router.get('/info/:fid/get', function(req, res, next) { ...@@ -86,25 +83,25 @@ router.get('/info/:fid/get', function(req, res, next) {
} }
}); });
}, },
function(info, callback) { function(callback) {
forumThreadService.getAllCountByFid({ forumThreadService.getAllCountByFid({
info: fid info: fid
}, function(err, threadCount) { }, function(err, threadCount) {
if (err) { if (err) {
callback(err, null, null); callback(err, null);
} else { } else {
callback(null, info, threadCount); callback(null, threadCount);
} }
}); });
} }
], function(err, info, threadCount) { ], function(err, results) {
if (err) { if (err) {
console.error(err); console.error(err);
res.json(returnCode.BUSY); res.json(returnCode.BUSY);
} else { } else {
var rs = {}; var rs = {};
rs.data = info; rs.data = results[0];
rs.data.threadCount = threadCount; rs.data.threadCount = results[1];
res.json(_.assign(rs, returnCode.SUCCESS)); res.json(_.assign(rs, returnCode.SUCCESS));
} }
}); });
...@@ -170,17 +167,18 @@ router.get('/info/:fid/threads', function(req, res, next) { ...@@ -170,17 +167,18 @@ router.get('/info/:fid/threads', function(req, res, next) {
if (fid) { if (fid) {
async.waterfall([ async.waterfall([
function(callback) { function(callback) {
forumUserThreadControlService.getUserThreadControlById(user.getMobileUser(req), function(err, doc) {
if (err) {
callback(err, null);
} else {
if (doc) {
callback(null, doc);
} else {
callback(null, null); callback(null, null);
} // forumUserThreadControlService.getUserThreadControlById(user.getMobileUser(req), function(err, doc) {
} // if (err) {
}); // callback(err, null);
// } else {
// if (doc) {
// callback(null, doc);
// } else {
// callback(null, null);
// }
// }
// });
} }
], function(err, result) { ], function(err, result) {
if (err) { if (err) {
...@@ -667,6 +665,8 @@ router.get('/info/:fid/tags', function(req, res, next) { ...@@ -667,6 +665,8 @@ router.get('/info/:fid/tags', function(req, res, next) {
console.error(err); console.error(err);
res.json(returnCode.BUSY); res.json(returnCode.BUSY);
} else { } else {
res.setHeader('Cache-Control', 'public, max-age=3600'); // 公共缓存一个小时
res.setHeader('Expires', new Date(Date.now() + 3600000).toUTCString());
res.json(_.assign(results, returnCode.SUCCESS)); res.json(_.assign(results, returnCode.SUCCESS));
} }
}); });
......
...@@ -72,6 +72,8 @@ router.get('/moderators/plates', function(req, res, next) { ...@@ -72,6 +72,8 @@ router.get('/moderators/plates', function(req, res, next) {
if(result && result.infoIds){ if(result && result.infoIds){
items = result.infoIds; items = result.infoIds;
} }
res.setHeader('Cache-Control', 'public, max-age=3600'); // 公共缓存一个小时
res.setHeader('Expires', new Date(Date.now() + 3600000).toUTCString());
res.json(_.assign({items:items}, returnCode.SUCCESS)); res.json(_.assign({items:items}, returnCode.SUCCESS));
}); });
}); });
......
...@@ -518,20 +518,16 @@ router.get('/thread/:tid/get', function(req, res, next) { ...@@ -518,20 +518,16 @@ router.get('/thread/:tid/get', function(req, res, next) {
var tid = req.params.tid || null; var tid = req.params.tid || null;
var ent_code = req.session.user.ent_code; var ent_code = req.session.user.ent_code;
if (tid) { if (tid) {
async.waterfall([ async.waterfall([
function(callback) { function(callback) {
forumThreadService.updateThreadPvCount(tid, function(err, doc) {
forumThreadService.getThreadById(tid, function(err, thread) {
//文章类型 1、文章 2、话题 3、照片墙
if (err) { if (err) {
callback(err, null); callback(err, null);
} else { } else {
callback(null, null); callback(null, thread);
} }
});
},
function(data, callback) {
forumThreadService.getThreadById(tid, function(err, thread) {
//文章类型 1、文章 2、话题 3、照片墙
var info_id = thread.info._id, var info_id = thread.info._id,
pid = thread.pid, pid = thread.pid,
source = req.session.mobileForumUser.source; source = req.session.mobileForumUser.source;
...@@ -544,13 +540,10 @@ router.get('/thread/:tid/get', function(req, res, next) { ...@@ -544,13 +540,10 @@ router.get('/thread/:tid/get', function(req, res, next) {
} else if (thread.type == 3 && thread.level == 1) { } else if (thread.type == 3 && thread.level == 1) {
httpService.createLog(req, source, info_id, 3, 3, tid); httpService.createLog(req, source, info_id, 3, 3, tid);
} }
});
forumThreadService.updateThreadPvCount(tid, function(err, doc) {
if (err) { if (err) {
callback(err, null); console.error(err);
} else {
callback(null, thread);
} }
}); });
}, },
......
...@@ -4,6 +4,18 @@ var ForumComment = mongoose.model('ForumComment'); ...@@ -4,6 +4,18 @@ var ForumComment = mongoose.model('ForumComment');
var forumUserService = require('./forumUserService'); var forumUserService = require('./forumUserService');
var async = require('async'); var async = require('async');
var listCommentFields = {
from:1,
content:1,
level:1,
floor:1,
praise_count:1,
comment_count:1,
comments:1,
status:1
};
//创建评论 //创建评论
exports.createComment = function(entity, callback) { exports.createComment = function(entity, callback) {
var forum = new ForumComment(entity); var forum = new ForumComment(entity);
...@@ -125,7 +137,7 @@ exports.getAllComment = function(conditions, pageNo, pageSize, callback) { ...@@ -125,7 +137,7 @@ exports.getAllComment = function(conditions, pageNo, pageSize, callback) {
var skip = (pageNo - 1) * pageSize; var skip = (pageNo - 1) * pageSize;
var limit = count - skip > pageSize ? pageSize : (count - skip); var limit = count - skip > pageSize ? pageSize : (count - skip);
ForumComment.find(conditions).populate('from').populate('to').limit(limit).skip(skip).sort('-created').exec(function(err, docs) { ForumComment.find(conditions,listCommentFields).populate('from','uid mid nickName icon exp').populate('to','uid mid nickName icon exp').limit(limit).skip(skip).sort('-created').exec(function(err, docs) {
if (err) { if (err) {
console.error(err); console.error(err);
callback(err, null); callback(err, null);
......
...@@ -10,6 +10,80 @@ var forumCommentService = require('./forumCommentService'); ...@@ -10,6 +10,80 @@ var forumCommentService = require('./forumCommentService');
var async = require('async'); var async = require('async');
var then = require('thenjs'); var then = require('thenjs');
var listThreadFields = {
content:1,
type:1,
title:1,
pid:1,
from:1,
created:1,
recommend:1,
praise_count:1,
comment_count:1,
images:1,
comments:1,
level:1,
top:1,
pv_count:1,
address:1
};
var singleThreadFields = {
content:1,
type:1,
title:1,
pid:1,
info:1,
from:1,
created:1,
recommend:1,
praise_count:1,
comment_count:1,
images:1,
level:1,
top:1,
pv_count:1,
address:1
};
var subThreadFields = {
from:1
};
/**
* 过滤 html 标签正则
* @type {RegExp}
*/
var htmlElReg = /<[^>]*>/ig;
/**
* 过滤表情正则
* @type {RegExp}
*/
var emElReg = /\【em_([0-9]*)\】/ig;
/**
* 帖子内容处理函数
* @param {String} content 原始帖子内容
* @return {String} 处理完的帖子
*/
function handleContent(content) {
content = content || "";
content = content
.replace(htmlElReg, "")
.replace(emElReg, "");
if (content.length > 200) {
content = content.substr(0, 200) + "……";
}
return content.trim();
}
//根据发帖者分页查询话题列表 //根据发帖者分页查询话题列表
exports.findThreadByPage = function(pageNo, pageSize, q, callback) { exports.findThreadByPage = function(pageNo, pageSize, q, callback) {
then(function(cont) { then(function(cont) {
...@@ -135,21 +209,19 @@ exports.getByConditions = function(conditions, callback) { ...@@ -135,21 +209,19 @@ exports.getByConditions = function(conditions, callback) {
} }
//根据ID获取文章 //根据ID获取文章
exports.getThreadById = function(tid, callback) { exports.getThreadById = function(tid, callback) {
async.parallel([
function(cb) { ForumThread.findOne({_id: tid}, singleThreadFields).populate('from','uid mid nickName icon exp').populate('info','name icon').populate('share').exec(function(err, doc) {
var conditions = {
_id: tid
};
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 {
cb(null, doc); if (doc) {
} async.parallel([
});
},
function(cb) { function(cb) {
if (doc.type !== 1) {
cb(null, null);
return;
}
var conditions = { var conditions = {
thread: tid, thread: tid,
level: '1' level: '1'
...@@ -167,6 +239,10 @@ exports.getThreadById = function(tid, callback) { ...@@ -167,6 +239,10 @@ exports.getThreadById = function(tid, callback) {
}, },
function(cb) { function(cb) {
//获取话题子文章列表 //获取话题子文章列表
if (doc.type !== 2) {
cb(null, null);
return;
}
var conditions = { var conditions = {
pid: tid, pid: tid,
type: 2, type: 2,
...@@ -184,6 +260,10 @@ exports.getThreadById = function(tid, callback) { ...@@ -184,6 +260,10 @@ exports.getThreadById = function(tid, callback) {
}, },
function(cb) { function(cb) {
//获取照片墙最新文章列表 //获取照片墙最新文章列表
if (doc.type !== 3) {
cb(null, null);
return;
}
var conditions = { var conditions = {
pid: tid, pid: tid,
type: 3, type: 3,
...@@ -201,6 +281,10 @@ exports.getThreadById = function(tid, callback) { ...@@ -201,6 +281,10 @@ exports.getThreadById = function(tid, callback) {
}, },
function(cb) { function(cb) {
//获取照片墙最热文章列表 //获取照片墙最热文章列表
if (doc.type !== 3) {
cb(null, null);
return;
}
var conditions = { var conditions = {
pid: tid, pid: tid,
type: 3, type: 3,
...@@ -243,12 +327,12 @@ exports.getThreadById = function(tid, callback) { ...@@ -243,12 +327,12 @@ exports.getThreadById = function(tid, callback) {
if (err) { if (err) {
callback(err, null); callback(err, null);
} else { } else {
var thread = results[0]; var thread = doc;
var comments = results[1]; var comments = results[0] || [];
var subThreads = results[2] || []; //话题子文章列表 var subThreads = results[1] || []; //话题子文章列表
var latestPhotos = results[3] || []; //最新照片墙列表 var latestPhotos = results[2] || []; //最新照片墙列表
var hotPhotos = results[4] || []; //最热照片墙列表 var hotPhotos = results[3] || []; //最热照片墙列表
var canyuPeopleCount = results[5] || 0; //参与人数 var canyuPeopleCount = results[4] || 0; //参与人数
var threadObj = thread && thread.toObject() || {}; var threadObj = thread && thread.toObject() || {};
threadObj.comments = comments; threadObj.comments = comments;
...@@ -260,6 +344,12 @@ exports.getThreadById = function(tid, callback) { ...@@ -260,6 +344,12 @@ exports.getThreadById = function(tid, callback) {
callback(null, threadObj); callback(null, threadObj);
} }
}); });
} else {
callback(null, {});
}
}
});
}; };
//根据ID获取文章 //根据ID获取文章
exports.getThreadByIdSimple = function(conditions, callback) { exports.getThreadByIdSimple = function(conditions, callback) {
...@@ -274,7 +364,7 @@ exports.getThreadByIdNoLimit = function(tid, callback) { ...@@ -274,7 +364,7 @@ exports.getThreadByIdNoLimit = function(tid, callback) {
var conditions = { var conditions = {
_id: tid _id: tid
}; };
ForumThread.findOne(conditions).populate('from').populate('info').populate('share').exec(function(err, doc) { ForumThread.findOne(conditions,singleThreadFields).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);
...@@ -582,6 +672,7 @@ function populateSubComment(subComments, callback) { ...@@ -582,6 +672,7 @@ function populateSubComment(subComments, callback) {
//获取数量 //获取数量
function countAllByFid(conditions, callback) { function countAllByFid(conditions, callback) {
// callback(null, 21);
ForumThread.count(conditions, function(err, count) { ForumThread.count(conditions, function(err, count) {
if (err) { if (err) {
console.error(err); console.error(err);
...@@ -611,7 +702,7 @@ function getSubThreads(doc, sort, callback) { ...@@ -611,7 +702,7 @@ function getSubThreads(doc, sort, callback) {
if (sort) { if (sort) {
sortBy = sort; sortBy = sort;
} }
ForumThread.find(conditions).populate('from').sort(sortBy).exec(function(err, docs) { ForumThread.find(conditions, subThreadFields).populate('from', 'icon').sort(sortBy).exec(function(err, docs) {
if (err) { if (err) {
console.error(err); console.error(err);
callback(err, null); callback(err, null);
...@@ -642,8 +733,8 @@ function getAllThreadByFidHelp(conditions, pageNo, pageSize, sort, callback) { ...@@ -642,8 +733,8 @@ function getAllThreadByFidHelp(conditions, pageNo, pageSize, sort, callback) {
if (sort) { if (sort) {
sortBy = sort; sortBy = sort;
} }
ForumThread.find(conditions).populate('from').populate('info').populate({ ForumThread.find(conditions,listThreadFields).populate('from', 'uid mid nickName icon').populate('info','name icon pv_count').populate({
path: 'tag' path: 'tag',select: 'title pv_count'
}).populate({ }).populate({
path: 'comments', path: 'comments',
options: { options: {
...@@ -662,7 +753,6 @@ function getAllThreadByFidHelp(conditions, pageNo, pageSize, sort, callback) { ...@@ -662,7 +753,6 @@ function getAllThreadByFidHelp(conditions, pageNo, pageSize, sort, callback) {
obj.pageNo = pageNo; obj.pageNo = pageNo;
obj.pageSize = pageSize; obj.pageSize = pageSize;
obj.items = docs; obj.items = docs;
if (docs && docs.length > 0) { if (docs && docs.length > 0) {
var asyncTasks = []; var asyncTasks = [];
...@@ -710,8 +800,8 @@ function getAllThreadByFidHelpNoLimit(conditions, pageNo, pageSize, sort, callba ...@@ -710,8 +800,8 @@ function getAllThreadByFidHelpNoLimit(conditions, pageNo, pageSize, sort, callba
if (sort) { if (sort) {
sortBy = sort; sortBy = sort;
} }
ForumThread.find(conditions).populate('from').populate('info').populate({ ForumThread.find(conditions,listThreadFields).populate('from','uid mid nickName icon').populate('info','name icon pv_count').populate({
path: 'tag' path: 'tag',select: 'title pv_count'
}).populate({ }).populate({
path: 'comments', path: 'comments',
options: { options: {
...@@ -774,6 +864,7 @@ exports.getAllThreadByFidNoLimit = function(conditions, pageNo, pageSize, sort, ...@@ -774,6 +864,7 @@ exports.getAllThreadByFidNoLimit = function(conditions, pageNo, pageSize, sort,
//获取全部列表数据 //获取全部列表数据
exports.getAllThreadByFid = function(conditions, pageNo, pageSize, sort, callback) { exports.getAllThreadByFid = function(conditions, pageNo, pageSize, sort, callback) {
getAllThreadByFidHelp(conditions, pageNo, pageSize, sort, callback); getAllThreadByFidHelp(conditions, pageNo, pageSize, sort, callback);
}; };
//根据板块ID更新板块下的top为0,并把当前文章的top设为1(置顶) //根据板块ID更新板块下的top为0,并把当前文章的top设为1(置顶)
...@@ -1027,8 +1118,8 @@ function getAllThreadByFidAndUserHelp(user_ids, conditions, pageNo, pageSize, so ...@@ -1027,8 +1118,8 @@ function getAllThreadByFidAndUserHelp(user_ids, conditions, pageNo, pageSize, so
if (sort) { if (sort) {
sortBy = sort; sortBy = sort;
} }
ForumThread.find(conditions).populate('from').populate('info').populate({ ForumThread.find(conditions, listThreadFields).populate('from','uid mid nickName icon').populate('info','name icon pv_count').populate({
path: 'tag' path: 'tag', select: 'title pv_count'
}).populate({ }).populate({
path: 'comments', path: 'comments',
options: { options: {
......
...@@ -21,7 +21,7 @@ module.exports = function(app, config) { ...@@ -21,7 +21,7 @@ module.exports = function(app, config) {
app.use(bodyParser.urlencoded({ app.use(bodyParser.urlencoded({
extended: true extended: true
})); }));
app.use(compress()); // app.use(compress());
app.use(cookieParser()); app.use(cookieParser());
app.use(methodOverride()); app.use(methodOverride());
app.use(multiparty()); app.use(multiparty());
......
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