RefreshCheckTool.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace App\Module\GameItems\AdminControllers\Tools;
  3. use Dcat\Admin\Grid\Tools\AbstractTool;
  4. use Illuminate\Http\Request;
  5. class RefreshCheckTool extends AbstractTool
  6. {
  7. protected $shouldDisplay;
  8. protected $style = 'btn btn-warning 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 confirm()
  18. {
  19. return '确定要刷新检查同步状态吗?';
  20. }
  21. public function handle(Request $request)
  22. {
  23. return $this->response()->success('刷新成功')->refresh();
  24. }
  25. public function render()
  26. {
  27. if (!$this->shouldDisplay) {
  28. return '';
  29. }
  30. return parent::render();
  31. }
  32. public static function checkSyncStatus(): array
  33. {
  34. $jsonPath = public_path('json/items.json');
  35. $lastUpdated = \Carbon\Carbon::parse(\App\Module\GameItems\Models\Item::max('updated_at'));
  36. if (!file_exists($jsonPath)) {
  37. return [
  38. 'should_display' => true,
  39. 'message' => 'JSON文件不存在',
  40. 'is_synced' => false
  41. ];
  42. }
  43. $json = json_decode(file_get_contents($jsonPath), true);
  44. $generatedAt = \Carbon\Carbon::parse($json['generated_at']);
  45. $isSynced = $generatedAt->gte($lastUpdated);
  46. return [
  47. 'should_display' => !$isSynced,
  48. 'message' => $isSynced
  49. ? 'JSON数据已同步,生成于 '.$generatedAt->diffForHumans()
  50. : 'JSON数据已过期,最后更新于 '.$lastUpdated->diffForHumans(),
  51. 'is_synced' => $isSynced
  52. ];
  53. }
  54. public static function shouldDisplay(): bool
  55. {
  56. return self::checkSyncStatus()['should_display'];
  57. }
  58. }