JsonController.php 2.6 KB

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