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.
153 lines
4.6 KiB
153 lines
4.6 KiB
<?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('picture_ad')->image('', 60, 60);
|
|
$grid->column('agent_product_id', '专题产品')
|
|
->display('查看')
|
|
->modal(function ($modal) use ($grid) {
|
|
$data = AgentProduct::with('product:id,title,pictures')
|
|
->whereIn('id', $this->agent_product_id)
|
|
->get(['id', 'product_id', 'sale', 'stock']);
|
|
$result = [];
|
|
$prefix = Storage::disk('public')->url('');
|
|
foreach ($data as $k => $v) {
|
|
$result[] = [
|
|
$v->id,
|
|
$v->product->title,
|
|
'<img data-action="preview-img" src="'.$prefix.$v->product->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('sort');
|
|
$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_ad')->image('', 80, 80);
|
|
$show->field('picture')->image('', 80, 80);
|
|
$show->field('agent_product_id', '产品')
|
|
->unescape()
|
|
->as(function ($v) {
|
|
$data = AgentProduct::with('product:id,title')
|
|
->whereIn('id', $v)
|
|
->orderBy('id')->get(['id', 'product_id']);
|
|
return join("<br>", $data->map(fn($v) => $v->product->title)->toArray());
|
|
});
|
|
$show->field('sort');
|
|
$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->image('picture_ad')
|
|
->required()->removable(false)->uniqueName()
|
|
->help('图片大小:750*230');
|
|
$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::with('product:id,title')
|
|
->select(['id', 'product_id'])
|
|
->whereIn('id', $v)
|
|
->orderBy('id')->get();
|
|
$result = [];
|
|
foreach ($agent_product as $v) {
|
|
$result[$v->id] = $v->product->title ?? '';
|
|
}
|
|
return $result;
|
|
})
|
|
->pluck('product.title')
|
|
->value(join(',', $form->model()->agent_product_id ?? []));
|
|
$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', '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('数据不存在');
|
|
}
|
|
});
|
|
}
|
|
}
|