Skip to content

Commit

Permalink
评论系统更新
Browse files Browse the repository at this point in the history
  • Loading branch information
tidezyc committed May 3, 2013
1 parent 06c3f30 commit 26bd9a3
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 84 deletions.
58 changes: 34 additions & 24 deletions WebRoot/WEB-INF/views/comment.html
Original file line number Diff line number Diff line change
@@ -1,38 +1,48 @@
<div class="com_container">
<div class="com_header">
<span>
评论&nbsp;&nbsp;&nbsp;&nbsp;共${count}条评论
评论&nbsp;&nbsp;&nbsp;&nbsp;共${comment_list.totalRow}条评论
</span>
<a href="#add_comment">
发表评论
</a>
</div>
<#list comment_list.list as comment>
<div class="com_item clearfix">
<a href="http://my.siyanjing.com/${comment.comment_uname}" class="avatar">
<img src="<#if comment.avatar ??>${comment.avatar}<#else>/images/photo.jpg</#if>" width="34" height="34"/>
</a>
<div class="com_cnt">
<p class="meta">
${comment_index+1}楼:${comment.comment_unickname}&nbsp;&nbsp;发布于${comment.comment_date}
<a href="javascript:reply('${comment.comment_uname}','${comment.comment_unickname}')">
回复
</a>
</p>
<p class="cnt">
${comment.comment}
</p>
<div id="comment_content">
<#list comment_list.list as comment>
<div class="com_item clearfix">
<a href="http://my.siyanjing.com/${comment.comment_uname}" class="avatar">
<img src="<#if comment.avatar ??>${comment.avatar}<#else>/images/photo.jpg</#if>" width="34" height="34"/>
</a>
<div class="com_cnt">
<p class="meta">
${(comment_list.pageNumber-1)*comment_list.pageSize+comment_index+1}楼:${comment.comment_unickname}&nbsp;&nbsp;发布于${comment.comment_date}
<a href="javascript:reply('${comment.comment_uname}','${comment.comment_unickname}')">
回复
</a>
</p>
<p class="cnt">
${comment.comment}
</p>
</div>
</div>
</#list>
<div id="comment_page">
<#include "/layout/_paginate.html" />
<@paginate currentPage=comment_list.pageNumber totalPage=comment_list.totalPage actionUrl="javascript:void();#" />
</div>
</div>
</#list>
<div id="comment_add">
<div id="comment_add" class="clearfix">
<#if auth_user??>
<div contenteditable="true" class="editor" name="${target}"></div>
<div class="btn-g blue submit">
发表评论
</div>
<div contenteditable="true" class="editor" name="${target}">
</div>
<div class="btn-g blue submit">
发表评论
</div>
<div id="submit_info">
</div>
<#else>
<a href="/login?redirect=">登录</a>后评论
</#if>
<a href="/login?redirect=">
登录
</a>后评论</#if>
</div>
</div>
51 changes: 32 additions & 19 deletions WebRoot/css/comnon.css
Original file line number Diff line number Diff line change
Expand Up @@ -419,47 +419,60 @@ body {
}

.com_cnt {
margin-left:45px;
margin-left: 45px;
}

.com_cnt .meta {
}

.com_cnt .cnt {
padding-left:3px;
padding-left: 3px;
}

#comment_add {
padding: 10px 0 0 5px;
}

#comment_add .editor {
width: 450px;
min-height: 100px;
_height: 100px;
padding:5px;
word-wrap: break-word;
width: 450px;
min-height: 100px;
_height: 100px;
padding: 5px;
word-wrap: break-word;
overflow-x: hidden;
overflow-y: auto;
_overflow-y: visible;
border: 1px solid #a0b3d6;
border: 1px solid #a0b3d6;
font-size: 12px;
outline: 0;
outline: 0;
}

#comment_add .editor .metion{
vertical-align: baseline;
cursor: text;
display: inline-block;
font-size: 1em;
border: none;
background: none;
overflow: visible;
padding: 0;
margin: 0 1px;
#comment_add .editor .metion {
vertical-align: baseline;
cursor: text;
display: inline-block;
font-size: 1em;
border: none;
background: none;
overflow: visible;
padding: 0;
margin: 0 1px;
}

#comment_add .submit {
margin-top: 5px;
float:left;
}

#submit_info {
width: 100px;
float:left;
margin:10px;
}

/*
* comment END
*/
.footer {
width: 100%;
padding: 20px 0;
Expand Down
76 changes: 57 additions & 19 deletions WebRoot/js/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,9 @@
*/
$com_dis = $('#comment_display');
$com_id = $com_dis.attr('rel');
$.ajax({
type: "get",
url: "/comment/" + $com_id,
beforeSend: function(XMLHttpRequest){
$com_dis.append('<div id="com_loading"><img src="/images/loading.gif"/></div>');
},
success: function(data, status){
$com_dis.html(data);
},
complete: function(XMLHttpRequest, textStatus){
//HideLoading();
},
error: function(){
$com_dis.html('加载评论粗无');
}
});
$locked = false;

function reply(uname, nickname){
var reply = function(uname, nickname){
$editor = $('#comment_add .editor');
var at = document.createElement('button');
at.name = '@{uname:' + uname + ',unickname:' + nickname + '}';
Expand All @@ -33,9 +18,62 @@ function reply(uname, nickname){
$editor.focus();
}

/*
var submit_form = function(){
if ($locked) {
return;
}
$locked = true;
$clone = $('#comment_add .editor').clone();
$button = $clone.children('button.metion');
$name = $button.attr('name');
$button.remove();
$.ajax({
type: "post",
url: "/comment/add/",
data: {
'target': $com_id,
'motion': $name,
'comment': $clone.html()
},
beforeSend: function(XMLHttpRequest){
$('#submit_info').html("").append('<img src="/images/loading.gif"/>');
},
success: function(data, status){
$('#submit_info').html("").append(data);
comment_load(false, $('#comment_page .selected').html());
$locked = false;
},
error: function(){
$('#submit_info').html("").append("评论系统出错");
}
});
}

* 光标最后 END
var comment_load = function(cache, page){
$url = cache ? "/comment/" + $com_id + "-" + page : "/comment/update/" + $com_id + "-" + page;
$.ajax({
type: "get",
url: $url,
beforeSend: function(XMLHttpRequest){
$com_dis.append('<div id="com_loading"><img src="/images/loading.gif"/></div>');
},
success: function(data, status){
$com_dis.html(data);
$('#comment_add .submit').click(function(){
submit_form();
});
$('#comment_page a').click(function(e){
comment_load(true, $(this).attr('href').split('#')[1]);
});
},
error: function(){
$com_dis.html('加载评论错误');
}
});
}

comment_load(true, 1);// 初始化加载评论
/*
* 光标最后 END
*/

72 changes: 50 additions & 22 deletions src/me/thinkjet/controller/CommentController.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,74 @@
package me.thinkjet.controller;

import java.util.HashMap;
import java.util.Map;

import me.thinkjet.auth.AuthManager;
import me.thinkjet.auth.AuthUser;
import me.thinkjet.model.Comment;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.ehcache.CacheInterceptor;
import com.jfinal.plugin.ehcache.CacheName;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.ehcache.CacheKit;

/**
* CommonController
*/
@ControllerBind(controllerKey = "/comment", viewPath = "/")
public class CommentController extends Controller {
@Before(CacheInterceptor.class)
@CacheName("comment")
public void index() {
String target = this.getPara(0);
int page = this.getParaToInt(1, 1);
this.setAttr("target", target);
this.setAttr("page", page);
this.setAttr("comment_list", Comment.dao.paginateByCache("comment",
target + "_" + page, page, 6, "select c.*,u.avatar as avatar",
target + "_" + page, page, 20, "select c.*,u.avatar as avatar",
"from comment c left join users u "
+ "on c.comment_uname=u.username "
+"where c.target=? "
+ "order by c.id asc", target));
this.setAttr(
"count",
Comment.dao.findByCache("comment", "count_" + target,
"select count(id) from comment where target=?", target)
.get(0).getAttrValues()[0]);
System.out.println(Comment.dao.paginateByCache("comment",
target + "_" + page, page, 6, "select c.*,u.avatar",
"as avatar from comment c left join users u "
+ "on c.target=? and c.comment_uname=u.username "
+ "order by c.id asc", target));
+ "where c.target=? " + "order by c.id asc", target));
this.render("comment.html");
}
public void add(){
Comment comment = getModel(Comment.class);
comment.save();

public void update() {
String target = this.getPara(0);
int page = this.getParaToInt(1, 1);
this.setAttr("target", target);
this.setAttr("page", page);
Page<Comment> comments = Comment.dao.paginate(page, 20,
"select c.*,u.avatar as avatar",
"from comment c left join users u "
+ "on c.comment_uname=u.username "
+ "where c.target=? " + "order by c.id asc", target);
CacheKit.put("comment", target + "_" + page, comments);
this.setAttr("comment_list", comments);
this.render("comment.html");
}

public void add() {
Map<String, Object> attrs = new HashMap<String, Object>();
if (this.getPara("target") == null || this.getPara("comment") == null
|| AuthManager.getSession(this) == null) {
this.renderHtml("评论失败");
return;
}
attrs.put("target", this.getPara("target"));
attrs.put("comment", this.getPara("comment"));
AuthUser user = AuthManager.getSession(this);
attrs.put("comment_uname", user.getUser().getStr("username"));
attrs.put("comment_unickname", user.getUser().getStr("name") == null
|| user.getUser().getStr("name").equals("") ? user.getUser()
.getStr("username") : user.getUser().getStr("name"));
if (this.getPara("motion") != null) {
String motion = this.getPara("motion");
String[] motions = motion.substring(2, motion.length() - 1).split(
",");
String uname = motions[0].split(":")[1];
String unickname = motions[1].split(":")[1];
attrs.put("at_uname", uname);
attrs.put("at_unickname", unickname);
}
new Comment().setAttrs(attrs).save();
this.renderHtml("评论成功!");
}

}

0 comments on commit 26bd9a3

Please sign in to comment.