From 7adb8316e19f6c9a8de2cdc518ced85f55146ed1 Mon Sep 17 00:00:00 2001 From: vine_liutk <961510893@qq.com> Date: Mon, 20 Mar 2023 15:35:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=87=E7=AB=A0=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E6=A0=B7=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Admin/Components.php | 9 ++- .../Controllers/ArticleCategoryController.php | 62 +++++++++++++++++++ app/Admin/Controllers/ArticleController.php | 61 ++++++++++++++++++ app/Admin/routes.php | 4 ++ app/Models/Article.php | 19 ++++++ app/Models/ArticleCategory.php | 19 ++++++ app/Services/Admin/ArticleCategoryService.php | 21 +++++++ app/Services/Admin/ArticleService.php | 15 +++++ ...114110_create_article_categories_table.php | 38 ++++++++++++ ...023_03_20_114120_create_articles_table.php | 42 +++++++++++++ 10 files changed, 287 insertions(+), 3 deletions(-) create mode 100644 app/Admin/Controllers/ArticleCategoryController.php create mode 100644 app/Admin/Controllers/ArticleController.php create mode 100644 app/Models/Article.php create mode 100644 app/Models/ArticleCategory.php create mode 100644 app/Services/Admin/ArticleCategoryService.php create mode 100644 app/Services/Admin/ArticleService.php create mode 100644 database/migrations/2023_03_20_114110_create_article_categories_table.php create mode 100644 database/migrations/2023_03_20_114120_create_articles_table.php diff --git a/app/Admin/Components.php b/app/Admin/Components.php index 93b66ee..a765ddc 100644 --- a/app/Admin/Components.php +++ b/app/Admin/Components.php @@ -9,10 +9,10 @@ class Components extends BaseRenderer { /** * 父级选择器 */ - public function parentControl($apiUrl = '', $name ='parent_id', $labelField = 'name', $valueField = 'id') + public function parentControl($apiUrl = '', $name ='parent_id', $label ='父级', $labelField = 'name', $valueField = 'id') { return amisMake()->TreeSelectControl() - ->name($name)->label('父级') + ->name($name)->label($label) ->showIcon(false) ->labelField($labelField) ->valueField($valueField) @@ -23,7 +23,10 @@ class Components extends BaseRenderer { * 排序字段 */ public function sortControl($name ='sort', $label = '排序'){ - return amisMake()->NumberControl()->name($name)->label($label)->min(0); + return amisMake()->NumberControl() + ->name($name)->label($label) + ->value(0) + ->min(0); } /** diff --git a/app/Admin/Controllers/ArticleCategoryController.php b/app/Admin/Controllers/ArticleCategoryController.php new file mode 100644 index 0000000..31857a5 --- /dev/null +++ b/app/Admin/Controllers/ArticleCategoryController.php @@ -0,0 +1,62 @@ +baseCRUD() + ->filterTogglable(false) + ->headerToolbar([ + $this->createButton(true), + ...$this->baseHeaderToolBar(), + ]) + ->columns([ + TableColumn::make()->name('id')->label('ID')->sortable(true), + TableColumn::make()->name('name')->label('名称'), + TableColumn::make()->name('created_at')->label('创建时间')->type('datetime')->sortable(true), + TableColumn::make()->name('updated_at')->label('更新时间')->type('datetime')->sortable(true), + $this->rowActions(true), + ]); + + return $this->baseList($crud); + } + + public function form(): Form + { + return $this->baseForm()->body([ + TextControl::make()->name('name')->label('名称')->required(true), + amisMake()->ImageControl()->name('icon')->label('icon')->autoUpload(true), + Components::make()->parentControl(), + Components::make()->sortControl(), + amisMake()->SwitchControl()->name('is_enable')->label('状态'), + ]); + } + + public function detail(): Form + { + return $this->baseDetail()->body([ + TextControl::make()->static(true)->name('id')->label('ID'), + TextControl::make()->static(true)->name('name')->label('名称'), + TextControl::make()->static(true)->name('created_at')->label('创建时间'), + TextControl::make()->static(true)->name('updated_at')->label('更新时间') + ]); + } + + public function getTreeList(Request $request){ + return $this->service->getTree(); + } +} diff --git a/app/Admin/Controllers/ArticleController.php b/app/Admin/Controllers/ArticleController.php new file mode 100644 index 0000000..ad0a84f --- /dev/null +++ b/app/Admin/Controllers/ArticleController.php @@ -0,0 +1,61 @@ +baseCRUD() + ->filterTogglable(false) + ->headerToolbar([ + $this->createButton(true, 'lg'), + ...$this->baseHeaderToolBar(), + ]) + ->columns([ + TableColumn::make()->name('id')->label('ID')->sortable(true), + TableColumn::make()->name('title')->label('标题'), + TableColumn::make()->name('created_at')->label('创建时间')->type('datetime')->sortable(true), + TableColumn::make()->name('updated_at')->label('更新时间')->type('datetime')->sortable(true), + $this->rowActions(true, 'lg'), + ]); + + return $this->baseList($crud); + } + + public function form(): Form + { + return $this->baseForm()->body([ + TextControl::make()->name('title')->label('标题')->required(true), + TextControl::make()->name('sub_title')->label('副标题'), + amisMake()->ImageControl()->name('cover')->label('封面')->autoUpload(true), + Components::make()->parentControl('', 'category_id', '分类'), + Components::make()->fuEditorControl(), + Components::make()->sortControl(), + \amisMake()->DateTimeControl()->name('published_at')->label('发布时间')->description('*不填写则默认为创建时间'), + \amisMake()->SwitchControl()->name('is_enable')->label('显示'), + ]); + } + + public function detail(): Form + { + return $this->baseDetail()->body([ + TextControl::make()->static(true)->name('id')->label('ID'), + TextControl::make()->static(true)->name('title')->label('标题'), + TextControl::make()->static(true)->name('created_at')->label('创建时间'), + TextControl::make()->static(true)->name('updated_at')->label('更新时间') + ]); + } +} diff --git a/app/Admin/routes.php b/app/Admin/routes.php index 6f70672..3314fff 100644 --- a/app/Admin/routes.php +++ b/app/Admin/routes.php @@ -18,6 +18,10 @@ Route::group([ //公告管理 $router->resource('admin-notices', \App\Admin\Controllers\AdminNoticeController::class); + //文章分类 + $router->resource('article-categories', \App\Admin\Controllers\ArticleCategoryController::class); + //文章管理 + $router->resource('articles', \App\Admin\Controllers\ArticleController::class); $router->resource('keywords', \App\Admin\Controllers\KeywordController::class); diff --git a/app/Models/Article.php b/app/Models/Article.php new file mode 100644 index 0000000..384a9f3 --- /dev/null +++ b/app/Models/Article.php @@ -0,0 +1,19 @@ +format('Y-m-d H:i:s'); + } +} diff --git a/app/Models/ArticleCategory.php b/app/Models/ArticleCategory.php new file mode 100644 index 0000000..dacce63 --- /dev/null +++ b/app/Models/ArticleCategory.php @@ -0,0 +1,19 @@ +format('Y-m-d H:i:s'); + } +} diff --git a/app/Services/Admin/ArticleCategoryService.php b/app/Services/Admin/ArticleCategoryService.php new file mode 100644 index 0000000..fa9e3f1 --- /dev/null +++ b/app/Services/Admin/ArticleCategoryService.php @@ -0,0 +1,21 @@ +query()->orderByDesc('sort')->get()->toArray(); + return array2tree($list); + } +} diff --git a/app/Services/Admin/ArticleService.php b/app/Services/Admin/ArticleService.php new file mode 100644 index 0000000..726efee --- /dev/null +++ b/app/Services/Admin/ArticleService.php @@ -0,0 +1,15 @@ +id(); + $table->string('name')->comment('名称'); + $table->string('icon')->nullable()->comment('图标'); + $table->unsignedTinyInteger('is_enable')->default(1)->comment('显示开关'); + $table->unsignedInteger('sort')->default(0)->comment('排序'); + $table->unsignedBigInteger('parent_id')->default(0)->comment('上级ID'); + $table->unsignedInteger('level')->default(1)->comment('层级'); + $table->string('path')->default('-')->comment('所有的父级ID'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('article_categories'); + } +}; diff --git a/database/migrations/2023_03_20_114120_create_articles_table.php b/database/migrations/2023_03_20_114120_create_articles_table.php new file mode 100644 index 0000000..bc33412 --- /dev/null +++ b/database/migrations/2023_03_20_114120_create_articles_table.php @@ -0,0 +1,42 @@ +id(); + $table->string('title'); + $table->string('sub_title')->nullable()->comment('副标题'); + $table->unsignedBigInteger('category_id')->nullable()->comment('文章分类'); + $table->text('content')->nullable()->comment('文章内容'); + $table->timestamp('published_at')->nullable()->comment('发布时间'); + $table->unsignedTinyInteger('is_enable')->default(1)->comment('显示开关'); + $table->unsignedInteger('sort')->default(0)->comment('排序'); + + //可能用到的额外字段 + $table->string('cover')->nullable()->comment('封面'); + $table->string('author')->nullable()->comment('作者/来源'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('articles'); + } +};