diff --git a/app/common/controller/PostController.php b/app/common/controller/PostController.php index b06bfcc..80504d9 100644 --- a/app/common/controller/PostController.php +++ b/app/common/controller/PostController.php @@ -88,6 +88,28 @@ class PostController extends BaseController return $data; } + + + /** + * 获取详情 + * + * @param Request $request 请求对象 + * @param int|string $id 资源 ID + * @return mixed 返回资源详情数据 + */ + public function show(Request $request) + { + try { + $raw = $this->service->serviceShow($request); + $data = $this->transformData($raw, method_exists($this, 'showFormat') + ? [$this, 'showFormat'] + : null); + return $this->success($data); + } catch (\Throwable $e) { + return $this->error($e->getMessage().$e->getFile().$e->getLine()); + } + } + /** * 列表页数据格式化 * @@ -100,8 +122,21 @@ class PostController 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['media'] = PostMedia::where('post_id', $data['id'])->get(); + $ossservice = new OssService(); + if (isset($data['id'])){ + $data['media'] = PostMedia::where('post_id', $data['id'])->get()->toArray(); + foreach ($data['media'] as &$media) { + $media['url_pull'] = $ossservice->generatePrivateUrl($media['url']); + } + } + if (isset($data['user_id'])){ + $data['user'] = User::where('id', $data['user_id'])->first(); + unset($data['user']['password']); + $data['user']['avatar_pull'] = $data['user']['avatar']; + if(!str_contains($data['user']['avatar'], 'http') && !empty($data['user']['avatar'])){ + $data['user']['avatar_pull'] = $ossservice->generatePrivateUrl($data['user']['avatar']); + } + } return $data; } } \ No newline at end of file diff --git a/app/common/service/PostFavoriteService.php b/app/common/service/PostFavoriteService.php index 25ec4c6..492f3d8 100644 --- a/app/common/service/PostFavoriteService.php +++ b/app/common/service/PostFavoriteService.php @@ -126,9 +126,12 @@ class PostFavoriteService $where['user_id'] = $param['user_id']; $count = PostFavorite::where($where)->delete(); - $post = PostFavorite::where(['id' => $param['post_id']])->first(); - $post['favorite'] = $post['favorite']-1; - $post->save(); + if ($count) { + $post = PostFavorite::where(['id' => $param['post_id']])->first(); + $post['favorite'] = $post['favorite']-1; + $post['favorite'] = $post['favorite']<0?0:$post['favorite']; + $post->save(); + } return $count; } diff --git a/app/common/service/PostLikeService.php b/app/common/service/PostLikeService.php index 770af9e..1b58d09 100644 --- a/app/common/service/PostLikeService.php +++ b/app/common/service/PostLikeService.php @@ -126,9 +126,12 @@ class PostLikeService $where['user_id'] = $param['user_id']; $count = PostLike::where($where)->delete(); - $post = Post::where(['id' => $param['post_id']])->first(); - $post['like'] = $post['like']-1; - $post->save(); + if ($count) { + $post = Post::where(['id' => $param['post_id']])->first(); + $post['like'] = $post['like']-1; + $post['like'] = $post['like']<0?0:$post['like']; + $post->save(); + } return $count; } diff --git a/config/route.php b/config/route.php index 1b8b3b5..c5b18c5 100644 --- a/config/route.php +++ b/config/route.php @@ -56,6 +56,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('/store', [app\admin\controller\PostController::class, 'store']); // 评论