shouldDisplay = $shouldDisplay; } /** * 按钮标题 * * @return string */ public function title() { return '生成JSON'; } /** * 确认提示 * * @return string */ public function confirm() { return '确定要生成物品合成配方配置JSON数据吗?'; } /** * 处理请求 * * @param Request $request * @return mixed */ public function handle(Request $request) { try { // 直接调用缓存类刷新数据,这会同时生成JSON文件 RecipeJsonConfig::getData([], true); return $this->response()->success('生成成功')->refresh(); } catch (\Exception $e) { Log::error('Generate recipe.json exception: '.$e->getMessage()); return $this->response()->error('生成失败:'.$e->getMessage()); } } /** * 渲染按钮 * * @return string */ public function render() { if (!$this->shouldDisplay) { return ''; } return parent::render(); } /** * 判断是否应该显示按钮 * * @return bool */ public static function shouldDisplay(): bool { // 获取缓存数据 $json = RecipeJsonConfig::getData(); // 如果没有生成时间戳,说明需要生成 if (!isset($json['generated_ts'])) { return true; } // 获取生成时间和最后更新时间 $generatedAt = \Carbon\Carbon::createFromTimestamp($json['generated_ts']); // 获取合成配方的最后更新时间 $lastUpdated = \Carbon\Carbon::parse(ItemRecipe::max('updated_at') ?: '2000-01-01'); // 如果生成时间早于最后更新时间,说明需要重新生成 return $generatedAt->lt($lastUpdated); } }