SyncFarmHouseJsonTool.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace App\Module\Farm\AdminControllers\Tools;
  3. use App\Module\Game\DCache\FarmHouseJsonConfig;
  4. use Dcat\Admin\Grid\Tools\AbstractTool;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\Log;
  7. class SyncFarmHouseJsonTool extends AbstractTool
  8. {
  9. protected $shouldDisplay;
  10. protected $style = 'btn btn-primary waves-effect';
  11. public function __construct(bool $shouldDisplay = true)
  12. {
  13. $this->shouldDisplay = $shouldDisplay;
  14. }
  15. public function title()
  16. {
  17. return '生成JSON';
  18. }
  19. public function confirm()
  20. {
  21. return '确定要生成房屋配置JSON数据吗?';
  22. }
  23. public function handle(Request $request)
  24. {
  25. try {
  26. // 直接调用命令生成JSON
  27. $process = new \Symfony\Component\Process\Process(['php', 'artisan', 'farm:generate-house-json']);
  28. $process->setWorkingDirectory(base_path());
  29. $process->run();
  30. if (!$process->isSuccessful()) {
  31. Log::error('Generate farm_house.json failed: ' . $process->getErrorOutput());
  32. return $this->response()->error('生成失败:' . $process->getErrorOutput());
  33. }
  34. return $this->response()->success('生成成功')->refresh();
  35. } catch (\Exception $e) {
  36. Log::error('Generate farm_house.json exception: '.$e->getMessage());
  37. return $this->response()->error('生成失败:'.$e->getMessage());
  38. }
  39. }
  40. public function render()
  41. {
  42. if (!$this->shouldDisplay) {
  43. return '';
  44. }
  45. return parent::render();
  46. }
  47. public static function shouldDisplay(): bool
  48. {
  49. $json = FarmHouseJsonConfig::getData();
  50. $generatedAt = \Carbon\Carbon::parse($json['generated_ts']);
  51. $lastUpdated = \Carbon\Carbon::parse(\App\Module\Farm\Models\FarmHouseConfig::max('updated_at'));
  52. return $generatedAt->lt($lastUpdated);
  53. }
  54. }