header('列表') // 设置页面标题 ->description(':xxx 是变量模板,建议不要操作') // 设置页面描述 ->body($this->grid()); // 设置页面主体为网格 } /** * 显示详细信息界面. * * @param mixed $id * @param Content $content * @return Content */ public function show($id, Content $content) { return $content ->header('详情') // 设置页面标题 ->description('') // 设置页面描述 ->body($this->detail($id)); // 设置页面主体为详细信息 } /** * 编辑界面. * * @param mixed $id * @param Content $content * @return Content */ public function edit($id, Content $content) { return $content ->header('Edit') // 设置页面标题 ->description('') // 设置页面描述 ->body($this->form($id)->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 ScoreRule); // 创建新的网格实例,基于 ScoreRule 模型 $grid->model()->latest(); // 按照创建时间降序排列数据 // 定义网格的列 $grid->column('id', 'id'); // 显示日志 ID $grid->column('description', '描述'); // 显示描述 $grid->column('replace_text', '替换文本'); // 显示替换文本 $grid->column('score', '积分'); // 显示积分 $grid->column('times', '次数')->display(function ($times) { // 显示次数,如果没有则返回空 return $times ? $times : ''; }); $grid->column('created_at', '创建时间'); // 显示创建时间 $grid->column('updated_at', '修改时间'); // 显示修改时间 // 定义行操作 $grid->actions(function (Grid\Displayers\Actions $actions) { $rule = $actions->row; // 获取当前行数据 if (! $rule->can_delete) { // 如果该规则不可删除 $actions->disableDelete(); // 禁用删除按钮 } }); return $grid; // 返回构建好的网格 } /** * 创建展示构建器. * * @param mixed $id * @return Show */ protected function detail($id) { $show = new Show(ScoreRule::findOrFail($id)); // 创建展示实例,查找指定 ID 的记录 // 定义展示的字段 $show->field('id', 'id'); // 显示 ID $show->field('description', '描述'); // 显示描述 $show->field('replace_text', '替换文本'); // 显示替换文本 $show->field('score', '积分'); // 显示积分 $show->field('times', '次数')->as(function ($times) { // 显示次数,如果没有则返回空 return $times ? $times : ''; }); $show->field('created_at', '创建时间'); // 显示创建时间 $show->field('updated_at', '修改时间'); // 显示修改时间 return $show; // 返回展示实例 } /** * 创建表单构建器. * * @param null $id * @return Form */ protected function form($id = null) { $form = new Form(new ScoreRule); // 创建新的表单实例,基于 ScoreRule 模型 // 定义可选的积分规则类型 $options = [ ScoreRuleIndexEnum::CONTINUE_LOGIN => '连续登录', ScoreRuleIndexEnum::VISITED_PRODUCT => '每日浏览商品' ]; if (is_null($id)) { // 如果是创建新记录 $form->select('index_code', '类型')->options($options)->rules('required'); // 选择积分类型,必填 } $form->number('score', '积分'); // 输入积分 if (! is_null($id)) { // 如果是编辑现有记录 // 只有当是连续登录和修改的才有次数 $scoreRule = ScoreRule::query()->findOrFail($id); // 查找指定 ID 的规则 $form->textarea('replace_text', '替换文本'); // 输入替换文本 $form->textarea('description', '描述'); // 输入描述 if (array_key_exists($scoreRule->index_code, $options)) { // 如果规则类型在选项中 $form->number('times', '次数'); // 输入次数 } } else { $form->number('times', '次数'); // 输入次数 } // 表单保存前的处理 $form->saving(function (Form $form) { // 如果是新建记录,复制模板 if (! $form->model()->exists) { $rule = ScoreRule::query() ->where('can_delete', 0) // 查找不可删除的规则 ->where('index_code', $form->index_code) // 根据类型查找 ->firstOrFail(); // 查找失败则抛出异常 // 复制规则的描述和替换文本 $form->model()->description = $rule->description; $form->model()->replace_text = $rule->replace_text; } }); return $form; // 返回构建好的表单 } /** * 从存储中删除指定资源. * * @param int $id * @return \Illuminate\Http\Response * @throws \Exception */ public function destroy($id) { $rule = ScoreRule::query()->findOrFail($id); // 查找指定 ID 的规则 if (! $rule->can_delete) { // 如果该规则不可删除 return response()->json([ 'status' => false, 'message' => '这个等级不允许删除', // 返回错误信息 ]); } // 尝试删除规则 if ($rule->delete()) { $data = [ 'status' => true, 'message' => trans('admin.delete_succeeded'), // 返回成功信息 ]; } else { $data = [ 'status' => false, 'message' => trans('admin.delete_failed'), // 返回失败信息 ]; } return response()->json($data); // 返回 JSON 响应 } }