Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
pisns-forum-api
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
scrmGroup
pisns-forum-api
Commits
7a3eb34a
Commit
7a3eb34a
authored
Jul 23, 2015
by
陈志良
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1
parent
b50a286f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
195 additions
and
167 deletions
+195
-167
forumThread.js
app/controllers/admin/forumThread.js
+195
-167
No files found.
app/controllers/admin/forumThread.js
View file @
7a3eb34a
...
...
@@ -25,6 +25,20 @@ module.exports = function(app) {
//---------------------文章表单操作---------------------------------
var
getClientIP
=
function
(
req
)
{
var
ipAddress
;
var
headers
=
req
.
headers
;
var
forwardedIpsStr
=
headers
[
'x-real-ip'
]
||
headers
[
'x-forwarded-for'
];
if
(
forwardedIpsStr
)
{
ipAddress
=
forwardedIpsStr
;
}
else
{
ipAddress
=
null
;
}
if
(
!
ipAddress
)
{
ipAddress
=
req
.
connection
.
remoteAddress
;
}
return
ipAddress
;
};
//新增论坛文章
router
.
post
(
'/thread/create'
,
function
(
req
,
res
,
next
)
{
...
...
@@ -162,7 +176,6 @@ router.post('/thread/:tid/update', function(req, res, next) {
}
});
//删除文章
router
.
post
(
'/thread/:tid/delete'
,
function
(
req
,
res
,
next
)
{
var
tid
=
req
.
params
.
tid
;
...
...
@@ -212,6 +225,7 @@ router.post('/thread/:tid/:fid/unTop', function(req, res, next) {
res
.
json
(
returnCode
.
WRONG_PARAM
);
}
});
//文章推荐
router
.
post
(
'/thread/:tid/:fid/recommend'
,
function
(
req
,
res
,
next
)
{
var
tid
=
req
.
params
.
tid
,
...
...
@@ -250,13 +264,8 @@ router.post('/thread/:tid/:fid/unRecommend', function(req, res, next) {
res
.
json
(
returnCode
.
WRONG_PARAM
);
}
});
/**
* [description]
* @param {[type]}
* @param {[type]}
* @param {[type]}
* @return {[type]}
*/
//文章列表
router
.
get
(
'/threads/list'
,
function
(
req
,
res
,
next
)
{
var
pageNo
=
req
.
query
.
pageNo
||
1
;
var
pageSize
=
req
.
query
.
pageSize
||
10
;
...
...
@@ -324,24 +333,88 @@ router.get('/thread/:tid/comment/list', function(req, res, next) {
var
tid
=
req
.
params
.
tid
||
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
,
thread
:
tid
,
level
:
'1'
,
status
:
{
$ne
:
2
}
//过滤被删除的数据,status 为 2 表示删除
//
level: '1',
//
status: {
//
$ne: 2
//
} //过滤被删除的数据,status 为 2 表示删除
};
if
(
content
){
conditions
.
content
=
{
$regex
:
content
,
$options
:
'i'
};
}
if
(
tid
)
{
//获取最新5条评论
forumCommentService
.
get
AllCommen
t
(
conditions
,
pageNo
,
pageSize
,
function
(
err
,
results
)
{
forumCommentService
.
get
CommentLis
t
(
conditions
,
pageNo
,
pageSize
,
function
(
err
,
results
)
{
if
(
err
)
{
console
.
log
(
err
);
res
.
json
(
returnCode
.
BUSY
);
}
else
{
res
.
json
(
_
.
assign
(
results
,
returnCode
.
SUCCESS
));
var
asyncTasks
=
[];
_
.
forEach
(
results
.
items
,
function
(
e
){
asyncTasks
.
push
(
function
(
cb
){
if
(
e
.
level
==
1
){
cb
(
null
,
e
);
}
else
{
forumCommentService
.
getCommentParent
(
e
.
_id
,
function
(
err
,
p_comment
){
var
comment
=
e
.
toObject
();
comment
.
floor
=
p_comment
.
floor
;
cb
(
null
,
comment
);
});
}
});
});
async
.
parallel
(
asyncTasks
,
function
(
err
,
items
){
results
.
items
=
items
;
res
.
json
(
_
.
assign
(
results
,
returnCode
.
SUCCESS
));
});
}
});
}
else
{
res
.
json
(
returnCode
.
WRONG_PARAM
);
}
});
//评论列表(根据楼层查询)
router
.
get
(
'/thread/:tid/comment/list/:floor'
,
function
(
req
,
res
,
next
)
{
var
tid
=
req
.
params
.
tid
||
null
,
floor
=
req
.
params
.
floor
||
null
,
content
=
req
.
query
.
content
||
''
;
var
conditions
=
{
ent_code
:
req
.
session
.
user
.
ent_code
,
thread
:
tid
,
level
:
'1'
,
floor
:
floor
};
if
(
tid
&&
floor
)
{
forumCommentService
.
getAllComment
(
conditions
,
1
,
1
,
function
(
err
,
results
)
{
if
(
err
)
{
console
.
log
(
err
);
res
.
json
(
returnCode
.
BUSY
);
}
else
{
var
asyncTasks
=
[];
if
(
results
.
items
&&
results
.
items
[
0
]
&&
results
.
items
[
0
].
comments
.
length
>
0
){
var
items
=
[
results
.
items
[
0
]];
_
.
forEach
(
results
.
items
[
0
].
comments
,
function
(
e
){
var
subComment
=
e
.
toObject
();
subComment
.
floor
=
results
.
items
[
0
].
floor
;
if
(
content
){
if
(
subComment
.
content
.
indexOf
(
content
)
!=
-
1
){
items
.
push
(
subComment
);
}
}
else
{
items
.
push
(
subComment
);
}
});
results
.
items
=
items
;
results
.
total
=
items
.
length
;
res
.
json
(
_
.
assign
(
results
,
returnCode
.
SUCCESS
));
}
else
{
res
.
json
(
_
.
assign
(
results
,
returnCode
.
SUCCESS
));
}
}
});
}
else
{
...
...
@@ -353,82 +426,63 @@ router.get('/thread/:tid/comment/list', function(req, res, next) {
router
.
post
(
'/thread/comment/update/:cid'
,
function
(
req
,
res
,
next
)
{
var
cid
=
req
.
params
.
cid
||
null
;
var
status
=
req
.
body
.
status
;
var
tid
=
req
.
body
.
tid
;
var
comment_count
=
req
.
body
.
comment_count
;
var
level
=
req
.
body
.
level
;
var
parent_cid
=
req
.
body
.
parent_cid
;
if
(
cid
)
{
forumCommentService
.
updateCommentStatusById
(
cid
,
status
,
function
(
err
,
result
)
{
if
(
err
)
{
res
.
json
(
returnCode
.
BUSY
);
}
else
{
// 如果是删除
if
(
status
==
2
)
{
if
(
level
==
1
)
{
// 如果删除的是一级评论
// 获取评论的评论列表,并移除cid评论
forumThreadService
.
getById
(
tid
,
function
(
err
,
result
)
{
if
(
err
)
{
console
.
error
(
err
);
res
.
json
(
returnCode
.
BUSY
);
}
else
{
// result.comment_count -= 1;
// var commentList = result.comments;
// result.comments = forumThreadService.remove(commentList, cid);
var
commentList
=
result
.
comments
;
var
comments
=
forumThreadService
.
remove
(
commentList
,
cid
);
forumThreadService
.
updateThreadById
(
tid
,
{
comments
:
comments
,
$inc
:
{
comment_count
:
-
1
}
},
function
(
err
,
result
)
{
if
(
err
)
{
console
.
error
(
err
);
res
.
json
(
returnCode
.
BUSY
);
}
else
{
res
.
json
(
returnCode
.
SUCCESS
);
}
});
}
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
(
status
==
2
){
//一级评论
if
(
comment
.
level
==
1
){
var
commentList
=
result
.
comments
;
var
comments
=
forumThreadService
.
remove
(
commentList
,
cid
);
forumThreadService
.
updateThreadById
(
comment
.
thread
,
{
comments
:
comments
,
$inc
:
{
comment_count
:
-
1
}
},
function
(
err
,
result
)
{
callback
(
err
);
});
}
else
{
// 删除子评论
// 更新文章评论 - 1
forumThreadService
.
updateThreadCommentCount
(
tid
,
function
(
err
,
result
)
{
}
else
{
forumThreadService
.
updateThreadCommentCount
(
comment
.
thread
,
function
(
err
,
result
)
{
if
(
err
)
{
res
.
json
(
returnCode
.
BUSY
);
callback
(
err
);
}
else
{
// 更新评论的子评论列表
forumCommentService
.
getCommentParent
(
cid
,
function
(
err
,
p_comment
){
if
(
err
||
!
p_comment
){
callback
(
err
||
'comment not exist'
);
}
else
{
var
commentList
=
p_comment
.
comments
;
var
comments
=
forumCommentService
.
remove
(
commentList
,
cid
);
forumCommentService
.
updateCommentById
(
p_comment
.
_id
,
{
comments
:
comments
},
function
(
err
,
update
)
{
callback
(
err
);
});
}
});
}
});
// 更新评论的子评论列表
if
(
parent_cid
)
{
forumCommentService
.
getCommentById
(
parent_cid
,
function
(
err
,
result
)
{
if
(
err
)
{
console
.
error
(
err
);
res
.
json
(
returnCode
.
BUSY
);
}
else
{
var
commentList
=
result
.
comments
;
var
comments
=
forumCommentService
.
remove
(
commentList
,
cid
);
forumCommentService
.
updateCommentById
(
parent_cid
,
{
comments
:
comments
},
function
(
err
,
result
)
{
if
(
err
)
{
res
.
json
(
returnCode
.
BUSY
);
}
else
{
res
.
json
(
returnCode
.
SUCCESS
);
}
});
}
})
};
}
}
else
{
res
.
json
(
returnCode
.
SUCCESS
);
}
else
{
callback
(
);
}
}
],
function
(
err
,
restult
){
if
(
err
){
console
.
error
(
err
);
res
.
json
(
returnCode
.
BUSY
);
}
else
{
res
.
json
(
returnCode
.
SUCCESS
);
}
});
}
else
{
...
...
@@ -436,7 +490,6 @@ router.post('/thread/comment/update/:cid', function(req, res, next) {
}
});
// 更新回复评论
router
.
post
(
'/thread/comment/update/:cid/comments'
,
function
(
req
,
res
,
next
)
{
var
tid
=
req
.
body
.
tid
||
null
;
...
...
@@ -514,103 +567,94 @@ router.post('/thread/comment/update/:cid/comments', function(req, res, next) {
// }
// });
});
//添加评论
router
.
post
(
'/thread/comment/add'
,
function
(
req
,
res
,
next
)
{
var
tid
=
req
.
body
.
tid
||
null
;
var
entity
=
req
.
body
;
var
ip
=
getClientIP
(
req
);
entity
.
created
=
new
Date
();
entity
.
ent_code
=
req
.
session
.
user
.
ent_code
;
entity
.
ip
=
ip
;
entity
.
ip
=
getClientIP
(
req
);
var
uid
=
req
.
session
.
user
.
id
;
// 正式环境
if
(
tid
)
{
async
.
waterfall
([
function
(
callback
)
{
forumUserService
.
getUserByUid
(
uid
,
callback
);
}
],
function
(
err
,
user
)
{
if
(
err
)
{
res
.
json
(
returnCode
.
BUSY
);
}
else
{
if
(
user
)
{
entity
.
from
=
user
.
_id
;
// 添加评论
forumCommentService
.
createComment
(
entity
,
function
(
err
,
result
)
{
if
(
err
)
{
res
.
json
(
returnCode
.
BUSY
);
}
else
{
var
returnData
=
{
comment
:
result
,
errorcode
:
0
,
errormsg
:
'请求成功'
}
res
.
json
(
returnData
);
}
});
}
else
{
var
userentity
=
{
},
function
(
user
,
callback
){
//获取发表人
if
(
!
user
){
var
userentity
=
{
uid
:
uid
,
nickName
:
req
.
session
.
user
.
name
,
icon
:
req
.
session
.
user
.
headPic
};
forumUserService
.
createUser
(
userentity
,
function
(
err
,
doc
)
{
if
(
err
)
{
console
.
log
(
err
);
res
.
json
(
returnCode
.
BUSY
);
callback
(
err
);
}
else
{
entity
.
from
=
doc
.
_id
;
// 添加评论
forumCommentService
.
createComment
(
entity
,
function
(
err
,
result
)
{
if
(
err
)
{
res
.
json
(
returnCode
.
BUSY
);
}
else
{
var
returnData
=
{
comment
:
result
,
errorcode
:
0
,
errormsg
:
'请求成功'
}
res
.
json
(
returnData
);
}
});
callback
();
}
});
}
else
{
entity
.
from
=
user
;
callback
();
}
},
function
(
callback
){
//获取楼层
if
(
entity
.
level
==
1
){
var
conditions
=
{
thread
:
entity
.
thread
,
level
:
'1'
}
forumCommentService
.
count
(
conditions
,
function
(
err
,
count
)
{
if
(
err
)
{
callback
(
err
);
}
else
{
if
(
count
){
entity
.
floor
=
count
+
1
;
}
else
{
entity
.
floor
=
1
;
}
callback
();
}
});
}
else
{
callback
();
}
},
function
(
callback
){
// 添加评论
forumCommentService
.
createComment
(
entity
,
function
(
err
,
result
)
{
if
(
err
)
{
callback
(
err
);
}
else
{
var
returnData
=
{
comment
:
result
,
errorcode
:
0
,
errormsg
:
'请求成功'
}
callback
(
null
,
returnData
);
}
});
}
],
function
(
err
,
returnData
)
{
if
(
err
)
{
console
.
error
(
err
);
res
.
json
(
returnCode
.
BUSY
);
}
else
{
res
.
json
(
returnData
);
}
});
}
else
{
res
.
json
(
returnCode
.
WRONG_PARAM
);
}
// 测试环境
// entity.from = '55015675868b65a028187c49';
// if (tid) {
// // 添加评论
// forumCommentService.createComment(entity, function(err, result) {
// if (err) {
// res.json(returnCode.BUSY);
// } else {
// var returnData = {
// comment: result,
// errorcode: 0,
// errormsg: '请求成功'
// }
// res.json(returnData);
// }
// });
// }
});
// 更新文章评论
router
.
post
(
'/thread/:tid/updateComments'
,
function
(
req
,
res
,
next
)
{
var
tid
=
req
.
params
.
tid
||
null
;
...
...
@@ -653,26 +697,10 @@ router.post('/thread/:tid/updateComments', function(req, res, next) {
// });
// }
// });
});
var
getClientIP
=
function
(
req
)
{
var
ipAddress
;
var
headers
=
req
.
headers
;
var
forwardedIpsStr
=
headers
[
'x-real-ip'
]
||
headers
[
'x-forwarded-for'
];
if
(
forwardedIpsStr
)
{
ipAddress
=
forwardedIpsStr
;
}
else
{
ipAddress
=
null
;
}
if
(
!
ipAddress
)
{
ipAddress
=
req
.
connection
.
remoteAddress
;
}
return
ipAddress
;
};
//给以前的已一级评论添加楼层
// router.get('/thread/updateConmentFloor', function(req, res, next) {
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment