| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- <?php
- namespace App\Module\GameItems\AdminControllers\Tools;
- use Dcat\Admin\Grid\Tools\AbstractTool;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Log;
- class SyncItemsJsonTool extends AbstractTool
- {
- protected $shouldDisplay;
- protected $style = 'btn btn-danger waves-effect';
- public function __construct(bool $shouldDisplay = true)
- {
- $this->shouldDisplay = $shouldDisplay;
- }
- public function title()
- {
- return '立即同步';
- }
- public function confirm()
- {
- return '确定要立即同步JSON数据吗?';
- }
- public function handle(Request $request)
- {
- try {
- $success = \App\Module\GameItems\Commands\GenerateItemsJsonCommand::generateJson();
- if (!$success) {
- Log::error('Sync items.json failed - see previous logs for details');
- return $this->response()->error('同步失败:请检查日志获取详细信息');
- }
- return $this->response()->success('同步成功')->refresh();
- } catch (\Exception $e) {
- Log::error('Sync items.json exception: '.$e->getMessage());
- return $this->response()->error('同步失败:'.$e->getMessage());
- }
- }
- public function render()
- {
- if (!$this->shouldDisplay) {
- return '';
- }
- return parent::render();
- }
- public static function shouldDisplay(): bool
- {
- $jsonPath = public_path('json/items.json');
- if (!file_exists($jsonPath)) {
- return true;
- }
- $json = json_decode(file_get_contents($jsonPath), true);
- $generatedAt = \Carbon\Carbon::parse($json['generated_at']);
- $lastUpdated = \Carbon\Carbon::parse(\App\Module\GameItems\Models\Item::max('updated_at'));
- return $generatedAt->lt($lastUpdated);
- }
- }
|