RefreshCheckTool.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace App\Module\GameItems\AdminControllers\Tools;
  3. use App\Module\Game\DCache\ItemJsonConfig;
  4. use Carbon\CarbonInterface;
  5. use Dcat\Admin\Grid\Tools\AbstractTool;
  6. use Illuminate\Http\Request;
  7. class RefreshCheckTool extends AbstractTool
  8. {
  9. protected $shouldDisplay;
  10. protected $style = 'btn btn-warning waves-effect';
  11. public function __construct(bool $shouldDisplay = true)
  12. {
  13. $this->shouldDisplay = $shouldDisplay;
  14. }
  15. public function title()
  16. {
  17. return '刷新检查';
  18. }
  19. public function confirm()
  20. {
  21. return '确定要刷新检查同步状态吗?';
  22. }
  23. public function handle(Request $request)
  24. {
  25. return $this->response()->success('刷新成功')->refresh();
  26. }
  27. public function render()
  28. {
  29. if (!$this->shouldDisplay) {
  30. return '';
  31. }
  32. return parent::render();
  33. }
  34. public static function checkSyncStatus(): array
  35. {
  36. $json = ItemJsonConfig::getData();
  37. $lastUpdated = \Carbon\Carbon::parse(\App\Module\GameItems\Models\Item::max('updated_at'));
  38. // dd($json);
  39. $generatedAt = \Carbon\Carbon::createFromTimestamp($json['generated_ts']);
  40. $isSynced = $generatedAt->gte($lastUpdated);
  41. // 使用绝对时间差,避免相对时间导致的"X小时后"这样的显示问题
  42. $options = ['syntax' => \Carbon\CarbonInterface::DIFF_ABSOLUTE];
  43. return [
  44. 'should_display' => !$isSynced,
  45. 'message' => $isSynced
  46. ? '【物品配置表】已同步,生成于 '.$generatedAt->diffForHumans(null, $options)
  47. : '【物品配置表】需要更新,数据库最后更新于 '.$lastUpdated->diffForHumans(null, $options),
  48. 'is_synced' => $isSynced
  49. ];
  50. }
  51. public static function shouldDisplay(): bool
  52. {
  53. return self::checkSyncStatus()['should_display'];
  54. }
  55. }