优化访问接口报错Server internal error

This commit is contained in:
2026-03-06 10:33:44 +08:00
parent 7e8867ed12
commit 768cf5137c
6 changed files with 96 additions and 68 deletions

View File

@@ -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);
}

View File

@@ -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));
// }
}

View File

@@ -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);
}