diff --git a/app/common/controller/PostFavoriteController.php b/app/common/controller/PostFavoriteController.php index 27f0e6e..93e76e6 100644 --- a/app/common/controller/PostFavoriteController.php +++ b/app/common/controller/PostFavoriteController.php @@ -50,4 +50,20 @@ class PostFavoriteController extends BaseController $data['updated_at'] = date('Y-m-d H:i:s', $data['updated_at']); return $data; } + + /** + * 删除 + * + * @param Request $request 请求对象 + * @return mixed 返回删除操作结果 + */ + public function delete(Request $request) + { + try { + $count = $this->service->servicedelete($request); + return $this->success([], "取消收藏成功"); + } catch (\Throwable $e) { + return $this->error($e->getMessage()); + } + } } \ No newline at end of file diff --git a/app/common/controller/PostLikeController.php b/app/common/controller/PostLikeController.php index ceea299..223b79b 100644 --- a/app/common/controller/PostLikeController.php +++ b/app/common/controller/PostLikeController.php @@ -36,6 +36,22 @@ class PostLikeController extends BaseController } } + /** + * 删除 + * + * @param Request $request 请求对象 + * @return mixed 返回删除操作结果 + */ + public function delete(Request $request) + { + try { + $count = $this->service->servicedelete($request); + return $this->success([], "取消点赞成功"); + } catch (\Throwable $e) { + return $this->error($e->getMessage()); + } + } + /** * 列表页数据格式化 * diff --git a/app/common/service/PostFavoriteService.php b/app/common/service/PostFavoriteService.php index 46e59ae..2359c1f 100644 --- a/app/common/service/PostFavoriteService.php +++ b/app/common/service/PostFavoriteService.php @@ -4,6 +4,7 @@ namespace app\common\service; use app\common\model\MessageNotice; use app\common\model\Post; +use app\common\model\PostLike; use app\common\validate\PostFavoriteValidate; use app\common\traits\crud\CrudServiceTrait; use app\common\model\PostFavorite; @@ -107,4 +108,28 @@ class PostFavoriteService $post['favorite'] = $post['favorite']+1; $post->save(); } + + + /** + * 添加后置处理 + */ + public function servicedelete(Request $request) + { + $param = $request->all(); + + if (empty($param['post_id']) && empty($param['user_id'])) { + throw new \Exception("参数错误"); + } + + $where = []; + $where['post_id'] = $param['post_id']; + $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(); + + return $count; + } } \ No newline at end of file diff --git a/app/common/service/PostLikeService.php b/app/common/service/PostLikeService.php index 597fc8d..d2bad4b 100644 --- a/app/common/service/PostLikeService.php +++ b/app/common/service/PostLikeService.php @@ -109,4 +109,27 @@ class PostLikeService $post['like'] = $post['like']+1; $post->save(); } + + /** + * 添加后置处理 + */ + public function servicedelete(Request $request) + { + $param = $request->all(); + + if (empty($param['post_id']) && empty($param['user_id'])) { + throw new \Exception("参数错误"); + } + + $where = []; + $where['post_id'] = $param['post_id']; + $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(); + + return $count; + } } \ No newline at end of file diff --git a/app/common/service/UserService.php b/app/common/service/UserService.php index b715810..802aed2 100644 --- a/app/common/service/UserService.php +++ b/app/common/service/UserService.php @@ -125,7 +125,6 @@ class UserService 'openid' => $data['openid'], ]; $wechatUserInfo = $logic->getWechatUserInfo($wechat); - var_dump($wechatUserInfo); $uData = [ 'nickname' => $wechatUserInfo['nickname'], 'avatar' => $wechatUserInfo['headimgurl'], diff --git a/config/route.php b/config/route.php index a1d9425..9680098 100644 --- a/config/route.php +++ b/config/route.php @@ -69,14 +69,14 @@ Route::group('/common', function () { Route::group('/like', function () { Route::any('/list', [app\common\controller\PostLikeController::class, 'index']); Route::any('/store', [app\common\controller\PostLikeController::class, 'store']); - Route::any('/destroy', [app\common\controller\PostLikeController::class, 'destroy']); + Route::any('/destroy', [app\common\controller\PostLikeController::class, 'delete']); }); // 收藏 Route::group('/favorite', function () { Route::any('/list', [app\common\controller\PostFavoriteController::class, 'index']); Route::any('/store', [app\common\controller\PostFavoriteController::class, 'store']); - Route::any('/destroy', [app\common\controller\PostFavoriteController::class, 'destroy']); + Route::any('/destroy', [app\common\controller\PostFavoriteController::class, 'delete']); }); // 消息