| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?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\FarmSeedJsonConfig;
- use App\Module\Game\DCache\FarmShrineJsonConfig;
- use App\Module\Game\DCache\FundCurrencyJsonConfig;
- use App\Module\Game\DCache\ItemJsonConfig;
- use App\Module\Game\DCache\PetConfigJsonConfig;
- use App\Module\Game\DCache\PetLevelJsonConfig;
- use App\Module\Game\DCache\PetSkillJsonConfig;
- use App\Module\Game\DCache\RecipeJsonConfig;
- use App\Module\Game\DCache\ShopItemsJsonConfig;
- use Illuminate\Support\Facades\Log;
- use UCore\App;
- 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)
- {
- $f= request()->query('f',0);
- // 配置表映射关系
- $map = [
- 'items' => ItemJsonConfig::class,
- 'chest' => ChestJsonConfig::class,
- 'pet_config' => PetConfigJsonConfig::class,
- 'pet_levels' => PetLevelJsonConfig::class,
- 'pet_skills' => PetSkillJsonConfig::class,
- 'farm_house' => FarmHouseJsonConfig::class,
- 'farm_land' => FarmLandJsonConfig::class,
- 'farm_shrine' => FarmShrineJsonConfig::class,
- 'currencies' => FundCurrencyJsonConfig::class,
- 'recipe' => RecipeJsonConfig::class,
- 'dismantle' => DismantleJsonConfig::class,
- 'farm_seed' => FarmSeedJsonConfig::class,
- 'shop_items' => ShopItemsJsonConfig::class,
- ];
- // 检查请求的配置表是否存在
- if (!isset($map[$key])) {
- return response()->json([
- 'code' => RESPONSE_CODE::REQUEST_ERROR,
- 'message' => '配置表不存在',
- 'data' => null
- ], 400)->withHeaders([
- 'Access-Control-Allow-Origin' => '*',
- 'Access-Control-Allow-Methods' => 'GET, POST, OPTIONS',
- 'Access-Control-Allow-Headers' => 'Content-Type, Authorization'
- ]);
- }
- try {
- // 获取配置表数据
- $configClass = $map[$key];
- if($f && App::is_local()){
- $data = $configClass::getData([],true);
- }else{
- // ShopItemsJsonConfig::getData()
- $data = $configClass::getData();
- }
- // 如果数据为空,返回错误
- if (empty($data)) {
- return response()->json([
- 'code' => RESPONSE_CODE::SERVER_ERROR,
- 'message' => '配置表数据为空',
- 'data' => null
- ], 500)->withHeaders([
- 'Access-Control-Allow-Origin' => '*',
- 'Access-Control-Allow-Methods' => 'GET, POST, OPTIONS',
- 'Access-Control-Allow-Headers' => 'Content-Type, Authorization'
- ]);
- }
- // 返回配置表数据
- if (is_array($data) || is_object($data)) {
- return response()->json($data)->withHeaders([
- 'Access-Control-Allow-Origin' => '*',
- 'Access-Control-Allow-Methods' => 'GET, POST, OPTIONS',
- 'Access-Control-Allow-Headers' => 'Content-Type, Authorization'
- ]);
- }
- return response($data)->withHeaders([
- 'Access-Control-Allow-Origin' => '*',
- 'Access-Control-Allow-Methods' => 'GET, POST, OPTIONS',
- 'Access-Control-Allow-Headers' => 'Content-Type, Authorization'
- ]);
- } 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)->withHeaders([
- 'Access-Control-Allow-Origin' => '*',
- 'Access-Control-Allow-Methods' => 'GET, POST, OPTIONS',
- 'Access-Control-Allow-Headers' => 'Content-Type, Authorization'
- ]);
- }
- }
- }
|