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.
133 lines
3.6 KiB
133 lines
3.6 KiB
<?php
|
|
|
|
namespace App\AdminAgent\Controllers;
|
|
|
|
use App\Models\AgentProduct;
|
|
use App\Models\Category;
|
|
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;
|
|
|
|
|
|
class CategoryController extends AdminController
|
|
{
|
|
public function index(Content $content)
|
|
{
|
|
return $content->header('产品分类')
|
|
->body(function (Row $row) {
|
|
$tree = new Tree(new Category);
|
|
$tree->query(function ($model) {
|
|
//agent_id为0是系统分类,其它是代理商分类
|
|
return $model->where('agent_id', Admin::user()->id);
|
|
});
|
|
$row->column(12, $tree);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Make a grid builder.
|
|
*
|
|
* @return Grid
|
|
*/
|
|
protected function grid()
|
|
{
|
|
return Grid::make(new Category(), function (Grid $grid) {
|
|
$grid->model()->where('agent_id', Admin::user()->id);
|
|
|
|
$grid->column('id')->sortable();
|
|
$grid->column('name');
|
|
$grid->column('pid');
|
|
$grid->column('sort');
|
|
$grid->column('template');
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Make a show builder.
|
|
*
|
|
* @param mixed $id
|
|
*
|
|
* @return Show
|
|
*/
|
|
protected function detail($id)
|
|
{
|
|
return Show::make($id, new Category(), function (Show $show) {
|
|
//不允许查看非自己的数据
|
|
if ($show->model()->agent_id != Admin::user()->id) {
|
|
Admin::exit('数据不存在');
|
|
}
|
|
|
|
$show->field('id');
|
|
$show->field('name');
|
|
$show->field('pid');
|
|
$show->field('sort');
|
|
$show->field('template');
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Make a form builder.
|
|
*
|
|
* @return Form
|
|
*/
|
|
protected function form()
|
|
{
|
|
return Form::make(new Category(), function (Form $form) {
|
|
//不允许查看非自己的数据
|
|
if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
|
|
return $form->response()->error('数据不存在');
|
|
}
|
|
|
|
$agent_id = Admin::user()->id;
|
|
$options = Category::selectOptions(fn($query) => $query->where('agent_id', $agent_id));
|
|
|
|
$form->display('id');
|
|
$form->select('pid')->options($options)->required();
|
|
$form->text('name')->required();
|
|
$form->text('sort')->default(255)->help('越小越靠前');
|
|
// $form->text('template');
|
|
})->saving(function (Form $form) {
|
|
//不允许修改非自己的数据
|
|
if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
|
|
return $form->response()->error('数据不存在');
|
|
}
|
|
|
|
//不允许编辑的字段
|
|
$form->ignore(['id', 'deleted_at']);
|
|
|
|
$form->agent_id = Admin::user()->id;
|
|
$form->sort = $form->sort ?? 255;
|
|
})->deleting(function (Form $form) {
|
|
//不允许修改非自己的数据
|
|
if (array_filter($form->model()->toArray(), fn($v) => $v['agent_id'] != Admin::user()->id)) {
|
|
return $form->response()->error('数据不存在');
|
|
}
|
|
|
|
//获取要删除类目的所有下级ID
|
|
$agent_id = Admin::user()->id;
|
|
$category = Category::where('agent_id', $agent_id)->pluck('pid', 'id')->toArray();
|
|
$ids = getChildCate((int)$form->getKey(), $category);
|
|
|
|
if (AgentProduct::where('agent_id', $agent_id)->whereIn('category_id', $ids)->exists()) {
|
|
return $form->response()->error('该分类下已经发布产品,不允许删除');
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
//获取$pid下的所有子类目
|
|
function getChildCate($pid, $category): array
|
|
{
|
|
$cate_arr = [$pid];
|
|
foreach ($category as $k => $v) {
|
|
if ($v == $pid) {
|
|
$cate_arr = array_merge($cate_arr, getChildCate($k, $category));
|
|
}
|
|
}
|
|
return $cate_arr;
|
|
}
|