RefreshFundCurrencyJsonTool.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace App\Module\Fund\AdminControllers\Tools;
  3. use App\Module\Game\DCache\FundCurrencyJsonConfig;
  4. use Dcat\Admin\Grid\Tools\AbstractTool;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\Log;
  7. /**
  8. * 货币配置表刷新工具
  9. *
  10. * 用于在后台管理界面中刷新货币配置表数据
  11. */
  12. class RefreshFundCurrencyJsonTool extends AbstractTool
  13. {
  14. /**
  15. * 是否显示按钮
  16. *
  17. * @var bool
  18. */
  19. protected $shouldDisplay;
  20. /**
  21. * 按钮样式
  22. *
  23. * @var string
  24. */
  25. protected $style = 'btn btn-danger waves-effect';
  26. /**
  27. * 构造函数
  28. *
  29. * @param bool $shouldDisplay 是否显示按钮
  30. */
  31. public function __construct(bool $shouldDisplay = true)
  32. {
  33. $this->shouldDisplay = $shouldDisplay;
  34. }
  35. /**
  36. * 按钮标题
  37. *
  38. * @return string
  39. */
  40. public function title()
  41. {
  42. return '立即刷新';
  43. }
  44. /**
  45. * 确认提示
  46. *
  47. * @return string
  48. */
  49. public function confirm()
  50. {
  51. return '确定要立即刷新货币配置JSON数据吗?';
  52. }
  53. /**
  54. * 处理请求
  55. *
  56. * @param Request $request
  57. * @return mixed
  58. */
  59. public function handle(Request $request)
  60. {
  61. try {
  62. // 调用命令生成JSON
  63. $process = new \Symfony\Component\Process\Process(['php', 'artisan', 'fund:generate-currency-json']);
  64. $process->setWorkingDirectory(base_path());
  65. $process->run();
  66. if (!$process->isSuccessful()) {
  67. return $this->response()->error('刷新失败: ' . $process->getErrorOutput());
  68. }
  69. // 强制刷新缓存
  70. FundCurrencyJsonConfig::getData([], true);
  71. return $this->response()->success('刷新成功')->refresh();
  72. } catch (\Exception $e) {
  73. Log::error('Refresh fund currency JSON exception: '.$e->getMessage());
  74. return $this->response()->error('刷新失败:'.$e->getMessage());
  75. }
  76. }
  77. /**
  78. * 渲染按钮
  79. *
  80. * @return string
  81. */
  82. public function render()
  83. {
  84. if (!$this->shouldDisplay) {
  85. return '';
  86. }
  87. return parent::render();
  88. }
  89. /**
  90. * 判断是否应该显示按钮
  91. *
  92. * @return bool
  93. */
  94. public static function shouldDisplay(): bool
  95. {
  96. return true;
  97. }
  98. }