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.
243 lines
9.8 KiB
243 lines
9.8 KiB
<?php
|
|
|
|
namespace App\Admin\Controllers;
|
|
|
|
use App\Admin\Actions\Post\DividerAction; // 引入分隔符操作
|
|
use App\Admin\Actions\Post\ForceDeleteProductAction; // 引入强制删除商品操作
|
|
use App\Admin\Actions\Post\ProductStatusAction; // 引入商品状态操作
|
|
use App\Admin\Transforms\YesNoTransform; // 引入是/否转换器
|
|
use App\Http\Controllers\Controller; // 引入基础控制器
|
|
use App\Models\Category; // 引入分类模型
|
|
use App\Models\Product; // 引入商品模型
|
|
use Encore\Admin\Controllers\HasResourceActions; // 引入资源控制器特性
|
|
use Encore\Admin\Form; // 引入表单构建器
|
|
use Encore\Admin\Grid; // 引入网格构建器
|
|
use Encore\Admin\Layout\Content; // 引入内容布局
|
|
use Encore\Admin\Show; // 引入展示构建器
|
|
|
|
class ProductController extends Controller
|
|
{
|
|
use HasResourceActions; // 使用资源控制器特性,使该控制器支持资源操作
|
|
|
|
/**
|
|
* 显示商品列表界面.
|
|
*
|
|
* @param Content $content 内容布局实例
|
|
* @return Content 返回内容布局
|
|
*/
|
|
public function index(Content $content)
|
|
{
|
|
return $content
|
|
->header('商品列表') // 设置页面标题
|
|
->description('') // 设置页面描述(可选)
|
|
->body($this->grid()); // 渲染商品网格
|
|
}
|
|
|
|
/**
|
|
* 显示商品详情界面.
|
|
*
|
|
* @param mixed $id 商品 ID
|
|
* @param Content $content 内容布局实例
|
|
* @return Content 返回内容布局
|
|
*/
|
|
public function show($id, Content $content)
|
|
{
|
|
return $content
|
|
->header('详情') // 设置页面标题
|
|
->description('') // 设置页面描述(可选)
|
|
->body($this->detail($id)); // 渲染商品详情
|
|
}
|
|
|
|
/**
|
|
* 显示编辑商品界面.
|
|
*
|
|
* @param mixed $id 商品 ID
|
|
* @param Content $content 内容布局实例
|
|
* @return Content 返回内容布局
|
|
*/
|
|
public function edit($id, Content $content)
|
|
{
|
|
return $content
|
|
->header('编辑') // 设置页面标题
|
|
->description('') // 设置页面描述(可选)
|
|
->body($this->form()->edit($id)); // 渲染编辑表单
|
|
}
|
|
|
|
/**
|
|
* 显示添加商品界面.
|
|
*
|
|
* @param Content $content 内容布局实例
|
|
* @return Content 返回内容布局
|
|
*/
|
|
public function create(Content $content)
|
|
{
|
|
return $content
|
|
->header('添加商品') // 设置页面标题
|
|
->description('为你的商城添加一个商品') // 设置页面描述
|
|
->body($this->form()); // 渲染添加商品表单
|
|
}
|
|
|
|
/**
|
|
* 创建商品网格构建器.
|
|
*
|
|
* @return Grid 返回网格实例
|
|
*/
|
|
protected function grid()
|
|
{
|
|
$grid = new Grid(new Product); // 创建新的网格实例,基于 Product 模型
|
|
|
|
$grid->model()->withTrashed()->latest(); // 显示软删除的记录,并按创建时间降序排列
|
|
|
|
// 定义网格的列
|
|
$grid->column('id'); // 显示商品 ID
|
|
$grid->column('category.title', '商品类别'); // 显示商品类别
|
|
$grid->column('name', '商品名')->display(function ($name) {
|
|
return str_limit($name, 30); // 显示商品名,限制字符数为 30
|
|
});
|
|
$grid->column('thumb', '首图')->image('', 50, 50); // 显示商品缩略图
|
|
$grid->column('price', '价格')->display(function ($price) {
|
|
return $price . '/' . $this->original_price; // 显示销售价和原价
|
|
});
|
|
$grid->column('view_count', '浏览次数')->sortable(); // 显示并支持排序的浏览次数
|
|
$grid->column('sale_count', '售出数量')->sortable(); // 显示并支持排序的售出数量
|
|
$grid->column('count', '库存量')->sortable(); // 显示并支持排序的库存量
|
|
$grid->column('deleted_at', '是否上架')->display(function ($isAlive) {
|
|
return YesNoTransform::trans(is_null($isAlive)); // 转换并显示是否上架
|
|
});
|
|
$grid->column('created_at', '创建时间'); // 显示创建时间
|
|
$grid->column('updated_at', '修改时间'); // 显示修改时间
|
|
|
|
// 查询过滤
|
|
$grid->filter(function (Grid\Filter $filter) {
|
|
$categories = Category::query()
|
|
->orderBy('order')
|
|
->latest()
|
|
->pluck('title', 'id')
|
|
->all(); // 获取分类列表
|
|
|
|
$filter->disableIdFilter(); // 禁用 ID 过滤
|
|
$filter->equal('category_id', '分类')->select($categories); // 分类过滤
|
|
$filter->equal('id', 'ID'); // ID 过滤
|
|
$filter->equal('uuid', 'UUID'); // UUID 过滤
|
|
$filter->like('name', '商品名字'); // 商品名字模糊查询
|
|
});
|
|
|
|
// 增加一个上架/下架功能
|
|
$grid->actions(function (Grid\Displayers\DropdownActions $actions) {
|
|
$actions->disableDelete(); // 禁用删除按钮
|
|
$actions->add(new ForceDeleteProductAction()); // 添加强制删除商品操作
|
|
$actions->add(new DividerAction()); // 添加分隔符
|
|
|
|
// 根据商品是否上架设置操作按钮名称
|
|
$name = is_null($actions->row->deleted_at) ? "下架" : "上架";
|
|
$statusAction = new ProductStatusAction(); // 创建商品状态操作
|
|
$statusAction->setName($name); // 设置操作按钮名称
|
|
|
|
$actions->add($statusAction); // 添加状态操作按钮
|
|
});
|
|
|
|
return $grid; // 返回构建好的网格
|
|
}
|
|
|
|
/**
|
|
* 创建商品详情展示构建器.
|
|
*
|
|
* @param mixed $id 商品 ID
|
|
* @return Show 返回展示实例
|
|
*/
|
|
protected function detail($id)
|
|
{
|
|
$show = new Show(Product::query()->withTrashed()->findOrFail($id)); // 查找并显示指定 ID 的商品记录
|
|
|
|
// 定义详情展示的字段
|
|
$show->field('id'); // 显示商品 ID
|
|
$show->field('category.title', '商品类别'); // 显示商品类别
|
|
$show->field('name', '商品名'); // 显示商品名
|
|
$show->field('title', '卖点'); // 显示商品卖点
|
|
$show->field('thumb', '缩略图')->image(); // 显示商品缩略图
|
|
$show->field('price', '价格')->as(function ($price) {
|
|
return $price . '/' . $this->original_price; // 显示销售价和原价
|
|
});
|
|
$show->field('view_count', '浏览次数'); // 显示浏览次数
|
|
$show->field('sale_count', '售出数量'); // 显示售出数量
|
|
$show->field('count', '库存量'); // 显示库存量
|
|
$show->field('deleted_at', '是否上架')->as(function ($isAlive) {
|
|
return is_null($isAlive) ? '上架' : '下架'; // 显示商品是否上架
|
|
});
|
|
$show->field('created_at', '创建时间'); // 显示创建时间
|
|
$show->field('updated_at', '修改时间'); // 显示修改时间
|
|
|
|
return $show; // 返回构建好的详情视图
|
|
}
|
|
|
|
/**
|
|
* 创建商品表单构建器.
|
|
*
|
|
* @return Form 返回表单实例
|
|
*/
|
|
protected function form()
|
|
{
|
|
$form = new Form(new Product); // 创建新的表单实例,基于 Product 模型
|
|
|
|
// 定义表单字段
|
|
$form->select('category_id', '类别')->options(Category::selectOrderAll())->rules('required|exists:categories,id'); // 选择商品类别
|
|
$form->text('name', '商品名字')->rules(function (Form $form) {
|
|
$rules = 'required|max:50|unique:products,name'; // 商品名字的验证规则
|
|
if ($id = $form->model()->id) {
|
|
$rules .= ',' . $id; // 在编辑时排除当前商品
|
|
}
|
|
return $rules; // 返回验证规则
|
|
});
|
|
$form->textarea('title', '卖点')->rules('required|max:199'); // 商品卖点
|
|
$form->currency('price', '销售价')->symbol('¥')->rules('required|numeric'); // 商品销售价
|
|
$form->currency('original_price', '原价')->symbol('¥')->rules('required|numeric'); // 商品原价
|
|
$form->number('count', '库存量')->rules('required|integer|min:0'); // 商品库存量
|
|
|
|
$form->image('thumb', '缩略图')->uniqueName()->move('products/thumb')->rules('required'); // 商品缩略图
|
|
$form->multipleImage('pictures', '轮播图')->uniqueName()->move('products/lists'); // 商品轮播图
|
|
|
|
$form->editor('detail.content', '详情')->rules('required'); // 商品详情
|
|
|
|
// 保存前的钩子
|
|
$form->saving(function (Form $form) {
|
|
if (app()->environment('dev')) { // 在开发环境中禁止操作
|
|
admin_toastr('开发环境不允许操作', 'error'); // 显示错误提示
|
|
return back()->withInput(); // 返回输入
|
|
}
|
|
});
|
|
|
|
return $form; // 返回构建好的表单
|
|
}
|
|
|
|
/**
|
|
* 删除指定商品.
|
|
*
|
|
* @param mixed $id 商品 ID
|
|
* @return \Illuminate\Http\JsonResponse 返回 JSON 响应
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
$product = Product::query()->withTrashed()->findOrFail($id); // 查找指定 ID 的商品
|
|
|
|
if (app()->environment('dev')) { // 在开发环境中禁止操作
|
|
admin_toastr('开发环境不允许操作', 'error'); // 显示错误提示
|
|
return back()->withInput(); // 返回输入
|
|
}
|
|
|
|
// 尝试强制删除商品
|
|
if ($product->forceDelete()) {
|
|
$data = [
|
|
'status' => true,
|
|
'message' => trans('admin.delete_succeeded'), // 返回成功消息
|
|
];
|
|
} else {
|
|
$data = [
|
|
'status' => false,
|
|
'message' => trans('admin.delete_failed'), // 返回失败消息
|
|
];
|
|
}
|
|
|
|
return response()->json($data); // 返回 JSON 响应
|
|
}
|
|
}
|