SyncItemsJsonTool.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace App\Module\GameItems\AdminControllers\Tools;
  3. use App\Module\Game\DCache\ItemJsonConfig;
  4. use Dcat\Admin\Grid\Tools\AbstractTool;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\Log;
  7. class SyncItemsJsonTool extends AbstractTool
  8. {
  9. protected $shouldDisplay;
  10. protected $style = 'btn btn-danger 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 '确定要立即同步JSON数据吗?';
  22. }
  23. public function handle(Request $request)
  24. {
  25. try {
  26. $json =ItemJsonConfig::getData([],true);
  27. return $this->response()->success('同步成功')->refresh();
  28. } catch (\Exception $e) {
  29. Log::error('Sync items.json exception: '.$e->getMessage());
  30. return $this->response()->error('同步失败:'.$e->getMessage());
  31. }
  32. }
  33. public function render()
  34. {
  35. if (!$this->shouldDisplay) {
  36. return '';
  37. }
  38. return parent::render();
  39. }
  40. public static function shouldDisplay(): bool
  41. {
  42. $json =ItemJsonConfig::getData();
  43. $generatedAt = \Carbon\Carbon::createFromTimestamp($json['generated_ts']);
  44. $lastUpdated = \Carbon\Carbon::parse(\App\Module\GameItems\Models\Item::max('updated_at'));
  45. return $generatedAt->lt($lastUpdated);
  46. }
  47. }