shuzheng 9 vuotta sitten
vanhempi
sitoutus
291bfb424d

+ 16 - 0
zheng-cms/zheng-cms-web/src/main/java/com/zheng/cms/web/controller/ArticleController.java

@@ -1,7 +1,10 @@
 package com.zheng.cms.web.controller;
 
 import com.zheng.cms.dao.model.CmsArticle;
+import com.zheng.cms.dao.model.CmsComment;
+import com.zheng.cms.dao.model.CmsCommentExample;
 import com.zheng.cms.rpc.api.CmsArticleService;
+import com.zheng.cms.rpc.api.CmsCommentService;
 import com.zheng.common.base.BaseController;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -12,6 +15,8 @@ import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 
+import java.util.List;
+
 /**
  * 文章控制器
  * Created by shuzheng on 2017/3/26.
@@ -25,10 +30,21 @@ public class ArticleController extends BaseController {
     @Autowired
     private CmsArticleService cmsArticleService;
 
+    @Autowired
+    private CmsCommentService cmsCommentService;
+
     @RequestMapping(value = "/{articleId}", method = RequestMethod.GET)
     public String index(@PathVariable("articleId") int articleId, Model model) {
         CmsArticle article = cmsArticleService.selectByPrimaryKey(articleId);
         model.addAttribute("article", article);
+        // 评论列表
+        CmsCommentExample cmsCommentExample = new CmsCommentExample();
+        cmsCommentExample.createCriteria()
+                .andArticleIdEqualTo(articleId)
+                .andStatusEqualTo((byte) 1);
+        cmsCommentExample.setOrderByClause("ctime desc");
+        List<CmsComment> comments = cmsCommentService.selectByExampleWithBLOBs(cmsCommentExample);
+        model.addAttribute("comments", comments);
         return thymeleaf("/article/index");
     }
 

+ 49 - 0
zheng-cms/zheng-cms-web/src/main/java/com/zheng/cms/web/controller/CommentController.java

@@ -0,0 +1,49 @@
+package com.zheng.cms.web.controller;
+
+import com.zheng.cms.common.constant.CmsResult;
+import com.zheng.cms.common.constant.CmsResultConstant;
+import com.zheng.cms.dao.model.CmsComment;
+import com.zheng.cms.rpc.api.CmsCommentService;
+import com.zheng.common.base.BaseController;
+import com.zheng.common.util.RequestUtil;
+import org.apache.shiro.authz.annotation.RequiresPermissions;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.ResponseBody;
+
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ * 评论控制器
+ * Created by shuzheng on 2017/3/26.
+ */
+@Controller
+@RequestMapping(value = "/comment")
+public class CommentController extends BaseController {
+
+    private static Logger _log = LoggerFactory.getLogger(CommentController.class);
+
+    @Autowired
+    private CmsCommentService cmsCommentService;
+
+    @RequiresPermissions("cms:comment:create")
+    @RequestMapping(value = "/create/{articleId}", method = RequestMethod.POST)
+    @ResponseBody
+    public Object create(@PathVariable("articleId") int articleId, CmsComment cmsComment, HttpServletRequest request) {
+        long time = System.currentTimeMillis();
+        cmsComment.setCtime(time);
+        cmsComment.setArticleId(articleId);
+        cmsComment.setUserId(1);
+        cmsComment.setStatus((byte) 1);
+        cmsComment.setIp(RequestUtil.getIpAddr(request));
+        cmsComment.setAgent(request.getHeader("User-Agent"));
+        int count = cmsCommentService.insertSelective(cmsComment);
+        return new CmsResult(CmsResultConstant.SUCCESS, count);
+    }
+
+}

+ 19 - 0
zheng-ui/src/zheng-cms-web/article/index.html

@@ -92,6 +92,14 @@
 <div class="container" style="padding-bottom:80px;">
     <h3 th:text="${article.title}">标题</h3>
     <div th:utext="${article.content}">内容</div>
+    <form id="commentForm" th:action="@{'/comment/create/' + ${article.articleId}}" method="post">
+        <textarea id="" name="content" style="width:100%;height:50px;"></textarea>
+        <input id="submit" type="button" value="评论"/><!--{"code":1,"message":"success","data":1}-->
+    </form>
+    <dl>
+        <dt>评论列表</dt>
+        <dd th:each="comment : ${comments}" th:utext="${comment.content}">评论内容</dd>
+    </dl>
 </div>
 <!-- /.container -->
 <!-- footer -->
@@ -102,5 +110,16 @@
 <script src="../js/jquery-1.11.2.min.js" th:src="@{${uiPath} + ${appName} + '/js/jquery-1.11.2.min.js'}"></script>
 <script src="../js/bootstrap.min.js" th:src="@{${uiPath} + ${appName} + '/js/bootstrap.min.js'}"></script>
 <script src="../js/main.js" th:src="@{${uiPath} + ${appName} + '/js/main.js'}"></script>
+<script>
+$('#submit').click(function () {
+    $.post($('#commentForm').attr('action'), $('#commentForm').serialize(), function (json) {
+        if (json.code == 1) {
+            location.reload();
+        } else {
+            alert(json.data);
+        }
+    })
+});
+</script>
 </body>
 </html>