From 1c9ffc6d1384f561cd58d9054992479a4045aeba Mon Sep 17 00:00:00 2001 From: "WIN-C27C5MSVTPO\\16961" <1696136552@qq.com> Date: Fri, 28 Nov 2025 11:19:20 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=84=E8=AE=BA=E6=95=B0=E9=87=8F=EF=BC=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/PostCommentController.php | 15 +++++++- app/common/controller/PostController.php | 36 +++++++++++++++++++ app/common/service/PostCommentService.php | 7 ++++ config/route.php | 1 + 4 files changed, 58 insertions(+), 1 deletion(-) diff --git a/app/common/controller/PostCommentController.php b/app/common/controller/PostCommentController.php index ec1da99..b9a174a 100644 --- a/app/common/controller/PostCommentController.php +++ b/app/common/controller/PostCommentController.php @@ -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']); diff --git a/app/common/controller/PostController.php b/app/common/controller/PostController.php index 80504d9..1d64256 100644 --- a/app/common/controller/PostController.php +++ b/app/common/controller/PostController.php @@ -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()); diff --git a/app/common/service/PostCommentService.php b/app/common/service/PostCommentService.php index 495a2ff..e50fdd4 100644 --- a/app/common/service/PostCommentService.php +++ b/app/common/service/PostCommentService.php @@ -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; diff --git a/config/route.php b/config/route.php index c5b18c5..b846ddc 100644 --- a/config/route.php +++ b/config/route.php @@ -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']); // 评论