setWorkingDirectory(base_path()); $process->run(); if (!$process->isSuccessful()) { return response()->json([ 'status' => 'error', 'message' => '刷新失败: ' . $process->getErrorOutput() ]); } // 强制刷新缓存 FundCurrencyJsonConfig::getData([], true); return response()->json([ 'status' => 'success', 'message' => '刷新成功' ]); } catch (\Exception $e) { return response()->json([ 'status' => 'error', 'message' => '刷新失败: ' . $e->getMessage() ]); } } /** * 刷新土地配置表 * * @return \Illuminate\Http\JsonResponse */ #[Get('game-jsonconfigs/refresh-farm-land')] public function refreshFarmLand() { try { // 调用命令生成JSON $process = new \Symfony\Component\Process\Process(['php', 'artisan', 'farm:generate-land-json']); $process->setWorkingDirectory(base_path()); $process->run(); if (!$process->isSuccessful()) { return response()->json([ 'status' => 'error', 'message' => '刷新失败: ' . $process->getErrorOutput() ]); } // 强制刷新缓存 FarmLandJsonConfig::getData([], true); return response()->json([ 'status' => 'success', 'message' => '刷新成功' ]); } catch (\Exception $e) { return response()->json([ 'status' => 'error', 'message' => '刷新失败: ' . $e->getMessage() ]); } } /** * 页面标题 * * @var string */ protected $title = '游戏配置表管理'; /** * 页面描述 * * @var string */ protected $description = '查看和刷新游戏中的各种配置表'; /** * 配置表首页 * * @param Content $content * @return Content */ public function index(Content $content) { return $content ->title($this->title) ->description($this->description) ->body(function (Row $row) { // 物品配置表卡片 $row->column(6, $this->createConfigCard( '物品配置表', 'items.json', 'gameitems:generate-json', SyncItemsJsonTool::make(), $this->getItemConfigInfo() )); // 宝箱配置表卡片 $row->column(6, $this->createConfigCard( '宝箱配置表', 'chest.json', 'gameitems:generate-chest-json', SyncChetsJsonTool::make(), $this->getChestConfigInfo() )); }) ->body(function (Row $row) { // 合成配方配置表卡片 $row->column(6, $this->createConfigCard( '物品合成配方配置表', 'recipe.json', 'gameitems:generate-recipe-json', SyncRecipeJsonTool::make(), $this->getRecipeConfigInfo() )); // 分解配方配置表卡片 $row->column(6, $this->createConfigCard( '物品分解配方配置表', 'dismantle.json', 'gameitems:generate-dismantle-json', SyncDismantleJsonTool::make(), $this->getDismantleConfigInfo() )); }) ->body(function (Row $row) { // 宠物配置表卡片 $row->column(6, $this->createConfigCard( '宠物配置表', 'pet_config.json, pet_level_config.json, pet_skill_config.json', 'pet:generate-json', 'game-jsonconfigs/refresh-pets', $this->getPetConfigInfo() )); // 农场房屋配置表卡片 $row->column(6, $this->createConfigCard( '农场房屋配置表', 'farm_house.json', 'farm:generate-house-json', 'game-jsonconfigs/refresh-farm-house', $this->getFarmHouseConfigInfo() )); }) ->body(function (Row $row) { // 土地配置表卡片 $row->column(6, $this->createConfigCard( '土地配置表', 'farm_land.json', 'farm:generate-land-json', 'game-jsonconfigs/refresh-farm-land', $this->getFarmLandConfigInfo() )); // 货币配置表卡片 $row->column(6, $this->createConfigCard( '货币配置表', 'currencies.json', 'fund:generate-currency-json', 'game-jsonconfigs/refresh-currencies', $this->getFundCurrencyConfigInfo() )); }); } /** * 创建配置表信息卡片 * * @param string $title 卡片标题 * @param string $filename 文件名 * @param string $command 生成命令 * @param string $refreshUrl 刷新URL * @param array $info 配置信息 * @return Card */ protected function createConfigCard($title, $filename, $command, $refresh, $info) { $headers = [ '属性', '值' ]; $rows = []; foreach ($info as $key => $value) { $rows[] = [ $key, $value ]; } $card = new Card($title, Table::make($headers, $rows)); $card->tool($refresh); // 处理文件名,获取第一个文件名(如果有多个文件,只取第一个) $firstFilename = explode(',', $filename)[0]; $firstFilename = trim($firstFilename); // 特殊处理各种配置表的映射关系 if (strpos($filename, 'pet_config.json') !== false) { $key = 'pets'; } elseif ($firstFilename === 'farm_house.json') { $key = 'farm_house'; } elseif ($firstFilename === 'farm_land.json') { $key = 'farm_land'; } elseif ($firstFilename === 'currencies.json') { $key = 'currencies'; } elseif ($firstFilename === 'chest.json') { $key = 'chest'; } elseif ($firstFilename === 'items.json') { $key = 'items'; } else { // 从文件名中提取key(去掉.json后缀) $key = str_replace('.json', '', $firstFilename); } // 构建查看JSON文件的链接 $jsonViewLink = "查看JSON内容"; $card->footer("文件: {$filename}
命令: php artisan {$command}
{$jsonViewLink}"); return $card; } /** * 获取物品配置表信息 * * @return array */ protected function getItemConfigInfo() { $data = ItemJsonConfig::getData(); $info = [ '生成时间' => Datetime::ts2string($data['generated_ts']), '物品数量' => isset($data['items']) ? count($data['items']) : 0, ]; return $info; } /** * 获取物品合成配方配置表信息 * * @return array */ protected function getRecipeConfigInfo() { $data = RecipeJsonConfig::getData(); $info = [ '生成时间' => isset($data['generated_ts']) ? Datetime::ts2string($data['generated_ts']) : '未生成', '配方数量' => isset($data['recipes']) ? count($data['recipes']) : 0, ]; return $info; } /** * 获取物品分解配方配置表信息 * * @return array */ protected function getDismantleConfigInfo() { $data = DismantleJsonConfig::getData(); $info = [ '生成时间' => isset($data['generated_ts']) ? Datetime::ts2string($data['generated_ts']) : '未生成', '规则数量' => isset($data['dismantle_rules']) ? count($data['dismantle_rules']) : 0, ]; return $info; } /** * 获取宝箱配置表信息 * * @return array */ protected function getChestConfigInfo() { $data = ChestJsonConfig::getData(); $info = [ '生成时间' => Datetime::ts2string($data['generated_ts']), '宝箱数量' => isset($data['chest']) ? count($data['chest']) : 0, ]; return $info; } /** * 获取宠物配置表信息 * * @return array */ protected function getPetConfigInfo() { $data = PetJsonConfig::getData(); $petConfig = $data['pet_config'] ?? []; $petLevelConfig = $data['pet_level_config'] ?? []; $petSkillConfig = $data['pet_skill_config'] ?? []; $info = [ '生成时间' => Datetime::ts2string($data['generated_ts']), '宠物数量' => isset($petConfig['pets']) ? count($petConfig['pets']) : 0, '等级配置数量' => isset($petLevelConfig['pet_levels']) ? count($petLevelConfig['pet_levels']) : 0, '技能配置数量' => isset($petSkillConfig['pet_skills']) ? count($petSkillConfig['pet_skills']) : 0, ]; return $info; } /** * 获取农场房屋配置表信息 * * @return array */ protected function getFarmHouseConfigInfo() { $data = FarmHouseJsonConfig::getData(); $info = [ '生成时间' => Datetime::ts2string($data['generated_ts']), '房屋配置数量' => isset($data['house_configs']) ? count($data['house_configs']) : 0, ]; return $info; } /** * 获取土地配置表信息 * * @return array */ protected function getFarmLandConfigInfo() { $data = FarmLandJsonConfig::getData(); $info = [ '生成时间' => Datetime::ts2string($data['generated_ts']), '土地类型数量' => isset($data['land_types']) ? count($data['land_types']) : 0, '升级路径数量' => isset($data['upgrade_paths']) ? count($data['upgrade_paths']) : 0, ]; return $info; } /** * 获取货币配置表信息 * * @return array */ protected function getFundCurrencyConfigInfo() { $data = FundCurrencyJsonConfig::getData(); // 计算有关联币种的账户种类数量 $linkedAccountsCount = 0; if (isset($data['fund_configs'])) { foreach ($data['fund_configs'] as $fundConfig) { if (!empty($fundConfig['currency_id'])) { $linkedAccountsCount++; } } } $info = [ '生成时间' => Datetime::ts2string($data['generated_ts']), '币种数量' => isset($data['currencies']) ? count($data['currencies']) : 0, '账户种类数量' => isset($data['fund_configs']) ? count($data['fund_configs']) : 0, '已关联币种的账户数量' => $linkedAccountsCount, ]; return $info; } /** * 友好地显示JSON配置数据 * * @param string $key 配置表键名 * @return Content */ #[Get('game-jsonconfigs/view-json/{key}')] public function viewJson($key, Content $content) { // 配置表映射关系 $map = [ 'items' => [ItemJsonConfig::class, '物品配置表'], 'chest' => [ChestJsonConfig::class, '宝箱配置表'], 'recipe' => [RecipeJsonConfig::class, '物品合成配方配置表'], 'dismantle' => [DismantleJsonConfig::class, '物品分解配方配置表'], 'pets' => [PetJsonConfig::class, '宠物配置表'], 'farm_house' => [FarmHouseJsonConfig::class, '农场房屋配置表'], 'farm_land' => [FarmLandJsonConfig::class, '土地配置表'], 'currencies' => [FundCurrencyJsonConfig::class, '货币配置表'], ]; // 检查请求的配置表是否存在 if (!isset($map[$key])) { return $content ->title('错误') ->description('配置表查看') ->body(new Card('错误', '配置表不存在')); } try { // 获取配置表数据 $configClass = $map[$key][0]; $title = $map[$key][1]; $data = $configClass::getData(); // 如果数据为空,返回错误 if (empty($data)) { return $content ->title('错误') ->description('配置表查看') ->body(new Card('错误', '配置表数据为空')); } // 创建JSON查看器 $jsonViewerId = 'json-viewer-' . uniqid(); // 格式化JSON数据 $formattedJson = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); if ($formattedJson === false) { $formattedJson = json_encode(["error" => "无法解析JSON数据"], JSON_PRETTY_PRINT); } // 转义HTML特殊字符 $escapedJson = htmlspecialchars($formattedJson, ENT_QUOTES, 'UTF-8'); $html = <<
{$escapedJson}
HTML; // 创建卡片 $card = new Card($title, $html); // 添加原始JSON链接 $card->tool('查看原始JSON'); return $content ->title('配置表查看') ->description($title) ->body($card); } catch (\Exception $e) { // 返回错误响应 return $content ->title('错误') ->description('配置表查看') ->body(new Card('错误', '获取配置表数据失败: ' . $e->getMessage())); } } /** * 创建JSON查看器 * * @param mixed $data 要显示的数据(数组或对象) * @return string */ protected function createJsonViewer($data) { // 生成唯一ID,避免多个查看器冲突 $viewerId = 'json-viewer-' . uniqid(); // 确保数据是格式化的JSON字符串 $jsonString = is_string($data) ? $data : json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); // 转义HTML特殊字符 $escapedJson = htmlspecialchars($jsonString, ENT_QUOTES, 'UTF-8'); // 使用简单的方式显示JSON数据 $html = <<
{$escapedJson}
HTML; return $html; } }