FundCurrencyController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 Dcat\Admin\Form;
  9. use Dcat\Admin\Grid;
  10. use Dcat\Admin\Show;
  11. use UCore\DcatAdmin\AdminController;
  12. use Spatie\RouteAttributes\Attributes\Resource;
  13. use Spatie\RouteAttributes\Attributes\Get;
  14. /**
  15. * 货币配置控制器
  16. */
  17. #[Resource('fund-currencies', names: 'dcat.admin.fund-currencies')]
  18. class FundCurrencyController extends AdminController
  19. {
  20. /**
  21. * 页面标题
  22. *
  23. * @var string
  24. */
  25. protected $title = '货币配置';
  26. /**
  27. * 生成货币配置JSON数据
  28. */
  29. #[Get('fund-currencies/generate-json')]
  30. public function generateJson()
  31. {
  32. try {
  33. // 直接调用命令生成JSON
  34. $process = new \Symfony\Component\Process\Process(['php', 'artisan', 'fund:generate-currency-json']);
  35. $process->setWorkingDirectory(base_path());
  36. $process->run();
  37. if (!$process->isSuccessful()) {
  38. return response()->json([
  39. 'status' => 'error',
  40. 'message' => 'JSON生成失败: ' . $process->getErrorOutput()
  41. ]);
  42. }
  43. return response()->json([
  44. 'status' => 'success',
  45. 'message' => 'JSON生成成功'
  46. ]);
  47. } catch (\Exception $e) {
  48. return response()->json([
  49. 'status' => 'error',
  50. 'message' => 'JSON生成失败: ' . $e->getMessage()
  51. ]);
  52. }
  53. }
  54. /**
  55. * 列表页
  56. *
  57. * @return Grid
  58. */
  59. protected function grid()
  60. {
  61. return Grid::make(new FundCurrencyRepository(), function (Grid $grid) {
  62. $helper = new GridHelper($grid, $this);
  63. // 检查配置表状态
  64. $status = SyncFundCurrencyJsonTool::shouldDisplay();
  65. if ($status) {
  66. admin_warning('JSON配置表状态', '货币配置表需要更新');
  67. } else {
  68. admin_success('JSON配置表状态', '货币配置表已是最新');
  69. }
  70. // 添加工具按钮
  71. $grid->tools([
  72. new SyncFundCurrencyJsonTool()
  73. ]);
  74. $helper->columnId();
  75. $grid->column('identification', '货币标识')->sortable();
  76. $grid->column('name', '货币名称')->sortable();
  77. $grid->column('icon', '图标')->image('', 40, 40);
  78. $grid->column('data1', '额外数据')->display(function ($value) {
  79. if (empty($value)) {
  80. return '-';
  81. }
  82. $data = json_decode($value, true);
  83. return json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
  84. });
  85. $grid->column('create_time', '创建时间')->display(function ($value) {
  86. return date('Y-m-d H:i:s', $value);
  87. })->sortable();
  88. $grid->column('update_time', '更新时间')->display(function ($value) {
  89. return date('Y-m-d H:i:s', $value);
  90. })->sortable();
  91. // 筛选器
  92. $grid->filter(function (Grid\Filter $filter) {
  93. $filter->equal('id', 'ID');
  94. $filter->like('identification', '货币标识');
  95. $filter->like('name', '货币名称');
  96. });
  97. });
  98. }
  99. /**
  100. * 详情页
  101. *
  102. * @param mixed $id
  103. * @return Show
  104. */
  105. protected function detail($id)
  106. {
  107. return Show::make($id, new FundCurrencyRepository(), function (Show $show) {
  108. $helper = new ShowHelper($show, $this);
  109. $show->field('id', 'ID');
  110. $show->field('identification', '货币标识');
  111. $show->field('name', '货币名称');
  112. $show->field('icon', '图标')->image();
  113. $show->field('data1', '额外数据')->json();
  114. $show->field('create_time', '创建时间')->as(function ($value) {
  115. return date('Y-m-d H:i:s', $value);
  116. });
  117. $show->field('update_time', '更新时间')->as(function ($value) {
  118. return date('Y-m-d H:i:s', $value);
  119. });
  120. });
  121. }
  122. /**
  123. * 表单
  124. *
  125. * @return Form
  126. */
  127. protected function form()
  128. {
  129. return Form::make(new FundCurrencyRepository(), function (Form $form) {
  130. $helper = new FormHelper($form, $this);
  131. $form->display('id', 'ID');
  132. $form->text('identification', '货币标识')
  133. ->required()
  134. ->maxLength(10)
  135. ->help('货币的唯一标识符,最多10个字符');
  136. $form->text('name', '货币名称')
  137. ->required()
  138. ->maxLength(30)
  139. ->help('货币的显示名称,最多30个字符');
  140. $form->image('icon', '图标')
  141. ->required()
  142. ->autoUpload()
  143. ->uniqueName()
  144. ->help('货币的图标,建议尺寸64x64像素');
  145. $form->textarea('data1', '额外数据')
  146. ->help('额外的货币属性,JSON格式');
  147. // 保存前处理
  148. $form->saving(function (Form $form) {
  149. // 设置时间戳
  150. if ($form->isCreating()) {
  151. $form->create_time = time();
  152. }
  153. $form->update_time = time();
  154. // 验证额外数据是否为有效的JSON
  155. if (!empty($form->data1)) {
  156. $data = json_decode($form->data1, true);
  157. if (json_last_error() !== JSON_ERROR_NONE) {
  158. return $form->response()->error('额外数据必须是有效的JSON格式');
  159. }
  160. $form->data1 = json_encode($data);
  161. }
  162. });
  163. // 保存后处理
  164. $form->saved(function (Form $form) {
  165. // 提示用户更新JSON配置
  166. admin_toastr('货币配置已保存,请点击"生成JSON"按钮更新配置文件', 'info');
  167. });
  168. });
  169. }
  170. }