Commit 81d07316 authored by 蒋林's avatar 蒋林

评论

parent 823f6bce
......@@ -14,21 +14,29 @@ if (empty($user)) {
}
$newsid = isset($_POST['newsid']) ? intval($_POST['newsid']) : 0;
$content = isset($_POST['content']) ? addslashes(trim($_POST['content'])) : '';
$plid = isset($_POST['plid']) ? intval($_POST['plid']) : 0;
if (!$newsid) {
returnmsg(false, 'newsid参数错误');
returnmsg(false, '参数错误');
}
if ($mod == 'create') {
$content = isset($_POST['content']) ? addslashes($_POST['content']) : '';
if ($content == '') {
if (!$content) {
returnmsg(false, '评论内容不能为空');
}
createComment($newsid, $content, $user);
} elseif ($mod == 'reply') {
if (!$plid) {
returnmsg(false, '参数错误');
}
if (!$content) {
returnmsg(false, '评论内容不能为空');
}
replyComment($plid, $content, $user);
} else {
comments($newsid,12);
comments($newsid,10);
}
//创建评论
......@@ -64,9 +72,12 @@ function replyComment($plid, $content, $user) {
if (empty($comment)) {
returnmsg(false, '该评论不存在');
}
if ($comment['user_id'] == $user['userid']) {
returnmsg(false, '不能评论自己');
}
$ip = $_SERVER['REMOTE_ADDR'];
$now = time();
$sql = "insert into {$table} (user_id,classid,newsid,content,ip,created_at,updated_at) VALUES ({$user['userid']}, {$comment['classid']}, {$comment}, '{$content}', '{$ip}', {$now}, {$now})";
$sql = "insert into {$table} (user_id,classid,newsid,content,ip,created_at,updated_at,reply_uid,reply_id) VALUES ({$user['userid']}, {$comment['classid']}, {$comment['newsid']}, '{$content}', '{$ip}', {$now}, {$now}, {$comment['user_id']}, $plid)";
$empire->query($sql);
$id = $empire->lastid();
......@@ -88,12 +99,30 @@ function comments($newsid, $num) {
$list = [];
while ($row = $empire->fetchassoc($query)) {
$row['reply'] = replyComments($row['id']);
$list[] = handelcomment($row);
}
returnmsg(true, '', $list);
}
//回复的评论
function replyComments($plid) {
global $empire,$dbtbpre;
$tablecomment = $dbtbpre.'ecms_comments';
$tablemember = $dbtbpre.'enewsmember';
$sql = "select c.content,c.created_at,u.nickname,u.username from $tablecomment c left join {$tablemember} u on c.user_id=u.userid where reply_id={$plid}";
$query = $empire->query($sql);
$list = [];
while ($row = $empire->fetchassoc($query)) {
$list[] = handelcomment($row);
}
return $list;
}
//获取评论
function comment($id) {
global $empire,$dbtbpre;
......@@ -101,11 +130,13 @@ function comment($id) {
$tablemember = $dbtbpre.'enewsmember';
$sql = "select c.*,u.avatar,u.nickname,u.username from $tablecomment c left join {$tablemember} u on c.user_id=u.userid where c.id={$id} limit 1";
return handelcomment($empire->fetchassoc1($sql));
$comment = $empire->fetchassoc1($sql);
return handelcomment($comment);
}
function handelcomment($comment) {
$comment['created_at'] = date('Y-m-d H:i:s', $comment['created_at']);
$comment['created_at'] = date('Y-m-d H:i', $comment['created_at']);
return $comment;
}
......
//加载评论
var newsid = $("input[name='newsid']").val();
comments(newsid);
......@@ -7,7 +6,7 @@ comments(newsid);
$(".send-pl").bind("click", function () {
var data = {
'newsid': newsid,
'content': $("textarea[name='content']").val()
'content': $.trim($("textarea[name='content']").val())
};
var self = this;
......@@ -38,7 +37,7 @@ $(".send-pl").bind("click", function () {
});
function comments(newsid) {
$.post("/e/pl/comments.php", {newsid:newsid}, function (e) {
$.post("/e/pl/comments.php", {newsid: newsid}, function (e) {
if (!$.isEmptyObject(e.data)) {
var comments = e.data;
var html = '';
......@@ -52,27 +51,96 @@ function comments(newsid) {
function tpl(comment) {
var html = '<li>\
<img src="'+comment.avatar+'">\
<div class="name">'+comment.nickname+'</div>\
<div class="comment-main">'+comment.content+'</div>\
var html = '<li data-id="'+ comment.id +'">\
<img src="' + comment.avatar + '">\
<div class="name">' + comment.nickname + '</div>\
<div class="comment-main">' + comment.content + '</div>\
<div class="comment-footbar clearfix">\
<span>'+comment.created_at+'</span>\
<a href="javascript:replyComment('+comment.id+');" class="pl-reply"><i class="iconfont icon-comment_light"></i>回复</a> \
<a href="javascript:;" class="pl-zan" data-id="'+comment.id+'" style="display: none"><i class="iconfont icon-zan"></i>赞</a> \
</div>\
</li>';
return html;
<span>' + comment.created_at + '</span>\
<a href="javascript:popReplyComment(' + comment.id + ');" class="pl-reply"><i class="iconfont icon-comment_light"></i>回复</a> \
<a href="javascript:;" class="pl-zan" data-id="' + comment.id + '" style="display: none"><i class="iconfont icon-zan"></i>赞</a> \
</div>';
if (!$.isEmptyObject(comment.reply)) {
var replys = comment.reply;
html += '<div class="comment-replay">';
for (i in replys) {
html += '<div class="comment-replay-list"><span>'+replys[i].nickname+':</span>'+replys[i].content+' <span>'+replys[i].created_at+'</span></div>';
}
html += '</div>';
}
return html + '</li>';
}
//回复评论
function replyComment(id) {
//回复弹窗
function popReplyComment(id) {
$('.mask').show();
$(".comment-reply").show();
$("input[name='plid']").val(id);
}
//回复评论
$(".reply-pl").bind("click", function () {
var data = {
'newsid': newsid,
'plid': $("input[name='plid']").val(),
'content': $.trim($("textarea[name='replycontent']").val())
};
var self = this;
if (!data['newsid'] || !data['plid']) {
pop('参数错误');
return false;
}
if (!data['content']) {
pop('评论内容不能为空');
return false;
}
console.log(id);
$(self).attr("disabled", true);
$.post("/e/pl/comments.php?mod=reply", data, function (e) {
$(self).attr("disabled", false);
if (e.status) {
handelReplyComment(data.plid, e.data);
$("textarea[name='replycontent']").val('');
$(".comment-reply").hide();
$('.mask').hide();
} else {
pop(e.msg);
}
}, 'json');
});
function handelReplyComment(plid, comment) {
$.each( $('.comment-list li'), function(){
if ($(this).attr('data-id') == plid) {
var html = '';
if($(this).children(".comment-replay").html()) {
html = '<div class="comment-replay-list"><span>'+comment.nickname+':</span>'+comment.content+' <span>'+comment.created_at+'</span></div>';
$(".comment-replay").append(html);
} else {
html = '<div class="comment-replay">\
<div class="comment-replay-list"><span>'+comment.nickname+':</span>'+comment.content+' <span>'+comment.created_at+'</span></div>\
</div>';
$(this).append(html);
}
}
});
}
function pop(msg) {
alert(msg);
}
$.each( $('.comment-list li'), function(){
if ($(this).attr('data-id') == 19) {
if($(this).children(".comment-replay").html()) {
console.log('3');
} else {
console.log('433');
}
}
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment