TaoLer/app/index/controller/Comment.php

109 lines
2.6 KiB
PHP
Raw Normal View History

2020-01-01 13:17:19 +08:00
<?php
namespace app\index\controller;
use app\common\controller\BaseController;
use think\facade\Session;
2022-08-02 18:46:05 +08:00
use think\facade\Cache;
2020-01-01 13:17:19 +08:00
use app\common\model\Comment as CommentModel;
use app\common\model\Article;
use app\common\model\UserZan;
class Comment extends BaseController
{
//采纳评论
2020-04-24 15:07:26 +08:00
public function jiedaCai()
{
2020-01-01 13:17:19 +08:00
$id = input('id');
$comms = CommentModel::find($id);
$result = $comms->save(['cai' =>1]);
$res = [];
if($result){
$art = Article::find($comms['article_id']);
$jie = $art->save(['jie' => 1]);
if($jie){
$res['status'] = 0;
}
2022-08-02 18:46:05 +08:00
// 清除文章tag缓存
Cache::tag('tagArtDetail')->clear();
2020-01-01 13:17:19 +08:00
}
return json($res);
}
//删除评论
2020-04-24 15:07:26 +08:00
public function jiedaDelete()
{
2020-01-01 13:17:19 +08:00
$id = input('id');
//$arid = intval($id);
$comms = CommentModel::find($id);
$result = $comms->delete();
if($result){
2020-04-24 15:07:26 +08:00
$res = ['status' => 0,'msg' => '删除成功'];
} else {
$res = ['status' => -1,'msg' => '删除失败'];
2020-01-01 13:17:19 +08:00
}
return json($res);
}
2020-04-24 15:07:26 +08:00
2020-01-01 13:17:19 +08:00
//编辑评论
public function getDa()
{
2020-04-24 15:07:26 +08:00
//获取原评论
2020-01-01 13:17:19 +08:00
$this->isLogin();
$id = input('id');
$comms = CommentModel::find($id);
$res['rows'] = [];
if($comms) {
2020-10-12 13:15:02 +08:00
$res['status'] = 0;
2020-01-01 13:17:19 +08:00
$res['rows']['content'] = $comms['content'];
}
return json($res);
}
2020-02-25 15:40:06 +08:00
//更新评论
2020-01-01 13:17:19 +08:00
public function updateDa()
{
$this->isLogin();
$id = input('id');
$content = input('content');
$comms = CommentModel::find($id);
$result = $comms->save(['content' => $content]);
if($result) {
$res['status'] = 0;
$res['msg'] = '更新成功';
} else {
$res['msg'] = '更新失败';
}
return json($res);
}
2020-04-24 15:07:26 +08:00
//评论点赞
2020-01-01 13:17:19 +08:00
public function jiedaZan()
{
$this->isLogin();
$data['comment_id'] = input('post.id');
$data['user_id'] = session('user_id');
//查询是否已存在点赞
$zan = UserZan::where(['comment_id'=>input('post.id'),'user_id'=>session('user_id')])->find();
2020-09-24 17:16:18 +08:00
if(!$zan){ //如果没有点过赞执行点赞操作
2020-01-01 13:17:19 +08:00
$coms = CommentModel::find(input('post.id'));
if($coms['user_id'] == session('user_id')){
2020-04-24 15:07:26 +08:00
$res = ['msg' => '不能给自己点赞哦'];
2020-01-01 13:17:19 +08:00
} else {
2020-04-24 15:07:26 +08:00
$result = UserZan::create($data);
if($result){
2020-01-01 13:17:19 +08:00
//评论点赞数加1
$coms->save(['zan' => $coms['zan']+1]);
2020-04-24 15:07:26 +08:00
$res = ['status' => 0, 'msg' => '点赞成功'];
2020-01-01 13:17:19 +08:00
}else {
2020-04-24 15:07:26 +08:00
$res = ['status' => -1, 'msg' => '点赞失败'];
2020-01-01 13:17:19 +08:00
}
}
} else {
2020-09-24 17:16:18 +08:00
Session::set('ok',$zan['comment_id']);
2020-04-24 15:07:26 +08:00
$res = ['status'=>-1,'msg' => '你已赞过了'];
2020-01-01 13:17:19 +08:00
}
2020-04-24 15:07:26 +08:00
return json($res);
}
2020-01-01 13:17:19 +08:00
}