Prechádzať zdrojové kódy

评论展示和向下拉加载更多

huangct 8 rokov pred
rodič
commit
0ce6befa95

+ 4 - 0
jpress-model/src/main/java/io/jpress/model/Comment.java

@@ -148,4 +148,8 @@ public class Comment extends BaseComment<Comment> {
 		}
 		return false;
 	}
+
+	public Comment getlatestSon(BigInteger id){
+        return CommentQuery.me().findLatestSon(id);
+	}
 }

+ 45 - 1
jpress-model/src/main/java/io/jpress/model/query/CommentQuery.java

@@ -21,6 +21,7 @@ import io.jpress.model.Comment;
 
 import java.math.BigInteger;
 import java.util.LinkedList;
+import java.util.List;
 
 public class CommentQuery extends JBaseQuery {
 
@@ -65,6 +66,38 @@ public class CommentQuery extends JBaseQuery {
 		return DAO.paginate(pageNumber, pageSize, select, fromBuilder.toString(), params.toArray());
 	}
 
+	public Page<Comment> uuHelperPaginateWithContent(int pageNumber, int pageSize, String module, String type,
+											 BigInteger contentId, BigInteger parentCommentId, String status) {
+
+		String select = " select DISTINCT c.id, c.*,content.title content_title,u.username,u.nickname,u.avatar, "
+				+ "quote_comment.text qc_content,quote_comment.author qc_author,"
+				+ "quote_user.username qc_username,quote_user.nickname qc_nickname,quote_user.avatar qc_avatar ";
+
+		StringBuilder fromBuilder = new StringBuilder("  from comment c");
+		fromBuilder.append(" left join content on c.content_id = content.id");
+		fromBuilder.append(" left join user u on c.user_id = u.id ");
+		fromBuilder.append(" left join comment quote_comment on c.id = quote_comment.parent_id");
+		fromBuilder.append(" left join user quote_user on quote_comment.user_id = quote_user.id ");
+
+		LinkedList<Object> params = new LinkedList<Object>();
+		boolean needWhere = true;
+		needWhere = appendIfNotEmpty(fromBuilder, " c.`type`", type, params, needWhere);
+		needWhere = appendIfNotEmpty(fromBuilder, " c.content_module", module, params, needWhere);
+		needWhere = appendIfNotEmpty(fromBuilder, " c.`status`", status, params, needWhere);
+		//needWhere = appendIfNotEmpty(fromBuilder, " c.parent_id", parentCommentId, params, needWhere);
+		needWhere = appendIfNotEmpty(fromBuilder, " content.id", contentId, params, needWhere);
+
+
+		//软文详情页评论排序,先按是否置顶排,再按点赞数排,最后按评论时间排。
+		fromBuilder.append("and c.parent_id is null ORDER BY c.order_number desc,c.vote_up desc, c.created desc");
+
+
+		if (params.isEmpty()) {
+			return DAO.paginate(pageNumber, pageSize, select, fromBuilder.toString());
+		}
+		return DAO.paginate(pageNumber, pageSize, select, fromBuilder.toString(), params.toArray());
+	}
+
 	public Page<Comment> paginateWithContentNotInDelete(int pageNumber, int pageSize, String module, String type,
 			BigInteger contentId, BigInteger parentCommentId) {
 
@@ -135,7 +168,7 @@ public class CommentQuery extends JBaseQuery {
 	}
 
 	public Page<Comment> uuHelperPaginateByContentId(int pageNumber, int pageSize, BigInteger contentId, String module) {
-		return paginateWithContent(pageNumber, pageSize, module, null, contentId, null, Comment.STATUS_NORMAL);
+		return uuHelperPaginateWithContent(pageNumber, pageSize, module, null, contentId, null, Comment.STATUS_NORMAL);
 	}
 
 	public long findCountByContentIdInNormal(BigInteger contentId) {
@@ -221,4 +254,15 @@ public class CommentQuery extends JBaseQuery {
 	public long findCountByContentIdAndUuUserId(BigInteger contentId, BigInteger uuUserId) {
 		return DAO.doFindCount(" content_id = ? and uu_user_id=? ", contentId, uuUserId);
 	}
+
+	//只获取最新的一条回复
+	public Comment findLatestSon(BigInteger id) {
+		Comment comment = null;
+		List<Comment> list = DAO.find("SELECT * from jpress_comment where parent_id= " + id +" ORDER BY created desc LIMIT 1");
+		if (list.size() > 0) {
+			comment = list.get(0);
+		}
+		return comment;
+	}
+
 }

+ 36 - 0
jpress-web-admin/src/main/java/io/jpress/admin/controller/_CommentController.java

@@ -32,7 +32,9 @@ import io.jpress.utils.JsoupUtils;
 import io.jpress.utils.StringUtils;
 
 import java.math.BigInteger;
+import java.text.SimpleDateFormat;
 import java.util.Date;
+import java.util.List;
 
 @RouterMapping(url = "/admin/comment", viewPath = "/WEB-INF/admin/comment")
 @Before(ActionCacheClearInterceptor.class)
@@ -241,5 +243,39 @@ public class _CommentController extends JBaseCRUDController<Comment> {
 			renderAjaxResultForError("restore error!");
 		}
 	}
+	@Before(UCodeInterceptor.class)
+	public void lazyLoad() {
+		Page<Comment> page = CommentQuery.me().uuHelperPaginateByContentId(getParaToInt("pagenumber"), getParaToInt("pagesize"), getParaToBigInteger("contId"), "uuhelper");
+		List<Comment> list = page.getList();
+		StringBuilder htmlBuilder = new StringBuilder();
+		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+		for (int i = 0; i< list.size() ;i ++) {
+			Comment comment = list.get(i);
+			htmlBuilder.append("<div class=\"message\">");
+			htmlBuilder.append("<div class=\"left\">");
+			htmlBuilder.append("<div class=\"img\">");
+			htmlBuilder.append("<img src=\"/jpress/static/jpress/admin/image/dot.png\" alt=\"用户头像\">");
+			htmlBuilder.append("</div>");
+			htmlBuilder.append("</div>");
+			htmlBuilder.append("<div class=\"right\">");
+			htmlBuilder.append("<div class=\"detail\">");
+			htmlBuilder.append("<div class=\"title\">");
+			htmlBuilder.append("<span>"+ comment.getAuthor() +"<em>" + sdf.format(comment.getCreated()) + "</em></span>");
+			htmlBuilder.append("<span><img src=\"/jpress/static/jpress/admin/image/hands.png\" alt=\"\"><em>" + comment.getVoteUp() + "</em></span>");
+			htmlBuilder.append("</div>");
+			htmlBuilder.append("<p>"+comment.getText()+"</p>");
+			if (comment.getlatestSon(comment.getId()) != null) {
+				Comment sonComment = comment.getlatestSon(comment.getId());
+				htmlBuilder.append("<div class=\"title reply\">");
+				htmlBuilder.append("<span>作者回复: <em>" + sdf.format(sonComment.getCreated()) + "</em></span>");
+				htmlBuilder.append("<span><img src=\"/jpress/static/jpress/admin/image/hands.png\" alt=\"\"><em>" + sonComment.getVoteUp() + "</em></span>");
+				htmlBuilder.append("</div>");
+				htmlBuilder.append("<p>"+sonComment.getText()+"</p>");
+			}
+			htmlBuilder.append("</div>\n" + "</div>\n" + " </div>");
+		}
+
+		renderAjaxResultForSuccess(htmlBuilder.toString());
+	}
 
 }

+ 2 - 0
jpress-web-core/src/main/java/io/jpress/ui/freemarker/tag/CommentPageTag.java

@@ -44,6 +44,7 @@ public class CommentPageTag extends JTag {
 	public void onRender() {
 
 		int pageSize = getParamToInt("pageSize", 10);
+		int pageNumber = getParamToInt("pageNumber", 1);
 
 		Page<Comment> page;
 		if ("uuhelper".equals(content.getModule())) {
@@ -60,6 +61,7 @@ public class CommentPageTag extends JTag {
 		renderBody();
 	}
 
+
 	public static class CommentPaginateTag extends BasePaginateTag {
 
 		final Content content;

+ 1 - 0
jpress-web-front/src/main/java/io/jpress/front/controller/CommentController.java

@@ -60,6 +60,7 @@ public class CommentController extends BaseFrontController {
 		}
 
 		BigInteger userId = StringUtils.toBigInteger(CookieUtils.get(this, Consts.COOKIE_LOGINED_USER), null);
+		userId = null;
 		BigInteger uuUserId = getParaToBigInteger("uuUserId");
 
 		// 允许未登录用户评论

+ 25 - 50
jpress-web-template-usoftchina/src/main/webapp/templates/usoftchina/content_uuhelper.html

@@ -227,8 +227,8 @@
                 <form action="${CPATH}/comment/submit" method="post"  id="comment">
                     <input type="hidden" name="cid" value="${(content.id)!}" >
                     <input type="hidden" id="parent_id" name="parent_id" >
-                    <input type="hidden" name="uuUserId" value="${userid!}" >
-                    <input type="hidden" name="uuUserName" value="${username!}" >
+                    <input type="hidden" name="uuUserId" value="250" >
+                    <input type="hidden" name="uuUserName" value="无敌风火轮" >
                     <input type="hidden" name="uuUserAvatar" value="${iconurl!}" >
 
                     <div class="footer-header">
@@ -241,7 +241,7 @@
                 </form>
             </#if>
             <div class="footer-section">
-                <@jp.commentPage pageSize="2">
+                <@jp.commentPage pageSize="1">
                 <#if page ??>
                     <#list page.getList() as comment>
                         <div class="message">
@@ -313,55 +313,30 @@
         $(this).next('.writeMessage').toggle()
     })
 //    上拉加载更多
-    var page = 1,
-        timers = null;
-        //加载数据
-        var LoadingDataFn = function() {
+    var aa = 2,
+    timers = null;
+    //加载数据
+    var LoadingDataFn = function() {
+        $.get("${CPATH}/admin/comment/lazyLoad?ucode=${ucode}&contId=${(content.id)!}&pagesize=1&pagenumber="+aa, function(result){
             var dom = '';
-            dom += ` <@jp.commentPage pageSize="2">
-                <#if page ??>
-                    <#list page.getList() as comment>
-                        <div class="message">
-                            <div class="left">
-                                <div class="img" >
-                                    <img src="/jpress/static/jpress/admin/image/dot.png" alt="用户头像">
-                                </div>
-                            </div>
-                            <div class="right">
-                                <div class="detail">
-                                    <div class="title">
-                                        <span>${comment.author!} <em>${(comment.created?string("yyyy-MM-dd"))!}</em></span>
-                                        <span><img src="/jpress/static/jpress/admin/image/hands.png" alt=""><em>${(comment.vote_up)!'0'}</em></span>
-                                    </div>
-                                    <p>${comment.text!}</p>
-                                    <#if comment.qc_content ??>
-                                        <div class="title reply">
-                                            <span>作者回复: <em>${(comment.qc_created?string("yyyy-MM-dd"))!}</em></span>
-                                            <span><img src="/jpress/static/jpress/admin/image/hands.png" alt=""><em>${(comment.vote_up)!'0'}</em></span>
-                                        </div>
-                                        <p>${comment.qc_content!}</p>
-                                    </#if>
-                                </div>
-                            </div>
-                        </div>
-                    </#list>
-                </#if>
-            </@jp.commentPage>`;
+            dom += result.message;
             $('.footer-section').append(dom);
-        };
-        //初始化
-        $(document).ready(function() {
-            LoadingDataFn();
-        });
-        //滚动加载方法
-        $(window).scroll(function() {
-            if (($(window).height() + $(window).scrollTop() + 60) >= $(document).height()) {
-                clearTimeout(timers);
-                timers = setTimeout(function() {
-                    page++;
-                    LoadingDataFn();
-                }, 300);
-            }
         });
+
+    };
+    //初始化
+    /*$(document).ready(function() {
+        LoadingDataFn();
+    });*/
+    //滚动加载方法
+    $(window).scroll(function() {
+        if (($(window).height() + $(window).scrollTop() + 60) >= $(document).height()) {
+            clearTimeout(timers);
+            timers = setTimeout(function() {
+                aa++;
+                LoadingDataFn();
+            }, 300);
+        }
+    });
 </script>
 </html>