From 4d94614d407d13bf64050164a3340ab7d6aba099 Mon Sep 17 00:00:00 2001 From: "WIN-C27C5MSVTPO\\16961" <1696136552@qq.com> Date: Wed, 10 Dec 2025 10:29:41 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E5=B8=96=E5=AD=90=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=202=E3=80=81=E6=94=B6=E8=97=8F=E6=90=9C=E7=B4=A2=203=E3=80=81?= =?UTF-8?q?=E5=B8=96=E5=AD=90=E3=80=81=E8=AF=84=E8=AE=BA=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/PostCommentController.php | 3 +- app/common/controller/PostController.php | 16 +++++++ .../controller/PostFavoriteController.php | 3 +- app/common/model/Post.php | 2 +- app/common/model/PostFavorite.php | 5 ++ app/common/service/PostCommentService.php | 8 ++-- app/common/service/PostFavoriteService.php | 19 ++++++++ app/common/service/PostService.php | 48 ++++++++++++++++++- config/route.php | 1 + 9 files changed, 98 insertions(+), 7 deletions(-) diff --git a/app/common/controller/PostCommentController.php b/app/common/controller/PostCommentController.php index 801e6c7..b2689b0 100644 --- a/app/common/controller/PostCommentController.php +++ b/app/common/controller/PostCommentController.php @@ -70,7 +70,8 @@ class PostCommentController extends BaseController public function delete(Request $request) { try { - $count = $this->service->serviceDelete($request); + $param = $request->all(); + $count = $this->service->serviceDelete($request,'mini'); return $this->success([], "删除评论成功"); } catch (\Throwable $e) { return $this->error($e->getMessage().$e->getLine().$e->getFile()); diff --git a/app/common/controller/PostController.php b/app/common/controller/PostController.php index 0300fa3..dfadd96 100644 --- a/app/common/controller/PostController.php +++ b/app/common/controller/PostController.php @@ -196,4 +196,20 @@ class PostController extends BaseController } return $data; } + + /** + * 删除 + * + * @param Request $request 请求对象 + * @return mixed 返回删除操作结果 + */ + public function destroy(Request $request) + { + try { + $count = $this->service->serviceDelete($request, 'mini'); + return $this->success([], "成功删除了 {$count} 条"); + } catch (\Throwable $e) { + return $this->error($e->getMessage()); + } + } } \ No newline at end of file diff --git a/app/common/controller/PostFavoriteController.php b/app/common/controller/PostFavoriteController.php index 223d2ea..46b46cb 100644 --- a/app/common/controller/PostFavoriteController.php +++ b/app/common/controller/PostFavoriteController.php @@ -30,7 +30,8 @@ class PostFavoriteController extends BaseController public function self(Request $request) { try { - $raw = $this->service->serviceSelfList($request); +// $raw = $this->service->serviceSelfList($request); + $raw = $this->service->serviceList($request); $data = $this->transformData($raw, method_exists($this, 'indexFormat') ? [$this, 'indexFormat'] : null); diff --git a/app/common/model/Post.php b/app/common/model/Post.php index a67a269..3ccca08 100644 --- a/app/common/model/Post.php +++ b/app/common/model/Post.php @@ -40,7 +40,7 @@ class Post extends Model /** * 允许自动填充模型字段 */ - protected $fillable = ['id', 'user_id', 'category_id', 'content', 'location_name', 'latitude', 'longitude', 'status', 'sort', 'created_at', 'updated_at']; + protected $fillable = ['id', 'user_id', 'category_id', 'content', 'location_name', 'latitude', 'longitude', 'status', 'sort', 'like', 'favorite', 'comment', 'browse', 'created_at', 'updated_at']; /** * 不允许自动填充模型字段 diff --git a/app/common/model/PostFavorite.php b/app/common/model/PostFavorite.php index d442133..f7b66b8 100644 --- a/app/common/model/PostFavorite.php +++ b/app/common/model/PostFavorite.php @@ -46,4 +46,9 @@ class PostFavorite extends Model * 不允许自动填充模型字段 */ protected $guarded = []; + + public function post() + { + return $this->hasOne('app\common\model\Post', 'id', 'post_id'); + } } \ No newline at end of file diff --git a/app/common/service/PostCommentService.php b/app/common/service/PostCommentService.php index 57bf76b..dbf4fb8 100644 --- a/app/common/service/PostCommentService.php +++ b/app/common/service/PostCommentService.php @@ -129,12 +129,10 @@ class PostCommentService $post->save(); } - - /** * 添加后置处理 */ - public function serviceDelete(Request $request) + public function serviceDelete(Request $request,$scene = 'admin') { $param = $request->all(); @@ -146,6 +144,10 @@ class PostCommentService $where['id'] = $param['id']; $comment = PostComment::where($where)->first(); + if($scene == 'mini' && $comment['user_id'] != $request->user->uid){ + throw new \Exception("只能删除本人评论"); + } + $post = Post::where(['id' => $comment['post_id']])->first(); $post['comment'] = $post['comment']-1; $post->save(); diff --git a/app/common/service/PostFavoriteService.php b/app/common/service/PostFavoriteService.php index c9da19a..033b8c8 100644 --- a/app/common/service/PostFavoriteService.php +++ b/app/common/service/PostFavoriteService.php @@ -46,6 +46,25 @@ class PostFavoriteService return $this->postProcess(ucfirst($scene), $result); } + public function complexQueryIndex(Request $request) + { + $param = $request->all(); + $limit = (int)$request->get('size', 15); + $page = (int)$request->get('current', 1); + $query = PostFavorite::with(['post']) + ->where('user_id',$request->user->uid) + ->whereHas('post', function ($q) use ($param) { + // 固定过滤 status = 1 + $q->where('status', 1); + + // 关键字过滤(如果存在) + if (!empty($param['keyword'])) { + $q->where('content', 'like', "%{$param['keyword']}%"); + } + }); + return $query->paginate($limit, ['*'], 'page', $page); + } + /** * 构造 CrudUtil 查询配置 */ diff --git a/app/common/service/PostService.php b/app/common/service/PostService.php index 825c9a2..e3db7a4 100644 --- a/app/common/service/PostService.php +++ b/app/common/service/PostService.php @@ -67,9 +67,19 @@ class PostService 'between', [$from, $to] ]; } + if (isset($p['keyword']) && $p['keyword']) { + $config['where']['content'] = ['like', '%'.$p['keyword'].'%']; + } $config['where']['status'] = 1; $config['order']['sort'] = 'desc'; - $config['order']['id'] = 'desc'; + + if (isset($p['order']) && $p['order'] == 'favorite') { + $config['order']['favorite'] = 'desc'; + } elseif (isset($p['order']) && $p['order'] == 'browse') { + $config['order']['browse'] = 'desc'; + } else { + $config['order']['id'] = 'desc'; + } break; case 'show': $config['where']['id'] = $p['id']; @@ -154,6 +164,20 @@ class PostService } } + /** + * 添加前置处理 + */ + public function beforeServiceShow($data, Request $request) + { + $post = Post::find($data['id']); + if(!$post){ + $post->browse = $post->browse+1; + $post->save(); + } + + return $data; + } + function getMediaTypeByUrl(string $url): int { // 提取后缀 @@ -172,5 +196,27 @@ class PostService } return 0; // 未知类型 + } /** + * 添加后置处理 + */ + public function serviceDelete(Request $request,$scene = 'admin') + { + $param = $request->all(); + + if (empty($param['id'])) { + throw new \Exception("参数错误"); + } + + $where = []; + $where['id'] = $param['id']; + $post = Post::where($where)->first(); + + if($scene == 'mini' && $post['user_id'] != $request->user->uid){ + throw new \Exception("只能删除本人发布内容"); + } + + $count = $post->delete(); + + return $count; } } \ No newline at end of file diff --git a/config/route.php b/config/route.php index b846ddc..eb5a6d7 100644 --- a/config/route.php +++ b/config/route.php @@ -59,6 +59,7 @@ Route::group('/common', function () { 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']); + Route::any('/destroy', [app\common\controller\PostController::class, 'destroy']); // 评论 Route::group('/comment', function () {