CommonConfigController.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\Redis;
  6. class CommonConfigController extends Controller
  7. {
  8. public function index()
  9. {
  10. // 从 Redis 获取公用配置数据
  11. $configData = Redis::get("GameConfigX_Common");
  12. $config = $configData ? json_decode($configData, true) : null;
  13. // 5档配置
  14. $configs = [];
  15. for ($level = 1; $level <= 5; $level++) {
  16. $configs[] = [
  17. 'level' => $level,
  18. 'levelName' => "第{$level}档",
  19. 'config' => $config
  20. ];
  21. }
  22. return view('admin.common_config.index', compact('configs'));
  23. }
  24. public function update(Request $request)
  25. {
  26. $config = $request->input('config');
  27. \Log::info('公用配置更新请求', [
  28. 'config' => $config,
  29. 'all_data' => $request->all()
  30. ]);
  31. if (!$config) {
  32. \Log::error('公用配置更新失败:参数错误');
  33. return response()->json(['status' => 'error', 'message' => '参数错误']);
  34. }
  35. try {
  36. $configData = json_decode($config, true);
  37. if (json_last_error() !== JSON_ERROR_NONE) {
  38. \Log::error('公用配置更新失败:JSON格式错误', ['error' => json_last_error_msg()]);
  39. throw new \Exception('JSON格式错误: ' . json_last_error_msg());
  40. }
  41. Redis::set("GameConfigX_Common", $config);
  42. \Log::info('公用配置更新成功');
  43. return response()->json(['status' => 'success', 'message' => '更新成功']);
  44. } catch (\Exception $e) {
  45. \Log::error('公用配置更新失败:异常', ['message' => $e->getMessage()]);
  46. return response()->json(['status' => 'error', 'message' => $e->getMessage()]);
  47. }
  48. }
  49. }