提供评论的api

pull/89/head^2
ruibaby 2019-01-31 16:14:22 +08:00
parent 74e27a431c
commit d65379d362
3 changed files with 103 additions and 5 deletions

View File

@ -1,10 +1,29 @@
package cc.ryanc.halo.web.controller.api;
import cc.ryanc.halo.model.domain.Comment;
import cc.ryanc.halo.model.domain.Post;
import cc.ryanc.halo.model.dto.JsonResult;
import cc.ryanc.halo.model.enums.BlogPropertiesEnum;
import cc.ryanc.halo.model.enums.ResponseStatusEnum;
import cc.ryanc.halo.model.enums.TrueFalseEnum;
import cc.ryanc.halo.service.CommentService;
import cc.ryanc.halo.service.PostService;
import cc.ryanc.halo.utils.OwoUtil;
import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.http.HtmlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import static cc.ryanc.halo.model.dto.HaloConst.OPTIONS;
/**
* <pre>
@ -21,4 +40,66 @@ public class ApiCommentController {
@Autowired
private CommentService commentService;
@Autowired
private PostService postService;
/**
*
*
* @param comment comment
* @param result result
* @param postId postId
* @param request request
*
* @return JsonResult
*/
@PostMapping(value = "/save")
@ResponseBody
public JsonResult save(@Valid Comment comment,
BindingResult result,
@RequestParam(value = "postId") Long postId,
HttpServletRequest request) {
if (result.hasErrors()) {
for (ObjectError error : result.getAllErrors()) {
return new JsonResult(ResponseStatusEnum.ERROR.getCode(), error.getDefaultMessage());
}
}
try {
Comment lastComment = null;
final Post post = postService.findByPostId(postId).orElse(new Post());
comment.setCommentAuthorEmail(HtmlUtil.escape(comment.getCommentAuthorEmail()).toLowerCase());
comment.setPost(post);
comment.setCommentAuthorIp(ServletUtil.getClientIP(request));
comment.setIsAdmin(0);
comment.setCommentAuthor(HtmlUtil.escape(comment.getCommentAuthor()));
if (StrUtil.isNotBlank(comment.getCommentAuthorEmail())) {
comment.setCommentAuthorAvatarMd5(SecureUtil.md5(comment.getCommentAuthorEmail()));
}
if (comment.getCommentParent() > 0) {
lastComment = commentService.findCommentById(comment.getCommentParent()).orElse(new Comment());
final StrBuilder buildContent = new StrBuilder("<a href='#comment-id-");
buildContent.append(lastComment.getCommentId());
buildContent.append("'>@");
buildContent.append(lastComment.getCommentAuthor());
buildContent.append("</a> ");
buildContent.append(OwoUtil.markToImg(HtmlUtil.escape(comment.getCommentContent()).replace("&lt;br/&gt;", "<br/>")));
comment.setCommentContent(buildContent.toString());
} else {
//将评论内容的字符专为安全字符
comment.setCommentContent(OwoUtil.markToImg(HtmlUtil.escape(comment.getCommentContent()).replace("&lt;br/&gt;", "<br/>")));
}
if (StrUtil.isNotEmpty(comment.getCommentAuthorUrl())) {
comment.setCommentAuthorUrl(URLUtil.normalize(comment.getCommentAuthorUrl()));
}
commentService.save(comment);
if (StrUtil.equals(OPTIONS.get(BlogPropertiesEnum.NEW_COMMENT_NEED_CHECK.getProp()), TrueFalseEnum.TRUE.getDesc()) || OPTIONS.get(BlogPropertiesEnum.NEW_COMMENT_NEED_CHECK.getProp()) == null) {
return new JsonResult(ResponseStatusEnum.SUCCESS.getCode(), "你的评论已经提交,待博主审核之后可显示。");
} else {
return new JsonResult(ResponseStatusEnum.SUCCESS.getCode(), "你的评论已经提交,刷新后即可显示。");
}
} catch (Exception e) {
return new JsonResult(ResponseStatusEnum.ERROR.getCode(), "评论失败!");
}
}
}

View File

@ -85,8 +85,8 @@ public class ApiOptionController {
*
* @return JsonResult
*/
@GetMapping(value = "/{optionName}")
public JsonResult option(@PathVariable(value = "optionName") String optionName) {
@GetMapping(value = "/one")
public JsonResult option(@RequestParam(value = "optionName") String optionName) {
final String optionValue = optionsService.findOneOption(optionName);
return new JsonResult(ResponseStatusEnum.SUCCESS.getCode(), ResponseStatusEnum.SUCCESS.getMsg(), optionValue);
}

View File

@ -322,6 +322,23 @@
</div>
</div>
</div>
<div class="form-group">
<label class="col-lg-2 col-sm-4 control-label">API 评论开关:</label>
<div class="col-lg-4 col-sm-8 control-radio">
<div class="pretty p-default p-round">
<input type="radio" name="comment_api_switch" value="true" ${((options.comment_api_switch!)=='true')?string('checked','')}>
<div class="state p-primary">
<label><@spring.message code='common.radio.enable' /></label>
</div>
</div>
<div class="pretty p-default p-round">
<input type="radio" name="comment_api_switch" value="false" ${((options.comment_api_switch!'false')=='false')?string('checked','')}>
<div class="state p-primary">
<label><@spring.message code='common.radio.disable' /></label>
</div>
</div>
</div>
</div>
<div class="form-group">
<label for="indexComments" class="col-lg-2 col-sm-4 control-label"><@spring.message code='admin.setting.form.index-comments' />
<span data-toggle="tooltip" data-placement="top" title="<@spring.message code='admin.setting.form.index-comments-tips' />" style="cursor: pointer">