SyncItemsJsonTool.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. $success = \App\Module\GameItems\Commands\GenerateItemsJsonCommand::generateJson();
  27. if (!$success) {
  28. Log::error('Sync items.json failed - see previous logs for details');
  29. return $this->response()->error('同步失败:请检查日志获取详细信息');
  30. }
  31. return $this->response()->success('同步成功')->refresh();
  32. } catch (\Exception $e) {
  33. Log::error('Sync items.json exception: '.$e->getMessage());
  34. return $this->response()->error('同步失败:'.$e->getMessage());
  35. }
  36. }
  37. public function render()
  38. {
  39. if (!$this->shouldDisplay) {
  40. return '';
  41. }
  42. return parent::render();
  43. }
  44. public static function shouldDisplay(): bool
  45. {
  46. $json =ItemJsonConfig::getData();
  47. $generatedAt = \Carbon\Carbon::createFromTimestamp($json['generated_ts']);
  48. $lastUpdated = \Carbon\Carbon::parse(\App\Module\GameItems\Models\Item::max('updated_at'));
  49. return $generatedAt->lt($lastUpdated);
  50. }
  51. }