| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- <?php
- namespace App\Module\Fund\AdminControllers;
- use App\Module\Fund\AdminControllers\Helper\FormHelper;
- use App\Module\Fund\AdminControllers\Helper\GridHelper;
- use App\Module\Fund\AdminControllers\Helper\ShowHelper;
- use App\Module\Fund\AdminControllers\Tools\SyncFundCurrencyJsonTool;
- use App\Module\Fund\Repositorys\FundCurrencyRepository;
- use App\Module\Game\DCache\FundCurrencyJsonConfig;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use UCore\DcatAdmin\AdminController;
- use Spatie\RouteAttributes\Attributes\Resource;
- use Spatie\RouteAttributes\Attributes\Get;
- /**
- * 货币配置控制器
- */
- #[Resource('fund-currencies', names: 'dcat.admin.fund-currencies')]
- class FundCurrencyController extends AdminController
- {
- /**
- * 页面标题
- *
- * @var string
- */
- protected $title = '货币配置';
- /**
- * 生成货币配置JSON数据
- */
- #[Get('fund-currencies/generate-json')]
- public function generateJson()
- {
- try {
- // 直接调用命令生成JSON
- FundCurrencyJsonConfig::getData([],true);
- return response()->json([
- 'status' => 'success',
- 'message' => 'JSON生成成功'
- ]);
- } catch (\Exception $e) {
- return response()->json([
- 'status' => 'error',
- 'message' => 'JSON生成失败: ' . $e->getMessage()
- ]);
- }
- }
- /**
- * 列表页
- *
- * @return Grid
- */
- protected function grid()
- {
- return Grid::make(new FundCurrencyRepository(), function (Grid $grid) {
- $helper = new GridHelper($grid, $this);
- // 检查配置表状态
- $status = \App\Module\Fund\AdminControllers\Tools\RefreshCheckTool::checkSyncStatus();
- if ($status['is_synced']) {
- admin_success('JSON配置表状态', $status['message']);
- } else {
- admin_warning('JSON配置表状态', $status['message']);
- }
- // 添加工具按钮
- $grid->tools([
- new SyncFundCurrencyJsonTool()
- ]);
- $helper->columnId();
- $grid->column('identification', '货币标识')->sortable();
- $grid->column('name', '货币名称')->sortable();
- $grid->column('icon', '图标')->image('', 40, 40);
- $grid->column('display_attributes', '显示属性')->display(function ($value) {
- if (empty($value)) {
- return '-';
- }
- if (is_string($value)) {
- $value = json_decode($value, true);
- }
- return json_encode($value, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
- });
- $grid->column('data1', '额外数据')->display(function ($value) {
- if (empty($value)) {
- return '-';
- }
- $data = json_decode($value, true);
- return json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
- });
- $grid->column('create_time', '创建时间')->display(function ($value) {
- return date('Y-m-d H:i:s', $value);
- })->sortable();
- $grid->column('update_time', '更新时间')->display(function ($value) {
- return date('Y-m-d H:i:s', $value);
- })->sortable();
- // 筛选器
- $grid->filter(function (Grid\Filter $filter) {
- $filter->equal('id', 'ID');
- $filter->like('identification', '货币标识');
- $filter->like('name', '货币名称');
- });
- });
- }
- /**
- * 详情页
- *
- * @param mixed $id
- * @return Show
- */
- protected function detail($id)
- {
- return Show::make($id, new FundCurrencyRepository(), function (Show $show) {
- $helper = new ShowHelper($show, $this);
- $show->field('id', 'ID');
- $show->field('identification', '货币标识');
- $show->field('name', '货币名称');
- $show->field('icon', '图标')->image();
- $show->field('display_attributes', '显示属性')->unescape()->as(function ($value) {
- if (empty($value)) {
- return '无';
- }
- if (is_string($value)) {
- $value = json_decode($value, true);
- }
- $html = '<table class="table table-bordered">';
- $html .= '<thead><tr><th>属性名</th><th>属性值</th></tr></thead>';
- $html .= '<tbody>';
- foreach ((array)$value as $key => $val) {
- $html .= '<tr>';
- $html .= '<td>' . htmlspecialchars($key) . '</td>';
- $html .= '<td>' . htmlspecialchars((string)$val) . '</td>';
- $html .= '</tr>';
- }
- $html .= '</tbody></table>';
- return $html;
- });
- $show->field('data1', '额外数据')->json();
- $show->field('create_time', '创建时间')->as(function ($value) {
- return date('Y-m-d H:i:s', $value);
- });
- $show->field('update_time', '更新时间')->as(function ($value) {
- return date('Y-m-d H:i:s', $value);
- });
- });
- }
- /**
- * 表单
- *
- * @return Form
- */
- protected function form()
- {
- return Form::make(new FundCurrencyRepository(), function (Form $form) {
- $helper = new FormHelper($form, $this);
- $form->display('id', 'ID');
- $form->text('identification', '货币标识')
- ->required()
- ->maxLength(10)
- ->help('货币的唯一标识符,最多10个字符');
- $form->text('name', '货币名称')
- ->required()
- ->maxLength(30)
- ->help('货币的显示名称,最多30个字符');
- $form->image('icon', '图标')
- ->required()
- ->autoUpload()
- ->uniqueName()
- ->help('货币的图标,建议尺寸64x64像素');
- $form->textarea('data1', '额外数据')
- ->help('额外的货币属性,JSON格式');
- // 显示属性表单
- $form->fieldset('显示属性', function (Form $form) {
- $form->text('display_attributes.icon', '图标路径');
- $form->color('display_attributes.color', '颜色');
- $form->text('display_attributes.description', '描述');
- $form->text('display_attributes.background', '背景图片');
- $form->text('display_attributes.animation', '动画效果');
- $form->text('display_attributes.badge', '特殊标记');
- });
- // 保存前处理
- $form->saving(function (Form $form) {
- // 设置时间戳
- if ($form->isCreating()) {
- $form->create_time = time();
- }
- $form->update_time = time();
- // 验证额外数据是否为有效的JSON
- if (!empty($form->data1)) {
- $data = json_decode($form->data1, true);
- if (json_last_error() !== JSON_ERROR_NONE) {
- return $form->response()->error('额外数据必须是有效的JSON格式');
- }
- $form->data1 = json_encode($data);
- }
- });
- // 保存后处理
- $form->saved(function (Form $form) {
- // 提示用户更新JSON配置
- admin_toastr('货币配置已保存,请点击"生成JSON"按钮更新配置文件', 'info');
- });
- });
- }
- }
|