完成评论删除和评论字数长度校验
This commit is contained in:
parent
be982beb9e
commit
0bc34bd784
@ -106,4 +106,11 @@ public class ForumController {
|
|||||||
@RequestParam @Min(0) int page) {
|
@RequestParam @Min(0) int page) {
|
||||||
return RestBean.success(topicService.comments(tid, page + 1));
|
return RestBean.success(topicService.comments(tid, page + 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/delete-comment")
|
||||||
|
public RestBean<Void> deleteComment(@RequestParam @Min(0) int id,
|
||||||
|
@RequestAttribute(Const.ATTR_USER_ID) int uid) {
|
||||||
|
topicService.deleteComment(id, uid);
|
||||||
|
return RestBean.success();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,5 +24,6 @@ public interface TopicService extends IService<Topic> {
|
|||||||
void interact(Interact interact, boolean state);
|
void interact(Interact interact, boolean state);
|
||||||
List<TopicPreviewVO> listCollectTopic(int uid);
|
List<TopicPreviewVO> listCollectTopic(int uid);
|
||||||
String createComment(AddCommentVO vo, int uid);
|
String createComment(AddCommentVO vo, int uid);
|
||||||
|
void deleteComment(int id, int uid);
|
||||||
List<CommentVO> comments(int tid, int pageNumber);
|
List<CommentVO> comments(int tid, int pageNumber);
|
||||||
}
|
}
|
||||||
|
@ -74,7 +74,7 @@ public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic> implements
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String createTopic(int uid, TopicCreateVO vo) {
|
public String createTopic(int uid, TopicCreateVO vo) {
|
||||||
if(!this.textLimitCheck(vo.getContent()))
|
if(!this.textLimitCheck(vo.getContent(), 20000))
|
||||||
return "文章长度过大,发文失败!";
|
return "文章长度过大,发文失败!";
|
||||||
String key = Const.FORUM_TOPIC_CREATE_COUNTER + uid;
|
String key = Const.FORUM_TOPIC_CREATE_COUNTER + uid;
|
||||||
if(!flowUtils.limitPeriodCounterCheck(key, 3, 3600))
|
if(!flowUtils.limitPeriodCounterCheck(key, 3, 3600))
|
||||||
@ -161,6 +161,8 @@ public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic> implements
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String createComment(AddCommentVO vo, int uid) {
|
public String createComment(AddCommentVO vo, int uid) {
|
||||||
|
if(!this.textLimitCheck(JSONObject.parseObject(vo.getContent()), 2000))
|
||||||
|
return "评论长度过大,发表失败!";
|
||||||
String key = Const.FORUM_TOPIC_COMMENT_COUNTER + uid;
|
String key = Const.FORUM_TOPIC_COMMENT_COUNTER + uid;
|
||||||
if(!flowUtils.limitPeriodCounterCheck(key, 2, 60))
|
if(!flowUtils.limitPeriodCounterCheck(key, 2, 60))
|
||||||
return "发表评论频繁,请稍后再试!";
|
return "发表评论频繁,请稍后再试!";
|
||||||
@ -172,6 +174,14 @@ public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic> implements
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deleteComment(int id, int uid) {
|
||||||
|
commentMapper.delete(Wrappers.<TopicComment>query()
|
||||||
|
.eq("id", id)
|
||||||
|
.eq("uid", uid)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<CommentVO> comments(int tid, int pageNumber) {
|
public List<CommentVO> comments(int tid, int pageNumber) {
|
||||||
Page<TopicComment> comments = Page.of(pageNumber, 10);
|
Page<TopicComment> comments = Page.of(pageNumber, 10);
|
||||||
@ -279,12 +289,12 @@ public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic> implements
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean textLimitCheck(JSONObject object) {
|
private boolean textLimitCheck(JSONObject object, int limit) {
|
||||||
if(object == null) return false;
|
if(object == null) return false;
|
||||||
long length = 0;
|
long length = 0;
|
||||||
for (Object op : object.getJSONArray("ops")) {
|
for (Object op : object.getJSONArray("ops")) {
|
||||||
length += JSONObject.from(op).getString("insert").length();
|
length += JSONObject.from(op).getString("insert").length();
|
||||||
}
|
}
|
||||||
return length <= 20000;
|
return length <= limit;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,10 @@ function init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function submitComment() {
|
function submitComment() {
|
||||||
|
if(deltaToText(content).length > 2000) {
|
||||||
|
ElMessage.warning('评论字数长度已经超出最大限制,请缩减评论内容!')
|
||||||
|
return
|
||||||
|
}
|
||||||
post('/api/forum/add-comment', {
|
post('/api/forum/add-comment', {
|
||||||
tid: props.tid,
|
tid: props.tid,
|
||||||
content: JSON.stringify(content.value),
|
content: JSON.stringify(content.value),
|
||||||
@ -29,11 +33,17 @@ function submitComment() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function deltaToSimpleText(delta) {
|
function deltaToText(delta) {
|
||||||
|
if(!delta?.ops) return ""
|
||||||
let str = ''
|
let str = ''
|
||||||
for (let op of JSON.parse(delta).ops) {
|
for (let ops of delta.ops) {
|
||||||
str += op.insert
|
str += ops.insert
|
||||||
}
|
}
|
||||||
|
return str.replace(/\s/g, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
function deltaToSimpleText(delta) {
|
||||||
|
let str = deltaToText(JSON.parse(delta))
|
||||||
if(str.length > 35) str = str.substring(0, 35) + "..."
|
if(str.length > 35) str = str.substring(0, 35) + "..."
|
||||||
return str
|
return str
|
||||||
}
|
}
|
||||||
@ -52,7 +62,10 @@ function deltaToSimpleText(delta) {
|
|||||||
<quill-editor style="height: 100px" v-model:content="content"
|
<quill-editor style="height: 100px" v-model:content="content"
|
||||||
placeholder="请发表友善的评论,不要使用脏话骂人,都是大学生素质高一点"/>
|
placeholder="请发表友善的评论,不要使用脏话骂人,都是大学生素质高一点"/>
|
||||||
</div>
|
</div>
|
||||||
<div style="margin-top: 10px;text-align: right">
|
<div style="margin-top: 10px;display: flex">
|
||||||
|
<div style="flex: 1;font-size: 13px;color: grey">
|
||||||
|
字数统计: {{deltaToText(content).length}}(最大支持2000字)
|
||||||
|
</div>
|
||||||
<el-button @click="submitComment" type="success" plain>发表评论</el-button>
|
<el-button @click="submitComment" type="success" plain>发表评论</el-button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -77,6 +77,13 @@ function loadComments(page){
|
|||||||
topic.page = page + 1
|
topic.page = page + 1
|
||||||
get(`/api/forum/comments?tid=${tid}&page=${page}`, data => topic.comments = data)
|
get(`/api/forum/comments?tid=${tid}&page=${page}`, data => topic.comments = data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function deleteComment(id) {
|
||||||
|
get(`/api/forum/delete-comment?id=${id}`, () => {
|
||||||
|
ElMessage.success('删除评论成功!')
|
||||||
|
loadComments(topic.page - 1)
|
||||||
|
})
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
@ -176,6 +183,7 @@ function loadComments(page){
|
|||||||
<el-link :icon="ChatSquare" @click="comment.show = true;comment.quote = item"
|
<el-link :icon="ChatSquare" @click="comment.show = true;comment.quote = item"
|
||||||
type="info"> 回复评论</el-link>
|
type="info"> 回复评论</el-link>
|
||||||
<el-link :icon="Delete" type="danger" v-if="item.user.id === store.user.id"
|
<el-link :icon="Delete" type="danger" v-if="item.user.id === store.user.id"
|
||||||
|
@click="deleteComment(item.id)"
|
||||||
style="margin-left: 20px"> 删除评论</el-link>
|
style="margin-left: 20px"> 删除评论</el-link>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@ -194,7 +202,7 @@ function loadComments(page){
|
|||||||
:submit="updateTopic"/>
|
:submit="updateTopic"/>
|
||||||
<topic-comment-editor :show="comment.show" @close="comment.show = false"
|
<topic-comment-editor :show="comment.show" @close="comment.show = false"
|
||||||
:quote="comment.quote" :tid="tid"
|
:quote="comment.quote" :tid="tid"
|
||||||
@comment="comment.show = false;loadComments(Math.min(1, Math.ceil(topic.data.commentCount / 10)))"/>
|
@comment="comment.show = false;loadComments(Math.ceil(topic.data.commentCount / 10) - 1)"/>
|
||||||
<div class="add-comment" @click="comment.show = true">
|
<div class="add-comment" @click="comment.show = true">
|
||||||
<el-icon><Plus /></el-icon>
|
<el-icon><Plus /></el-icon>
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user