|
|
<?php
namespace App\AdminAgent\Controllers;
use App\AdminAgent\Renderable\SelectAgentProduct;use App\AdminAgent\Repositories\Special;use App\Models\AgentProduct;use Dcat\Admin\Admin;use Dcat\Admin\Form;use Dcat\Admin\Grid;use Dcat\Admin\Show;use Dcat\Admin\Http\Controllers\AdminController;use Dcat\Admin\Widgets\Table;use Illuminate\Support\Facades\Storage;
class SpecialController extends AdminController{ /** * Make a grid builder. * * @return Grid */ protected function grid() { return Grid::make(new Special(), function (Grid $grid) { $grid->disableFilterButton();
$grid->model()->where('agent_id', Admin::user()->id);
$grid->column('id')->sortable(); $grid->column('agent_product_id', '专题产品') ->display('查看') ->modal(function ($modal) use ($grid) { $data = AgentProduct::whereIn('id', $this->agent_product_id) ->get(['id', 'product_id', 'sale', 'stock', 'title', 'pictures']); $result = []; $prefix = Storage::disk('public')->url(''); foreach ($data as $k => $v) { $result[] = [ $v->id, $v->title, '<img data-action="preview-img" src="'.$prefix.$v->picture.'" style="max-width:60px;max-height:60px;cursor:pointer" class="img img-thumbnail">', $v->sale, $v->stock, ]; } return Table::make(['产品ID', '标题', '图片', '销量', '库存'], $result); }); $grid->column('url', '专题链接') ->display(fn() => '/pages/activityList/index?special_id=' . $this->id)->copyable(); $grid->column('created_at'); $grid->column('updated_at')->sortable();
$grid->filter(function (Grid\Filter $filter) { $filter->panel();
$filter->equal('id')->width(2); }); }); }
/** * Make a show builder. * * @param mixed $id * * @return Show */ protected function detail($id) { return Show::make($id, new Special(), function (Show $show) { //不允许查看非自己的数据
if ($show->model()->agent_id != Admin::user()->id) { Admin::exit('数据不存在'); }
$show->field('id'); $show->field('picture')->image('', 80, 80); $show->field('agent_product_id', '产品') ->unescape() ->as(function ($v) { $data = AgentProduct::whereIn('id', $v) ->orderBy('id')->get(['id', 'product_id']); return join("<br>", $data->map(fn($v) => $v->title)->toArray()); }); $show->field('created_at'); $show->field('updated_at'); }); }
/** * Make a form builder. * * @return Form */ protected function form() { return Form::make(new Special(), function (Form $form) { //不允许查看非自己的数据
if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) { return $form->response()->error('数据不存在'); }
$form->display('id'); $form->multipleImage('picture')->required()->removable(false)->uniqueName() ->help('图片大小:750*490'); $form->multipleSelectTable('agent_product_id') ->title('选择在售产品')->required() ->dialogWidth('80%;min-width:825px;') ->from(SelectAgentProduct::make()) ->options(function ($v) { if (!$v) return []; $agent_product = AgentProduct::select(['id', 'product_id', 'title']) ->whereIn('id', $v) ->orderBy('id')->get(); $result = []; foreach ($agent_product as $v) { $result[$v->id] = $v->title ?? ''; } return $result; }) ->pluck('title', 'id') ->value(join(',', $form->model()->agent_product_id ?? [])); })->saving(function (Form $form) { //不允许修改非自己的数据
if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) { return $form->response()->error('数据不存在'); }
//处理特殊字段
$form->hidden(['agent_id', 'created_at', 'updated_at']); $form->agent_id = Admin::user()->id;
//不允许编辑的字段
$form->ignore(['id', 'agent_id', 'created_at', 'updated_at']); })->deleting(function (Form $form) { //不允许删除非自己的数据
if (array_filter($form->model()->toArray(), fn($v) => $v['agent_id'] != Admin::user()->id)) { return $form->response()->error('数据不存在'); } }); }}
|