| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- <?php
- namespace App\Module\GameItems\AdminControllers\Tools;
- use App\Module\Game\DCache\ItemJsonConfig;
- use Carbon\CarbonInterface;
- 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();
- $lastUpdated = \Carbon\Carbon::parse(\App\Module\GameItems\Models\Item::max('updated_at'));
- // dd($json);
- $generatedAt = \Carbon\Carbon::createFromTimestamp($json['generated_ts']);
- $isSynced = $generatedAt->gte($lastUpdated);
- // 使用绝对时间差,避免相对时间导致的"X小时后"这样的显示问题
- $options = ['syntax' => \Carbon\CarbonInterface::DIFF_ABSOLUTE];
- return [
- 'should_display' => !$isSynced,
- 'message' => $isSynced
- ? '【物品配置表】已同步,生成于 '.$generatedAt->diffForHumans(null, $options)
- : '【物品配置表】需要更新,数据库最后更新于 '.$lastUpdated->diffForHumans(null, $options),
- 'is_synced' => $isSynced
- ];
- }
- public static function shouldDisplay(): bool
- {
- return self::checkSyncStatus()['should_display'];
- }
- }
|