Commit a5d369b4 authored by 张淼's avatar 张淼

1

parent da9fb3fb
...@@ -30,22 +30,10 @@ router.post('/thread/create', function(req, res, next) { ...@@ -30,22 +30,10 @@ router.post('/thread/create', function(req, res, next) {
var rs = {}; var rs = {};
req.body.ent_code=req.session.user.ent_code; req.body.ent_code=req.session.user.ent_code;
// var uid=req.session.user.id; var uid=req.session.user.id;
var uid='12345'; // var uid='12345';
if(userUtil.getUserSession(req)){ async.waterfall([
req.body.from=userUtil.getUserSession._id;
forumThreadService.createThread(req.body,function(err,entity){
if (err) {
console.error(err);
res.json(returnCode.BUSY);
} else {
rs.data = {'id':entity._id};
res.json(_.assign(rs, returnCode.SUCCESS));
}
});
}else{
async.waterfall([
function(callback){ function(callback){
forumUserService.getUserByUid(uid,callback); forumUserService.getUserByUid(uid,callback);
} }
...@@ -76,8 +64,6 @@ router.post('/thread/create', function(req, res, next) { ...@@ -76,8 +64,6 @@ router.post('/thread/create', function(req, res, next) {
if(err){ if(err){
res.json(returnCode.BUSY); res.json(returnCode.BUSY);
}else{ }else{
//put forumUser to session
userUtil.setUserSession(req,doc);
req.body.from=doc._id; req.body.from=doc._id;
forumThreadService.createThread(req.body,function(err,entity){ forumThreadService.createThread(req.body,function(err,entity){
...@@ -94,7 +80,6 @@ router.post('/thread/create', function(req, res, next) { ...@@ -94,7 +80,6 @@ router.post('/thread/create', function(req, res, next) {
} }
} }
}); });
}
}); });
//获取目标论坛文章 //获取目标论坛文章
...@@ -169,8 +154,8 @@ router.post('/thread/:tid/delete', function(req, res, next) { ...@@ -169,8 +154,8 @@ router.post('/thread/:tid/delete', function(req, res, next) {
//文章置顶 //文章置顶
router.post('/thread/:tid/:fid/top', function(req, res, next) { router.post('/thread/:tid/:fid/top', function(req, res, next) {
var tid=req.params.tid; var tid=req.params.tid;//文章ID
var fid=req.params.fid; var fid=req.params.fid;//板块ID
if(tid && fid){ if(tid && fid){
forumThreadService.updateTopByThreadId(fid,tid,function(err,thread){ forumThreadService.updateTopByThreadId(fid,tid,function(err,thread){
if(err){ if(err){
...@@ -184,6 +169,23 @@ router.post('/thread/:tid/:fid/top', function(req, res, next) { ...@@ -184,6 +169,23 @@ router.post('/thread/:tid/:fid/top', function(req, res, next) {
} }
}); });
//文章取消置顶
router.post('/thread/:tid/:fid/unTop', function(req, res, next) {
var tid=req.params.tid;//文章ID
var fid=req.params.fid;//板块ID
if(tid && fid){
forumThreadService.updateUnTopByThreadId(fid,tid,function(err,thread){
if(err){
res.json(returnCode.BUSY);
}else{
res.json(returnCode.SUCCESS);
}
});
}else{
res.json(returnCode.WRONG_PARAM);
}
});
/** /**
* [description] * [description]
* @param {[type]} * @param {[type]}
...@@ -198,6 +200,7 @@ router.get('/threads/list', function(req, res, next) { ...@@ -198,6 +200,7 @@ router.get('/threads/list', function(req, res, next) {
var tagId=req.query.tagId; var tagId=req.query.tagId;
var conditions={ var conditions={
ent_code:req.session.user.ent_code, ent_code:req.session.user.ent_code,
level:1
}; };
if(infoId){ if(infoId){
conditions.info=infoId; conditions.info=infoId;
...@@ -205,7 +208,7 @@ router.get('/threads/list', function(req, res, next) { ...@@ -205,7 +208,7 @@ router.get('/threads/list', function(req, res, next) {
if(tagId){ if(tagId){
conditions.tag={$in:[tagId]}; conditions.tag={$in:[tagId]};
} }
forumThreadService.getAllThreadByFid(conditions,pageNo,pageSize,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);
......
...@@ -172,3 +172,214 @@ router.get('/info/:fid/threads', function(req, res, next) { ...@@ -172,3 +172,214 @@ router.get('/info/:fid/threads', function(req, res, next) {
res.json(returnCode.WRONG_PARAM); res.json(returnCode.WRONG_PARAM);
} }
}); });
//获取论坛最热文章列表
router.get('/info/:fid/hotThreads', function(req, res, next) {
var fid=req.params.fid || null;
var pageNo = req.query.pageNo || 1;
var pageSize = req.query.pageSize || 10;
var conditions={
ent_code:req.session.user.ent_code,
level:1,
info:fid
};
if(fid){
async.waterfall([
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);
}
}
});
}
],function(err,result){
if(err){
console.error(err);
res.json(returnCode.BUSY);
}else{
if(result){
conditions._id={$nin:result.thread};
}
forumThreadService.getAllThreadByFid(conditions,pageNo,pageSize,'-comment_count -praise_count',function(err,results){
if(err){
console.error(err);
res.json(returnCode.BUSY);
}else{
res.json(_.assign(results, returnCode.SUCCESS));
}
});
}
});
}else{
res.json(returnCode.WRONG_PARAM);
}
});
//获取论坛晒图文章列表
router.get('/info/:fid/photoThreads', function(req, res, next) {
var fid=req.params.fid || null;
var pageNo = req.query.pageNo || 1;
var pageSize = req.query.pageSize || 10;
var conditions={
ent_code:req.session.user.ent_code,
level:1,
info:fid,
type:3
};
if(fid){
async.waterfall([
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);
}
}
});
}
],function(err,result){
if(err){
console.error(err);
res.json(returnCode.BUSY);
}else{
if(result){
conditions._id={$nin:result.thread};
}
forumThreadService.getAllThreadByFid(conditions,pageNo,pageSize,null,function(err,results){
if(err){
console.error(err);
res.json(returnCode.BUSY);
}else{
res.json(_.assign(results, returnCode.SUCCESS));
}
});
}
});
}else{
res.json(returnCode.WRONG_PARAM);
}
});
//获取我的文章列表
router.get('/info/:fid/myThreads', function(req, res, next) {
var fid=req.params.fid || null;
var pageNo = req.query.pageNo || 1;
var pageSize = req.query.pageSize || 10;
var conditions={
ent_code:req.session.user.ent_code,
from:user.getMobileUser(req),
info:fid
};
if(fid){
async.waterfall([
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);
}
}
});
}
],function(err,result){
if(err){
console.error(err);
res.json(returnCode.BUSY);
}else{
if(result){
conditions._id={$nin:result.thread};
}
//获取子话题数据
forumThreadService.getAllThreadByFid(conditions,pageNo,pageSize,'-praise_count',function(err,results){
if(err){
console.error(err);
res.json(returnCode.BUSY);
}else{
res.json(_.assign(results, returnCode.SUCCESS));
}
});
}
});
}else{
res.json(returnCode.WRONG_PARAM);
}
});
//搜索文章列表
router.get('/info/:fid/serachThreads', function(req, res, next) {
var fid=req.params.fid || null;
var pageNo = req.query.pageNo || 1;
var pageSize = req.query.pageSize || 10;
var content=req.query.content;
var conditions={
ent_code:req.session.user.ent_code,
info:fid
};
if(content){
conditions.content={
$regex: content,
$options: 'i'
};
}
if(fid){
async.waterfall([
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);
}
}
});
}
],function(err,result){
if(err){
console.error(err);
res.json(returnCode.BUSY);
}else{
if(result){
conditions._id={$nin:result.thread};
}
//获取子话题数据
forumThreadService.getAllThreadByFid(conditions,pageNo,pageSize,'-praise_count',function(err,results){
if(err){
console.error(err);
res.json(returnCode.BUSY);
}else{
res.json(_.assign(results, returnCode.SUCCESS));
}
});
}
});
}else{
res.json(returnCode.WRONG_PARAM);
}
});
...@@ -84,7 +84,7 @@ router.get('/tag/:tid/threads', function(req, res, next) { ...@@ -84,7 +84,7 @@ router.get('/tag/:tid/threads', function(req, res, next) {
if(result){ if(result){
conditions._id={$nin:result.thread}; conditions._id={$nin:result.thread};
} }
forumThreadService.getAllThreadByFid(conditions,pageNo,pageSize,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);
......
...@@ -24,13 +24,19 @@ module.exports = function(app) { ...@@ -24,13 +24,19 @@ module.exports = function(app) {
function create(req,callback){ function create(req,callback){
req.body.ent_code=req.session.user.ent_code; var entity=req.body;
req.body.from=user.getMobileUser(req);
if(entity.pid==''){
entity.pid=null;
}
entity.ent_code=req.session.user.ent_code;
entity.from=user.getMobileUser(req);
//-----------处理前端上传图片开始 //-----------处理前端上传图片开始
//格式为['http://imageurl','http://imageurl'], //格式为['http://imageurl','http://imageurl'],
//转换为[{urlFileName:'http://imageUrL'},{urlFileName:'http://imageUrL'}] //转换为[{urlFileName:'http://imageUrL'},{urlFileName:'http://imageUrL'}]
var images=req.body.images; var images=entity.images;
if(images){ if(images){
var array=[]; var array=[];
for(var i=0;i<images.length;i=i+1){ for(var i=0;i<images.length;i=i+1){
...@@ -38,11 +44,11 @@ function create(req,callback){ ...@@ -38,11 +44,11 @@ function create(req,callback){
urlFileName:images[i] urlFileName:images[i]
}); });
} }
req.body.images=array; entity.images=array;
} }
//-----------处理前端上传图片结束 //-----------处理前端上传图片结束
forumThreadService.createThread(req.body,function(err,entity){ forumThreadService.createThread(entity,function(err,entity){
if (err) { if (err) {
console.error(err); console.error(err);
callback(err,null); callback(err,null);
...@@ -590,9 +596,10 @@ router.get('/thread/:tid/comment/list', function(req, res, next) { ...@@ -590,9 +596,10 @@ router.get('/thread/:tid/comment/list', function(req, res, next) {
var pageSize = req.query.pageSize || 10; var pageSize = req.query.pageSize || 10;
var conditions={ var conditions={
thread:tid, ent_code:req.session.user.ent_code,
level:'1' thread:tid,
}; level:'1'
};
if(tid){ if(tid){
//获取最新5条评论 //获取最新5条评论
...@@ -748,20 +755,45 @@ router.get('/thread/:tid/topics', function(req, res, next) { ...@@ -748,20 +755,45 @@ router.get('/thread/:tid/topics', function(req, res, next) {
var pageSize = req.query.pageSize || 10; var pageSize = req.query.pageSize || 10;
var conditions={ var conditions={
ent_code:req.session.user.ent_code,
pid:tid, pid:tid,
type:3 type:2
}; };
if(tid){ if(tid){
//获取最新5条评论 async.waterfall([
forumThreadService.getAllThreadByFid(conditions,pageNo,pageSize,null,function(err,results){ function(callback){
if(err){ forumUserThreadControlService.getUserThreadControlById(user.getMobileUser(req),function(err,doc){
console.log(err); if(err){
res.json(returnCode.BUSY); callback(err,null);
}else{ }else{
res.json(_.assign(results, returnCode.SUCCESS)); if(doc){
} callback(null,doc);
}); }else{
callback(null,null);
}
}
});
}
],function(err,result){
if(err){
console.error(err);
res.json(returnCode.BUSY);
}else{
if(result){
conditions._id={$nin:result.thread};
}
//获取子话题数据
forumThreadService.getAllThreadByFid(conditions,pageNo,pageSize,null,function(err,results){
if(err){
console.error(err);
res.json(returnCode.BUSY);
}else{
res.json(_.assign(results, returnCode.SUCCESS));
}
});
}
});
}else{ }else{
res.json(returnCode.WRONG_PARAM); res.json(returnCode.WRONG_PARAM);
} }
...@@ -774,20 +806,45 @@ router.get('/thread/:tid/latestPhotos', function(req, res, next) { ...@@ -774,20 +806,45 @@ router.get('/thread/:tid/latestPhotos', function(req, res, next) {
var pageSize = req.query.pageSize || 10; var pageSize = req.query.pageSize || 10;
var conditions={ var conditions={
ent_code:req.session.user.ent_code,
pid:tid, pid:tid,
type:3 type:3
}; };
if(tid){ if(tid){
//获取最新5条评论 async.waterfall([
forumThreadService.getAllThreadByFid(conditions,pageNo,pageSize,'-created',function(err,results){ function(callback){
if(err){ forumUserThreadControlService.getUserThreadControlById(user.getMobileUser(req),function(err,doc){
console.log(err); if(err){
res.json(returnCode.BUSY); callback(err,null);
}else{ }else{
res.json(_.assign(results, returnCode.SUCCESS)); if(doc){
} callback(null,doc);
}); }else{
callback(null,null);
}
}
});
}
],function(err,result){
if(err){
console.error(err);
res.json(returnCode.BUSY);
}else{
if(result){
conditions._id={$nin:result.thread};
}
//获取子话题数据
forumThreadService.getAllThreadByFid(conditions,pageNo,pageSize,'-created',function(err,results){
if(err){
console.error(err);
res.json(returnCode.BUSY);
}else{
res.json(_.assign(results, returnCode.SUCCESS));
}
});
}
});
}else{ }else{
res.json(returnCode.WRONG_PARAM); res.json(returnCode.WRONG_PARAM);
} }
...@@ -800,22 +857,49 @@ router.get('/thread/:tid/hotPhotos', function(req, res, next) { ...@@ -800,22 +857,49 @@ router.get('/thread/:tid/hotPhotos', function(req, res, next) {
var pageSize = req.query.pageSize || 10; var pageSize = req.query.pageSize || 10;
var conditions={ var conditions={
ent_code:req.session.user.ent_code,
pid:tid, pid:tid,
type:3 type:3
}; };
if(tid){ if(tid){
//获取最新5条评论 async.waterfall([
forumThreadService.getAllThreadByFid(conditions,pageNo,pageSize,'-praise_count',function(err,results){ function(callback){
if(err){ forumUserThreadControlService.getUserThreadControlById(user.getMobileUser(req),function(err,doc){
console.log(err); if(err){
res.json(returnCode.BUSY); callback(err,null);
}else{ }else{
res.json(_.assign(results, returnCode.SUCCESS)); if(doc){
} callback(null,doc);
}); }else{
callback(null,null);
}
}
});
}
],function(err,result){
if(err){
console.error(err);
res.json(returnCode.BUSY);
}else{
if(result){
conditions._id={$nin:result.thread};
}
//获取子话题数据
forumThreadService.getAllThreadByFid(conditions,pageNo,pageSize,'-praise_count',function(err,results){
if(err){
console.error(err);
res.json(returnCode.BUSY);
}else{
res.json(_.assign(results, returnCode.SUCCESS));
}
});
}
});
}else{ }else{
res.json(returnCode.WRONG_PARAM); res.json(returnCode.WRONG_PARAM);
} }
}); });
'use strict';
var express = require('express'),
router = express.Router(),
returnCode = require('../../utils/returnCode'),
_ = require('lodash');
var mongoose = require('mongoose');
var forumUserService=require('../../service/forumUserService');
var forumThreadService=require('../../service/forumThreadService');
var async=require('async');
var user=require('../../utils/user');
module.exports = function(app) {
app.use('/v1/forum', router);
};
//获取用户信息
router.get('/user/get', function(req, res, next) {
var rs = {};
async.parallel([
function(cb){
forumUserService.getUserById(user.getMobileUser(),function(err,entity){
if (err) {
cb(err,null);
} else {
cb(null,entity);
}
});
},
function(cb){
var conditions={
ent_code:req.session.user.ent_code,
from:user.getMobileUser()
};
forumThreadService.getAllCountByFid(conditions,function(err,count){
if (err) {
cb(err,null);
} else {
cb(null,count);
}
});
}
],function(err,results){
if (err) {
console.error(err);
res.json(returnCode.BUSY);
} else {
rs.data = results[0] || {};
rs.myThreadCount=results[1] || 0;
res.json(_.assign(rs, returnCode.SUCCESS));
}
});
});
\ No newline at end of file
...@@ -53,6 +53,9 @@ var ForumThreadSchema = new Schema({ ...@@ -53,6 +53,9 @@ var ForumThreadSchema = new Schema({
require: true, require: true,
default: 0 default: 0
}, },
topTime:{//置顶时间
type: Date
},
tag: [{type: Schema.Types.ObjectId, ref: 'ForumTag'}],//话题归属标签, tag: [{type: Schema.Types.ObjectId, ref: 'ForumTag'}],//话题归属标签,
comments: [{type: Schema.Types.ObjectId, ref: 'ForumComment'}], //评论内容列表 comments: [{type: Schema.Types.ObjectId, ref: 'ForumComment'}], //评论内容列表
images: {//文章图片列表 images: {//文章图片列表
......
...@@ -12,6 +12,7 @@ var ForumUserSchema = new Schema({ ...@@ -12,6 +12,7 @@ var ForumUserSchema = new Schema({
uid: { //用户ID,这里直接存放用户的openId uid: { //用户ID,这里直接存放用户的openId
type: String, type: String,
index: true, index: true,
unique:true,
require: true require: true
}, },
nickName: { //用户昵称,这里直接存放用户的nickname nickName: { //用户昵称,这里直接存放用户的nickname
......
...@@ -169,7 +169,7 @@ function populateComment(doc, callback){ ...@@ -169,7 +169,7 @@ function populateComment(doc, callback){
} }
}); });
}else{ }else{
callback(null,null); callback(null,doc);
} }
} }
}); });
...@@ -180,13 +180,16 @@ function populateComment(doc, callback){ ...@@ -180,13 +180,16 @@ function populateComment(doc, callback){
console.log(err); console.log(err);
callback(null, null); callback(null, null);
} else { } else {
// console.log(results); if(results && results.length>0){
callback(null, results); callback(null,results[0]);
}else{
callback(null,{});
}
// callback(null, results);
} }
}); });
} else{ } else{
callback(null, null); callback(null, doc);
} }
} }
...@@ -237,6 +240,39 @@ exports.getAllCountByFid=function(conditions,callback){ ...@@ -237,6 +240,39 @@ exports.getAllCountByFid=function(conditions,callback){
countAllByFid(conditions,callback); countAllByFid(conditions,callback);
} }
//获取话题、照片墙子文章数据
function getSubThreads(doc,sort,callback){
var conditions={
pid:doc._id
};
countAllByFid(conditions,function(err,count){
if(err){
console.error(err);
callback(err,null);
}else{
var sortBy='-top -created';
if(sort){
sortBy=sort;
}
ForumThread.find(conditions).populate('from').sort(sortBy).exec(function(err, docs) {
if (err) {
console.error(err);
callback(err,null);
} else {
var obj={};
obj.total=count;
obj.items=docs;
var newDoc=doc.toObject();
newDoc.subThreads=obj;
callback(null,newDoc);
}
});
}
});
}
function getAllThreadByFidHelp(conditions,pageNo,pageSize,sort,callback){ function getAllThreadByFidHelp(conditions,pageNo,pageSize,sort,callback){
countAllByFid(conditions,function(err,count){ countAllByFid(conditions,function(err,count){
if(err){ if(err){
...@@ -245,7 +281,7 @@ function getAllThreadByFidHelp(conditions,pageNo,pageSize,sort,callback){ ...@@ -245,7 +281,7 @@ function getAllThreadByFidHelp(conditions,pageNo,pageSize,sort,callback){
}else{ }else{
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);
var sortBy='-top -created'; var sortBy='-topTime -created';
if(sort){ if(sort){
sortBy=sort; sortBy=sort;
} }
...@@ -262,16 +298,25 @@ function getAllThreadByFidHelp(conditions,pageNo,pageSize,sort,callback){ ...@@ -262,16 +298,25 @@ function getAllThreadByFidHelp(conditions,pageNo,pageSize,sort,callback){
if (docs && docs.length > 0) { if (docs && docs.length > 0) {
var asyncTasks = []; var asyncTasks = [];
docs.forEach(function(doc){ docs.forEach(function(doc){
asyncTasks.push(function(callback) { if(doc.type===1){
populateComment(doc, callback); asyncTasks.push(function(callback) {
}); populateComment(doc, callback);
});
}else{
asyncTasks.push(function(callback) {
getSubThreads(doc,null, callback);
});
}
}); });
async.parallel(asyncTasks, function(err, results) { async.parallel(asyncTasks, function(err, results) {
if (err) { if (err) {
console.error(err); console.error(err);
callback(null, null); callback(null, null);
} else { } else {
obj.items=results;
callback(null,obj); callback(null,obj);
} }
}); });
...@@ -292,17 +337,35 @@ exports.getAllThreadByFid= function(conditions,pageNo,pageSize,sort,callback) { ...@@ -292,17 +337,35 @@ exports.getAllThreadByFid= function(conditions,pageNo,pageSize,sort,callback) {
//根据板块ID更新板块下的top为0,并把当前文章的top设为1(置顶) //根据板块ID更新板块下的top为0,并把当前文章的top设为1(置顶)
exports.updateTopByThreadId=function(infoId,threadId,callback){ exports.updateTopByThreadId=function(infoId,threadId,callback){
ForumThread.update({info:infoId}, {top:0},{multi:true}, function(err, doc) { // ForumThread.update({info:infoId}, {top:0},{multi:true}, function(err, doc) {
// if(err){
// callback(err,null);
// }else{
// ForumThread.findOneAndUpdate({_id:threadId}, {top:1}, function(err, doc) {
// if(err){
// callback(err,null);
// }else{
// callback(null,null);
// }
// });
// }
// });
ForumThread.findOneAndUpdate({_id:threadId}, {top:1,topTime:new Date()}, function(err, doc) {
if(err){ if(err){
callback(err,null); callback(err,null);
}else{ }else{
ForumThread.findOneAndUpdate({_id:threadId}, {top:1}, function(err, doc) { callback(null,null);
if(err){ }
callback(err,null); });
}else{ };
callback(null,null);
} //根据板块ID更新板块下的top为0,并把当前文章的top设为1(置顶)
}); exports.updateUnTopByThreadId=function(infoId,threadId,callback){
ForumThread.findOneAndUpdate({_id:threadId}, {top:0,topTime:null}, function(err, doc) {
if(err){
callback(err,null);
}else{
callback(null,null);
} }
}); });
}; };
......
...@@ -29,3 +29,18 @@ exports.getUserByUid=function(uid,callback){ ...@@ -29,3 +29,18 @@ exports.getUserByUid=function(uid,callback){
} }
}); });
}; };
//根据id获取用户
exports.getUserById=function(id,callback){
ForumUser.findOne({_id:id}).exec(function(err,result){
if(err){
callback(err,null);
}else{
if(result ){
callback(null,result);
}else{
callback(null,null);
}
}
});
};
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