Преглед изворни кода

点赞逻辑调整,避免被拦截

huangct пре 8 година
родитељ
комит
9651675b05

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

@@ -23,10 +23,8 @@ import io.jpress.interceptor.UCodeInterceptor;
 import io.jpress.model.Comment;
 import io.jpress.model.Content;
 import io.jpress.model.User;
-import io.jpress.model.Vote;
 import io.jpress.model.query.CommentQuery;
 import io.jpress.model.query.UserQuery;
-import io.jpress.model.query.VoteQuery;
 import io.jpress.router.RouterMapping;
 import io.jpress.router.RouterNotAllowConvert;
 import io.jpress.template.TemplateManager;
@@ -34,9 +32,7 @@ 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)
@@ -245,125 +241,7 @@ 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=\"\" onclick=\"vote("+ comment.getId()+")\"><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=\"\" onclick=\"vote("+ sonComment.getId()+")\"><em>" + sonComment.getVoteUp() + "</em></span>");
-				htmlBuilder.append("</div>");
-				htmlBuilder.append("<p>"+sonComment.getText()+"</p>");
-			}
-			htmlBuilder.append("</div>\n" + "</div>\n" + " </div>");
-		}
-
-		renderAjaxResultForSuccess(htmlBuilder.toString());
-	}
-
-	//新增点赞
-	private void voteUp(Comment comment,BigInteger userId,BigInteger uuUserId){
-//		BigInteger commentId = getParaToBigInteger("comment_id");
-//		Comment comment = CommentQuery.me().findById(commentId);
-//		BigInteger userId = getParaToBigInteger("user_id");
-//		BigInteger uuUserId = getParaToBigInteger("uu_user_id");
-		Vote vote = getModel(Vote.class);
-		vote.setCommentId(comment.getId());
-		vote.setUserId(userId);
-		vote.setUuUserId(uuUserId);
-		vote.setUp(true);
-		vote.setDown(null);
-		vote.setIp(getIPAddress());
-		vote.setAgent(getUserAgent());
-		vote.setStatus(Vote.STATUS_NORMAL);
-		vote.setCreated(new Date());
-
-		if (vote.save()) {
-			//更新对应评论的点赞数
-			comment.updateVoteUpCount();
-			renderAjaxResultForSuccess();
-		} else {
-			renderAjaxResultForError();
-		}
-
-	}
-
-	//取消赞
-	private void cancelVoteUp(Comment comment,Vote vote){
-//		BigInteger commentId = getParaToBigInteger("comment_id");
-//		Comment comment = CommentQuery.me().findById(commentId);
-//		BigInteger userId = getParaToBigInteger("user_id");
-//		BigInteger uuUserId = getParaToBigInteger("uu_user_id");
-		vote.setUp(null);
-		if (vote.update()) {
-			//更新对应评论的点赞数
-			comment.updateVoteUpCount();
-			renderAjaxResultForSuccess();
-		} else {
-			renderAjaxResultForError();
-		}
-	}
-
-	//再次赞
-	private void againVoteUp(Comment comment,Vote vote){
-//		BigInteger commentId = getParaToBigInteger("comment_id");
-//		Comment comment = CommentQuery.me().findById(commentId);
-//		BigInteger userId = getParaToBigInteger("user_id");
-//		BigInteger uuUserId = getParaToBigInteger("uu_user_id");
-		vote.setUp(true);
-		if (vote.update()) {
-			//更新对应评论的点赞数
-			comment.updateVoteUpCount();
-			renderAjaxResultForSuccess();
-		} else {
-			renderAjaxResultForError();
-		}
-	}
 
-	//点赞接口
-	public void vote() {
-		BigInteger commentId = getParaToBigInteger("comment_id");
-		Comment comment = CommentQuery.me().findById(commentId);
-		BigInteger userId = getParaToBigInteger("user_id");
-		BigInteger uuUserId = getParaToBigInteger("uu_user_id");
-		if ("0".equals(userId.toString())) {
-			userId = null;
-		}
-		if ("0".equals(uuUserId.toString())) {
-			uuUserId = null;
-		}
-
-		Vote vote = VoteQuery.me().findVote(commentId,userId,uuUserId);
-		//判断此用户是否给此评论点过赞
-		if (vote != null) {
-			if (vote.getUp() == null) {
-				againVoteUp(comment,vote);
-			} else {
-				cancelVoteUp(comment,vote);
-			}
-		} else {
-			voteUp(comment,userId,uuUserId);
-		}
-	}
 
 
 }

+ 124 - 3
jpress-web-front/src/main/java/io/jpress/front/controller/CommentController.java

@@ -15,21 +15,23 @@
  */
 package io.jpress.front.controller;
 
+import com.jfinal.plugin.activerecord.Page;
 import io.jpress.Consts;
 import io.jpress.core.BaseFrontController;
 import io.jpress.core.cache.ActionCacheManager;
 import io.jpress.model.Comment;
 import io.jpress.model.Content;
 import io.jpress.model.User;
-import io.jpress.model.query.ContentQuery;
-import io.jpress.model.query.OptionQuery;
-import io.jpress.model.query.UserQuery;
+import io.jpress.model.Vote;
+import io.jpress.model.query.*;
 import io.jpress.router.RouterMapping;
 import io.jpress.utils.CookieUtils;
 import io.jpress.utils.StringUtils;
 
 import java.math.BigInteger;
+import java.text.SimpleDateFormat;
 import java.util.Date;
+import java.util.List;
 
 @RouterMapping(url = "/comment")
 public class CommentController extends BaseFrontController {
@@ -180,4 +182,123 @@ public class CommentController extends BaseFrontController {
 		}
 	}
 
+	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=\"\" onclick=\"vote("+ comment.getId()+")\"><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=\"\" onclick=\"vote("+ sonComment.getId()+")\"><em>" + sonComment.getVoteUp() + "</em></span>");
+				htmlBuilder.append("</div>");
+				htmlBuilder.append("<p>"+sonComment.getText()+"</p>");
+			}
+			htmlBuilder.append("</div>\n" + "</div>\n" + " </div>");
+		}
+
+		renderAjaxResultForSuccess(htmlBuilder.toString());
+	}
+
+	//新增点赞
+	private void voteUp(Comment comment,BigInteger userId,BigInteger uuUserId){
+//		BigInteger commentId = getParaToBigInteger("comment_id");
+//		Comment comment = CommentQuery.me().findById(commentId);
+//		BigInteger userId = getParaToBigInteger("user_id");
+//		BigInteger uuUserId = getParaToBigInteger("uu_user_id");
+		Vote vote = getModel(Vote.class);
+		vote.setCommentId(comment.getId());
+		vote.setUserId(userId);
+		vote.setUuUserId(uuUserId);
+		vote.setUp(true);
+		vote.setDown(null);
+		vote.setIp(getIPAddress());
+		vote.setAgent(getUserAgent());
+		vote.setStatus(Vote.STATUS_NORMAL);
+		vote.setCreated(new Date());
+
+		if (vote.save()) {
+			//更新对应评论的点赞数
+			comment.updateVoteUpCount();
+			renderAjaxResultForSuccess();
+		} else {
+			renderAjaxResultForError();
+		}
+
+	}
+
+	//取消赞
+	private void cancelVoteUp(Comment comment,Vote vote){
+//		BigInteger commentId = getParaToBigInteger("comment_id");
+//		Comment comment = CommentQuery.me().findById(commentId);
+//		BigInteger userId = getParaToBigInteger("user_id");
+//		BigInteger uuUserId = getParaToBigInteger("uu_user_id");
+		vote.setUp(null);
+		if (vote.update()) {
+			//更新对应评论的点赞数
+			comment.updateVoteUpCount();
+			renderAjaxResultForSuccess();
+		} else {
+			renderAjaxResultForError();
+		}
+	}
+
+	//再次赞
+	private void againVoteUp(Comment comment,Vote vote){
+//		BigInteger commentId = getParaToBigInteger("comment_id");
+//		Comment comment = CommentQuery.me().findById(commentId);
+//		BigInteger userId = getParaToBigInteger("user_id");
+//		BigInteger uuUserId = getParaToBigInteger("uu_user_id");
+		vote.setUp(true);
+		if (vote.update()) {
+			//更新对应评论的点赞数
+			comment.updateVoteUpCount();
+			renderAjaxResultForSuccess();
+		} else {
+			renderAjaxResultForError();
+		}
+	}
+
+	//点赞接口
+	public void vote() {
+		BigInteger commentId = getParaToBigInteger("comment_id");
+		Comment comment = CommentQuery.me().findById(commentId);
+		BigInteger userId = getParaToBigInteger("user_id");
+		BigInteger uuUserId = getParaToBigInteger("uu_user_id");
+		if ("0".equals(userId.toString())) {
+			userId = null;
+		}
+		if ("0".equals(uuUserId.toString())) {
+			uuUserId = null;
+		}
+
+		Vote vote = VoteQuery.me().findVote(commentId,userId,uuUserId);
+		//判断此用户是否给此评论点过赞
+		if (vote != null) {
+			if (vote.getUp() == null) {
+				againVoteUp(comment,vote);
+			} else {
+				cancelVoteUp(comment,vote);
+			}
+		} else {
+			voteUp(comment,userId,uuUserId);
+		}
+	}
+
 }

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

@@ -1,5 +1,5 @@
 <!--手机列表页-->
-<#if USER??> <#assign userId=USER.id></#if>
+<#if USER??> <#assign userId=USER.id><#else><#assign userId=0></#if>
 <!DOCTYPE html>
 <html>
 <head lang="en">
@@ -318,7 +318,7 @@
     timers = null;
     //加载数据
     var LoadingDataFn = function() {
-        $.get("${CPATH}/admin/comment/lazyLoad?ucode=${ucode!}&contId=${(content.id)!}&pagesize=1&pagenumber="+aa, function(result){
+        $.get("${CPATH}/comment/lazyLoad?contId=${(content.id)!}&pagesize=1&pagenumber="+aa, function(result){
             var dom = '';
             dom += result.message;
             $('.footer-section').append(dom);
@@ -345,8 +345,8 @@
 
     //点赞
     var vote = function(commentid) {
-        $.get("${CPATH}/admin/comment/vote?comment_id="+commentid+"&user_id="+userId+"&uu_user_id="+uuUserId, function(result){
-
+        $.get("${CPATH}/comment/vote?comment_id="+commentid+"&user_id="+userId+"&uu_user_id="+uuUserId, function(result){
+            console.log(result.message);
         });
     }
 </script>