| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- <?php
- namespace App\Module\GameItems\AdminControllers;
- use App\Module\GameItems\Repositorys\ItemChestOpenLogRepository;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use UCore\DcatAdmin\AdminController;
- use Dcat\Admin\Layout\Content;
- use Spatie\RouteAttributes\Attributes\Resource;
- use App\Module\GameItems\AdminControllers\Helper\FilterHelper;
- use App\Module\GameItems\AdminControllers\Helper\GridHelper;
- use App\Module\GameItems\AdminControllers\Helper\ShowHelper;
- /**
- * 宝箱开启记录管理控制器
- *
- * @package App\Module\GameItems\AdminControllers
- */
- #[Resource('game-items-chest-open-logs', names: 'dcat.admin.game-items-chest-open-logs')]
- class ChestOpenLogController extends AdminController
- {
- /**
- * 标题
- *
- * @var string
- */
- protected $title = '宝箱开启记录';
- /**
- * 禁用创建按钮
- *
- * @var bool
- */
- protected $showCreateButton = false;
- /**
- * 列表页
- *
- * @return Grid
- */
- protected function grid()
- {
- return Grid::make(new ItemChestOpenLogRepository(), function (Grid $grid) {
- $helper = new GridHelper($grid, $this);
- // 禁用创建、编辑和删除按钮
- $grid->disableCreateButton();
- $grid->disableBatchDelete();
- $grid->disableDeleteButton();
- $grid->disableEditButton();
- // 只保留详情按钮
- $grid->actions(function (Grid\Displayers\Actions $actions) {
- $actions->disableDelete();
- $actions->disableEdit();
- $actions->disableQuickEdit();
- // 保留详情按钮(show)
- });
- $helper->columnId();
- $grid->column('user_id', '用户ID');
- $grid->column('chest.name', '宝箱名称');
- $grid->column('open_quantity', '开启数量');
- // 奖励内容列 - 使用RewardTypeDescriptor解析
- $grid->column('result_items', '奖励内容')->display(function ($resultItems) {
- if (empty($resultItems)) {
- return '无';
- }
- $rewardTexts = [];
- // 遍历每次开箱结果
- foreach ($resultItems as $index => $chestResult) {
- $chestRewards = [];
- // 遍历每个奖励物品(RewardItemDto格式)
- foreach ($chestResult as $item) {
- // 使用RewardTypeDescriptor解析奖励信息
- $rewardText = \App\Module\Game\Services\RewardTypeDescriptor::formatRewardDisplay(
- $item['rewardType'], // 奖励类型
- $item['targetId'], // 目标ID
- $item['quantity'], // 数量
- $item['param1'] ?? 0, // 参数1
- $item['param2'] ?? 0, // 参数2
- false // 不使用HTML徽章样式
- );
- $chestRewards[] = $rewardText;
- }
- if (count($resultItems) > 1) {
- $rewardTexts[] = '第' . ($index + 1) . '次: ' . implode(', ', $chestRewards);
- } else {
- $rewardTexts = $chestRewards;
- }
- }
- return implode('<br>', $rewardTexts);
- });
- $grid->column('open_time', '开启时间')->sortable();
- $grid->column('ip_address', 'IP地址');
- $grid->column('created_at', '创建时间');
- // 筛选
- $grid->filter(function ($filter) {
- $helper = new FilterHelper($filter, $this);
- $helper->equal('id', 'ID');
- $helper->equal('user_id', '用户ID');
- $filter->equal('chest_id', '宝箱')->select(
- \App\Module\GameItems\Models\Item::where('type', 5)->pluck('name', 'id')
- );
- $filter->between('open_time', '开启时间')->datetime();
- });
- });
- }
- /**
- * 详情页
- *
- * @param mixed $id
- * @param Content $content
- * @return Content
- */
- public function show($id, Content $content)
- {
- return $content
- ->header($this->title)
- ->description('详情')
- ->body($this->detail($id));
- }
- /**
- * 详情页
- *
- * @param mixed $id
- * @return Show
- */
- protected function detail($id)
- {
- return Show::make($id, new ItemChestOpenLogRepository(), function (Show $show) {
- $helper = new ShowHelper($show, $this);
- // 禁用编辑和删除按钮
- $show->panel()->tools(function ($tools) {
- $tools->disableEdit();
- $tools->disableDelete();
- });
- $helper->field('id', 'ID');
- $helper->field('user_id', '用户ID');
- $show->field('chest.name', '宝箱名称');
- $helper->field('open_quantity', '开启数量');
- // 显示奖励内容
- $show->field('result_items', '奖励内容')->as(function ($resultItems) {
- if (empty($resultItems)) {
- return '无';
- }
- if (is_string($resultItems)) {
- $resultItems = json_decode($resultItems, true);
- }
- if (is_array($resultItems)) {
- $html = '';
- foreach ($resultItems as $index => $chestResult) {
- $html .= '<h4>第' . ($index + 1) . '次开箱</h4>';
- $html .= '<table class="table table-bordered">';
- $html .= '<thead><tr><th>奖励类型</th><th>奖励名称</th><th>数量</th></tr></thead>';
- $html .= '<tbody>';
- foreach ($chestResult as $item) {
- // 使用RewardTypeDescriptor解析奖励信息(RewardItemDto格式)
- $rewardTypeName = \App\Module\Game\Enums\REWARD_TYPE::getName($item['rewardType']);
- $targetName = \App\Module\Game\Services\RewardTypeDescriptor::getTargetName(
- $item['rewardType'],
- $item['targetId'],
- $item['param1'] ?? 0,
- $item['param2'] ?? 0
- );
- $html .= '<tr>';
- $html .= '<td>' . $rewardTypeName . '</td>';
- $html .= '<td>' . $targetName . '</td>';
- $html .= '<td>' . $item['quantity'] . '</td>';
- $html .= '</tr>';
- }
- $html .= '</tbody></table>';
- }
- return $html;
- }
- return $resultItems;
- });
- $helper->field('open_time', '开启时间');
- $helper->field('ip_address', 'IP地址');
- $helper->field('device_info', '设备信息');
- $helper->field('created_at', '创建时间');
- });
- }
- }
|