RefreshCheckTool.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace App\Module\Farm\AdminControllers\Tools;
  3. use Dcat\Admin\Grid\Tools\AbstractTool;
  4. use Illuminate\Support\Facades\Log;
  5. class RefreshCheckTool extends AbstractTool
  6. {
  7. protected $shouldDisplay;
  8. protected $style = 'btn btn-default waves-effect';
  9. public function __construct(bool $shouldDisplay = true)
  10. {
  11. $this->shouldDisplay = $shouldDisplay;
  12. }
  13. public function title()
  14. {
  15. return '检查状态';
  16. }
  17. public function render()
  18. {
  19. if (!$this->shouldDisplay) {
  20. return '';
  21. }
  22. return parent::render();
  23. }
  24. public static function checkSyncStatus(): array
  25. {
  26. $jsonPath = public_path('json/farm_house.json');
  27. if (!file_exists($jsonPath)) {
  28. return [
  29. 'is_synced' => false,
  30. 'message' => '配置文件不存在,请立即生成',
  31. 'should_display' => true
  32. ];
  33. }
  34. try {
  35. $json = json_decode(file_get_contents($jsonPath), true);
  36. $generatedAt = \Carbon\Carbon::parse($json['generated_at']);
  37. $lastUpdated = \Carbon\Carbon::parse(\App\Module\Farm\Models\FarmHouseConfig::max('updated_at'));
  38. if ($generatedAt->lt($lastUpdated)) {
  39. return [
  40. 'is_synced' => false,
  41. 'message' => "配置文件需要更新(生成于 {$generatedAt->format('Y-m-d H:i:s')},最后修改于 {$lastUpdated->format('Y-m-d H:i:s')})",
  42. 'should_display' => true
  43. ];
  44. } else {
  45. return [
  46. 'is_synced' => true,
  47. 'message' => "配置文件已是最新(生成于 {$generatedAt->format('Y-m-d H:i:s')})",
  48. 'should_display' => false
  49. ];
  50. }
  51. } catch (\Exception $e) {
  52. Log::error('Check farm house config sync status failed: '.$e->getMessage());
  53. return [
  54. 'is_synced' => false,
  55. 'message' => '检查配置文件状态失败:'.$e->getMessage(),
  56. 'should_display' => true
  57. ];
  58. }
  59. }
  60. }