FundCurrencyController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. namespace App\Module\Fund\AdminControllers;
  3. use App\Module\Fund\AdminControllers\Helper\FormHelper;
  4. use App\Module\Fund\AdminControllers\Helper\GridHelper;
  5. use App\Module\Fund\AdminControllers\Helper\ShowHelper;
  6. use App\Module\Fund\AdminControllers\Tools\SyncFundCurrencyJsonTool;
  7. use App\Module\Fund\Repositorys\FundCurrencyRepository;
  8. use App\Module\Game\DCache\FundCurrencyJsonConfig;
  9. use Dcat\Admin\Form;
  10. use Dcat\Admin\Grid;
  11. use Dcat\Admin\Show;
  12. use UCore\DcatAdmin\AdminController;
  13. use Spatie\RouteAttributes\Attributes\Resource;
  14. use Spatie\RouteAttributes\Attributes\Get;
  15. /**
  16. * 货币配置控制器
  17. */
  18. #[Resource('fund-currencies', names: 'dcat.admin.fund-currencies')]
  19. class FundCurrencyController extends AdminController
  20. {
  21. /**
  22. * 页面标题
  23. *
  24. * @var string
  25. */
  26. protected $title = '货币配置';
  27. /**
  28. * 生成货币配置JSON数据
  29. */
  30. #[Get('fund-currencies/generate-json')]
  31. public function generateJson()
  32. {
  33. try {
  34. // 直接调用命令生成JSON
  35. FundCurrencyJsonConfig::getData([],true);
  36. return response()->json([
  37. 'status' => 'success',
  38. 'message' => 'JSON生成成功'
  39. ]);
  40. } catch (\Exception $e) {
  41. return response()->json([
  42. 'status' => 'error',
  43. 'message' => 'JSON生成失败: ' . $e->getMessage()
  44. ]);
  45. }
  46. }
  47. /**
  48. * 列表页
  49. *
  50. * @return Grid
  51. */
  52. protected function grid()
  53. {
  54. return Grid::make(new FundCurrencyRepository(), function (Grid $grid) {
  55. $helper = new GridHelper($grid, $this);
  56. // 检查配置表状态
  57. $status = \App\Module\Fund\AdminControllers\Tools\RefreshCheckTool::checkSyncStatus();
  58. if ($status['is_synced']) {
  59. admin_success('JSON配置表状态', $status['message']);
  60. } else {
  61. admin_warning('JSON配置表状态', $status['message']);
  62. }
  63. // 添加工具按钮
  64. $grid->tools([
  65. new SyncFundCurrencyJsonTool()
  66. ]);
  67. $helper->columnId();
  68. $grid->column('identification', '货币标识')->sortable();
  69. $grid->column('name', '货币名称')->sortable();
  70. $grid->column('icon', '图标')->image('', 40, 40);
  71. $grid->column('display_attributes', '显示属性')->display(function ($value) {
  72. if (empty($value)) {
  73. return '-';
  74. }
  75. if (is_string($value)) {
  76. $value = json_decode($value, true);
  77. }
  78. return json_encode($value, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
  79. });
  80. $grid->column('data1', '额外数据')->display(function ($value) {
  81. if (empty($value)) {
  82. return '-';
  83. }
  84. $data = json_decode($value, true);
  85. return json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
  86. });
  87. $grid->column('create_time', '创建时间')->display(function ($value) {
  88. return date('Y-m-d H:i:s', $value);
  89. })->sortable();
  90. $grid->column('update_time', '更新时间')->display(function ($value) {
  91. return date('Y-m-d H:i:s', $value);
  92. })->sortable();
  93. // 筛选器
  94. $grid->filter(function (Grid\Filter $filter) {
  95. $filter->equal('id', 'ID');
  96. $filter->like('identification', '货币标识');
  97. $filter->like('name', '货币名称');
  98. });
  99. });
  100. }
  101. /**
  102. * 详情页
  103. *
  104. * @param mixed $id
  105. * @return Show
  106. */
  107. protected function detail($id)
  108. {
  109. return Show::make($id, new FundCurrencyRepository(), function (Show $show) {
  110. $helper = new ShowHelper($show, $this);
  111. $show->field('id', 'ID');
  112. $show->field('identification', '货币标识');
  113. $show->field('name', '货币名称');
  114. $show->field('icon', '图标')->image();
  115. $show->field('display_attributes', '显示属性')->unescape()->as(function ($value) {
  116. if (empty($value)) {
  117. return '无';
  118. }
  119. if (is_string($value)) {
  120. $value = json_decode($value, true);
  121. }
  122. $html = '<table class="table table-bordered">';
  123. $html .= '<thead><tr><th>属性名</th><th>属性值</th></tr></thead>';
  124. $html .= '<tbody>';
  125. foreach ((array)$value as $key => $val) {
  126. $html .= '<tr>';
  127. $html .= '<td>' . htmlspecialchars($key) . '</td>';
  128. $html .= '<td>' . htmlspecialchars((string)$val) . '</td>';
  129. $html .= '</tr>';
  130. }
  131. $html .= '</tbody></table>';
  132. return $html;
  133. });
  134. $show->field('data1', '额外数据')->json();
  135. $show->field('create_time', '创建时间')->as(function ($value) {
  136. return date('Y-m-d H:i:s', $value);
  137. });
  138. $show->field('update_time', '更新时间')->as(function ($value) {
  139. return date('Y-m-d H:i:s', $value);
  140. });
  141. });
  142. }
  143. /**
  144. * 表单
  145. *
  146. * @return Form
  147. */
  148. protected function form()
  149. {
  150. return Form::make(new FundCurrencyRepository(), function (Form $form) {
  151. $helper = new FormHelper($form, $this);
  152. $form->display('id', 'ID');
  153. $form->text('identification', '货币标识')
  154. ->required()
  155. ->maxLength(10)
  156. ->help('货币的唯一标识符,最多10个字符');
  157. $form->text('name', '货币名称')
  158. ->required()
  159. ->maxLength(30)
  160. ->help('货币的显示名称,最多30个字符');
  161. $form->image('icon', '图标')
  162. ->required()
  163. ->autoUpload()
  164. ->uniqueName()
  165. ->help('货币的图标,建议尺寸64x64像素');
  166. $form->textarea('data1', '额外数据')
  167. ->help('额外的货币属性,JSON格式');
  168. // 显示属性表单
  169. $form->fieldset('显示属性', function (Form $form) {
  170. $form->text('display_attributes.icon', '图标路径');
  171. $form->color('display_attributes.color', '颜色');
  172. $form->text('display_attributes.description', '描述');
  173. $form->text('display_attributes.background', '背景图片');
  174. $form->text('display_attributes.animation', '动画效果');
  175. $form->text('display_attributes.badge', '特殊标记');
  176. });
  177. // 保存前处理
  178. $form->saving(function (Form $form) {
  179. // 设置时间戳
  180. if ($form->isCreating()) {
  181. $form->create_time = time();
  182. }
  183. $form->update_time = time();
  184. // 验证额外数据是否为有效的JSON
  185. if (!empty($form->data1)) {
  186. $data = json_decode($form->data1, true);
  187. if (json_last_error() !== JSON_ERROR_NONE) {
  188. return $form->response()->error('额外数据必须是有效的JSON格式');
  189. }
  190. $form->data1 = json_encode($data);
  191. }
  192. });
  193. // 保存后处理
  194. $form->saved(function (Form $form) {
  195. // 提示用户更新JSON配置
  196. admin_toastr('货币配置已保存,请点击"生成JSON"按钮更新配置文件', 'info');
  197. });
  198. });
  199. }
  200. }