Commit 627a03c4 authored by strong's avatar strong

Merge commit '11b32e95'

# Conflicts:
#	app/service/forumCommentService.js
#	app/service/forumThreadService.js
parents 937eb9a3 11b32e95
...@@ -24,7 +24,8 @@ var forumThreadService = require('../../service/forumThreadService'), ...@@ -24,7 +24,8 @@ var forumThreadService = require('../../service/forumThreadService'),
forumLimitOperationService = require('../../service/forumLimitOperationService'), forumLimitOperationService = require('../../service/forumLimitOperationService'),
forumAboutMEService = require('../../service/forumAboutMEService'), forumAboutMEService = require('../../service/forumAboutMEService'),
httpService = require('../../service/httpService'), httpService = require('../../service/httpService'),
redisThreadList = require('../../utils/redisThreadList'); redisThreadList = require('../../utils/redisThreadList'),
forumFollowThreadService = require('../../service/forumFollowThreadService');
var userUtil = require('../../utils/user'); var userUtil = require('../../utils/user');
var MemoryCache = require('../../utils/simpleCache'); var MemoryCache = require('../../utils/simpleCache');
...@@ -115,7 +116,8 @@ router.post('/thread/create', function(req, res, next) { ...@@ -115,7 +116,8 @@ router.post('/thread/create', function(req, res, next) {
} else { } else {
forumAboutMEService.saveThread(entity); forumAboutMEService.saveThread(entity);
rs.data = { rs.data = {
'id': entity._id 'id': entity._id,
'ent_code' : ent_code
}; };
res.json(_.assign(rs, returnCode.SUCCESS)); res.json(_.assign(rs, returnCode.SUCCESS));
} }
...@@ -141,7 +143,8 @@ router.post('/thread/create', function(req, res, next) { ...@@ -141,7 +143,8 @@ router.post('/thread/create', function(req, res, next) {
} else { } else {
forumAboutMEService.saveThread(entity); forumAboutMEService.saveThread(entity);
rs.data = { rs.data = {
'id': entity._id 'id': entity._id,
'ent_code' : ent_code
}; };
res.json(_.assign(rs, returnCode.SUCCESS)); res.json(_.assign(rs, returnCode.SUCCESS));
} }
...@@ -157,6 +160,7 @@ router.post('/thread/create', function(req, res, next) { ...@@ -157,6 +160,7 @@ router.post('/thread/create', function(req, res, next) {
router.get('/thread/:tid/get', function(req, res, next) { router.get('/thread/:tid/get', function(req, res, next) {
var uid = req.session.user.id; var uid = req.session.user.id;
var tid = req.params.tid || null; var tid = req.params.tid || null;
var ent_code = req.session.user.ent_code;
var rs = {}; var rs = {};
if (tid) { if (tid) {
async.waterfall([ async.waterfall([
...@@ -172,7 +176,7 @@ router.get('/thread/:tid/get', function(req, res, next) { ...@@ -172,7 +176,7 @@ router.get('/thread/:tid/get', function(req, res, next) {
function(thread, callback) { function(thread, callback) {
if (thread && thread.info && thread.info._id) { if (thread && thread.info && thread.info._id) {
forumTagService.getAllTag({ forumTagService.getAllTag({
ent_code: req.session.user.ent_code, ent_code: ent_code,
info: thread.info._id info: thread.info._id
}, 1, 100, function(err, results) { }, 1, 100, function(err, results) {
if (err) { if (err) {
...@@ -188,6 +192,17 @@ router.get('/thread/:tid/get', function(req, res, next) { ...@@ -188,6 +192,17 @@ router.get('/thread/:tid/get', function(req, res, next) {
}, },
function(datas, callback) { function(datas, callback) {
forumUserService.getUserByUid(uid, function(err, results) { forumUserService.getUserByUid(uid, function(err, results) {
if (err) {
callback(err, null);
} else {
datas.push(results);
callback(null, datas);
}
});
},
function(datas, callback) { //获取EXP
if(datas[0].from.mid){
userUtil.getMemberExp(ent_code, datas[0].from.mid, 3, function(err, results) {
if (err) { if (err) {
callback(err, null); callback(err, null);
} else { } else {
...@@ -195,6 +210,20 @@ router.get('/thread/:tid/get', function(req, res, next) { ...@@ -195,6 +210,20 @@ router.get('/thread/:tid/get', function(req, res, next) {
callback(null, datas); callback(null, datas);
} }
}); });
}else{
datas.push({})
callback(null, datas);
}
},
function(datas, callback) { //获取收藏数
forumFollowThreadService.count({thread: tid, ent_code: ent_code}, function(err, result){
if (err) {
callback(err, null);
} else {
datas.push(result);
callback(null, datas);
}
});
} }
], function(err, results) { ], function(err, results) {
if (err) { if (err) {
...@@ -210,6 +239,8 @@ router.get('/thread/:tid/get', function(req, res, next) { ...@@ -210,6 +239,8 @@ router.get('/thread/:tid/get', function(req, res, next) {
if (results[0].pid) { if (results[0].pid) {
forumThreadService.getById(results[0].pid, function(err, parentThread) { forumThreadService.getById(results[0].pid, function(err, parentThread) {
rs.data = results[0]; rs.data = results[0];
rs.data.from.exp = results[3] && results[3].allExp ? results[3].allExp : 0;
rs.data.followCount = results[4] ? results[4] : 0;
rs.tagList = results[1]; rs.tagList = results[1];
rs.parentThread = parentThread; rs.parentThread = parentThread;
rs.isSameAuthor = isSameAuthor; rs.isSameAuthor = isSameAuthor;
...@@ -217,6 +248,8 @@ router.get('/thread/:tid/get', function(req, res, next) { ...@@ -217,6 +248,8 @@ router.get('/thread/:tid/get', function(req, res, next) {
}); });
} else { } else {
rs.data = results[0]; rs.data = results[0];
rs.data.from.exp = results[3] && results[3].allExp ? results[3].allExp : 0;
rs.data.followCount = results[4] ? results[4] : 0;
rs.tagList = results[1]; rs.tagList = results[1];
rs.isSameAuthor = isSameAuthor; rs.isSameAuthor = isSameAuthor;
res.json(_.assign(rs, returnCode.SUCCESS)); res.json(_.assign(rs, returnCode.SUCCESS));
......
'use strict';
//插件
var router = require('express').Router(),
returnCode = require('../../utils/returnCode'),
_ = require('lodash'),
then = require('thenjs'),
moment = require('moment'),
mongoose = require('mongoose'),
util = require('../../utils/util'),
user = require('../../utils/user'),
redisPraiseLog = require('../../utils/redisPraiseLog'),
async = require('async');
//模型
var ForumThread = mongoose.model('ForumThread'),
ForumComment = mongoose.model('ForumComment'),
ForumPVLog = mongoose.model('ForumPVLog'),
ForumFollowThread = mongoose.model('ForumFollowThread');
//服务
var forumThreadService = require('../../service/forumThreadService'),
forumUserService = require('../../service/forumUserService'),
forumCommentService = require('../../service/forumCommentService'),
forumAboutMEService = require('../../service/forumAboutMEService');
module.exports = function(app) {
app.use('/admin/forum', router);
};
//格式化日期 (格式:年-月-日)
function date_format(date) {
return moment(date).format('YYYY-MM-DD HH:mm:ss');
}
//文章列表
router.get('/threadManagement/threads/list', function(req, res, next) {
var pageNo = req.query.pageNo || 1;
var pageSize = req.query.pageSize || 10;
var infoId = req.query.infoId;
var tagId = req.query.tagId;
var pid = req.query.pid;//父文章id
var content = req.query.content;//标题 或 内容
var type = req.query.type;//1普通文章,话题,照片墙
var nickName = req.query.nickName;
var mid = req.query.mid;
var begin_time=req.query.begin_time;
var end_time=req.query.end_time;
var tab = req.query.tab;//全部,官方贴,推荐贴,置顶帖,加精贴,活动贴,屏蔽贴
var asc = req.query.asc === 'true'?1:-1;
var sortBy = {//默认排序
created:asc
};
if(req.query.sort){//指定排序字段
sortBy = {};
sortBy[req.query.sort] = asc;
sortBy['_id'] = -1;
}
var conditions = {
ent_code: req.session.user.ent_code,
level: 1,
status: {
$ne: 3
}
};
if (infoId) {
conditions.info = infoId;
}
if (tagId) {
conditions.tag = {
$in: [tagId]
};
}
if (pid) {
conditions.pid = pid;
conditions.level = 2;
}
if (content) {
conditions.$or = [
{
title:{
$regex: content,
$options: 'i'
}
},
{
content:{
$regex: content,
$options: 'i'
}
}
];
}
if (type) {
conditions.type = Number(type);
}
if(tab){
if(tab === 'admin'){//官方贴
conditions.isPublishByBg = 1;
}
else if(tab === 'new_recommend'){//推荐贴
conditions.new_recommend = 1;
}
else if(tab === 'top'){//置顶贴
conditions.top = 1;
}
else if(tab === 'recommend'){//加精贴
conditions.recommend = 1;
}
else if(tab === 'event'){//活动贴
conditions.isEvent = 1;
}
else if(tab === 'closed'){//屏蔽贴
conditions.status = 0;
}
}
if(begin_time && end_time){
conditions.created = {$gte : begin_time, $lte : end_time};
} else if(end_time){
conditions.created = {$lte : end_time};
} else if(begin_time){
conditions.created = {$gte : begin_time};
}
var callback = function(err, results) {
if (err) {
console.error(err);
res.json(returnCode.BUSY);
} else {
if (pid) {
forumThreadService.getById(pid, function(err, parentThread) {
res.json(_.assign({
parentThread: parentThread
}, results, returnCode.SUCCESS));
});
} else {
res.json(_.assign(results, returnCode.SUCCESS));
}
}
};
if (mid) {
forumThreadService.getAllThreadByFidAndMid(mid, conditions, pageNo, pageSize, sortBy, callback);
} else if (nickName) {
forumThreadService.getAllThreadByFidAndNickName(nickName, conditions, pageNo, pageSize, sortBy, callback);
} else {
forumThreadService.getAllThreadByFid(conditions, pageNo, pageSize, sortBy, callback);
}
});
//删除查询结果的所有帖子
router.post('/threadManagement/threads/list/deleteAll', function(req, res, next) {
var infoId = req.body.infoId;
var tagId = req.body.tagId;
var pid = req.body.pid;//父文章id
var content = req.body.content;//标题 或 内容
var type = req.body.type;//1普通文章,话题,照片墙
var nickName = req.body.nickName;
var mid = req.body.mid;
var begin_time=req.body.begin_time;
var end_time=req.body.end_time;
var tab = req.body.tab;//全部,官方贴,推荐贴,置顶帖,加精贴,活动贴,屏蔽贴
var conditions = {
ent_code: req.session.user.ent_code,
level: 1,
status: {
$ne: 3
}
};
if (infoId) {
conditions.info = infoId;
}
if (tagId) {
conditions.tag = {
$in: [tagId]
};
}
if (pid) {
conditions.pid = pid;
conditions.level = 2;
}
if (content) {
conditions.$or = [
{
title:{
$regex: content,
$options: 'i'
}
},
{
content:{
$regex: content,
$options: 'i'
}
}
];
}
if (type) {
conditions.type = Number(type);
}
if(tab){
if(tab === 'admin'){//官方贴
conditions.isPublishByBg = 1;
}
else if(tab === 'new_recommend'){//推荐贴
conditions.new_recommend = 1;
}
else if(tab === 'top'){//置顶贴
conditions.top = 1;
}
else if(tab === 'recommend'){//加精贴
conditions.recommend = 1;
}
else if(tab === 'event'){//活动贴
conditions.isEvent = 1;
}
else if(tab === 'closed'){//屏蔽贴
conditions.status = 0;
}
}
if(begin_time && end_time){
conditions.created = {$gte : begin_time, $lte : end_time};
} else if(end_time){
conditions.created = {$lte : end_time};
} else if(begin_time){
conditions.created = {$gte : begin_time};
}
var callback = function(err, result) {
if (err) {
console.error(err);
res.json(returnCode.BUSY);
} else {
res.json(_.assign({
data:result
}, returnCode.SUCCESS));
}
};
var deleteAll= function(condition,callback){
ForumThread.find(conditions).select('_id').exec(function(err, docs) {
if (err) {
console.error(err);
callback(err, null);
} else {
var _tasks=[];
_.forEach(docs,function(doc){
_tasks.push(function(cb){
forumThreadService.logicDeleteThreadById(doc._id, function(err) {
if (err) {
cb(err, null);
} else {
forumAboutMEService.updateThreadStatus(doc._id, 3);
cb(null, true);
}
});
});
});
var chunks=_.chunk(_tasks, 100);
var tasks = [];
_.forEach(chunks,function(chunk){
tasks.push(function(cb){
async.parallel(chunk,function(err){
cb(err, true);
});
});
});
async.series(tasks,function(err){
callback(err, true);
});
}
});
};
if (mid) {
forumUserService.searchMembersByMid(mid, function(err, users) {
if (err) {
console.error(err);
callback(err, null);
} else {
//查询对应用户的文章
var user_ids = [];
for(var i in users){
user_ids.push(users[i]._id);
}
conditions.from = {
"$in" : user_ids
};
deleteAll(conditions,callback);
}
});
} else if (nickName) {
//查询到用户
forumUserService.searchMembersByNickName(nickName, function(err, users) {
if (err) {
console.error(err);
callback(err, null);
} else {
//查询对应用户的文章
var user_ids = [];
for(var i in users){
user_ids.push(users[i]._id);
}
conditions.from = {
"$in" : user_ids
};
deleteAll(conditions,callback);
}
});
} else {
deleteAll(conditions,callback);
}
});
//屏蔽查询结果的所有帖子
router.post('/threadManagement/threads/list/closeAll', function(req, res, next) {
var infoId = req.body.infoId;
var tagId = req.body.tagId;
var pid = req.body.pid;//父文章id
var content = req.body.content;//标题 或 内容
var type = req.body.type;//1普通文章,话题,照片墙
var nickName = req.body.nickName;
var mid = req.body.mid;
var begin_time=req.body.begin_time;
var end_time=req.body.end_time;
var tab = req.body.tab;//全部,官方贴,推荐贴,置顶帖,加精贴,活动贴,屏蔽贴
var conditions = {
ent_code: req.session.user.ent_code,
level: 1,
status: {
$ne: 3
}
};
if (infoId) {
conditions.info = infoId;
}
if (tagId) {
conditions.tag = {
$in: [tagId]
};
}
if (pid) {
conditions.pid = pid;
conditions.level = 2;
}
if (content) {
conditions.$or = [
{
title:{
$regex: content,
$options: 'i'
}
},
{
content:{
$regex: content,
$options: 'i'
}
}
];
}
if (type) {
conditions.type = Number(type);
}
if(tab){
if(tab === 'admin'){//官方贴
conditions.isPublishByBg = 1;
}
else if(tab === 'new_recommend'){//推荐贴
conditions.new_recommend = 1;
}
else if(tab === 'top'){//置顶贴
conditions.top = 1;
}
else if(tab === 'recommend'){//加精贴
conditions.recommend = 1;
}
else if(tab === 'event'){//活动贴
conditions.isEvent = 1;
}
else if(tab === 'closed'){//屏蔽贴
conditions.status = 0;
}
}
if(begin_time && end_time){
conditions.created = {$gte : begin_time, $lte : end_time};
} else if(end_time){
conditions.created = {$lte : end_time};
} else if(begin_time){
conditions.created = {$gte : begin_time};
}
var callback = function(err, result) {
if (err) {
console.error(err);
res.json(returnCode.BUSY);
} else {
res.json(_.assign({
data:result
}, returnCode.SUCCESS));
}
};
var closeAll= function(condition,callback){
ForumThread.find(conditions).select('_id').exec(function(err, docs) {
if (err) {
console.error(err);
callback(err, null);
} else {
var _tasks=[];
_.forEach(docs,function(doc){
_tasks.push(function(cb){
forumThreadService.updateThreadById(doc._id, {status:0}, function(err) {
if (err) {
cb(err, null);
} else {
forumAboutMEService.updateThreadStatus(doc._id, 0);
cb(null, true);
}
});
});
});
var chunks=_.chunk(_tasks, 100);
var tasks = [];
_.forEach(chunks,function(chunk){
tasks.push(function(cb){
async.parallel(chunk,function(err){
cb(err, true);
});
});
});
async.series(tasks,function(err){
callback(err, true);
});
}
});
};
if (mid) {
forumUserService.searchMembersByMid(mid, function(err, users) {
if (err) {
console.error(err);
callback(err, null);
} else {
//查询对应用户的文章
var user_ids = [];
for(var i in users){
user_ids.push(users[i]._id);
}
conditions.from = {
"$in" : user_ids
};
closeAll(conditions,callback);
}
});
} else if (nickName) {
//查询到用户
forumUserService.searchMembersByNickName(nickName, function(err, users) {
if (err) {
console.error(err);
callback(err, null);
} else {
//查询对应用户的文章
var user_ids = [];
for(var i in users){
user_ids.push(users[i]._id);
}
conditions.from = {
"$in" : user_ids
};
closeAll(conditions,callback);
}
});
} else {
closeAll(conditions,callback);
}
});
//获取帖子总数,帖子数,用户发帖数,帖子加精数
router.get('/threadManagement/threads/statistics', function(req, res, next) {
var infoId = req.query.infoId;
var tagId = req.query.tagId;
var pid = req.query.pid;//父文章id
var content = req.query.content;//标题 或 内容
var type = req.query.type;//1普通文章,话题,照片墙
var nickName = req.query.nickName;
var mid = req.query.mid;
var begin_time=req.query.begin_time;
var end_time=req.query.end_time;
var tab = req.query.tab;//全部,官方贴,推荐贴,置顶帖,加精贴,活动贴,屏蔽贴
var conditions = {
ent_code: req.session.user.ent_code,
level: 1,
status: {
$ne: 3
}
};
if (infoId) {
conditions.info = infoId;
}
if (tagId) {
conditions.tag = {
$in: [tagId]
};
}
if (pid) {
conditions.pid = pid;
conditions.level = 2;
}
if (content) {
conditions.$or = [
{
title:{
$regex: content,
$options: 'i'
}
},
{
content:{
$regex: content,
$options: 'i'
}
}
];
}
if (type) {
conditions.type = Number(type);
}
if(tab){
if(tab === 'admin'){//官方贴
conditions.isPublishByBg = 1;
}
else if(tab === 'new_recommend'){//推荐贴
conditions.new_recommend = 1;
}
else if(tab === 'top'){//置顶贴
conditions.top = 1;
}
else if(tab === 'recommend'){//加精贴
conditions.recommend = 1;
}
else if(tab === 'event'){//活动贴
conditions.isEvent = 1;
}
else if(tab === 'closed'){//屏蔽贴
conditions.status = 0;
}
}
if(begin_time && end_time){
conditions.created = {$gte : begin_time, $lte : end_time};
} else if(end_time){
conditions.created = {$lte : end_time};
} else if(begin_time){
conditions.created = {$gte : begin_time};
}
async.parallel([function(cb){//获取帖子总数
var _conditions = {
ent_code: req.session.user.ent_code,
level: 1,
status: {
$ne: 3
}
};
ForumThread.count(_conditions, function(err, count) {
cb(err,['thread_total',count || 0]);
});
},function(cb){//获取帖子数
var _conditions = _.assign({}, conditions);
if (mid) {//查询对应用户的文章
forumUserService.searchMembersByMid(mid, function(err, users) {
if (err) {
cb(err, null);
} else {
var user_ids = [];
if(users){
_.forEach(users,function(user){
user_ids.push(user._id);
});
}
_conditions.from = {
"$in" : user_ids
};
ForumThread.count(_conditions, function(err, count) {
cb(err,['thread_search_total',count || 0]);
});
}
});
} else if (nickName) {
forumUserService.searchMembersByNickName(nickName, function(err, users) {
if (err) {
cb(err, null);
} else {
var user_ids = [];
if(users){
_.forEach(users,function(user){
user_ids.push(user._id);
});
}
_conditions.from = {
"$in" : user_ids
};
ForumThread.count(_conditions, function(err, count) {
cb(err,['thread_search_total',count || 0]);
});
}
});
} else {
ForumThread.count(_conditions, function(err, count) {
cb(err,['thread_search_total',count || 0]);
});
}
},function(cb){//获取用户发帖数
var _conditions = _.assign({isPublishByBg:{$ne:1}}, conditions);
if (mid) {//查询对应用户的文章
forumUserService.searchMembersByMid(mid, function(err, users) {
if (err) {
cb(err, null);
} else {
var user_ids = [];
if(users){
_.forEach(users,function(user){
user_ids.push(user._id);
});
}
_conditions.from = {
"$in" : user_ids
};
ForumThread.count(_conditions, function(err, count) {
cb(err,['thread_search_user_publish',count || 0]);
});
}
});
} else if (nickName) {
forumUserService.searchMembersByNickName(nickName, function(err, users) {
if (err) {
cb(err, null);
} else {
var user_ids = [];
if(users){
_.forEach(users,function(user){
user_ids.push(user._id);
});
}
_conditions.from = {
"$in" : user_ids
};
ForumThread.count(_conditions, function(err, count) {
cb(err,['thread_search_user_publish',count || 0]);
});
}
});
} else {
ForumThread.count(_conditions, function(err, count) {
cb(err,['thread_search_user_publish',count || 0]);
});
}
},function(cb){//获取加精帖子数
var _conditions = _.assign({recommend:1}, conditions);
if (mid) {//查询对应用户的文章
forumUserService.searchMembersByMid(mid, function(err, users) {
if (err) {
cb(err, null);
} else {
var user_ids = [];
if(users){
_.forEach(users,function(user){
user_ids.push(user._id);
});
}
_conditions.from = {
"$in" : user_ids
};
ForumThread.count(_conditions, function(err, count) {
cb(err,['thread_search_recommend',count || 0]);
});
}
});
} else if (nickName) {
forumUserService.searchMembersByNickName(nickName, function(err, users) {
if (err) {
cb(err, null);
} else {
var user_ids = [];
if(users){
_.forEach(users,function(user){
user_ids.push(user._id);
});
}
_conditions.from = {
"$in" : user_ids
};
ForumThread.count(_conditions, function(err, count) {
cb(err,['thread_search_recommend',count || 0]);
});
}
});
} else {
ForumThread.count(_conditions, function(err, count) {
cb(err,['thread_search_recommend',count || 0]);
});
}
}],function(err,results){
if(err){
console.log(err);
return res.json(returnCode.BUSY);
}
// 数组转对象_.zipObject([[‘fred‘, 30], [‘barney‘, 40]]);→ { ‘fred‘: 30, ‘barney‘: 40 }
res.json(_.assign({
data:_.zipObject(results)
}, returnCode.SUCCESS));
});
});
//批量屏蔽帖子
router.post('/threadManagement/threads/batchclose', function(req, res, next) {
var ent_code = req.session.user.ent_code;
var ids = req.body.ids;
if(!ent_code || !ids){
return res.json(returnCode.BUSY);
}
forumThreadService.batchClose(ent_code,ids,function(err,result){
if(err){
return res.json(returnCode.BUSY);
}
return res.json(_.assign({data:true}, returnCode.SUCCESS));
});
});
//批量删除帖子
router.post('/threadManagement/threads/batchdel', function(req, res, next) {
var ent_code = req.session.user.ent_code;
var ids = req.body.ids;
if(!ent_code || !ids){
return res.json(returnCode.BUSY);
}
forumThreadService.batchLogicDelete(ent_code,ids,function(err,result){
if(err){
return res.json(returnCode.BUSY);
}
return res.json(_.assign({data:true}, returnCode.SUCCESS));
});
});
function updateComemntStatus(ent_code,id,status,cb){
if(!ent_code || !id){
return cb && cb(null,true);
}
var cid = id;
async.waterfall([
function(callback) {
//获取评论实例
forumCommentService.getCommentById(cid, callback);
},
function(comment, callback) {
//更新评论状态
forumCommentService.updateCommentStatusById(cid, status, function(err, update) {
callback(err, comment);
});
},
function(comment, callback) {
if (comment && comment.level == 1) {
forumAboutMEService.updateCommentLevel1Status(comment._id, status);
}
if (comment && comment.level == 2) {
forumAboutMEService.updateCommentLevel2Status(comment._id, status);
}
//是否删除评论
if (status == 2) {
if (comment.level == 1) {//一级评论,需要移除文章的comments里的cid,并且评论数-1
forumThreadService.updateThreadById(comment.thread, {
$pull:{
comments:cid
},
$inc: {
comment_count: -1
}
}, function(err, result) {
if(err){
console.log(err);
}
callback();
});
} else {//二级评论,移除父级评论的comments里的cid
forumCommentService.getCommentParent(cid, function(err, p_comment) {
if(err || !p_comment){
console.log(err);
}else{
forumCommentService.updateCommentById(p_comment._id, {
$pull:{
comments:cid
},
$inc: {
comment_count: -1
}
}, function(err, update) {
if(err){
console.log(err);
}
});
}
callback();
});
}
} else {
callback();
}
}
], function(err, restult) {
if (err) {
console.error(err);
}
cb(err,!!!err);
});
}
function batchUpdateCommentStatus(ent_code,ids,status,callback){
if(!ent_code || !ids){
return callback && callback('参数错误');
}
var tasks = [];
_.forEach(ids,function(id){
tasks.push(function(cb){
updateComemntStatus(ent_code,id,status,function(err,result){
cb();
})
});
});
async.parallel(tasks,function(err,results){
return callback && callback(err,!!!err);
});
}
//批量屏蔽帖子的评论
router.post('/threadManagement/threads/:id/comment/batchclose', function(req, res, next) {
var ent_code = req.session.user.ent_code;
var id = req.params.id;//帖子id
var ids = req.body.ids;//评论id数组
var closed_status = 0;
if(!ent_code || !id || !ids){
return res.json(returnCode.BUSY);
}
//获取有效的评论id数组
ForumComment.find({ent_code:ent_code,thread:id,_id:{
$in:ids
}}).select('_id').exec(function(err, docs) {
if(err){
return res.json(returnCode.BUSY);
}
if(!docs || docs.length == 0){
return res.json(_.assign({data:true}, returnCode.SUCCESS));
}
var available_ids = [];
_.forEach(docs,function(doc){
available_ids.push(doc._id);
});
batchUpdateCommentStatus(ent_code,available_ids,closed_status,function(err,result){
if(err){
return res.json(returnCode.BUSY);
}
return res.json(_.assign({data:true}, returnCode.SUCCESS));
});
});
});
//批量删除帖子的评论
router.post('/threadManagement/threads/:id/comment/batchdel', function(req, res, next) {
var ent_code = req.session.user.ent_code;
var id = req.params.id;//帖子id
var ids = req.body.ids;//评论id数组
var delete_status = 2;
if(!ent_code || !id || !ids){
return res.json(returnCode.BUSY);
}
//获取有效的评论id数组
ForumComment.find({ent_code:ent_code,thread:id,_id:{
$in:ids
}}).select('_id').exec(function(err, docs) {
if(err){
return res.json(returnCode.BUSY);
}
if(!docs || docs.length == 0){
return res.json(_.assign({data:true}, returnCode.SUCCESS));
}
var available_ids = [];
_.forEach(docs,function(doc){
available_ids.push(doc._id);
});
batchUpdateCommentStatus(ent_code,available_ids,delete_status,function(err,result){
if(err){
return res.json(returnCode.BUSY);
}
return res.json(_.assign({data:true}, returnCode.SUCCESS));
});
});
});
//查询评论列表
router.get('/threadManagement/threads/:tid/comment/search', function(req, res, next) {
var tid = req.params.tid || null,
pageNo = req.query.pageNo || 1,
pageSize = req.query.pageSize || 10,
ent_code = req.session.user.ent_code,
mid = req.query.mid,
floor_start = req.query.floor_start,
floor_end = req.query.floor_end,
content = req.query.content || '';
var asc = req.query.asc === 'true'?1:-1;
var sort = {//默认排序
floor:asc
};
if(req.query.sort){//指定排序字段
sort = {};
sort[req.query.sort] = asc;
sort['_id'] = -1;
}
var conditions = {
ent_code: ent_code,
thread: tid,
level:1,
status:{
$ne:2
}
};
if (content) {
conditions.content = {
$regex: content,
$options: 'i'
};
}
if(floor_start && floor_end){
conditions.floor = {
$gte:floor_start,
$lte:floor_end
};
}else if(floor_start){
conditions.floor = {
$gte:floor_start
};
}else if(floor_end){
conditions.floor = {
$lte:floor_end
};
}
if (tid && mid) {
forumCommentService.getCommentListByMidOrderBy(mid, conditions,sort, pageNo, pageSize, function(err, results) {
if (err) {
console.error(err);
res.json(returnCode.BUSY);
} else {
//判断是否已经点赞
util.loadLevel(ent_code, results.items, function() {
res.json(_.assign(results, returnCode.SUCCESS));
});
}
});
} else if(tid){
forumCommentService.getAllCommentOrderBy(conditions,sort, pageNo, pageSize, function(err, results) {
if (err) {
console.log(err);
res.json(returnCode.BUSY);
} else {
//判断是否已经点赞
util.loadLevel(ent_code, results.items, function() {
res.json(_.assign(results, returnCode.SUCCESS));
});
}
});
} else {
res.json(returnCode.WRONG_PARAM);
}
});
//删除帖子的评论(查询结果)
router.post('/threadManagement/threads/:tid/comment/search/deleteAll', function(req, res, next) {
var tid = req.params.tid || null,
mid = req.body.mid,
floor_start = req.body.floor_start,
floor_end = req.body.floor_end,
content = req.body.content || '';
var ent_code = req.session.user.ent_code;
var status = 2;//删除状态为2
var conditions = {
ent_code: ent_code,
thread: tid,
level:1,
status:{
$ne:2
}
};
if (content) {
conditions.content = {
$regex: content,
$options: 'i'
};
}
if(floor_start){
conditions.floor = {
$gte:floor_start
};
}
if(floor_end){
conditions.floor = {
$lte:floor_end
};
}
var callback = function(err, result) {
if (err) {
console.error(err);
res.json(returnCode.BUSY);
} else {
res.json(_.assign({
data:result
}, returnCode.SUCCESS));
}
};
var deleteAll= function(condition,callback){
ForumComment.find(conditions).select('_id').exec(function(err, docs) {
if (err) {
console.error(err);
callback(err, null);
} else {
var ids=[];
_.forEach(docs,function(doc){
ids.push(doc._id);
});
var chunks=_.chunk(ids, 100);
var tasks = [];
_.forEach(chunks,function(chunk){
tasks.push(function(cb){
batchUpdateCommentStatus(ent_code,chunk,status,function(err,result){
cb(err,result);
});
});
});
async.series(tasks,function(err){
callback(err, true);
});
}
});
};
if (tid && mid) {
forumUserService.searchMembersByMid(mid, function(err, users) {
if (err) {
console.error(err);
callback(err, null);
} else {
//查询对应用户
var user_ids = [];
for (var i in users) {
user_ids.push(users[i]._id);
}
conditions.from = {
$in: user_ids
}
deleteAll(conditions,callback);
}
});
} else if(tid){
deleteAll(conditions,callback);
} else {
res.json(returnCode.WRONG_PARAM);
}
});
//屏蔽帖子的评论(查询结果)
router.post('/threadManagement/threads/:tid/comment/search/closeAll', function(req, res, next) {
var tid = req.params.tid || null,
mid = req.body.mid,
floor_start = req.body.floor_start,
floor_end = req.body.floor_end,
content = req.body.content || '';
var ent_code = req.session.user.ent_code;
var status = 0;//屏蔽的状态为0
var conditions = {
ent_code: ent_code,
thread: tid,
level:1,
status:{
$ne:2
}
};
if (content) {
conditions.content = {
$regex: content,
$options: 'i'
};
}
if(floor_start){
conditions.floor = {
$gte:floor_start
};
}
if(floor_end){
conditions.floor = {
$lte:floor_end
};
}
var callback = function(err, result) {
if (err) {
console.error(err);
res.json(returnCode.BUSY);
} else {
res.json(_.assign({
data:result
}, returnCode.SUCCESS));
}
};
var closeAll= function(condition,callback){
ForumComment.find(conditions).select('_id').exec(function(err, docs) {
if (err) {
console.error(err);
callback(err, null);
} else {
var ids=[];
_.forEach(docs,function(doc){
ids.push(doc._id);
});
var chunks=_.chunk(ids, 100);
var tasks = [];
_.forEach(chunks,function(chunk){
tasks.push(function(cb){
batchUpdateCommentStatus(ent_code,chunk,status,function(err,result){
cb(err,result);
});
});
});
async.series(tasks,function(err){
callback(err, true);
});
}
});
};
if (tid && mid) {
forumUserService.searchMembersByMid(mid, function(err, users) {
if (err) {
console.error(err);
callback(err, null);
} else {
//查询对应用户
var user_ids = [];
for (var i in users) {
user_ids.push(users[i]._id);
}
conditions.from = {
$in: user_ids
}
closeAll(conditions,callback);
}
});
} else if(tid){
closeAll(conditions,callback);
} else {
res.json(returnCode.WRONG_PARAM);
}
});
//查询帖子的收藏数
router.get('/threadManagement/threads/:tid/collects', function(req, res, next) {
var tid = req.params.tid || null;
var ent_code = req.session.user.ent_code;
var conditions = {
ent_code: ent_code,
thread: tid
};
if (tid && ent_code) {
ForumFollowThread.count(conditions, function(err, count) {
if (err) {
res.json(returnCode.BUSY);
} else {
res.json(_.assign({data:(count||0)}, returnCode.SUCCESS));
}
});
} else {
res.json(returnCode.WRONG_PARAM);
}
});
//查询帖子的子文章数
router.get('/threadManagement/threads/:tid/subtotal', function(req, res, next) {
var tid = req.params.tid || null;
var ent_code = req.session.user.ent_code;
var conditions = {
ent_code: ent_code,
pid: tid,
status:1
};
if (tid && ent_code) {
ForumThread.count(conditions, function(err, count) {
if (err) {
res.json(returnCode.BUSY);
} else {
res.json(_.assign({data:(count||0)}, returnCode.SUCCESS));
}
});
} else {
res.json(returnCode.WRONG_PARAM);
}
});
//查询(话题/照片墙)的参与人数
router.get('/threadManagement/threads/:tid/joined/total', function(req, res, next) {
var tid = req.params.tid || null;
var ent_code = req.session.user.ent_code;
if (tid && ent_code) {
ForumThread.aggregate({
$match: {
ent_code: ent_code,
pid: mongoose.Types.ObjectId(tid)
}
}, {
$group: {
_id: {
from: '$from'
},
count: {
$sum: 1
}
}
}, function(err, data) {
if (err) {
res.json(returnCode.BUSY);
} else {
res.json(_.assign({data:((data && data.length)||0)}, returnCode.SUCCESS));
}
});
} else {
res.json(returnCode.WRONG_PARAM);
}
});
\ No newline at end of file
'use strict';
//插件
var router = require('express').Router(),
returnCode = require('../../utils/returnCode'),
_ = require('lodash'),
then = require('thenjs'),
moment = require('moment'),
mongoose = require('mongoose'),
async = require('async');
//模型
var ForumThread = mongoose.model('ForumThread'),
ForumComment = mongoose.model('ForumComment'),
ForumPVLog = mongoose.model('ForumPVLog'),
ForumFollowThread = mongoose.model('ForumFollowThread');
//服务
var forumThreadService = require('../../service/forumThreadService'),
forumUserService = require('../../service/forumUserService'),
forumCommentService = require('../../service/forumCommentService'),
forumAboutMEService = require('../../service/forumAboutMEService');
var thread_batch_prefix = 'forum_thread_management_batchoperate_';
var comment_batch_prefix = 'forum_comment_management_batchoperate_';
module.exports = function(app) {
app.use('/admin/forum', router);
};
function getBatchStatus(key,callback){
redis.get(key,function(err,store){
if(err){
return callback && callback(err);
}
//store为null表示未进行,-1表示失败,status=0表示正在进行,status=1表示完成
if(!store){
return callback && callback(null,null);
}
try{
var obj = JSON.parse(store);
return callback && callback(null,obj);
}catch(e){
return callback && callback(e);
}
});
}
function setStatus(key,status,expire){
var _status = status || {status:0};
redis.set(key,JSON.stringify(_status),function(err){
if(err){
console.log(err);
}
redis.expire(key,expire || 180);
});
}
function canStartIf(key,cannot,can){
getBatchStatus(key,function(err,store){
if(err){
return cannot && cannot(err);
}
if(store && store.status === 0){//正在处理
console.log('已经在处理了');
return cannot && cannot(null,true);
}
//已结束或未开始
setStatus(key);
return can && can();
});
}
function getThreadBatchOperateConditions(req){
var infoId = req.body.infoId;
var tagId = req.body.tagId;
var pid = req.body.pid;//父文章id
var content = req.body.content;//标题 或 内容
var type = req.body.type;//1普通文章,话题,照片墙
var begin_time=req.body.begin_time;
var end_time=req.body.end_time;
var tab = req.body.tab;//全部,官方贴,推荐贴,置顶帖,加精贴,活动贴,屏蔽贴
var conditions = {
ent_code: req.session.user.ent_code,
level: 1,
status: {
$ne: 3
}
};
if (infoId) {
conditions.info = infoId;
}
if (tagId) {
conditions.tag = {
$in: [tagId]
};
}
if (pid) {
conditions.pid = pid;
conditions.level = 2;
}
if (content) {
conditions.$or = [
{
title:{
$regex: content,
$options: 'i'
}
},
{
content:{
$regex: content,
$options: 'i'
}
}
];
}
if (type) {
conditions.type = Number(type);
}
if(tab){
if(tab === 'admin'){//官方贴
conditions.isPublishByBg = 1;
}
else if(tab === 'new_recommend'){//推荐贴
conditions.new_recommend = 1;
}
else if(tab === 'top'){//置顶贴
conditions.top = 1;
}
else if(tab === 'recommend'){//加精贴
conditions.recommend = 1;
}
else if(tab === 'event'){//活动贴
conditions.isEvent = 1;
}
else if(tab === 'closed'){//屏蔽贴
conditions.status = 0;
}
}
if(begin_time && end_time){
conditions.created = {$gte : begin_time, $lte : end_time};
} else if(end_time){
conditions.created = {$lte : end_time};
} else if(begin_time){
conditions.created = {$gte : begin_time};
}
return conditions;
}
function batchAsyncUpdateThread(req,res,handlFn){
var conditions = getThreadBatchOperateConditions(req);
var key = thread_batch_prefix + req.session.user.ent_code;
var nickName = req.body.nickName;
var mid = req.body.mid;
var start_time = new Date().getTime();
var pageSize = 100;
var ingInterval = (function(){//定时设置正在进行
var interval = null;
return {
start:function(){
interval = setInterval(function(){
setStatus(key);
},3000);
},
clear:function(){
return interval && clearInterval(interval);
}
}
})();
var callback = function(err) {
ingInterval.clear();
if (err) {//失败
console.log(err);
setStatus(key,{status:-1},1800);
} else {//结束
setStatus(key,{status:1,start:start_time,end:new Date().getTime()},1800);
}
};
var progressAll= function(condition,callback){
ForumThread.find(conditions).count(function(err,count){
if(err || !count){
return callback(err, true);
}
var tasks = [];
var page_total = Math.ceil((count || 0) / pageSize) + 1;
_.each(_.range(page_total),function(pageNo){
tasks.push(function(cb){
ForumThread.find(conditions).select('_id').skip((pageNo)*pageSize).limit(pageSize)
.exec(function(err, docs) {
if (err) {
return cb(err, null);
}
var chunks=[];
if(docs && docs.length>0){
_.forEach(docs,function(doc){
chunks.push(function(cb2){
handlFn(doc,cb2);
});
});
}
async.parallel(chunks,function(err){
cb(err, true);
});
});
});
});
async.series(tasks,function(err){
callback(err, true);
});
});
ingInterval.start();
};
canStartIf(key,function(err,handling){
if(err){
console.log(err);
return res.json(returnCode.BUSY);
}
if(handling){//正在处理
return res.json(_.assign({
data:false
}, returnCode.SUCCESS));
}
},function(){
if (mid) {
forumUserService.searchMembersByMid(mid, function(err, users) {
if (err) {
callback(err, null);
} else {
//查询对应用户的文章
var user_ids = [];
for(var i in users){
user_ids.push(users[i]._id);
}
conditions.from = {
"$in" : user_ids
};
progressAll(conditions,callback);
}
});
} else if (nickName) {
//查询到用户
forumUserService.searchMembersByNickName(nickName, function(err, users) {
if (err) {
callback(err, null);
} else {
//查询对应用户的文章
var user_ids = [];
for(var i in users){
user_ids.push(users[i]._id);
}
conditions.from = {
"$in" : user_ids
};
progressAll(conditions,callback);
}
});
} else {
progressAll(conditions,callback);
}
res.json(_.assign({data:true}, returnCode.SUCCESS));
});
}
router.get('/threadManagement/threads/list/batch/result',function(req,res,next){
var key = thread_batch_prefix + req.session.user.ent_code;
getBatchStatus(key,function(err,store){
if(err){
return res.json(returnCode.BUSY);
}
res.json(_.assign({
data:store
}, returnCode.SUCCESS));
});
});
//删除查询结果的所有帖子
router.post('/threadManagement/threads/list/deleteAllAsync', function(req, res, next) {
batchAsyncUpdateThread(req,res,function(thread,cb){
forumThreadService.logicDeleteThreadById(thread._id, function(err) {
if (err) {
cb(err, null);
} else {
forumAboutMEService.updateThreadStatus(doc._id, 3);
cb(null, true);
}
});
})
});
//屏蔽查询结果的所有帖子
router.post('/threadManagement/threads/list/closeAllAsync', function(req, res, next) {
batchAsyncUpdateThread(req,res,function(thread,cb){
forumThreadService.updateThreadById(thread._id, {status:0}, function(err) {
if (err) {
cb(err, null);
} else {
forumAboutMEService.updateThreadStatus(thread._id, 0);
cb(null, true);
}
});
})
});
function updateCommentStatus(ent_code,comment,status,cb){
if(!ent_code || !comment){
return cb && cb(null,true);
}
var cid = comment._id;
var tid = comment.thread;
async.parallel([
function(callback) {
//更新评论状态
forumCommentService.updateCommentStatusById(cid, status, function(err) {
callback(err);
});
},
function(callback){
forumAboutMEService.updateCommentLevel1Status(cid, status);
callback();
},
function(callback) {
//是否删除评论
if (status == 2 && tid) {
forumThreadService.updateThreadById(tid, {
$pull:{
comments:cid
},
$inc: {
comment_count: -1
}
}, function(err) {
callback(err);
});
} else {
callback();
}
}
], function(err) {
cb(err);
});
}
function batchAsyncUpdateCommentStatus(status,req,res){
var conditions = getCommentBatchOperateConditions(req);
var ent_code = req.session.user.ent_code;
var tid = req.params.tid;
var key = comment_batch_prefix + ent_code + tid;
var mid = req.body.mid;
var status = status;//status=0屏蔽,status=2删除
var start_time = new Date().getTime();
var pageSize = 100;
var ingInterval = (function(){//定时设置正在进行
var interval = null;
return {
start:function(){
interval = setInterval(function(){
setStatus(key);
},3000);
},
clear:function(){
return interval && clearInterval(interval);
}
}
})();
var callback = function(err) {
ingInterval.clear();
if (err) {//失败
console.log(err);
setStatus(key,{status:-1},1800);
} else {//结束
setStatus(key,{status:1,start:start_time,end:new Date().getTime()},1800);
}
};
var progressAll= function(condition,callback){
ForumComment.find(conditions).count(function(err,count){
if(err || !count){
return callback(err, true);
}
var tasks = [];
var page_total = Math.ceil((count || 0) / pageSize) + 1;
_.each(_.range(page_total),function(pageNo){
tasks.push(function(cb){
ForumComment.find(conditions).select('_id thread').skip((pageNo)*pageSize).limit(pageSize)
.exec(function(err, docs) {
if (err) {
return cb(err, null);
}
var chunks=[];
if(docs && docs.length>0){
_.forEach(docs,function(doc){
chunks.push(function(cb2){
updateCommentStatus(ent_code,doc,status,function(err){
cb2(err,null);
})
});
});
}
async.parallel(chunks,function(err){
cb(err, true);
});
});
});
});
async.series(tasks,function(err){
callback(err, true);
});
});
ingInterval.start();
};
canStartIf(key,function(err,handling){
if(err){
console.log(err);
return res.json(returnCode.BUSY);
}
if(handling){//正在处理
return res.json(_.assign({
data:false
}, returnCode.SUCCESS));
}
},function(){
if (mid) {
forumUserService.searchMembersByMid(mid, function(err, users) {
if (err) {
callback(err, null);
} else {
//查询对应用户
var user_ids = [];
for (var i in users) {
user_ids.push(users[i]._id);
}
conditions.from = {
$in: user_ids
}
progressAll(conditions,callback);
}
});
} else {
progressAll(conditions,callback);
}
res.json(_.assign({data:true}, returnCode.SUCCESS));
});
}
function getCommentBatchOperateConditions(req){
var tid = req.params.tid || null,
floor_start = req.body.floor_start,
floor_end = req.body.floor_end,
content = req.body.content || '';
var ent_code = req.session.user.ent_code;
var conditions = {
ent_code: ent_code,
thread: tid,
level:1,
status:{
$ne:2
}
};
if (content) {
conditions.content = {
$regex: content,
$options: 'i'
};
}
if(floor_start){
conditions.floor = {
$gte:floor_start
};
}
if(floor_end){
conditions.floor = {
$lte:floor_end
};
}
return conditions;
}
router.get('/threadManagement/threads/:tid/comment/list/batch/result',function(req,res,next){
var key = comment_batch_prefix + req.session.user.ent_code + req.params.tid;
getBatchStatus(key,function(err,store){
if(err){
return res.json(returnCode.BUSY);
}
res.json(_.assign({
data:store
}, returnCode.SUCCESS));
});
});
//异步删除帖子的评论(查询结果)
router.post('/threadManagement/threads/:tid/comment/search/deleteAllAsync', function(req, res, next) {
batchAsyncUpdateCommentStatus(2,req,res);
});
//异步屏蔽帖子的评论(查询结果)
router.post('/threadManagement/threads/:tid/comment/search/closeAllAsync', function(req, res, next) {
batchAsyncUpdateCommentStatus(0,req,res);
});
\ No newline at end of file
...@@ -490,8 +490,234 @@ router.get('/sharev2.js', function(req, res, next) { ...@@ -490,8 +490,234 @@ router.get('/sharev2.js', function(req, res, next) {
} }
desc = thread.content?thread.content.replace(/<\/?.+?>/g,"").replace(/&nbsp;/g,"").replace(/\t/g,"").replace(/\n/g,""):''; desc = thread.content?thread.content.replace(/<\/?.+?>/g,"").replace(/&nbsp;/g,"").replace(/\t/g,"").replace(/\n/g,""):'';
// 分享图标:帖子带图片则使用第一张图片,否则使用发帖者的头像 // 分享图标:帖子设置了share_pic就使用share_pic ,否则帖子带图片则使用第一张图片,否则使用发帖者的头像
if(thread.images && thread.images[0]){ if(thread.share_pic){
imgUrl = thread.share_pic;
}else if(thread.images && thread.images[0]){
imgUrl = thread.images[0].urlFileName;
}else{
imgUrl = thread.from.displayIcon || thread.from.icon || thread.info.icon;
}
link += baseParamsUrl;
//获取分享设置
if (thread.share_type!==2) {
//文章默认分享
getWXV2(res, title, desc, link, imgUrl, mid,ent_code,thread._id,type,thread);
}else if(thread.share){
getWXV2(res, thread.share.title, thread.share.description, link, thread.share.icon, mid,ent_code,thread._id,type,thread);
} else {
getWXV2(res, title, desc, link, imgUrl, mid,ent_code,thread._id,type,thread);
}
}
}).fail(function(cont, err) {
console.error(err);
res.json(_.assign({result:false},returnCode.WRONG_PARAM));
});
}else{
res.json(_.assign({result:false},returnCode.WRONG_PARAM));
}
}
});
router.get('/sharev3.js', function(req, res, next) {
//记录分享日志
var _v = new Date().getTime();//用于sharelog 版本号
var share_log = {_v:_v};
req.session.shareLog = {_v:_v};//新的分享对象
console.log('forumshare 1',req.session.shareLog);
var rs = {},
type = req.query.type,
id = req.query.id,
uId= req.session.mobileForumUser.userId,
ent_code = req.session.user.ent_code,
mid = req.session.openUser.mid,
link = '/app/forum/' + ent_code + '/index_v3?pageUrl=';
var title = '',desc = '',imgUrl = '';
var key = ent_code + 'forum_share_index_info_v3';
if(type && type === 'index'){
var expire = 60 * 60;
redis.get(key, function(error, share){
if(share){//有缓存
try{
share = JSON.parse(share);
title=share.title;
desc=share.desc;
link=share.linkUrl;
imgUrl=share.imgUrl;
if(link.indexOf('?') === -1){
link = link + '?uId='+ uId;
}else{
link = link + '&uId='+ uId;
}
}catch(err){
console.log(err);
}
getWXV2(res, title, desc, link, imgUrl, mid,ent_code, id,type,null,true);
}else{
then.parallel([
function(cont){
link = link + 'newindex';
cont();
},function(cont){
request.get({url:config.rest_api+'/v1.0/internal/forum/config/share/byentcode?entCode='+ent_code,body:{},json:true}, function (e, r, result) {
if(result && result.data){
try{
var obj = JSON.parse(result.data);
title = obj.shareTitle || '';
desc = obj.shareDescription || '';
imgUrl = obj.sharePictureUrl || '';
}catch(err){
console.log(err);
}
}
cont();
});
}
]).then(function(cont, datas) {
//放入redis缓存
redis.set(key,JSON.stringify({
title:title,
desc:desc,
linkUrl:link,
imgUrl:imgUrl
}),function(){
redis.expire(key, expire);
});
if(link.indexOf('?') === -1){
link = link + '?uId='+ uId;
}else{
link = link + '&uId='+ uId;
}
getWXV2(res, title, desc, link, imgUrl, mid,ent_code, id,type,null,true);
}).fail(function(cont, err) {
res.json(_.assign({result:false},returnCode.WRONG_PARAM));
});
}
});
}else{
//板块微信接口
if(!type || !id){
console.error('请求参数错误')
res.json(_.assign({result:false},returnCode.WRONG_PARAM));
}else if(type === 'notice'){
Notice.findOne({_id:id},function(err,result){
if (err || !result) {
console.error(err);
res.json(_.assign({result:false},returnCode.BUSY));
}
link = link + 'noticeDetail&infoId=' + (result.plate || '') + '&ent_code=' + ent_code + '&tId=' + result._id +'&uId='+uId;
title = result.title;
desc = result.content?result.content.replace(/<\/?.+?>/g,"").replace(/&nbsp;/g,"").replace(/\t/g,"").replace(/\n/g,""):'';
redis.get(key, function(error, share){
if(share){//有缓存
try{
share = JSON.parse(share);
imgUrl=share.imgUrl;
}catch(err){
console.log(err);
}
getWXV2(res, title, desc, link, imgUrl, mid,ent_code, id,type,null);
}else{
request.get({url:config.rest_api+'/v1.0/internal/forum/config/share/byentcode?entCode='+ent_code,body:{},json:true}, function (e, r, result) {
if(result.data){
try{
var obj = JSON.parse(result.data);
imgUrl = obj.sharePictureUrl || '';
}catch(err){
console.log(err);
}
}
getWXV2(res, title, desc, link, imgUrl, mid,ent_code, id,type,null);
});
}
});
});
}else if (type === 'Info') {
forumInfoService.getInfoById(id, function(err, forumInfo) {
if (err) {
console.error(err);
res.json(_.assign({result:false},returnCode.BUSY));
}else{
//分享记录对象
share_log.type = 2;
share_log.info = id;
//替换分享log内容
if(!req.session.shareLog || !req.session.shareLog._v || _v >= req.session.shareLog._v){
console.log('forumshare 2',share_log);
req.session.shareLog = share_log;
}
if(forumInfo.share_type && forumInfo.share_type == 1){
var shareInfo = forumInfo.share || {};
title = shareInfo.title,desc = shareInfo.description,imgUrl = shareInfo.icon;
}else{
title = forumInfo.name,desc = forumInfo.description,imgUrl = forumInfo.icon;
}
link = link + 'index&infoId='+ id+'&uId='+uId;
getWXV2(res, title, desc, link, imgUrl, mid,ent_code, id,type,null);
}
});
}else if (type === 'Thread') {
then(function(cont) {
//获得文章
forumThreadService.getThreadByIdSimple({_id:id},cont);
}).then(function(cont, thread) {
if(!thread){
//文章不存在
cont('分享文章不存在');
}else{
share_log.type = 3;
share_log.thread = id;
share_log.p_thread = thread.pid;
share_log.info = thread.info._id || thread.info;
share_log.thread_type = thread.pid?4:thread.type;
//替换分享log内容
if(!req.session.shareLog || !req.session.shareLog._v || _v >= req.session.shareLog._v){
console.log('forumshare 3',share_log);
req.session.shareLog = share_log;
}
//组装分享路径与判断分享类型
var baseParamsUrl = '&infoId=' + thread.info._id + '&ent_code=' + ent_code + '&tId=' + thread._id +'&uId='+uId;
var share_type = '';
switch(thread.type){
case 1://文章
link = link + 'detail';
share_type = 'article';
title = thread.title == '发话题'?"":thread.title;
break;
case 2://话题
if(thread.level === 2){
link += 'detail';
share_type = 'article';
title = thread.pid.title;
}else if(thread.level === 1){
link += 'topic';
share_type = 'topic';
title = thread.title;
}
break;
case 3://照片墙
if(thread.level === 2){
link += 'photoDetail';
share_type = 'photo';
title = thread.pid.title;
}else if(thread.level === 1){
link += 'photoWall';
share_type = 'photo';
title = thread.title;
}
break;
}
desc = thread.content?thread.content.replace(/<\/?.+?>/g,"").replace(/&nbsp;/g,"").replace(/\t/g,"").replace(/\n/g,""):'';
// 分享图标:帖子设置了share_pic就使用share_pic ,否则帖子带图片则使用第一张图片,否则使用发帖者的头像
if(thread.share_pic){
imgUrl = thread.share_pic;
}else if(thread.images && thread.images[0]){
imgUrl = thread.images[0].urlFileName; imgUrl = thread.images[0].urlFileName;
}else{ }else{
imgUrl = thread.from.displayIcon || thread.from.icon || thread.info.icon; imgUrl = thread.from.displayIcon || thread.from.icon || thread.info.icon;
......
...@@ -79,7 +79,10 @@ var ForumThreadSchema = new Schema({ ...@@ -79,7 +79,10 @@ var ForumThreadSchema = new Schema({
images: {//文章图片列表 images: {//文章图片列表
type : Array type : Array
}, },
share: { //自定义分享 share_pic:{//帖子分享图标
type: String
},
share: { //自定义分享(已弃用,后台管理没有设置的页面了)
type: Schema.Types.ObjectId, type: Schema.Types.ObjectId,
ref: 'ForumShare' ref: 'ForumShare'
}, },
......
...@@ -230,6 +230,40 @@ exports.getAllCommentWithThreeeChildrenComment = function(conditions, pageNo, pa ...@@ -230,6 +230,40 @@ exports.getAllCommentWithThreeeChildrenComment = function(conditions, pageNo, pa
}); });
}; };
exports.getAllCommentOrderBy = function(conditions,orderBy, pageNo, pageSize, callback) {
countAll(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 sort = {
created:-1
};
if(orderBy){
sort = orderBy;
}
ForumComment.find(conditions,listCommentFields).populate({
path: 'from',
select: 'uid mid nickName icon exp honorTitles'
}).populate('to','uid mid nickName icon exp').limit(limit).skip(skip).sort(sort).exec(function(err, docs) {
if (err) {
console.error(err);
callback(err, null);
} else {
var obj = {};
obj.total = count;
obj.pageNo = pageNo;
obj.pageSize = pageSize;
obj.items = docs;
callback(null, obj);
}
});
}
});
};
//评论 //评论
function populateComment(doc, callback) { function populateComment(doc, callback) {
if (doc && doc.comments.length > 0) { if (doc && doc.comments.length > 0) {
...@@ -483,6 +517,54 @@ exports.getCommentListByMid = function(mid,conditions, pageNo, pageSize, callbac ...@@ -483,6 +517,54 @@ exports.getCommentListByMid = function(mid,conditions, pageNo, pageSize, callbac
}; };
exports.getCommentListByMidOrderBy = function(mid,conditions,orderBy, pageNo, pageSize, callback) {
//查询到用户
forumUserService.searchMembersByMid(mid, function(err, users) {
if (err) {
console.error(err);
callback(err, null);
} else {
//查询对应用户的文章
var user_ids = [];
for(var i in users){
user_ids.push(users[i]._id);
}
conditions.from = {
$in:user_ids
}
countAll(conditions, function(err, count) {
if (err) {
callback(err);
} else {
var skip = (pageNo - 1) * pageSize;
var limit = count - skip > pageSize ? pageSize : (count - skip);
var sort = {
created:-1
};
if(orderBy){
sort = orderBy;
}
ForumComment.find(conditions).populate('from').populate('to').limit(limit).skip(skip).sort(sort).exec(function(err, docs) {
if (err) {
console.error(err);
callback(err, null);
} else {
var obj = {};
obj.total = count;
obj.pageNo = pageNo;
obj.pageSize = pageSize;
obj.items = docs;
callback(null, obj);
}
});
}
});
}
});
};
exports.getCommentParent = function(cid, callback) { exports.getCommentParent = function(cid, callback) {
ForumComment.findOne({ ForumComment.findOne({
comments: { comments: {
......
...@@ -6,6 +6,7 @@ var ForumShare = mongoose.model('ForumShare'); ...@@ -6,6 +6,7 @@ var ForumShare = mongoose.model('ForumShare');
var forumUserService = require('./forumUserService'); var forumUserService = require('./forumUserService');
var forumCommentService = require('./forumCommentService'); var forumCommentService = require('./forumCommentService');
var forumAboutMEService = require('./forumAboutMEService');
var async = require('async'); var async = require('async');
var then = require('thenjs'); var then = require('thenjs');
...@@ -329,21 +330,29 @@ exports.getThreadById = function(tid, callback) { ...@@ -329,21 +330,29 @@ exports.getThreadById = function(tid, callback) {
}, },
function(cb) { function(cb) {
//获取话题子文章列表 //获取话题子文章列表
if (doc.type !== 2) { var conditions ={}
cb(null, null); if(doc.type == 2){
return; conditions = {
}
var conditions = {
pid: tid, pid: tid,
type: 2, type: 2,
status:1 status:1
}; };
getAllThreadByFidHelp(conditions, 1, 10, null, function(err, threads) { }else if(doc.type == 3){
conditions = {
pid: tid,
type: 3,
status:1,
images: {$exists: true, $not: {$size: 0}}
};
}else{
cb(null, null);
return;
}
getAllThreadByFidHelp(conditions, 1, 999999, null, function(err, threads) {
if (err) { if (err) {
console.error(err); console.error(err);
cb(err, null); cb(err, null);
} else { } else {
// console.log(threads);
cb(null, threads); cb(null, threads);
} }
}); });
...@@ -1309,6 +1318,32 @@ exports.updateThreadRaiseCount = function(threadId, callback) { ...@@ -1309,6 +1318,32 @@ exports.updateThreadRaiseCount = function(threadId, callback) {
}); });
}; };
//批量屏蔽帖子
exports.batchClose = function(ent_code,ids, callback) {
var status_closed = 0;
update({ent_code:ent_code,_id: {$in:ids}}, {status:status_closed}, { multi: true }, 'updateRedisRecommentThreads',function(err, result) {
callback(err, !!!err);
if(!err){
_.forEach(ids,function(id){
forumAboutMEService.updateThreadStatus(id, status_closed);
});
}
});
};
//批量删除
exports.batchLogicDelete = function(ent_code,ids, callback) {
var status_deleted = 3;
update({ent_code:ent_code,_id: {$in:ids}}, {status:status_deleted}, { multi: true }, 'updateRedisRecommentThreads',function(err, result) {
callback(err, !!!err);
if(!err){
_.forEach(ids,function(id){
forumAboutMEService.updateThreadStatus(id, status_deleted);
});
}
});
};
//减少文章点赞数 //减少文章点赞数
exports.updateThreadRaiseCountDec = function(threadId, callback) { exports.updateThreadRaiseCountDec = function(threadId, callback) {
// ForumThread.update({ // ForumThread.update({
......
...@@ -3,6 +3,15 @@ ...@@ -3,6 +3,15 @@
var _ = require('lodash'); var _ = require('lodash');
var loadUserLevel = require('./loadUserLevel'); var loadUserLevel = require('./loadUserLevel');
var async=require('async'); var async=require('async');
var request = require('request');
var env = process.env.NODE_ENV;
var API_ADDRESS = 'http://rest.wxpai.cn';
if (env == 'sandbox') {
API_ADDRESS = 'http://rest.wxpai.cn';
} else if (env == 'production') {
API_ADDRESS = 'https://rest.wxpai.cn';
}
exports.loadLevel=function(ent_code,items,callback){ exports.loadLevel=function(ent_code,items,callback){
var openIds = []; var openIds = [];
...@@ -27,7 +36,7 @@ exports.loadLevel=function(ent_code,items,callback){ ...@@ -27,7 +36,7 @@ exports.loadLevel=function(ent_code,items,callback){
var tasks = []; var tasks = [];
_.forEach(openIds, function(open_id) { _.forEach(openIds, function(open_id) {
tasks.push(function(cont){ tasks.push(function(cont){
loadUserLevel.loadLevelFromAPI(ent_code,open_id,cont); loadLevelFromAPI(ent_code,open_id,cont);
}); });
}); });
async.parallel(tasks,function(err,results){ async.parallel(tasks,function(err,results){
...@@ -112,3 +121,20 @@ exports.loadLevelByUser=function(ent_code,items,callback){ ...@@ -112,3 +121,20 @@ exports.loadLevelByUser=function(ent_code,items,callback){
return callback && callback(); return callback && callback();
}); });
}; };
function loadLevelFromAPI(ent_code,open_id,callback){
if(!ent_code || !open_id){
return callback && callback(null,null);
}
var url = API_ADDRESS + '/v1.0/internal/member/exp/byopenid?openId='+open_id+'&entCode='+ent_code;
request.get({
url: url,
json: {}
}, function(e, r, body) {
if (e) {
console.log(e)
}
return callback && callback(null,(body && body.data) || null);
});
};
\ No newline at end of file
...@@ -43,7 +43,8 @@ var config = { ...@@ -43,7 +43,8 @@ var config = {
service:'http://sandbox.wxpai.cn', service:'http://sandbox.wxpai.cn',
port: 3011, port: 3011,
mongodb: { mongodb: {
uri: 'mongodb://114.215.206.32:27018/pisns-forum-sandbox', //uri: 'mongodb://114.215.206.32:27018/pisns-forum-sandbox',
uri: 'mongodb://10.161.234.251:27018/pisns-forum-sandbox',
username: '', username: '',
password: '', password: '',
poolSize: 5 poolSize: 5
......
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
"glob": "~4.3.1", "glob": "~4.3.1",
"gridfs-stream": "^1.1.0", "gridfs-stream": "^1.1.0",
"hiredis": "^0.4.0", "hiredis": "^0.4.0",
"lodash": "~2.4.1", "lodash": "~3.10.1",
"method-override": "~2.3.0", "method-override": "~2.3.0",
"moment": "^2.10.6", "moment": "^2.10.6",
"mongoose": "^4.1.0", "mongoose": "^4.1.0",
......
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