| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- <?php
- namespace App\Module\AppGame\HttpControllers;
- use App\Http\Controllers\Controller;
- use App\Module\Game\DCache\ChestJsonConfig;
- use App\Module\Game\DCache\DismantleJsonConfig;
- use App\Module\Game\DCache\FarmHouseJsonConfig;
- use App\Module\Game\DCache\FarmLandJsonConfig;
- use App\Module\Game\DCache\FundCurrencyJsonConfig;
- use App\Module\Game\DCache\ItemJsonConfig;
- use App\Module\Game\DCache\PetJsonConfig;
- use App\Module\Game\DCache\RecipeJsonConfig;
- use Illuminate\Support\Facades\Log;
- use UCore\Exception\HandleNotException;
- use Uraus\Kku\Common\RESPONSE_CODE;
- /**
- *
- * 访问json配置表,输出配置表Json格式
- * @package App\Module\AppGame\HttpControllers
- *
- *
- */
- class JsonController extends Controller
- {
- /**
- * 配置表读取
- * @param string $key 配置表键名
- * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Foundation\Application|\Illuminate\Http\JsonResponse|\Illuminate\Http\Response|object
- * @throws HandleNotException
- */
- public function config($key)
- {
- // 配置表映射关系
- $map = [
- 'items' => ItemJsonConfig::class,
- 'chest' => ChestJsonConfig::class,
- 'pets' => PetJsonConfig::class,
- 'farm_house' => FarmHouseJsonConfig::class,
- 'farm_land' => FarmLandJsonConfig::class,
- 'currencies' => FundCurrencyJsonConfig::class,
- 'recipe' => RecipeJsonConfig::class,
- 'dismantle' => DismantleJsonConfig::class,
- ];
- // 检查请求的配置表是否存在
- if (!isset($map[$key])) {
- return response()->json([
- 'code' => RESPONSE_CODE::REQUEST_ERROR,
- 'message' => '配置表不存在',
- 'data' => null
- ], 400);
- }
- try {
- // 获取配置表数据
- $configClass = $map[$key];
- $data = $configClass::getData();
- // 如果数据为空,返回错误
- if (empty($data)) {
- return response()->json([
- 'code' => RESPONSE_CODE::SERVER_ERROR,
- 'message' => '配置表数据为空',
- 'data' => null
- ], 500);
- }
- // 返回配置表数据
- return $data;
- } catch (\Exception $e) {
- // 记录错误日志
- Log::error('获取配置表数据失败', [
- 'key' => $key,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- // 返回错误响应
- return response()->json([
- 'code' => RESPONSE_CODE::SERVER_ERROR,
- 'message' => '获取配置表数据失败',
- 'data' => null
- ], 500);
- }
- }
- }
|