| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- <?php
- namespace App\Module\Game\AdminControllers;
- use App\Module\Game\Enums\REWARD_TYPE;
- use App\Module\Game\Models\GameRewardGroup;
- use App\Module\Game\Models\GameRewardItem;
- use App\Module\Game\Repositorys\GameRewardGroupRepository;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use Dcat\Admin\Http\Controllers\AdminController;
- use Dcat\Admin\Layout\Content;
- use Dcat\Admin\Widgets\Card;
- use Dcat\Admin\Widgets\Table;
- use Spatie\RouteAttributes\Attributes\Resource;
- use Spatie\RouteAttributes\Attributes\Get;
- /**
- * 奖励组管理控制器
- */
- #[Resource('game-reward-groups', names: 'dcat.admin.game-reward-groups')]
- class GameRewardGroupController extends AdminController
- {
- /**
- * 标题
- *
- * @return string
- */
- protected function title()
- {
- return '奖励组管理';
- }
- /**
- * 创建表格
- *
- * @return Grid
- */
- protected function grid()
- {
- return Grid::make(new GameRewardGroupRepository(), function (Grid $grid) {
- $grid->column('id', 'ID')->sortable();
- $grid->column('name', '名称');
- $grid->column('code', '编码');
- $grid->column('description', '描述')->limit(30);
- $grid->column('is_random', '随机发放')->switch();
- $grid->column('random_count', '随机数量');
- $grid->column('created_at', '创建时间');
- $grid->column('updated_at', '更新时间');
- $grid->filter(function (Grid\Filter $filter) {
- $filter->equal('id', 'ID');
- $filter->like('name', '名称');
- $filter->like('code', '编码');
- $filter->equal('is_random', '随机发放')->select([0 => '否', 1 => '是']);
- });
- $grid->actions(function (Grid\Displayers\Actions $actions) {
- // 添加复制按钮
- $actions->append('<a href="' . admin_url('game/reward-groups/' . $actions->getKey() . '/duplicate') . '"><i class="fa fa-copy"></i> 复制</a>');
- });
- });
- }
- /**
- * 创建详情页
- *
- * @param mixed $id
- * @return Show
- */
- protected function detail($id)
- {
- return Show::make($id, new GameRewardGroupRepository(), function (Show $show) {
- $show->field('id', 'ID');
- $show->field('name', '名称');
- $show->field('code', '编码');
- $show->field('description', '描述');
- $show->field('is_random', '随机发放')->as(function ($value) {
- return $value ? '是' : '否';
- });
- $show->field('random_count', '随机数量');
- $show->field('created_at', '创建时间');
- $show->field('updated_at', '更新时间');
- // 显示奖励项
- $show->divider();
- $show->field('奖励项')->as(function () {
- $items = GameRewardItem::where('group_id', $this->id)->get();
- if ($items->isEmpty()) {
- return '无奖励项';
- }
- $headers = ['ID', '奖励类型', '目标ID', '参数1', '参数2', '数量', '权重', '必中'];
- $rows = [];
- foreach ($items as $item) {
- $rows[] = [
- $item->id,
- REWARD_TYPE::getName($item->reward_type),
- $item->target_id,
- $item->param1,
- $item->param2,
- $item->quantity,
- $item->weight,
- $item->is_guaranteed ? '是' : '否'
- ];
- }
- return Card::make(
- Table::make($headers, $rows)
- );
- });
- });
- }
- /**
- * 创建表单
- *
- * @return Form
- */
- protected function form()
- {
- return Form::make(new GameRewardGroupRepository(), function (Form $form) {
- $form->display('id', 'ID');
- $form->text('name', '名称')->required();
- $form->text('code', '编码')->required()->rules('required|alpha_dash|unique:game_reward_groups,code,' . $form->getKey());
- $form->textarea('description', '描述');
- $form->switch('is_random', '随机发放')->default(0);
- $form->number('random_count', '随机数量')->default(1)->min(1)->help('随机发放时的奖励数量,仅当随机发放开启时有效');
- $form->hasMany('rewardItems', '奖励项', function (Form\NestedForm $form) {
- $form->select('reward_type', '奖励类型')->options(REWARD_TYPE::getAll())->required();
- $form->number('target_id', '目标ID')->required()->help('根据奖励类型不同,表示物品ID、货币ID等');
- $form->number('param1', '参数1')->default(0)->help('根据奖励类型不同含义,如物品的品质、货币的来源等');
- $form->number('param2', '参数2')->default(0)->help('根据奖励类型不同含义,如物品的绑定状态、货币的类型等');
- $form->number('quantity', '数量')->default(1)->min(1)->required();
- $form->number('weight', '权重')->default(1.00)->step(0.01)->required()->help('随机发放时使用,权重越高,被选中的概率越大');
- $form->switch('is_guaranteed', '必中')->default(0)->help('开启后,在随机发放时会优先选择');
- $form->textarea('extra_data', '额外数据')->help('JSON格式,可存储特定奖励类型的额外参数');
- });
- $form->display('created_at', '创建时间');
- $form->display('updated_at', '更新时间');
- });
- }
- /**
- * 复制奖励组
- *
- * @param int $id
- * @param Content $content
- * @return Content
- */
- public function duplicate($id, Content $content)
- {
- $repository = new GameRewardGroupRepository();
- $newGroup = $repository->duplicate($id);
- return $content
- ->title($this->title())
- ->description('复制奖励组')
- ->body(admin_success('复制成功', "已成功复制奖励组 [{$newGroup->name}]"))
- ->body("<script>setTimeout(function(){ window.location.href = '" . admin_url('game/reward-groups') . "'; }, 2000);</script>");
- }
- }
|