SyncFarmHouseJsonTool.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. FarmHouseJsonConfig::getData([], true);
  28. return $this->response()->success('生成成功')->refresh();
  29. } catch (\Exception $e) {
  30. Log::error('Generate farm_house.json exception: '.$e->getMessage());
  31. return $this->response()->error('生成失败:'.$e->getMessage());
  32. }
  33. }
  34. public function render()
  35. {
  36. if (!$this->shouldDisplay) {
  37. return '';
  38. }
  39. return parent::render();
  40. }
  41. public static function shouldDisplay(): bool
  42. {
  43. $json = FarmHouseJsonConfig::getData();
  44. $generatedAt = \Carbon\Carbon::parse($json['generated_ts']);
  45. $lastUpdated = \Carbon\Carbon::parse(\App\Module\Farm\Models\FarmHouseConfig::max('updated_at'));
  46. return $generatedAt->lt($lastUpdated);
  47. }
  48. }