|
|
@@ -22,8 +22,10 @@ import io.jpress.core.addon.HookInvoker;
|
|
|
import io.jpress.core.cache.ActionCache;
|
|
|
import io.jpress.model.Content;
|
|
|
import io.jpress.model.Taxonomy;
|
|
|
+import io.jpress.model.Vote;
|
|
|
import io.jpress.model.query.ContentQuery;
|
|
|
import io.jpress.model.query.TaxonomyQuery;
|
|
|
+import io.jpress.model.query.VoteQuery;
|
|
|
import io.jpress.router.RouterMapping;
|
|
|
import io.jpress.template.TemplateManager;
|
|
|
import io.jpress.template.TplModule;
|
|
|
@@ -34,6 +36,7 @@ import io.jpress.ui.freemarker.tag.PreviousContentTag;
|
|
|
import io.jpress.utils.StringUtils;
|
|
|
|
|
|
import java.math.BigInteger;
|
|
|
+import java.util.Date;
|
|
|
import java.util.List;
|
|
|
|
|
|
@RouterMapping(url = Consts.ROUTER_CONTENT)
|
|
|
@@ -194,4 +197,77 @@ public class ContentController extends BaseFrontController {
|
|
|
HookInvoker.contentRenderAfter(this);
|
|
|
}
|
|
|
|
|
|
+ //文章点赞接口
|
|
|
+ public void vote() {
|
|
|
+ BigInteger contentId = getParaToBigInteger("content_id");
|
|
|
+ Content content = ContentQuery.me().findById(contentId);
|
|
|
+ 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().findContentVote(contentId,userId,uuUserId);
|
|
|
+ //判断此用户是否给此评论点过赞
|
|
|
+ if (vote != null) {
|
|
|
+ if (vote.getUp() == null) {
|
|
|
+ againVoteUp(content,vote);
|
|
|
+ } else {
|
|
|
+ cancelVoteUp(content,vote);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ voteUp(content,userId,uuUserId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //新增点赞
|
|
|
+ private void voteUp(Content content,BigInteger userId,BigInteger uuUserId){
|
|
|
+ Vote vote = getModel(Vote.class);
|
|
|
+ vote.setContentId(content.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()) {
|
|
|
+ //更新对应文章的点赞数
|
|
|
+ content.updateVoteUpCount("up");
|
|
|
+ renderAjaxResultForSuccess();
|
|
|
+ } else {
|
|
|
+ renderAjaxResultForError();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //取消赞
|
|
|
+ private void cancelVoteUp(Content content,Vote vote){
|
|
|
+ vote.setUp(null);
|
|
|
+ if (vote.update()) {
|
|
|
+ //更新对应评论的点赞数
|
|
|
+ content.updateVoteUpCount("down");
|
|
|
+ renderAjaxResultForSuccess();
|
|
|
+ } else {
|
|
|
+ renderAjaxResultForError();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //再次赞
|
|
|
+ private void againVoteUp(Content content,Vote vote){
|
|
|
+ vote.setUp(true);
|
|
|
+ if (vote.update()) {
|
|
|
+ //更新对应评论的点赞数
|
|
|
+ content.updateVoteUpCount("up");
|
|
|
+ renderAjaxResultForSuccess();
|
|
|
+ } else {
|
|
|
+ renderAjaxResultForError();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|