评论数量,

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
@@ -2,6 +2,7 @@
namespace app\common\controller;
use app\common\controller\BaseController;
use app\common\model\PostComment;
use app\common\model\User;
use app\common\service\OssService;
use app\common\traits\crud\CrudControllerTrait;
@@ -31,10 +32,22 @@ class PostCommentController extends BaseController
$data['created_at'] = date('Y-m-d H:i:s', $data['created_at']);
if (isset($data['updated_at']))
$data['updated_at'] = date('Y-m-d H:i:s', $data['updated_at']);
if (isset($data['id'])){
$data['reply']['child'] = PostComment::where(['parent_id' => $data['id']])->first();
$data['reply']['count'] = PostComment::where(['parent_id' => $data['id']])->count();
$child = $data['reply']['child'];
if (isset($child['user_id'])){
$ossservice = new OssService();
$child['user'] = User::where('id', $child['user_id'])->select(['name', 'avatar', 'openid'])->first();
if(!str_contains($child['user']['avatar'], 'http') && !empty($child['user']['avatar'])){
$child['user']['avatar_pull'] = $ossservice->generatePrivateUrl($child['user']['avatar']);
}
}
$data['reply']['child'] = $child;
}
if (isset($data['user_id'])){
$ossservice = new OssService();
$data['user'] = User::where('id', $data['user_id'])->select(['name', 'avatar', 'openid'])->first();
// $data['user']['avatar'] = $data['user']['avatar'];
if(!str_contains($data['user']['avatar'], 'http') && !empty($data['user']['avatar'])){
$data['user']['avatar_pull'] = $ossservice->generatePrivateUrl($data['user']['avatar']);
+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());
@@ -68,6 +68,10 @@ class PostCommentService
$config['where']['root_id'] = $p['parent_id'];
}
// 根评论ID
if (isset($p['received_id']) && !empty($p['received_id'])) {
$config['where']['id'] = ['not in', $p['parent_id']];
}
// 根评论ID
if (isset($p['status']) && !empty($p['status'])) {
$config['where']['status'] = $p['status'];
}
@@ -100,6 +104,9 @@ class PostCommentService
$data['root_id'] = 0;
if(!empty($data['parent_id'])){
$data['root_id'] = PostComment::where('id', $data['parent_id'])->value('root_id');
if($data['root_id'] == 0){
$data['root_id'] = $data['parent_id'];
}
}
return $data;
+1
View File
@@ -57,6 +57,7 @@ Route::group('/common', function () {
Route::group('/post', function () {
Route::any('/list', [app\common\controller\PostController::class, 'index']);
Route::any('/show', [app\common\controller\PostController::class, 'show']);
Route::any('/interaction', [app\common\controller\PostController::class, 'interaction']);
Route::any('/store', [app\admin\controller\PostController::class, 'store']);
// 评论