1
0
Fork 0
develop
panliang 2023-04-27 13:09:25 +08:00
parent b67e8f04af
commit 7baf63523d
7 changed files with 149 additions and 33 deletions

View File

@ -2,10 +2,8 @@
namespace App\Admin\Controllers;
use Slowlyo\OwlAdmin\Renderers\Page;
use Slowlyo\OwlAdmin\Renderers\Form;
use Slowlyo\OwlAdmin\Renderers\TableColumn;
use Slowlyo\OwlAdmin\Renderers\TextControl;
use Slowlyo\OwlAdmin\Renderers\{Page, Form};
use Slowlyo\OwlAdmin\Renderers\{TableColumn, TextControl, Image, ImageControl, SelectControl, SwitchControl, DateTimeControl, InputKV, Status, Json};
use Slowlyo\OwlAdmin\Controllers\AdminController;
use App\Services\Admin\BannerService;
use App\Admin\Components;
@ -14,22 +12,22 @@ class BannerController extends AdminController
{
protected string $serviceName = BannerService::class;
protected string $pageTitle = '图片管理';//待完善-todo
public function list(): Page
{
$crud = $this->baseCRUD()
->filterTogglable(false)
->headerToolbar([
$this->createButton(true),
...$this->baseHeaderToolBar(),
])
// ->headerToolbar([
// $this->createButton(true),
// ...$this->baseHeaderToolBar(),
// ])
->quickSaveItemApi(admin_url('quick-edit/banners/$id'))
->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),
TableColumn::make()->name('place.name')->label(__('banner.place_id')),
TableColumn::make()->name('name')->label(__('banner.name')),
Image::make()->name('picture')->label(__('banner.picture'))->width(100),
TableColumn::make()->name('sort')->label(__('banner.sort'))->align('center')->quickEdit(Components::make()->sortControl('sort', __('article-category.sort'))->saveImmediately(true)),
TableColumn::make()->name('is_enable')->label(__('banner.is_enable'))->type('switch')->quickEdit(SwitchControl::make()->mode('inline')->saveImmediately(true)),
$this->rowActions(),
]);
return $this->baseList($crud);
@ -37,23 +35,31 @@ class BannerController extends AdminController
public function form(): Form
{
return $this->baseForm()->body([
TextControl::make()->name('name')->label('名称')->required(true),
\amisMake()->ImageControl()->name('picture')->label('图片')->autoUpload(true),
Components::make()->parentControl('', 'place_id', '位置'),
Components::make()->sortControl(),
\amisMake()->DateTimeControl()->name('published_at')->label('发布时间')->description('*不填写则默认为创建时间'),
\amisMake()->SwitchControl()->name('is_enable')->label('显示'),
return $this->baseForm()->title('')->body([
SelectControl::make()->name('place_id')->label(__('banner.place_id'))->source(admin_url('api/banner-places/options'))->required(true),
TextControl::make()->name('name')->label(__('banner.name'))->required(true),
ImageControl::make()->name('picture')->label(__('banner.picture'))->autoUpload(true)->required(true),
InputKV::make()->name('link_config')->label(__('banner.link_config'))->draggable(false),
Components::make()->sortControl('sort', __('banner.sort')),
DateTimeControl::make()->name('published_at')->label(__('banner.published_at'))->value(now())->format('YYYY-MM-DD HH:mm:ss')->description('*不填写则默认为创建时间'),
SwitchControl::make()->name('is_enable')->label(__('banner.is_enable'))->value(true),
]);
}
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('更新时间')
return $this->baseDetail()->title('')->body([
TextControl::make()->name('place.name')->label(__('banner.place_id'))->static(true),
TextControl::make()->name('name')->label(__('banner.name'))->static(true),
TextControl::make()->name('picture')->label(__('banner.picture'))->static(true)->staticSchema(Image::make()),
TextControl::make()->name('link_config')->label(__('banner.link_config'))->static(true)->staticSchema(Json::make()),
TextControl::make()->name('sort')->label(__('banner.sort'))->static(true),
TextControl::make()->name('published_at')->label(__('banner.published_at'))->static(true),
TextControl::make()->name('is_enable')->label(__('banner.is_enable'))->static(true)->staticSchema(Status::make()->source([
['label' => '不显示', 'icon' => 'fa fa-close', 'color' => '#cc292e'],
['label' => '显示', 'icon' => 'fa fa-check', 'color' => '#30bf13'],
])),
TextControl::make()->name('created_at')->label(__('banner.created_at'))->static(true),
]);
}
}

View File

@ -67,4 +67,11 @@ class BannerPlaceController extends AdminController
TextControl::make()->static(true)->name('created_at')->label(__('banner-place.created_at')),
]);
}
public function options()
{
$list = $this->service->listQuery()->select(['id as value', 'name as label'])->get();
return $this->response()->success($list);
}
}

View File

@ -15,6 +15,7 @@ Route::group([
$router->get('keywords/tree-list', '\App\Admin\Controllers\KeywordController@getTreeList')->name('api.keywords.tree-list');
$router->get('article-categories/tree-list', [\App\Admin\Controllers\ArticleCategoryController::class, 'getTreeList'])->name('api.article-categories.tree-list');
$router->get('articles/options', [\App\Admin\Controllers\ArticleController::class, 'options'])->name('api.articles.options');
$router->get('banner-places/options', [\App\Admin\Controllers\BannerPlaceController::class, 'options'])->name('api.banner-places.options');
$router->get('region-categories/tree-list', '\App\Admin\Controllers\RegionCategoryController@getTreeList')->name('api.region-categories.tree-list');
});
@ -35,6 +36,7 @@ Route::group([
$router->post('quick-edit/banner-places/{banner_place}',[\App\Admin\Controllers\BannerPlaceController::class, 'update']);
// 图片管理
$router->resource('banners', \App\Admin\Controllers\BannerController::class);
$router->post('quick-edit/banners/{banner}',[\App\Admin\Controllers\BannerController::class, 'update']);
//友情链接
$router->resource('friend-links', \App\Admin\Controllers\FriendLinkController::class);
//字典表

View File

@ -0,0 +1,31 @@
<?php
namespace App\Filters;
use EloquentFilter\ModelFilter;
class BannerFilter extends ModelFilter
{
/**
* Related Models that have ModelFilters as well as the method on the ModelFilter
* As [relationMethod => [input_key1, input_key2]].
*
* @var array
*/
public $relations = [];
public function place($id)
{
$this->where('place_id', $id);
}
public function key($v)
{
$this->whereHas('place', fn ($q) => $q->whereIn('key', explode(',', $v)));
}
public function name($v)
{
$this->whereLike('name', $v);
}
}

View File

@ -2,10 +2,46 @@
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Str;
use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Banner extends Model
{
use HasFactory;
use Filterable;
protected $fillable = ['is_enable', 'link_config', 'name', 'picture', 'place_id', 'published_at', 'sort'];
protected $casts = [
'created_at' => 'datetime:Y-m-d H:i:s',
'updated_at' => 'datetime:Y-m-d H:i:s',
'published_at' => 'datetime:Y-m-d H:i:s',
'is_enable' => 'boolean',
'link_config' => 'json',
];
protected function picture(): Attribute
{
return Attribute::make(
get: fn($value) => $value ? (Str::startsWith($value, ['http://', 'https://']) ? $value : Storage::url($value)) : '',
);
}
public function place()
{
return $this->belongsTo(BannerPlace::class, 'place_id');
}
public function scopeSort($q)
{
return $q->orderBy('sort', 'desc');
}
public function scopeEnable($q)
{
return $q->where('is_enable', 1)->where('published_at', '<=', now());
}
}

View File

@ -3,13 +3,34 @@
namespace App\Services\Admin;
use App\Models\Banner;
use Slowlyo\OwlAdmin\Services\AdminService;
use App\Filters\BannerFilter;
/**
* @method Banner getModel()
* @method Banner|\Illuminate\Database\Query\Builder query()
*/
class BannerService extends AdminService
class BannerService extends BaseService
{
protected string $modelName = Banner::class;
protected array $withRelationships = ['place'];
protected string $modelFilterName = BannerFilter::class;
public function listQuery()
{
$model = $this->getModel();
$filter = $this->getModelFilter();
$query = $this->query();
if($this->withRelationships){
$query->with($this->withRelationships);
}
if ($filter) {
$query->filter(request()->input(), $filter);
}
return $query->sort();
}
}

View File

@ -0,0 +1,13 @@
<?php
return [
'id' => 'ID',
'created_at' => '创建时间',
'is_enable' => '显示',
'link_config' => '跳转',
'name' => '名称',
'picture' => '广告图',
'place_id' => '位置',
'published_at' => '发布时间',
'sort' => '排序(倒序)',
];