| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- <?php
- namespace App\Module\GameItems\AdminControllers\Tools;
- use App\Module\Game\DCache\ItemJsonConfig;
- 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
- {
- $json = ItemJsonConfig::getData([],false);
- $lastUpdated = \Carbon\Carbon::parse(\App\Module\GameItems\Models\Item::max('updated_at'));
- $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'];
- }
- }
|