You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
<?php
namespace App\AdminAgent\Controllers;
use App\AdminAgent\Repositories\Channel;use App\Models\Channel as ChannelModel;use Dcat\Admin\Admin;use Dcat\Admin\Form;use Dcat\Admin\Grid;use Dcat\Admin\Layout\Content;use Dcat\Admin\Layout\Row;use Dcat\Admin\Show;use Dcat\Admin\Http\Controllers\AdminController;use Dcat\Admin\Tree;use Illuminate\Support\Facades\Storage;
class ChannelController extends AdminController{ public function index(Content $content) { return $content->header('产品频道') ->body(function (Row $row) { $tree = new Tree(new ChannelModel()); $tree->query(function ($model) { //agent_id为0是系统分类,其它是代理商分类
return $model->where('agent_id', Admin::user()->id)->orderBy('sort')->orderBy('id'); });
/*$prefix = Storage::disk('public')->url(''); $tree->branch(function ($branch) use ($prefix) { $src = $prefix . $branch['icon'] ; $logo = '<img data-action="preview-img" src="'.$src.'" style="max-width:30px;max-height:30px;cursor:pointer;margin-left:20px;" class="img">';
return "{$branch['id']} - {$branch['name']} $logo"; });*/ $row->column(12, $tree); }); }
/** * Make a grid builder. * * @return Grid */ protected function grid() { return Grid::make(new Channel(['parent']), function (Grid $grid) { $grid->model()->where('agent_id', Admin::user()->id) ->orderBy('sort')->orderBy('id', 'desc');
$grid->column('id')->sortable(); $grid->column('icon')->image('', 60, 60); $grid->column('name'); $grid->column('parent.name'); $grid->column('sort')->editable()->width(120);
$grid->filter(function (Grid\Filter $filter) { $filter->panel();
$filter->equal('id')->width(2); $filter->like('name')->width(3); }); }); }
/** * Make a show builder. * * @param mixed $id * * @return Show */ protected function detail($id) { return Show::make($id, new Channel(['parent']), function (Show $show) { //不允许查看非自己的数据
if ($show->model()->agent_id != Admin::user()->id) { Admin::exit('数据不存在'); }
$show->field('id'); $show->field('icon')->image('', 80, 80); $show->field('name'); $show->field('parent.name'); $show->field('sort'); }); }
/** * Make a form builder. * * @return Form */ protected function form() { return Form::make(new Channel(), function (Form $form) { //不允许修改非自己的数据
if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) { return $form->response()->error('数据不存在'); }
$form->display('id'); $form->image('icon')->uniqueName()->removable(false); $form->text('name')->required(); $form->select('pid')->options(\App\Models\Channel::selectOptions())->default(0)->required(); $form->text('sort')->default(255); })->saving(function (Form $form) { //不允许修改非自己的数据
if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) { return $form->response()->error('数据不存在'); }
//处理特殊字段
$form->hidden(['agent_id']); $form->agent_id = Admin::user()->id; $form->sort = $form->sort ?? 255;
//不允许编辑的字段
$form->ignore(['id', 'deleted_at']); })->deleting(function (Form $form) { //不允许删除非自己的数据
if (array_filter($form->model()->toArray(), fn($v) => $v['agent_id'] != Admin::user()->id)) { return $form->response()->error('数据不存在'); } }); }}
|