From 768cf5137c6f2cc80c1dee88483588ed70c52340 Mon Sep 17 00:00:00 2001 From: zhenhui <1276357500@qq.com> Date: Fri, 6 Mar 2026 10:33:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=AE=BF=E9=97=AE=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E6=8A=A5=E9=94=99Server=20internal=20error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/app/api/controller/GameController.php | 8 ++- .../app/controller/SystemController.php | 27 +++++---- .../plugin/saiadmin/app/exception/Handler.php | 56 ++++++++++--------- .../saiadmin/app/middleware/CheckLogin.php | 7 ++- .../plugin/saiadmin/basic/BaseController.php | 20 ++++--- .../plugin/saiadmin/basic/think/BaseModel.php | 46 ++++++++------- 6 files changed, 96 insertions(+), 68 deletions(-) diff --git a/server/app/api/controller/GameController.php b/server/app/api/controller/GameController.php index d0b9120..f3d6f37 100644 --- a/server/app/api/controller/GameController.php +++ b/server/app/api/controller/GameController.php @@ -3,6 +3,7 @@ declare(strict_types=1); namespace app\api\controller; +use support\Log; use support\Request; use support\Response; use app\api\logic\GameLogic; @@ -110,10 +111,13 @@ class GameController extends OpenController 'roll_array' => '[]', 'status' => PlayStartLogic::RECORD_STATUS_TIMEOUT, ]); - } catch (\Throwable $_) { + } catch (\Exception $e) { + $timeout_message = $e->getMessage(); + Log::error("游玩记录写入超时: ". $e->getMessage()); } + $payload = $timeoutRecord ? ['record' => $timeoutRecord->toArray()] : []; - return $this->success($payload, '服务超时'); + return $this->success($payload, '服务超时,'.$timeout_message ?? '没有原因'); } } } diff --git a/server/plugin/saiadmin/app/controller/SystemController.php b/server/plugin/saiadmin/app/controller/SystemController.php index 3aed312..ba33b0c 100644 --- a/server/plugin/saiadmin/app/controller/SystemController.php +++ b/server/plugin/saiadmin/app/controller/SystemController.php @@ -33,24 +33,26 @@ class SystemController extends BaseController */ public function userInfo(): Response { - $info['user'] = $this->adminInfo; + if ($this->adminInfo === null || !is_array($this->adminInfo) || !isset($this->adminInfo['id'])) { + return $this->fail('登录已过期或用户信息无效,请重新登录', 401); + } $info = []; $info['id'] = $this->adminInfo['id']; $info['username'] = $this->adminInfo['username']; - $info['dashboard'] = $this->adminInfo['dashboard']; - $info['avatar'] = $this->adminInfo['avatar']; - $info['email'] = $this->adminInfo['email']; - $info['phone'] = $this->adminInfo['phone']; - $info['gender'] = $this->adminInfo['gender']; - $info['signed'] = $this->adminInfo['signed']; - $info['realname'] = $this->adminInfo['realname']; - $info['department'] = $this->adminInfo['deptList']; - if ($this->adminInfo['id'] === 1) { + $info['dashboard'] = $this->adminInfo['dashboard'] ?? ''; + $info['avatar'] = $this->adminInfo['avatar'] ?? ''; + $info['email'] = $this->adminInfo['email'] ?? ''; + $info['phone'] = $this->adminInfo['phone'] ?? ''; + $info['gender'] = $this->adminInfo['gender'] ?? ''; + $info['signed'] = $this->adminInfo['signed'] ?? ''; + $info['realname'] = $this->adminInfo['realname'] ?? ''; + $info['department'] = $this->adminInfo['deptList'] ?? []; + if ((int) $this->adminInfo['id'] === 1) { $info['buttons'] = ['*']; $info['roles'] = ['super_admin']; } else { $info['buttons'] = UserAuthCache::getUserAuth($this->adminInfo['id']); - $info['roles'] = Arr::getArrayColumn($this->adminInfo['roleList'], 'code'); + $info['roles'] = Arr::getArrayColumn($this->adminInfo['roleList'] ?? [], 'code'); } return $this->success($info); } @@ -70,6 +72,9 @@ class SystemController extends BaseController */ public function menu(): Response { + if ($this->adminInfo === null || !is_array($this->adminInfo) || !isset($this->adminInfo['id'])) { + return $this->fail('登录已过期或用户信息无效,请重新登录', 401); + } $data = UserMenuCache::getUserMenu($this->adminInfo['id']); return $this->success($data); } diff --git a/server/plugin/saiadmin/app/exception/Handler.php b/server/plugin/saiadmin/app/exception/Handler.php index 59ca1ed..3e702fc 100644 --- a/server/plugin/saiadmin/app/exception/Handler.php +++ b/server/plugin/saiadmin/app/exception/Handler.php @@ -40,29 +40,35 @@ class Handler extends ExceptionHandler $this->logger->error($logs); } - public function render(Request $request, Throwable $exception): Response - { - $debug = config('app.debug', true); - $code = $exception->getCode(); - $json = [ - 'code' => $code ? $code : 500, - 'message' => $code !== 500 ? $exception->getMessage() : 'Server internal error', - 'type' => 'failed' - ]; - if ($debug) { - $json['request_url'] = $request->method() . ' ' . $request->uri(); - $json['timestamp'] = date('Y-m-d H:i:s'); - $json['client_ip'] = $request->getRealIp(); - $json['request_param'] = $request->all(); - $json['exception_handle'] = get_class($exception); - $json['exception_info'] = [ - 'code' => $exception->getCode(), - 'message' => $exception->getMessage(), - 'file' => $exception->getFile(), - 'line' => $exception->getLine(), - 'trace' => explode("\n", $exception->getTraceAsString()) - ]; - } - return new Response(200, ['Content-Type' => 'application/json;charset=utf-8'], json_encode($json)); - } +// public function render(Request $request, Throwable $exception): Response +// { +// $debug = config('app.debug', true); +// $code = $exception->getCode(); +// $httpCode = ($code >= 400 && $code < 600) ? $code : 500; +// // 开启 debug 时始终返回真实错误信息,便于排查;未开启时 500 不暴露详情 +// $message = $exception->getMessage(); +// if (!$debug && $httpCode === 500) { +// $message = 'Server internal error'; +// } +// $json = [ +// 'code' => $httpCode, +// 'message' => $message, +// 'type' => 'failed' +// ]; +// if ($debug) { +// $json['request_url'] = $request->method() . ' ' . $request->uri(); +// $json['timestamp'] = date('Y-m-d H:i:s'); +// $json['client_ip'] = $request->getRealIp(); +// $json['request_param'] = $request->all(); +// $json['exception_handle'] = get_class($exception); +// $json['exception_info'] = [ +// 'code' => $exception->getCode(), +// 'message' => $exception->getMessage(), +// 'file' => $exception->getFile(), +// 'line' => $exception->getLine(), +// 'trace' => explode("\n", $exception->getTraceAsString()) +// ]; +// } +// return new Response(200, ['Content-Type' => 'application/json;charset=utf-8'], json_encode($json)); +// } } diff --git a/server/plugin/saiadmin/app/middleware/CheckLogin.php b/server/plugin/saiadmin/app/middleware/CheckLogin.php index 9e2c6b4..f32bf17 100644 --- a/server/plugin/saiadmin/app/middleware/CheckLogin.php +++ b/server/plugin/saiadmin/app/middleware/CheckLogin.php @@ -32,8 +32,11 @@ class CheckLogin implements MiddlewareInterface if ($token['plat'] !== 'saiadmin') { throw new ApiException('登录凭证校验失败'); } - $request->setHeader('check_login', true); - $request->setHeader('check_admin', $token); + // 一次合并设置,避免 setHeader 覆盖导致只保留最后一个 + $request->setHeader(array_merge($request->header() ?: [], [ + 'check_login' => true, + 'check_admin' => $token, + ])); } return $handler($request); } diff --git a/server/plugin/saiadmin/basic/BaseController.php b/server/plugin/saiadmin/basic/BaseController.php index 7420d7d..d08f346 100644 --- a/server/plugin/saiadmin/basic/BaseController.php +++ b/server/plugin/saiadmin/basic/BaseController.php @@ -45,16 +45,20 @@ class BaseController extends OpenController */ protected function init(): void { - // 登录模式赋值 + // 登录模式赋值(仅当 check_admin 有效时赋值,避免登录接口等未带 token 时访问 null 导致报错) $isLogin = request()->header('check_login', false); - if ($isLogin) { - $result = request()->header('check_admin'); - $this->adminId = $result['id']; - $this->adminName = $result['username']; - $this->adminInfo = UserInfoCache::getUserInfo($result['id']); + $result = request()->header('check_admin'); + if ($isLogin && $result !== null && (is_array($result) || is_object($result))) { + $arr = is_array($result) ? $result : (array) $result; + $adminId = $arr['id'] ?? null; + if ($adminId !== null) { + $this->adminId = (int) $adminId; + $this->adminName = $arr['username'] ?? ''; + $this->adminInfo = UserInfoCache::getUserInfo($adminId); - // 用户数据传递给逻辑层 - $this->logic && $this->logic->init($this->adminInfo); + // 用户数据传递给逻辑层 + $this->logic && $this->logic->init($this->adminInfo); + } } } diff --git a/server/plugin/saiadmin/basic/think/BaseModel.php b/server/plugin/saiadmin/basic/think/BaseModel.php index 3f8bb4d..962841c 100644 --- a/server/plugin/saiadmin/basic/think/BaseModel.php +++ b/server/plugin/saiadmin/basic/think/BaseModel.php @@ -106,18 +106,21 @@ class BaseModel extends Model implements ModelInterface */ public static function onBeforeInsert($model): void { - $createTime = $model->createTime ?? 'create_time'; - if ($createTime && !$model->getData($createTime)) { - $model->set($createTime, date('Y-m-d H:i:s')); + try { + $createTime = $model->createTime ?? 'create_time'; + if ($createTime && !$model->getData($createTime)) { + $model->set($createTime, date('Y-m-d H:i:s')); + } + } catch (\Throwable $e) { } - if (function_exists('getCurrentInfo')) { - $info = getCurrentInfo(); - if (!empty($info['id'])) { - try { + try { + if (function_exists('getCurrentInfo')) { + $info = getCurrentInfo(); + if (!empty($info['id'])) { $model->setAttr('created_by', $info['id']); - } catch (\Throwable $e) { } } + } catch (\Throwable $e) { } } @@ -128,20 +131,23 @@ class BaseModel extends Model implements ModelInterface */ public static function onBeforeWrite($model): void { - if ($model->isExists()) { - $updateTime = $model->updateTime ?? 'update_time'; - if ($updateTime) { - $model->set($updateTime, date('Y-m-d H:i:s')); - } - } - if (function_exists('getCurrentInfo')) { - $info = getCurrentInfo(); - if (!empty($info['id'])) { - try { - $model->setAttr('updated_by', $info['id']); - } catch (\Throwable $e) { + try { + if ($model->isExists()) { + $updateTime = $model->updateTime ?? 'update_time'; + if ($updateTime) { + $model->set($updateTime, date('Y-m-d H:i:s')); } } + } catch (\Throwable $e) { + } + try { + if (function_exists('getCurrentInfo')) { + $info = getCurrentInfo(); + if (!empty($info['id'])) { + $model->setAttr('updated_by', $info['id']); + } + } + } catch (\Throwable $e) { } } }