JsonController.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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\FundCurrencyJsonConfig;
  9. use App\Module\Game\DCache\ItemJsonConfig;
  10. use App\Module\Game\DCache\PetJsonConfig;
  11. use App\Module\Game\DCache\RecipeJsonConfig;
  12. use Illuminate\Support\Facades\Log;
  13. use UCore\Exception\HandleNotException;
  14. use Uraus\Kku\Common\RESPONSE_CODE;
  15. /**
  16. *
  17. * 访问json配置表,输出配置表Json格式
  18. * @package App\Module\AppGame\HttpControllers
  19. *
  20. *
  21. */
  22. class JsonController extends Controller
  23. {
  24. /**
  25. * 配置表读取
  26. * @param string $key 配置表键名
  27. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Foundation\Application|\Illuminate\Http\JsonResponse|\Illuminate\Http\Response|object
  28. * @throws HandleNotException
  29. */
  30. public function config($key)
  31. {
  32. // 配置表映射关系
  33. $map = [
  34. 'items' => ItemJsonConfig::class,
  35. 'chest' => ChestJsonConfig::class,
  36. 'pets' => PetJsonConfig::class,
  37. 'farm_house' => FarmHouseJsonConfig::class,
  38. 'farm_land' => FarmLandJsonConfig::class,
  39. 'currencies' => FundCurrencyJsonConfig::class,
  40. 'recipe' => RecipeJsonConfig::class,
  41. 'dismantle' => DismantleJsonConfig::class,
  42. ];
  43. // 检查请求的配置表是否存在
  44. if (!isset($map[$key])) {
  45. return response()->json([
  46. 'code' => RESPONSE_CODE::REQUEST_ERROR,
  47. 'message' => '配置表不存在',
  48. 'data' => null
  49. ], 400);
  50. }
  51. try {
  52. // 获取配置表数据
  53. $configClass = $map[$key];
  54. $data = $configClass::getData();
  55. // 如果数据为空,返回错误
  56. if (empty($data)) {
  57. return response()->json([
  58. 'code' => RESPONSE_CODE::SERVER_ERROR,
  59. 'message' => '配置表数据为空',
  60. 'data' => null
  61. ], 500);
  62. }
  63. // 返回配置表数据
  64. return $data;
  65. } catch (\Exception $e) {
  66. // 记录错误日志
  67. Log::error('获取配置表数据失败', [
  68. 'key' => $key,
  69. 'error' => $e->getMessage(),
  70. 'trace' => $e->getTraceAsString()
  71. ]);
  72. // 返回错误响应
  73. return response()->json([
  74. 'code' => RESPONSE_CODE::SERVER_ERROR,
  75. 'message' => '获取配置表数据失败',
  76. 'data' => null
  77. ], 500);
  78. }
  79. }
  80. }