| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- <?php
- namespace App\Module\GameItems\AdminControllers\Tools;
- use Dcat\Admin\Grid\Tools\AbstractTool;
- use Illuminate\Http\Request;
- class RefreshCheckTool extends AbstractTool
- {
- protected $shouldDisplay;
- protected $style = 'btn btn-warning waves-effect';
- public function __construct(bool $shouldDisplay = true)
- {
- $this->shouldDisplay = $shouldDisplay;
- }
- public function title()
- {
- return '刷新检查';
- }
- public function confirm()
- {
- return '确定要刷新检查同步状态吗?';
- }
- public function handle(Request $request)
- {
- return $this->response()->success('刷新成功')->refresh();
- }
- public function render()
- {
- if (!$this->shouldDisplay) {
- return '';
- }
- return parent::render();
- }
- public static function checkSyncStatus(): array
- {
- $jsonPath = public_path('json/items.json');
- $lastUpdated = \Carbon\Carbon::parse(\App\Module\GameItems\Models\Item::max('updated_at'));
- if (!file_exists($jsonPath)) {
- return [
- 'should_display' => true,
- 'message' => 'JSON文件不存在',
- 'is_synced' => false
- ];
- }
- $json = json_decode(file_get_contents($jsonPath), true);
- $generatedAt = \Carbon\Carbon::parse($json['generated_at']);
- $isSynced = $generatedAt->gte($lastUpdated);
- return [
- 'should_display' => !$isSynced,
- 'message' => $isSynced
- ? 'JSON数据已同步,生成于 '.$generatedAt->diffForHumans()
- : 'JSON数据已过期,最后更新于 '.$lastUpdated->diffForHumans(),
- 'is_synced' => $isSynced
- ];
- }
- public static function shouldDisplay(): bool
- {
- return self::checkSyncStatus()['should_display'];
- }
- }
|