| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- <?php
- namespace App\Module\Game\AdminControllers;
- use App\Module\Game\Enums\REWARD_SOURCE_TYPE;
- use App\Module\Game\Enums\REWARD_TYPE;
- use App\Module\Game\Models\GameRewardGroup;
- use App\Module\Game\Repositorys\GameRewardLogRepository;
- use App\Module\Game\Services\RewardSourceResolver;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use Dcat\Admin\Http\Controllers\AdminController;
- use Dcat\Admin\Widgets\Card;
- use Dcat\Admin\Widgets\Table;
- use Spatie\RouteAttributes\Attributes\Resource;
- /**
- * 奖励日志管理控制器
- */
- #[Resource('game-reward-logs', names: 'dcat.admin.game-reward-logs')]
- class GameRewardLogController extends AdminController
- {
- /**
- * 标题
- *
- * @return string
- */
- protected function title()
- {
- return '奖励日志管理';
- }
- /**
- * 创建表格
- *
- * @return Grid
- */
- protected function grid()
- {
- return Grid::make(new GameRewardLogRepository(), function (Grid $grid) {
- $grid->column('id', 'ID')->sortable();
- $grid->column('user_id', '用户ID');
- $grid->column('group_id', '奖励组')->display(function ($groupId) {
- $group = GameRewardGroup::find($groupId);
- return $group ? "{$group->name} ({$group->code})" : "未知 ({$groupId})";
- });
- $grid->column('source_type', '来源类型')->display(function ($type) {
- return REWARD_SOURCE_TYPE::getName($type);
- });
- $grid->column('source_id', '来源ID');
- $grid->column('source_detail', '来源详情')->display(function () {
- $sourceInfo = RewardSourceResolver::resolve($this->source_type, $this->source_id);
- $text = $sourceInfo['name'];
- if ($sourceInfo['link']) {
- return "<a href='{$sourceInfo['link']}' target='_blank' class='text-primary'>{$text}</a>";
- }
- return $text;
- });
- $grid->column('reward_items', '奖励项')->display(function ($items) {
- // 由于模型中已设置了json cast,$items已经是数组,无需再次json_decode
- if (empty($items)) {
- return '无奖励项';
- }
- $count = count($items);
- return "<span class=\"badge badge-primary\">{$count}个奖励项</span>";
- });
- $grid->column('created_at', '创建时间');
- $grid->filter(function (Grid\Filter $filter) {
- $filter->equal('id', 'ID');
- $filter->equal('user_id', '用户ID');
- $filter->equal('group_id', '奖励组')->select(
- GameRewardGroup::pluck('name', 'id')
- );
- $filter->equal('source_type', '来源类型')->select(REWARD_SOURCE_TYPE::getValueDescription());
- $filter->equal('source_id', '来源ID');
- $filter->between('created_at', '创建时间')->datetime();
- });
- // 禁用创建按钮
- $grid->disableCreateButton();
- // 禁用编辑和删除
- $grid->actions(function (Grid\Displayers\Actions $actions) {
- $actions->disableEdit();
- $actions->disableDelete();
- });
- });
- }
- /**
- * 创建详情页
- *
- * @param mixed $id
- * @return Show
- */
- protected function detail($id)
- {
- return Show::make($id, new GameRewardLogRepository(), function (Show $show) {
- $show->field('id', 'ID');
- $show->field('user_id', '用户ID');
- $show->field('group_id', '奖励组')->as(function ($groupId) {
- $group = GameRewardGroup::find($groupId);
- return $group ? "{$group->name} ({$group->code})" : "未知 ({$groupId})";
- });
- $show->field('source_type', '来源类型')->as(function ($type) {
- return REWARD_SOURCE_TYPE::getName($type);
- });
- $show->field('source_id', '来源ID');
- // 显示详细的来源信息
- $show->field('source_detail', '来源详情')->as(function () {
- $sourceInfo = RewardSourceResolver::resolve($this->source_type, $this->source_id);
- $html = "<div class='source-detail'>";
- $html .= "<h5>{$sourceInfo['type']}: {$sourceInfo['name']}</h5>";
- $html .= "<p class='text-muted'>{$sourceInfo['description']}</p>";
- if ($sourceInfo['link']) {
- $html .= "<p><a href='{$sourceInfo['link']}' target='_blank' class='btn btn-sm btn-primary'>查看详情</a></p>";
- }
- if (!empty($sourceInfo['extra'])) {
- $html .= "<div class='mt-2'>";
- $html .= "<small class='text-muted'>额外信息:</small><br>";
- foreach ($sourceInfo['extra'] as $key => $value) {
- $html .= "<small><strong>{$key}:</strong> {$value}</small><br>";
- }
- $html .= "</div>";
- }
- $html .= "</div>";
- return $html;
- })->unescape();
- $show->field('created_at', '创建时间');
- // 显示奖励项
- $show->divider();
- $show->field('奖励项')->as(function () {
- // 由于模型中已设置了json cast,reward_items已经是数组,无需再次json_decode
- $items = $this->reward_items;
- if (empty($items)) {
- return '无奖励项';
- }
- $headers = ['奖励类型', '目标ID', '参数1', '参数2', '数量', '额外数据'];
- $rows = [];
- foreach ($items as $item) {
- $rows[] = [
- REWARD_TYPE::getName($item['reward_type']),
- $item['target_id'],
- $item['param1'] ?? 0,
- $item['param2'] ?? 0,
- $item['quantity'],
- json_encode($item['extra_data'] ?? null)
- ];
- }
- return Card::make(
- Table::make($headers, $rows)
- );
- })->unescape();
- });
- }
- }
|