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
ae9968b0
Commit
ae9968b0
authored
Jun 01, 2016
by
刘文胜
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
帖子,评论 批量异步删除和屏蔽
parent
d95750c8
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
670 additions
and
0 deletions
+670
-0
forumThreadManagementAsynBatch.js
app/controllers/admin/forumThreadManagementAsynBatch.js
+670
-0
No files found.
app/controllers/admin/forumThreadManagementAsynBatch.js
0 → 100644
View file @
ae9968b0
'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
;
}
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
)
{
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
deleteAll
=
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
){
forumThreadService
.
logicDeleteThreadById
(
doc
.
_id
,
function
(
err
)
{
if
(
err
)
{
cb2
(
err
,
null
);
}
else
{
forumAboutMEService
.
updateThreadStatus
(
doc
.
_id
,
3
);
cb2
(
null
,
true
);
}
});
});
});
}
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
};
deleteAll
(
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
};
deleteAll
(
conditions
,
callback
);
}
});
}
else
{
deleteAll
(
conditions
,
callback
);
}
res
.
json
(
_
.
assign
({
data
:
true
},
returnCode
.
SUCCESS
));
});
});
//屏蔽查询结果的所有帖子
router
.
post
(
'/threadManagement/threads/list/closeAllAsync'
,
function
(
req
,
res
,
next
)
{
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
closeAll
=
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
){
forumThreadService
.
updateThreadById
(
doc
.
_id
,
{
status
:
0
},
function
(
err
)
{
if
(
err
)
{
cb2
(
err
,
null
);
}
else
{
forumAboutMEService
.
updateThreadStatus
(
doc
.
_id
,
0
);
cb2
(
null
,
true
);
}
});
});
});
}
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
};
closeAll
(
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
};
closeAll
(
conditions
,
callback
);
}
});
}
else
{
closeAll
(
conditions
,
callback
);
}
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
){
task
.
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/:tid/comment/search/deleteAllAsync'
,
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
};
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/closeAllAsync'
,
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
};
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
);
}
});
\ No newline at end of file
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