diff --git a/app/controller/CreationArticleController.php b/app/controller/CreationArticleController.php new file mode 100644 index 0000000..38e1a63 --- /dev/null +++ b/app/controller/CreationArticleController.php @@ -0,0 +1,64 @@ + '', + 'article_category_id' => '', + 'cover' => '', + 'content' => '', + 'status' => '', + ]); + validate(CreationArticleValidate::class)->check($params, 'save'); + return success($service->saveData($params)); + } + + public function update(CreationArticleService $service): Response + { + $params = requestOnly([ + 'id' => '', + 'title' => '', + 'article_category_id' => '', + 'cover' => '', + 'content' => '', + 'status' => '', + ]); + validate(CreationArticleValidate::class)->check($params, 'update'); + return success($service->updateData(ArrayUtils::filterNotEmpty($params))); + } + + public function index(CreationArticleService $service): Response + { + $params = requestOnly([ + 'article_category' => '', + 'task' => '', + 'title' => '', + 'status' => '', + 'begin_time' => '', + 'end_time' => '' + ]); + return success($service->listData($params)); + } + + public function read(CreationArticleService $service): Response + { + $id = input('id'); + return success($service->readData($id)); + } + + public function delete(CreationArticleService $service): Response + { + $ids = input('ids'); + return success($service->deleteData($ids)); + } + + +} \ No newline at end of file diff --git a/app/model/CreationArticle.php b/app/model/CreationArticle.php index 91463c5..c39a269 100644 --- a/app/model/CreationArticle.php +++ b/app/model/CreationArticle.php @@ -30,4 +30,8 @@ class CreationArticle extends BaseModel */ public $timestamps = true; + public function category() + { + return $this->belongsTo(ArticleCategory::class, 'article_category_id', 'id'); + } } \ No newline at end of file diff --git a/app/route/route.php b/app/route/route.php index 17e6f40..1f1dc26 100644 --- a/app/route/route.php +++ b/app/route/route.php @@ -2,6 +2,7 @@ use app\controller\AiCommandController; use app\controller\ArticleCategoryController; +use app\controller\CreationArticleController; use app\controller\CreationTaskController; use app\controller\DistillationQuestionsController; use app\controller\EnterprisePortraitCategoryController; @@ -111,7 +112,7 @@ Route::group('/service/v1', function () { Route::post('/delete', [CreationTaskController::class, 'delete'])->setParams(['perm' => ['creationTaskDelete']]); }); - //文章发呢类 + //文章分类 Route::group('/articleCategory', function () { //新增 Route::post('/save', [ArticleCategoryController::class, 'save'])->setParams(['perm' => ['articleCategorySave']]); @@ -124,6 +125,20 @@ Route::group('/service/v1', function () { //删除 Route::post('/delete', [ArticleCategoryController::class, 'delete'])->setParams(['perm' => ['articleCategoryDelete']]); }); + + //投稿文章管理 + Route::group('/article', function () { + //新增 + Route::post('/save', [CreationArticleController::class, 'save'])->setParams(['perm' => ['articleSave']]); + //修改 + Route::post('/update', [CreationArticleController::class, 'update'])->setParams(['perm' => ['articleUpdate']]); + //列表 + Route::get('/index', [CreationArticleController::class, 'index'])->setParams(['perm' => ['articleIndex']]); + //详情 + Route::get('/read', [CreationArticleController::class, 'read'])->setParams(['perm' => ['articleRead']]); + //删除 + Route::post('/delete', [CreationArticleController::class, 'delete'])->setParams(['perm' => ['articleDelete']]); + }); }); })->middleware([ AdminAuthorizationMiddleware::class, diff --git a/app/service/CreationArticleService.php b/app/service/CreationArticleService.php new file mode 100644 index 0000000..303126d --- /dev/null +++ b/app/service/CreationArticleService.php @@ -0,0 +1,131 @@ +dao = $dao; + $this->articleCategoryDao = app()->make(ArticleCategoryDao::class); + $this->taskDao = app()->make(CreationTaskDao::class); + } + + /** + * 保存信息 + * @param array $params + * @return array + */ + public function saveData(array $params): array + { + Db::startTrans(); + try { + $data = $this->dao->save($params); + Db::commit(); + } catch (\Exception $exception) { + Db::rollback(); + throw new ApiException($exception->getMessage()); + } + return $data->toArray(); + } + + /** + * 修改信息 + * @param array $params + * @return array + */ + public function updateData(array $params): array + { + // 落库 + Db::startTrans(); + try { + $this->dao->update(['id' => $params['id']], $params); + Db::commit(); + } catch (\Exception $exception) { + Db::rollback(); + throw new ApiException($exception->getMessage()); + } + return $params; + } + + /** + * 获取列表 + * @param array $params + * @return array + */ + public function listData(array $params): array + { + $query = [ + 'delete_time' => 0 + ]; + if (isNotBlank($params['article_category'])) { + $cids = $this->articleCategoryDao->getColumn([['name', 'like', '%' . $params['article_category'] . '%']], 'id'); + $query[] = ['article_category_id', 'in', $cids]; + } + if (isNotBlank($params['task'])) { + $tids = $this->taskDao->getColumn([['name', 'like', '%' . $params['task'] . '%']], 'id'); + $query[] = ['task_id', 'in', $tids]; + } + if (isNotBlank($params['title'])) { + $query[] = ['title', 'like', '%' . $params['title'] . '%']; + } + if (isNotBlank($params['status'])) { + $query[] = ['status', '=', $params['status']]; + } + if (isNotBlank($params['begin_time'])) { + $query[] = ['create_time', '>=', strtotime($params['begin_time'])]; + } + if (isNotBlank($params['end_time'])) { + $query[] = ['create_time', '<=', strtotime($params['end_time'] . ' 23:59:59')]; + } + $list = $this->dao->getList($query, 'id,title,status,create_time,article_category_id', 0, 0, 'id DESC', ['category']); + $count = $this->dao->getCount($query); + return compact('list', 'count'); + } + + /** + * 获取信息 + * @param mixed $id + * @return array + */ + public function readData(mixed $id): array + { + $aiCommand = $this->dao->get(['id' => $id]); + if (empty($aiCommand)) { + throw new ApiException('数据不存在'); + } + return $aiCommand->toArray(); + } + + /** + * 删除信息 + * @param mixed $id + * @return array + */ + public function deleteData(array $ids): array + { + // 落库 + Db::startTrans(); + try { + $this->dao->update([['id', 'in', $ids]], ['delete_time' => time()]); + Db::commit(); + } catch (\Exception $exception) { + Db::rollback(); + throw new ApiException($exception->getMessage()); + } + return ['id' => $ids]; + } + +} \ No newline at end of file diff --git a/app/validate/CreationArticleValidate.php b/app/validate/CreationArticleValidate.php new file mode 100644 index 0000000..99d6229 --- /dev/null +++ b/app/validate/CreationArticleValidate.php @@ -0,0 +1,25 @@ + [ + 'title' => 'require', + 'content' => 'require' + ], + 'update' => [ + 'id' => 'require', + 'title' => 'require', + 'content' => 'require' + ], + ]; + protected $message = [ + 'id.require' => 'ID不能为空', + 'title' => '标题不能为空', + 'content' => '内容不能为空' + ]; +} \ No newline at end of file