Browse Source

点赞逻辑调整

huangct 8 years ago
parent
commit
6eec9d49e1

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

@@ -148,4 +148,9 @@ public class Comment extends BaseComment<Comment> {
 		}
 		return false;
 	}
+
+	//判断是否处于已赞状态
+	public boolean isvoted(BigInteger commentId, BigInteger userId, BigInteger uuUserId) {
+		return VoteQuery.me().findCountVote(commentId, userId, uuUserId);
+	}
 }

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

@@ -89,7 +89,7 @@ public class CommentQuery extends JBaseQuery {
 
 
 		//软文详情页评论排序,先按是否置顶排,再按点赞数排,最后按评论时间排。
-		fromBuilder.append("and c.parent_id is null ORDER BY c.order_number desc,c.vote_up desc, c.created desc");
+		fromBuilder.append("and c.parent_id is null GROUP BY c.id ORDER BY c.order_number desc,c.vote_up desc, c.created desc");
 
 
 		if (params.isEmpty()) {

+ 10 - 0
jpress-model/src/main/java/io/jpress/model/query/VoteQuery.java

@@ -42,4 +42,14 @@ public class VoteQuery extends JBaseQuery {
 
 		return vote;
 	}
+
+	public boolean findCountVote(BigInteger commentId, BigInteger userId, BigInteger uuUserId){
+		long count = DAO.doFindCount("comment_id = ? and (user_id = ? or uu_user_id = ?) and up=1 ", commentId, userId, uuUserId);
+		if (count > 0) {
+			return true;
+		}
+
+		return false;
+	}
+
 }

+ 36 - 2
jpress-web-front/src/main/java/io/jpress/front/controller/CommentController.java

@@ -187,6 +187,16 @@ public class CommentController extends BaseFrontController {
 		List<Comment> list = page.getList();
 		StringBuilder htmlBuilder = new StringBuilder();
 		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+
+		BigInteger userId = getParaToBigInteger("userId");
+		BigInteger uuUserId = getParaToBigInteger("uuUserId");
+		if ("0".equals(userId.toString())) {
+			userId = null;
+		}
+		if ("0".equals(uuUserId.toString())) {
+			uuUserId = null;
+		}
+
 		for (int i = 0; i< list.size() ;i ++) {
 			Comment comment = list.get(i);
 			htmlBuilder.append("<div class=\"message\">");
@@ -199,14 +209,38 @@ public class CommentController extends BaseFrontController {
 			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=\"\" onclick=\"vote("+ comment.getId()+")\"><em>" + comment.getVoteUp() + "</em></span>");
+			htmlBuilder.append("<span><img class=\"votedUas"+comment.getId());
+
+			//如果已赞,添加class="voted"
+			String imageUrl = null;
+			if (comment.isvoted(comment.getId(), userId, uuUserId)) {
+				htmlBuilder.append(" voted\" ");
+				imageUrl = "/jpress/static/jpress/admin/image/support.png";
+			} else {
+				htmlBuilder.append("\"");
+				imageUrl = "/jpress/static/jpress/admin/image/hands.png";
+			}
+
+			htmlBuilder.append(" src="+ imageUrl + " alt=\"\" onclick=\"vote("+ comment.getId()+")\"><em class=\"em" + comment.getId() + "\">" + 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=\"\" onclick=\"vote("+ sonComment.getId()+")\"><em>" + sonComment.getVoteUp() + "</em></span>");
+				htmlBuilder.append("<span><img class=\"votedUas"+sonComment.getId());
+
+				//如果已赞,添加class="voted"
+				String sonImageUrl = null;
+				if (sonComment.isvoted(sonComment.getId(), userId, uuUserId)) {
+					htmlBuilder.append(" voted\" ");
+					sonImageUrl = "/jpress/static/jpress/admin/image/support.png";
+				} else {
+					htmlBuilder.append("\"");
+					sonImageUrl = "/jpress/static/jpress/admin/image/hands.png";
+				}
+
+				htmlBuilder.append(" src="+ sonImageUrl+ " alt=\"\" onclick=\"vote("+ sonComment.getId()+")\"><em class=\"em" + sonComment.getId() + "\">" + sonComment.getVoteUp() + "</em></span>");
 				htmlBuilder.append("</div>");
 				htmlBuilder.append("<p>"+sonComment.getText()+"</p>");
 			}

+ 44 - 34
jpress-web-template-usoftchina/src/main/webapp/templates/usoftchina/content_uuhelper.html

@@ -238,7 +238,7 @@
             </div>
         </div>
         <div class="footer">
-            <#if content.isCommentEnable() && !(content.isUuUserReplied(userid) || (USER?? && content.isUserReplied(USER.id)))>
+            <#if content.isCommentEnable() && !(content.isUuUserReplied(userid) || (USER?? && content.isUserReplied(USER.id))) && (USER?? || userid??)>
                 <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" >
@@ -269,15 +269,13 @@
                                 <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="" onclick="vote(${comment.id})"/><em>${(comment.vote_up)!'0'}</em></span>
-                                        <!--<span><img src="/jpress/static/jpress/admin/image/support.png" alt=""/><em>1</em></span>-->
+                                        <span><img class="votedUas${comment.id} <#if comment.isvoted(comment.id, userId!0, userid!0)>voted</#if>" src="/jpress/static/jpress/admin/image/hands.png" alt="" onclick="vote(${comment.id})"/><em class="em${comment.id}">${(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="" onclick="vote(${comment.qc_id})"><em>${(comment.qc_vote_up)!'0'}</em></span>
-                                            <!--<span><img src="/jpress/static/jpress/admin/image/support.png" alt=""/><em>1</em></span>-->
+                                            <span><img class="votedUas${comment.qc_id} <#if comment.isvoted(comment.qc_id, userId!0, userid!0)>voted</#if>" src="/jpress/static/jpress/admin/image/hands.png" alt="" onclick="vote(${comment.qc_id})"/><em class="em${comment.qc_id}">${(comment.qc_vote_up)!'0'}</em></span>
                                         </div>
                                         <p>${comment.qc_content!}</p>
                                     </#if>
@@ -297,34 +295,34 @@
 </body>
 <script>
     jQuery('html').css('fontSize',$(window).width()/640 * 30);
-    jQuery(window).load(function () {
-        jQuery("img").each(function () {
-            DrawImage(this, 100, 100);
-        });
-    });
-    function DrawImage(ImgD, FitWidth, FitHeight) {
-        var image = new Image();
-        image.src = ImgD.src;
-        if (image.width > 0 && image.height > 0) {
-            if (image.width / image.height >= FitWidth / FitHeight) {
-                if (image.width > FitWidth) {
-                    ImgD.width = FitWidth;
-                    ImgD.height = (image.height * FitWidth) / image.width;
-                } else {
-                    ImgD.width = image.width;
-                    ImgD.height = image.height;
-                }
-            } else {
-                if (image.height > FitHeight) {
-                    ImgD.height = FitHeight;
-                    ImgD.width = (image.width * FitHeight) / image.height;
-                } else {
-                    ImgD.width = image.width;
-                    ImgD.height = image.height;
-                }
-            }
-        }
-    }
+//    jQuery(window).load(function () {
+//        jQuery("img").each(function () {
+//            DrawImage(this, 100, 50);
+//        });
+//    });
+//    function DrawImage(ImgD, FitWidth, FitHeight) {
+//        var image = new Image();
+//        image.src = ImgD.src;
+//        if (image.width > 0 && image.height > 0) {
+//            if (image.width / image.height >= FitWidth / FitHeight) {
+//                if (image.width > FitWidth) {
+//                    ImgD.width = FitWidth;
+//                    ImgD.height = (image.height * FitWidth) / image.width;
+//                } else {
+//                    ImgD.width = image.width;
+//                    ImgD.height = image.height;
+//                }
+//            } else {
+//                if (image.height > FitHeight) {
+//                    ImgD.height = FitHeight;
+//                    ImgD.width = (image.width * FitHeight) / image.height;
+//                } else {
+//                    ImgD.width = image.width;
+//                    ImgD.height = image.height;
+//                }
+//            }
+//        }
+//    }
 //    写留言
     $('.footer-header>div.infoMessage').click(function () {
         $(this).next('.writeMessage').toggle()
@@ -334,7 +332,7 @@
     timers = null;
     //加载数据
     var LoadingDataFn = function() {
-        $.get("${CPATH}/comment/lazyLoad?contId=${(content.id)!}&pagesize=1&pagenumber="+aa, function(result){
+        $.get("${CPATH}/comment/lazyLoad?contId=${(content.id)!}&pagesize=3&pagenumber="+aa+"&userId=${userId!0}&uuUserId=${userid!0}", function(result){
             var dom = '';
             dom += result.message;
             $('.footer-section').append(dom);
@@ -347,6 +345,7 @@
     $(document).ready(function() {
         userId = "${userId!0}"
         uuUserId = "${userid!0}";
+        $('.voted').attr("src","/jpress/static/jpress/admin/image/support.png");
     });
     //滚动加载方法
     $(window).scroll(function() {
@@ -361,6 +360,17 @@
 
     //点赞
     var vote = function(commentid) {
+        var imgObj = $(".votedUas"+commentid);
+        var emObj = $(".em"+commentid);
+        if (imgObj.hasClass("voted")) {
+            emObj.text(""+(parseInt(emObj.text())-1));
+            imgObj.removeClass("voted");
+            imgObj.attr("src","/jpress/static/jpress/admin/image/hands.png");
+        } else {
+            emObj.text(""+(parseInt(emObj.text())+1));
+            imgObj.addClass("voted");
+            imgObj.attr("src","/jpress/static/jpress/admin/image/support.png")
+        }
         $.get("${CPATH}/comment/vote?comment_id="+commentid+"&user_id="+userId+"&uu_user_id="+uuUserId, function(result){
             console.log(result.message);
         });