diff --git a/app/api/controller/Article.php b/app/api/controller/Article.php index 597177ae..a43af426 100644 --- a/app/api/controller/Article.php +++ b/app/api/controller/Article.php @@ -89,6 +89,68 @@ class Article extends Controller return $this->renderError('添加失败'); } + /** + * @notes:编辑文章 + * @return Json + * @throws BaseException + * @author: wanghousheng + */ + public function edit(): Json + { + if (!UserService::isStore()) { + throwError("无权限", 403); + } + $articleId = intval($this->request->post('article_id')); + if (!$articleId) { + return $this->renderError('缺少必要参数'); + } + $title = $this->request->post('title'); + if (!$title) { + return $this->renderError('标题不能为空'); + } + $category_id = intval($this->request->post('category_id')); + if (!$category_id) { + return $this->renderError('分类不能为空'); + } + $image_id = intval($this->request->post('image_id')); + if (!$image_id) { + return $this->renderError('图片不能为空'); + } + $content = $this->request->post('content'); + if (!$content) { + return $this->renderError('内容不能为空'); + } + $sort = intval($this->request->post('sort', 100)); + $status = intval($this->request->post('status', 1)); + $data = compact('status', 'sort', 'content', 'image_id', 'category_id', 'title'); + // 文章详情 + $model = ArticleModel::detail($articleId); + // 更新记录 + if ($model->edit($data)) { + return $this->renderSuccess('更新成功'); + } + return $this->renderError($model->getError() ?: '更新失败'); + } + + /** + * @notes:删除文章 + * @return Json + * @author: wanghousheng + */ + public function delete(): Json + { + $articleId = intval($this->request->post('article_id')); + if (!$articleId) { + return $this->renderError('缺少必要参数'); + } + // 文章详情 + $model = ArticleModel::detail($articleId); + if (!$model->setDelete()) { + return $this->renderError($model->getError() ?: '删除失败'); + } + return $this->renderSuccess('删除成功'); + } + /** * @notes:添加分类 * @return Json diff --git a/app/api/model/Article.php b/app/api/model/Article.php index 98a5c189..5f14362f 100644 --- a/app/api/model/Article.php +++ b/app/api/model/Article.php @@ -121,4 +121,24 @@ class Article extends ArticleModel } return $cat; } + + /** + * 更新记录 + * @param array $data + * @return bool + */ + public function edit(array $data): bool + { + return $this->save($data) !== false; + } + + /** + * 软删除 + * @return bool + */ + public function setDelete(): bool + { + return $this->save(['is_delete' => 1]); + } + } \ No newline at end of file