点赞接口

//新闻点赞
public function praise()
{
if (!is_login()) {
$this->returnBack(2, '请先登录');
}
$id = I('post.jour_id'); //新闻id
$type = I('post.type'); //点赞状态
$article = M('app_journalism')->field('praise')->where(['id' => $id])->find();

if ($article) {
if ($type == 1) {
$praise = M('app_jour')->field('id')->where(['jour_id' => $id, 'user_id' => $_SESSION['onethink_home']['user_auth']['uid']])->find();

if ($praise) {
$this->returnBack(1, '您已经点赞过了');
}
$re = M('app_jour')->add(array(
'user_id' => $_SESSION['onethink_home']['user_auth']['uid'],
'jour_id' => $id,
'starttime' => time(),
));
$praise = $article['praise'] + 1;
} elseif ($type == 2) {
$re = M('app_jour')->where(['user_id' => $_SESSION['onethink_home']['user_auth']['uid'], 'article_id' => $id])->delete();
$praise = $article['praise'] - 1;
}

if ($re) {
M('app_journalism')->where(['id' => $id])->save(['praise' => $praise]);
$this->returnBack(0, '成功', array('num' => $praise));
}
}
$this->returnBack(1, '失败');
}
posted @ 2020-01-11 14:31  php毛宏历的博客  阅读(488)  评论(0编辑  收藏  举报