SystemHealthController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. use Illuminate\Support\Facades\Validator;
  7. class SystemHealthController extends Controller
  8. {
  9. // Redis键
  10. const REDIS_KEY = 'system:health';
  11. /**
  12. * 创建一个新的控制器实例
  13. *
  14. * @return void
  15. */
  16. public function __construct()
  17. {
  18. // 设置默认时区,以确保所有时间戳处理都使用系统配置的时区
  19. // 如果config/app.php中没有设置时区,默认使用'Asia/Shanghai'
  20. $timezone = config('app.timezone', 'Asia/Shanghai');
  21. date_default_timezone_set($timezone);
  22. }
  23. /**
  24. * 显示系统健康状态配置页面
  25. */
  26. public function index()
  27. {
  28. // 从Redis获取系统健康配置
  29. $healthConfig = $this->getHealthConfig();
  30. // 返回视图
  31. return view('admin.system_health.index', [
  32. 'healthConfig' => $healthConfig
  33. ]);
  34. }
  35. /**
  36. * 更新系统健康状态配置
  37. */
  38. public function update(Request $request)
  39. {
  40. // 验证输入
  41. $validator = Validator::make($request->all(), [
  42. 'status' => 'required|integer|in:0,1',
  43. 'pay_status' => 'required|integer|in:0,1',
  44. 'pay_start_time' => 'nullable|required_if:pay_status,0',
  45. 'pay_end_time' => 'nullable|required_if:pay_status,0',
  46. 'cashout_status' => 'required|integer|in:0,1',
  47. 'cashout_start_time' => 'nullable|required_if:cashout_status,0',
  48. 'cashout_end_time' => 'nullable|required_if:cashout_status,0',
  49. 'notice' => 'nullable|string|max:500',
  50. ]);
  51. if ($validator->fails()) {
  52. return redirect()->back()->withErrors($validator)->withInput();
  53. }
  54. // 构建健康状态配置
  55. $healthConfig = [
  56. 'status' => (int)$request->input('status'),
  57. 'notice' => $request->input('notice', ''),
  58. ];
  59. // 处理支付维护配置
  60. if ((int)$request->input('pay_status') === 0) {
  61. // 支付维护模式 - 配置时间
  62. // 将本地时间字符串转换为时间戳,保留原始时区设置
  63. $payStartTime = new \DateTime($request->input('pay_start_time'));
  64. $payEndTime = new \DateTime($request->input('pay_end_time'));
  65. $healthConfig['pay'] = [
  66. 'st' => $payStartTime->getTimestamp(),
  67. 'ed' => $payEndTime->getTimestamp(),
  68. 'status' => 0
  69. ];
  70. } else {
  71. // 支付正常模式
  72. $healthConfig['pay'] = 1;
  73. }
  74. // 处理提现维护配置
  75. if ((int)$request->input('cashout_status') === 0) {
  76. // 提现维护模式 - 配置时间
  77. // 将本地时间字符串转换为时间戳,保留原始时区设置
  78. $cashoutStartTime = new \DateTime($request->input('cashout_start_time'));
  79. $cashoutEndTime = new \DateTime($request->input('cashout_end_time'));
  80. $healthConfig['cashout'] = [
  81. 'st' => $cashoutStartTime->getTimestamp(),
  82. 'ed' => $cashoutEndTime->getTimestamp(),
  83. 'status' => 0
  84. ];
  85. } else {
  86. // 提现正常模式
  87. $healthConfig['cashout'] = 1;
  88. }
  89. // 存储到Redis
  90. Redis::set(self::REDIS_KEY, json_encode($healthConfig));
  91. return redirect()->route('admin.system_health.index')->with('success', '系统健康状态配置已更新');
  92. }
  93. /**
  94. * 从Redis获取系统健康配置
  95. */
  96. protected function getHealthConfig()
  97. {
  98. $config = Redis::get(self::REDIS_KEY);
  99. if (!$config) {
  100. // 默认配置
  101. return [
  102. 'status' => 1,
  103. 'pay' => 1,
  104. 'cashout' => 1,
  105. 'notice' => '',
  106. ];
  107. }
  108. return json_decode($config, true);
  109. }
  110. }