| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- <?php
- namespace App\Module\Pet\AdminControllers;
- use App\Module\Game\DCache\PetJsonConfig;
- use App\Module\Pet\Models\PetConfig;
- use App\Module\Pet\Repositorys\PetConfigRepository;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use Illuminate\Http\Request;
- use UCore\DcatAdmin\AdminController;
- use Spatie\RouteAttributes\Attributes\Resource;
- use Spatie\RouteAttributes\Attributes\Get;
- use App\Module\Pet\AdminControllers\Helper\FilterHelper;
- use App\Module\Pet\AdminControllers\Helper\FormHelper;
- use App\Module\Pet\AdminControllers\Helper\GridHelper;
- use App\Module\Pet\AdminControllers\Helper\ShowHelper;
- /**
- * 宠物配置管理控制器
- *
- * @package App\Module\Pet\AdminControllers
- */
- #[Resource('pet-configs', names: 'dcat.admin.pet-configs')]
- class PetConfigController extends AdminController
- {
- /**
- * 生成宠物JSON数据
- */
- #[Get('pet-configs/generate-json')]
- public function generateJson()
- {
- $result = PetJsonConfig::getData([], true);
- return response()->json([
- 'status' => $result['success'] ? 'success' : 'error',
- 'message' => $result['success'] ? 'JSON生成成功' : 'JSON生成失败: ' . ($result['error'] ?? '')
- ]);
- }
- /**
- * 标题
- *
- * @var string
- */
- protected $title = '宠物配置管理';
- /**
- * 列表页
- *
- * @return Grid
- */
- protected function grid()
- {
- return Grid::make(new PetConfigRepository(), function (Grid $grid) {
- $helper = new GridHelper($grid, $this);
- $grid->column('id', 'ID')->sortable();
- $grid->column('pet_type', '宠物类型');
- $grid->column('grade_probability', '品阶概率配置')->display(function ($value) {
- if (is_array($value) || is_object($value)) {
- return json_encode($value, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
- }
- return $value;
- });
- $grid->column('stamina_recovery', '体力恢复值/分钟')->display(function () {
- $numericAttributes = $this->numeric_attributes;
- if ($numericAttributes && isset($numericAttributes->stamina_recovery)) {
- return $numericAttributes->stamina_recovery . ' /分钟';
- }
- return '-';
- });
- $grid->column('created_at', '创建时间');
- $grid->column('updated_at', '更新时间');
- // 添加生成JSON配置按钮
- $grid->tools(function ($tools) {
- $tools->append('<a class="btn btn-primary btn-sm" href="javascript:void(0);" onclick="generatePetJson()">生成宠物配置JSON</a>');
- // 添加JavaScript代码
- admin_script(<<<JS
- function generatePetJson() {
- $.ajax({
- method: 'GET',
- url: '/admin/pet-configs/generate-json',
- success: function (data) {
- if (data.status === 'success') {
- Dcat.success(data.message);
- } else {
- Dcat.error(data.message);
- }
- },
- error: function (xhr) {
- Dcat.error('生成JSON失败: ' + xhr.statusText);
- }
- });
- }
- JS);
- });
- // 筛选
- $grid->filter(function ($filter) {
- $helper = new FilterHelper($filter, $this);
- $helper->equal('id', 'ID');
- $filter->like('pet_type', '宠物类型');
- $filter->equal('max_level', '最大等级');
- });
- return $grid;
- });
- }
- /**
- * 详情页
- *
- * @param mixed $id
- * @return Show
- */
- protected function detail($id)
- {
- return Show::make($id, new PetConfigRepository(), function (Show $show) {
- $helper = new ShowHelper($show, $this);
- $helper->field('id', 'ID');
- $show->field('pet_type', '宠物类型');
- $show->field('grade_probability', '品阶概率配置')->json();
- $show->field('display_attributes', '显示属性配置')->json();
- $show->field('numeric_attributes', '数值属性配置')->json();
- $show->field('created_at', '创建时间');
- $show->field('updated_at', '更新时间');
- return $show;
- });
- }
- /**
- * 表单
- *
- * @return Form
- */
- protected function form()
- {
- return Form::make(new PetConfigRepository(), function (Form $form) {
- $helper = new FormHelper($form, $this);
- $form->display('id', 'ID');
- $form->text('pet_type', '宠物类型')
- ->required()
- ->help('宠物类型标识,如:松狮、哈士奇等');
- $helper->embedsCats('grade_probability', '品阶概率配置')
- ->help('各品阶的概率配置,如:{"FIRST":0.6,"SECOND":0.25,"THIRD":0.1,"FOURTH":0.05}');
- $helper->embedsCats('display_attributes', '显示属性配置')
- ->help('宠物的显示属性配置');
- $helper->embedsCats('numeric_attributes', '数值属性配置')
- ->help('宠物的数值属性配置');
- $form->display('created_at', '创建时间');
- $form->display('updated_at', '更新时间');
- return $form;
- });
- }
- }
|