JsonController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace App\Module\AppGame\HttpControllers;
  3. use App\Http\Controllers\Controller;
  4. use App\Module\Game\DCache\ChestJsonConfig;
  5. use App\Module\Game\DCache\DismantleJsonConfig;
  6. use App\Module\Game\DCache\FarmHouseJsonConfig;
  7. use App\Module\Game\DCache\FarmLandJsonConfig;
  8. use App\Module\Game\DCache\FarmSeedJsonConfig;
  9. use App\Module\Game\DCache\FarmShrineJsonConfig;
  10. use App\Module\Game\DCache\FundCurrencyJsonConfig;
  11. use App\Module\Game\DCache\ItemJsonConfig;
  12. use App\Module\Game\DCache\PetConfigJsonConfig;
  13. use App\Module\Game\DCache\PetLevelJsonConfig;
  14. use App\Module\Game\DCache\PetSkillJsonConfig;
  15. use App\Module\Game\DCache\RecipeJsonConfig;
  16. use App\Module\Game\DCache\ShopItemsJsonConfig;
  17. use Illuminate\Support\Facades\Log;
  18. use UCore\Exception\HandleNotException;
  19. use Uraus\Kku\Common\RESPONSE_CODE;
  20. /**
  21. *
  22. * 访问json配置表,输出配置表Json格式
  23. * @package App\Module\AppGame\HttpControllers
  24. *
  25. *
  26. */
  27. class JsonController extends Controller
  28. {
  29. /**
  30. * 配置表读取
  31. * @param string $key 配置表键名
  32. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Foundation\Application|\Illuminate\Http\JsonResponse|\Illuminate\Http\Response|object
  33. * @throws HandleNotException
  34. */
  35. public function config($key)
  36. {
  37. // 配置表映射关系
  38. $map = [
  39. 'items' => ItemJsonConfig::class,
  40. 'chest' => ChestJsonConfig::class,
  41. 'pet_config' => PetConfigJsonConfig::class,
  42. 'pet_levels' => PetLevelJsonConfig::class,
  43. 'pet_skills' => PetSkillJsonConfig::class,
  44. 'farm_house' => FarmHouseJsonConfig::class,
  45. 'farm_land' => FarmLandJsonConfig::class,
  46. 'farm_shrine' => FarmShrineJsonConfig::class,
  47. 'currencies' => FundCurrencyJsonConfig::class,
  48. 'recipe' => RecipeJsonConfig::class,
  49. 'dismantle' => DismantleJsonConfig::class,
  50. 'farm_seed' => FarmSeedJsonConfig::class,
  51. 'shop_items' => ShopItemsJsonConfig::class,
  52. ];
  53. // 检查请求的配置表是否存在
  54. if (!isset($map[$key])) {
  55. return response()->json([
  56. 'code' => RESPONSE_CODE::REQUEST_ERROR,
  57. 'message' => '配置表不存在',
  58. 'data' => null
  59. ], 400)->withHeaders([
  60. 'Access-Control-Allow-Origin' => '*',
  61. 'Access-Control-Allow-Methods' => 'GET, POST, OPTIONS',
  62. 'Access-Control-Allow-Headers' => 'Content-Type, Authorization'
  63. ]);
  64. }
  65. try {
  66. // 获取配置表数据
  67. $configClass = $map[$key];
  68. $data = $configClass::getData();
  69. // 如果数据为空,返回错误
  70. if (empty($data)) {
  71. return response()->json([
  72. 'code' => RESPONSE_CODE::SERVER_ERROR,
  73. 'message' => '配置表数据为空',
  74. 'data' => null
  75. ], 500)->withHeaders([
  76. 'Access-Control-Allow-Origin' => '*',
  77. 'Access-Control-Allow-Methods' => 'GET, POST, OPTIONS',
  78. 'Access-Control-Allow-Headers' => 'Content-Type, Authorization'
  79. ]);
  80. }
  81. // 返回配置表数据
  82. if (is_array($data) || is_object($data)) {
  83. return response()->json($data)->withHeaders([
  84. 'Access-Control-Allow-Origin' => '*',
  85. 'Access-Control-Allow-Methods' => 'GET, POST, OPTIONS',
  86. 'Access-Control-Allow-Headers' => 'Content-Type, Authorization'
  87. ]);
  88. }
  89. return response($data)->withHeaders([
  90. 'Access-Control-Allow-Origin' => '*',
  91. 'Access-Control-Allow-Methods' => 'GET, POST, OPTIONS',
  92. 'Access-Control-Allow-Headers' => 'Content-Type, Authorization'
  93. ]);
  94. } catch (\Exception $e) {
  95. // 记录错误日志
  96. Log::error('获取配置表数据失败', [
  97. 'key' => $key,
  98. 'error' => $e->getMessage(),
  99. 'trace' => $e->getTraceAsString()
  100. ]);
  101. // 返回错误响应
  102. return response()->json([
  103. 'code' => RESPONSE_CODE::SERVER_ERROR,
  104. 'message' => '获取配置表数据失败',
  105. 'data' => null
  106. ], 500)->withHeaders([
  107. 'Access-Control-Allow-Origin' => '*',
  108. 'Access-Control-Allow-Methods' => 'GET, POST, OPTIONS',
  109. 'Access-Control-Allow-Headers' => 'Content-Type, Authorization'
  110. ]);
  111. }
  112. }
  113. }