Commit 78d0b4cf authored by 蒋林's avatar 蒋林

评论

parent 81d07316
......@@ -7,20 +7,22 @@ require("../class/q_functions.php");
$link=db_connect();
$empire=new mysqlquery();
$mod = isset($_GET['mod']) ? $_GET['mod']: '';
$user = islogin();
if (empty($user)) {
returnmsg(false, '请登录');
}
$mod = isset($_GET['mod']) ? $_GET['mod']: '';
$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, '参数错误');
}
$page = isset($_POST['p']) ? intval($_POST['p']) : 0;
$page = $page ? : 1;
if ($mod == 'create') {
if (!$newsid) {
returnmsg(false, '内容ID参数错误');
}
if (!$content) {
returnmsg(false, '评论内容不能为空');
}
......@@ -28,15 +30,24 @@ if ($mod == 'create') {
createComment($newsid, $content, $user);
} elseif ($mod == 'reply') {
if (!$plid) {
returnmsg(false, '参数错误');
returnmsg(false, '评论ID参数错误');
}
if (!$content) {
returnmsg(false, '评论内容不能为空');
}
replyComment($plid, $content, $user);
} else {
comments($newsid,10);
} elseif($mod == 'like') {
if (!$plid) {
returnmsg(false, '评论ID参数错误');
}
likeComment($plid, $user);
}else {
if (!$newsid) {
returnmsg(false, '内容ID参数错误');
}
comments($newsid,10, $page, $user);
}
//创建评论
......@@ -82,27 +93,54 @@ function replyComment($plid, $content, $user) {
$id = $empire->lastid();
if (!$id) {
returnmsg(false, '评论失败');
returnmsg(false, '回复失败');
} else {
returnmsg(true, '评论成功', comment($id));
returnmsg(true, '回复成功', comment($id));
}
}
//评论列表
function comments($newsid, $num) {
function comments($newsid, $num, $p=1, $user) {
global $empire,$dbtbpre;
$tablecomment = $dbtbpre.'ecms_comments';
$tablemember = $dbtbpre.'enewsmember';
$offset = ($p - 1) * $num;
$sql = "select c.*,u.avatar,u.nickname,u.username from $tablecomment c left join {$tablemember} u on c.user_id=u.userid where newsid={$newsid} and c.reply_id=0 order by c.id desc limit {$num}";
$sql = "select c.*,u.avatar,u.nickname,u.username from $tablecomment c left join {$tablemember} u on c.user_id=u.userid where newsid={$newsid} and c.reply_id=0 order by c.id desc limit {$offset},{$num}";
$query = $empire->query($sql);
$list = [];
while ($row = $empire->fetchassoc($query)) {
$row['islike'] = 0;
$row['reply'] = replyComments($row['id']);
$list[] = handelcomment($row);
}
if ($list) {
$ids = array_unique(array_column($list, 'id'));
$ids = implode(',', $ids);
$tablecommentlike = $dbtbpre.'ecms_comments_likenum';
$sql = "select * from {$tablecommentlike} where user_id={$user['userid']} and comment_id in ({$ids})";
$query = $empire->query($sql);
$commentlist = [];
while ($row = $empire->fetchassoc($query)) {
$commentlist[] = $row;
}
if ($commentlist) {
foreach ($list as $k => $v) {
foreach ($commentlist as $kk => $vv) {
if ($v['id'] == $vv['comment_id']) {
$v['islike'] = 1;
$list[$k] = $v;
}
}
}
}
}
returnmsg(true, '', $list);
}
......@@ -135,6 +173,39 @@ function comment($id) {
return handelcomment($comment);
}
//点赞
function likeComment($plid, $user) {
global $empire,$dbtbpre;
$tablecomment = $dbtbpre.'ecms_comments';
$tablecommentlike = $dbtbpre.'ecms_comments_likenum';
$sql = "select * from {$tablecomment} where id={$plid}";
$comment = $empire->fetchassoc1($sql);
if ($comment['user_id'] == $user['userid']) {
returnmsg(false, '不能赞自己的评论');
}
$sql = "select * from {$tablecommentlike} where comment_id={$plid} and user_id={$user['userid']}";
$commentlike = $empire->fetchassoc1($sql);
if ($commentlike) {
returnmsg(false, '您已赞过该评论');
}
$now = time();
$sql = "insert into {$tablecommentlike} (user_id,liked_uid,comment_id,created_at) VALUES ({$user['userid']}, {$comment['user_id']}, {$plid}, {$now})";
$empire->query($sql);
$id = $empire->lastid();
if ($id) {
$sql = "update {$tablecomment} set like_num=like_num+1 where id={$plid}";
$empire->query($sql);
returnmsg(true, '点赞成功');
} else {
returnmsg(false, '点赞失败');
}
}
function handelcomment($comment) {
$comment['created_at'] = date('Y-m-d H:i', $comment['created_at']);
return $comment;
......
//加载评论
var newsid = $("input[name='newsid']").val();
comments(newsid);
//评论
$(".send-pl").bind("click", function () {
$(".send-pl").click(function () {
var data = {
'newsid': newsid,
'content': $.trim($("textarea[name='content']").val())
......@@ -58,7 +57,7 @@ function tpl(comment) {
<div class="comment-footbar clearfix">\
<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> \
<a href="javascript:likeComment(' + comment.id + ', ' + comment.islike + ');" class="pl-zan"><i class="iconfont '+(comment.islike?"icon-zan-fill":"icon-zan")+'"></i>赞</a> \
</div>';
if (!$.isEmptyObject(comment.reply)) {
......@@ -80,7 +79,7 @@ function popReplyComment(id) {
}
//回复评论
$(".reply-pl").bind("click", function () {
$(".reply-pl").click(function () {
var data = {
'newsid': newsid,
'plid': $("input[name='plid']").val(),
......@@ -119,7 +118,7 @@ function handelReplyComment(plid, comment) {
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);
$(this).children(".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>\
......@@ -130,17 +129,69 @@ function handelReplyComment(plid, comment) {
});
}
//评论点赞
function likeComment(plid, islike) {
if (islike) {
return false;
}
$.post("/e/pl/comments.php?mod=like", {plid: plid}, function (e) {
if (e.status) {
$.each( $('.comment-list li'), function(){
if ($(this).attr('data-id') == plid) {
$(this).find(".icon-zan").addClass("icon-zan-fill").removeClass("icon-zan");
}
});
} else {
pop(e.msg);
}
}, 'json');
}
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');
var currentPage = 1;
var isLoading = false;
var scrollLoad = $("#scrollLoad");
var spinner = "/skin/default/images/spinner-rosetta-blue-26x26.gif";
var preload = new Image();
preload.src = spinner;
function scrollLoading() {
var currentBottom = parseFloat($(window).height()) + parseFloat($(window).scrollTop());
if ($(document).height() <= currentBottom) {
if (!isLoading) {
isLoading = true;
var nextPage = currentPage + 1;
scrollLoad.html("<img src='" + spinner + "'>");
$.post("/e/pl/comments.php", {newsid: newsid, p:currentPage}, function (e) {
if (!$.isEmptyObject(e.data)) {
var comments = e.data;
var html = '';
for (i in comments) {
html += tpl(comments[i]);
}
$(".comment-list").append(html);
currentPage = nextPage;
} else {
scrollLoad.html("全部加载完毕");
$(window).off("scroll", scrollLoading);
setTimeout(function() {
scrollLoad.slideUp();
}, 1000);
}
isLoading = false;
}, 'json');
}
}
});
}
if (scrollLoad.length > 0) {
$(window).on("scroll", scrollLoading);
scrollLoading();
}
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