| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- <?php
- namespace App\Module\Game\AdminControllers;
- use App\Module\Game\AdminControllers\Actions\DuplicateRewardGroupAction;
- use App\Module\Game\Enums\REWARD_TYPE;
- use App\Module\Game\Models\GameRewardItem;
- use App\Module\Game\Repositorys\GameRewardGroupRepository;
- use App\Module\GameItems\Models\Item;
- use App\Module\Fund\Models\FundCurrencyModel;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use Dcat\Admin\Http\Controllers\AdminController;
- use Spatie\RouteAttributes\Attributes\Resource;
- /**
- * 奖励组管理控制器
- */
- #[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 => '是']);
- });
- // 使用 RowAction 类添加行操作按钮
- $grid->actions(function (Grid\Displayers\Actions $actions) {
- // 添加复制按钮
- $actions->append(new DuplicateRewardGroupAction());
- $actions->disableDelete();
- });
- });
- }
- /**
- * 创建详情页
- *
- * @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();
- // 保存控制器实例的引用,以便在闭包中使用
- $controller = $this;
- $show->field('奖励项')->unescape()->as(function () use ($controller) {
- $items = GameRewardItem::where('group_id', $this->getKey())->get();
- if ($items->isEmpty()) {
- return '无奖励项';
- }
- $headers = ['ID', '奖励类型', '奖励内容', '数量', '权重', '必中'];
- $rows = [];
- foreach ($items as $item) {
- // 根据奖励类型获取实际内容,使用控制器实例调用方法
- $rewardContent = $controller->getRewardContent($item);
- $rows[] = [
- $item->id,
- REWARD_TYPE::getName($item->reward_type),
- $rewardContent,
- $item->quantity,
- $item->weight,
- $item->is_guaranteed ? '是' : '否'
- ];
- }
- // 创建HTML表格
- $html = '<div class="table-responsive"><table class="table table-bordered">';
- // 添加表头
- $html .= '<thead><tr>';
- foreach ($headers as $header) {
- $html .= "<th>{$header}</th>";
- }
- $html .= '</tr></thead>';
- // 添加表体
- $html .= '<tbody>';
- foreach ($rows as $row) {
- $html .= '<tr>';
- foreach ($row as $cell) {
- $html .= "<td>{$cell}</td>";
- }
- $html .= '</tr>';
- }
- $html .= '</tbody>';
- $html .= '</table></div>';
- return $html;
- });
- });
- }
- /**
- * 根据奖励项获取实际内容描述
- *
- * @param GameRewardItem $item 奖励项
- * @return string 奖励内容描述
- */
- public function getRewardContent(GameRewardItem $item): string
- {
- switch ($item->reward_type) {
- case REWARD_TYPE::ITEM->value:
- // 物品奖励
- try {
- $gameItem = Item::find($item->target_id);
- if ($gameItem) {
- $content = "<strong>{$gameItem->name}</strong> (ID: {$item->target_id})";
- // 添加参数说明
- $params = [];
- if ($item->param1 > 0) {
- $params[] = "品质: {$item->param1}";
- }
- if ($item->param2 > 0) {
- $params[] = "绑定: {$item->param2}";
- }
- if (!empty($params)) {
- $content .= "<br><small>" . implode(', ', $params) . "</small>";
- }
- return $content;
- }
- } catch (\Exception $e) {
- // 忽略异常,返回默认内容
- }
- return "物品 (ID: {$item->target_id})";
- case REWARD_TYPE::CURRENCY->value:
- // 货币奖励
- try {
- $currency = FundCurrencyModel::find($item->target_id);
- if ($currency) {
- $content = "<strong>{$currency->name}</strong> (ID: {$item->target_id})";
- // 添加参数说明
- $params = [];
- if ($item->param1 > 0) {
- $params[] = "来源: {$item->param1}";
- }
- if (!empty($params)) {
- $content .= "<br><small>" . implode(', ', $params) . "</small>";
- }
- return $content;
- }
- } catch (\Exception $e) {
- // 忽略异常,返回默认内容
- }
- return "货币 (ID: {$item->target_id})";
- case REWARD_TYPE::PET_EXP->value:
- // 宠物经验奖励
- return "宠物经验 (宠物ID: {$item->target_id})";
- case REWARD_TYPE::PET_ENERGY->value:
- // 宠物体力奖励
- return "宠物体力 (宠物ID: {$item->target_id})";
- case REWARD_TYPE::OTHER->value:
- // 其他奖励
- return "其他奖励 (ID: {$item->target_id})";
- default:
- return "未知奖励类型 (ID: {$item->target_id})";
- }
- }
- /**
- * 创建表单
- *
- * @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->display('created_at', '创建时间');
- $form->display('updated_at', '更新时间');
- // 添加提示信息,告知用户如何管理奖励项
- if ($form->isEditing()) {
- $id = $form->getKey();
- $form->html('<div class="alert alert-info">
- <p><i class="fa fa-info-circle"></i> 奖励项管理已移至单独页面,请保存当前表单后使用以下链接管理奖励项:</p>
- <a href="'.admin_url('game-reward-items?group_id='.$id).'" class="btn btn-primary" target="_blank">
- <i class="fa fa-list"></i> 管理奖励项
- </a>
- </div>');
- } else {
- $form->html('<div class="alert alert-info">
- <p><i class="fa fa-info-circle"></i> 奖励项管理已移至单独页面,请先保存当前表单,然后才能添加奖励项。</p>
- </div>');
- }
- });
- }
- }
|