评论数量,

This commit is contained in:
2025-11-28 11:19:20 +08:00
parent bf18a31d5f
commit 1c9ffc6d13
4 changed files with 58 additions and 1 deletions
+36
View File
@@ -2,6 +2,7 @@
namespace app\common\controller;
use app\common\controller\BaseController;
use app\common\model\Post;
use app\common\model\PostComment;
use app\common\model\PostFavorite;
use app\common\model\PostLike;
@@ -88,7 +89,32 @@ class PostController extends BaseController
return $data;
}
/**
* 获取评论,点赞,收藏数量
*
* @param Request $request 请求对象
* @param int|string $id 资源 ID
* @return mixed 返回资源详情数据
*/
public function interaction(Request $request)
{
try {
$param = $request->all();
$data = Post::where('id', $param['id'])->select(['like','favorite','comment'])->first();
$where = [
'post_id' => $data['id'],
'user_id' => $request->user->uid
];
$data['is_like'] = PostLike::where($where)->count();
$data['is_favorite'] = PostFavorite::where($where)->count();
$data['is_comment'] = PostComment::where($where)->count();
return $this->success($data);
} catch (\Throwable $e) {
return $this->error($e->getMessage().$e->getFile().$e->getLine());
}
}
/**
* 获取详情
@@ -104,6 +130,16 @@ class PostController extends BaseController
$data = $this->transformData($raw, method_exists($this, 'showFormat')
? [$this, 'showFormat']
: null);
$where = [
'post_id' => $data['id'],
'user_id' => $request->user->uid
];
$data['is_like'] = PostLike::where($where)->count();
$data['is_favorite'] = PostFavorite::where($where)->count();
$data['is_comment'] = PostComment::where($where)->count();
return $this->success($data);
} catch (\Throwable $e) {
return $this->error($e->getMessage().$e->getFile().$e->getLine());