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.
193 lines
6.2 KiB
193 lines
6.2 KiB
<?php
|
|
|
|
namespace App\Admin\Controllers;
|
|
|
|
use App\Models\Level; // 引入 Level 模型
|
|
use App\Http\Controllers\Controller; // 引入基础控制器
|
|
use Encore\Admin\Controllers\HasResourceActions; // 引入资源控制器特性
|
|
use Encore\Admin\Form; // 引入表单构建器
|
|
use Encore\Admin\Grid; // 引入网格构建器
|
|
use Encore\Admin\Layout\Content; // 引入内容布局
|
|
use Encore\Admin\Show; // 引入展示构建器
|
|
|
|
class LevelController 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 Level); // 创建新的网格实例,基于 Level 模型
|
|
|
|
$grid->model()->orderBy('min_score', 'desc'); // 按 min_score 降序排列
|
|
|
|
// 定义网格的列
|
|
$grid->column('id', 'ID'); // 显示 ID 列
|
|
$grid->column('icon', '图标')->image('', 90, 90); // 显示图标列,设置图像大小
|
|
$grid->column('name', '名字')->editable(); // 显示名字列,可编辑
|
|
$grid->column('level', '等级'); // 显示等级列
|
|
$grid->column('min_score', '分阶'); // 显示分阶列
|
|
$grid->column('created_at', '创建时间'); // 显示创建时间列
|
|
$grid->column('updated_at', '更新时间'); // 显示更新时间列
|
|
|
|
// 自定义操作按钮
|
|
$grid->actions(function (Grid\Displayers\Actions $actions) {
|
|
$level = $actions->row; // 获取当前行的数据
|
|
if (!$level->can_delete) { // 检查是否可以删除
|
|
$actions->disableDelete(); // 禁用删除按钮
|
|
}
|
|
});
|
|
|
|
return $grid; // 返回构建好的网格
|
|
}
|
|
|
|
/**
|
|
* 创建详情构建器.
|
|
*
|
|
* @param mixed $id 资源 ID
|
|
* @return Show 返回展示实例
|
|
*/
|
|
protected function detail($id)
|
|
{
|
|
$show = new Show(Level::findOrFail($id)); // 查找并显示指定 ID 的 Level 记录
|
|
|
|
// 定义详情展示的字段
|
|
$show->field('id', 'ID'); // 显示 ID
|
|
$show->field('icon', '图标')->image(); // 显示图标
|
|
$show->field('name', '名字'); // 显示名字
|
|
$show->field('level', '等级'); // 显示等级
|
|
$show->field('min_score', '分阶'); // 显示分阶
|
|
$show->field('created_at', '创建时间'); // 显示创建时间
|
|
$show->field('updated_at', '更新时间'); // 显示更新时间
|
|
|
|
return $show; // 返回构建好的详情视图
|
|
}
|
|
|
|
/**
|
|
* 创建表单构建器.
|
|
*
|
|
* @return Form 返回表单实例
|
|
*/
|
|
protected function form()
|
|
{
|
|
$form = new Form(new Level); // 创建新的表单实例,基于 Level 模型
|
|
|
|
// 添加图标字段,设置唯一名称
|
|
$icon = $form->image('icon', '图标')->uniqueName();
|
|
if (!windows_os()) { // 如果不是 Windows 系统
|
|
$icon->resize(160, 160); // 调整图标大小
|
|
}
|
|
|
|
// 添加其他字段
|
|
$form->text('name', '名字'); // 添加名字字段
|
|
$form->number('level', '等级'); // 添加等级字段
|
|
$form->number('min_score', '积分'); // 添加积分字段
|
|
|
|
// 添加保存前的钩子
|
|
$form->saving(function (Form $form) {
|
|
if (app()->environment('dev')) { // 如果当前环境是开发环境
|
|
admin_toastr('开发环境不允许操作', 'error'); // 显示错误提示
|
|
return back()->withInput(); // 返回并保留输入
|
|
}
|
|
});
|
|
|
|
return $form; // 返回构建好的表单
|
|
}
|
|
|
|
/**
|
|
* 删除指定资源.
|
|
*
|
|
* @param int $id 资源 ID
|
|
* @return \Illuminate\Http\Response 返回响应
|
|
* @throws \Exception
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
$level = Level::query()->findOrFail($id); // 查找指定 ID 的 Level 记录
|
|
|
|
if (!$level->can_delete) { // 检查是否可以删除
|
|
return response()->json([
|
|
'status' => false,
|
|
'message' => '这个等级不允许删除', // 返回错误消息
|
|
]);
|
|
}
|
|
|
|
// 尝试删除记录
|
|
if ($level->delete()) {
|
|
$data = [
|
|
'status' => true,
|
|
'message' => trans('admin.delete_succeeded'), // 返回成功消息
|
|
];
|
|
} else {
|
|
$data = [
|
|
'status' => false,
|
|
'message' => trans('admin.delete_failed'), // 返回失败消息
|
|
];
|
|
}
|
|
|
|
return response()->json($data); // 返回 JSON 响应
|
|
}
|
|
}
|