diff --git a/app/Admin/Controllers/ArticleController.php b/app/Admin/Controllers/ArticleController.php index 20d2b17..3f6b6c7 100644 --- a/app/Admin/Controllers/ArticleController.php +++ b/app/Admin/Controllers/ArticleController.php @@ -3,7 +3,7 @@ namespace App\Admin\Controllers; use Slowlyo\OwlAdmin\Renderers\{Form, Page}; -use Slowlyo\OwlAdmin\Renderers\{TableColumn, TextControl, Image, ImageControl, DateTimeControl, SwitchControl, Tabs, Tab}; +use Slowlyo\OwlAdmin\Renderers\{Component, Button, TableColumn, TextControl, Image, ImageControl, DateTimeControl, SwitchControl, Tabs, Tab, Status, Html}; use Slowlyo\OwlAdmin\Controllers\AdminController; use App\Services\Admin\ArticleService; use App\Admin\Components; @@ -16,6 +16,12 @@ class ArticleController extends AdminController { $crud = $this->baseCRUD() ->filterTogglable(false) + ->filter($this->baseFilter()->actions([])->body([ + TextControl::make()->name('title')->label(__('article.title'))->size('md'), + Components::make()->parentControl(admin_url('api/article-categories/tree-list'), 'category_path', __('article.category_id'))->size('lg'), + Button::make()->label(__('admin.reset'))->actionType('clear-and-submit'), + Component::make()->setType('submit')->label(__('admin.search'))->level('primary'), + ])) ->columns([ TableColumn::make()->name('id')->label(__('article.id'))->sortable(true), TableColumn::make()->name('title')->label(__('article.title')), @@ -43,23 +49,30 @@ class ArticleController extends AdminController DateTimeControl::make()->name('published_at')->label(__('article.published_at'))->value(now())->format('YYYY-MM-DD HH:mm:ss')->description('*不填写则默认为创建时间'), SwitchControl::make()->name('is_enable')->label(__('article.is_enable'))->value(true), ]), - Tab::make()->title('内容')->body(Components::make('content', __('article.content'))->fuEditorControl()) + Tab::make()->title('内容')->body(Components::make()->fuEditorControl('content', '')) ])); } public function detail(): Form { return $this->baseDetail()->title('')->body( - Tabs::make()->tabs( + Tabs::make()->tabs([ Tab::make()->title('基本信息')->body([ TextControl::make()->static(true)->name('id')->label(__('article.id')), TextControl::make()->static(true)->name('title')->label(__('article.title')), TextControl::make()->static(true)->name('category.name')->label(__('article.category_id')), - TextControl::make()->static(true)->name('title')->label(__('article.sub_title')), + TextControl::make()->static(true)->name('sub_title')->label(__('article.sub_title')), + TextControl::make()->name('cover')->label(__('article.cover'))->static(true)->staticSchema(Image::make()), + TextControl::make()->static(true)->name('sort')->label(__('article.sort')), + TextControl::make()->name('is_enable')->label(__('article.is_enable'))->static(true)->staticSchema(Status::make()->source([ + ['label' => '不显示', 'icon' => 'fa fa-close', 'color' => '#cc292e'], + ['label' => '显示', 'icon' => 'fa fa-check', 'color' => '#30bf13'], + ])), + TextControl::make()->static(true)->name('published_at')->label(__('article.published_at')), TextControl::make()->static(true)->name('created_at')->label(__('article.created_at')), ]), - Tab::make()->title('内容')->body('1234567') - ) + Tab::make()->title('内容')->body(Html::make()->name('content')) + ]) ); } } diff --git a/app/Filters/ArticleFilter.php b/app/Filters/ArticleFilter.php new file mode 100644 index 0000000..fd57422 --- /dev/null +++ b/app/Filters/ArticleFilter.php @@ -0,0 +1,31 @@ + [input_key1, input_key2]]. + * + * @var array + */ + public $relations = []; + + public function title($v) + { + $this->whereLike('title', $v); + } + + public function categoryId($v) + { + $this->where('category_id', $v); + } + + public function categoryPath($v) + { + $this->where('category_path', 'like', '%-'.$v.'-%'); + } +} diff --git a/app/Services/Admin/ArticleService.php b/app/Services/Admin/ArticleService.php index aace4a8..306faf6 100644 --- a/app/Services/Admin/ArticleService.php +++ b/app/Services/Admin/ArticleService.php @@ -2,9 +2,8 @@ namespace App\Services\Admin; -use App\Models\Article; -use Illuminate\Support\Facades\Storage; -use Illuminate\Support\Str; +use App\Models\{Article, ArticleCategory}; +use App\Filters\ArticleFilter; /** * @method Article getModel() @@ -16,6 +15,8 @@ class ArticleService extends BaseService protected string $modelName = Article::class; + protected string $modelFilterName = ArticleFilter::class; + public function listQuery() { $model = $this->getModel(); @@ -32,4 +33,22 @@ class ArticleService extends BaseService return $query->sort(); } + + public function update($primaryKey, $data): bool + { + $cid = data_get($data, 'category_id'); + if ($cid && $category = ArticleCategory::find($cid)) { + $data['category_path'] = $category->path . $category->id . '-'; + } + return parent::update($primaryKey, $data); + } + + public function store($data): bool + { + $cid = data_get($data, 'category_id'); + if ($cid && $category = ArticleCategory::find($cid)) { + $data['category_path'] = $category->path . $category->id . '-'; + } + return parent::store($data); + } } diff --git a/database/migrations/2023_03_20_114120_create_articles_table.php b/database/migrations/2023_03_20_114120_create_articles_table.php index 47e14c0..cdcfe9b 100644 --- a/database/migrations/2023_03_20_114120_create_articles_table.php +++ b/database/migrations/2023_03_20_114120_create_articles_table.php @@ -27,7 +27,7 @@ return new class extends Migration // 可能用到的额外字段 $table->string('cover')->nullable()->comment('封面'); $table->string('author')->nullable()->comment('作者/来源'); - $table->string('category_path')->nullable()->comment('所有上级分类id'); + $table->string('category_path')->nullable()->comment('所有分类id'); $table->timestamps(); }); }