2025-07-09 19:11:57 +08:00
|
|
|
<?php
|
|
|
|
|
namespace app\middleware;
|
|
|
|
|
|
|
|
|
|
use app\common\utils\Jwt;
|
|
|
|
|
use ReflectionClass;
|
|
|
|
|
use Webman\MiddlewareInterface;
|
|
|
|
|
use Webman\Http\Response;
|
|
|
|
|
use Webman\Http\Request;
|
2025-07-18 17:32:42 +08:00
|
|
|
use app\common\service\BasicDataService;
|
2025-07-09 19:11:57 +08:00
|
|
|
|
|
|
|
|
class AuthCheck implements MiddlewareInterface
|
|
|
|
|
{
|
|
|
|
|
public function process(Request $request, callable $handler) : Response
|
|
|
|
|
{
|
|
|
|
|
$controller = new ReflectionClass($request->controller);
|
2025-07-18 17:32:42 +08:00
|
|
|
$properties = $controller->getDefaultProperties();
|
|
|
|
|
$noNeedLogin = $properties['noNeedLogin'] ?? [];
|
|
|
|
|
$noNeedAuth = $properties['noNeedAuth'] ?? [];
|
|
|
|
|
$noNeedHeaderData = $properties['noNeedHeaderData'] ?? [];
|
2025-07-09 19:11:57 +08:00
|
|
|
|
2025-07-18 17:32:42 +08:00
|
|
|
// header数据
|
|
|
|
|
if (!in_array($request->action, $noNeedHeaderData)) { //如果需要header数据
|
|
|
|
|
try {
|
|
|
|
|
$corpAbbr = $request->header('Fdesk-Corp-Abbr');
|
|
|
|
|
$systemCode = $request->header('Fdesk-System-Code');
|
|
|
|
|
$loginType = $request->header('Fdesk-Login-Type');
|
|
|
|
|
$basicDataService = new BasicDataService();
|
|
|
|
|
if ($basicDataService->checkAndCacheBaseParams($loginType, $systemCode, $corpAbbr)) {
|
|
|
|
|
$request->login_type = $loginType;
|
|
|
|
|
$request->system_code = $systemCode;
|
|
|
|
|
$request->corp_abbr = $corpAbbr;
|
|
|
|
|
$request->basic_data = [
|
|
|
|
|
'login_type' => $loginType,
|
|
|
|
|
'system_code' => $systemCode,
|
|
|
|
|
'corp_abbr' => $corpAbbr
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
return $handler($request);
|
|
|
|
|
} catch (\Throwable $th) {
|
|
|
|
|
throw $th;
|
|
|
|
|
$body = [
|
|
|
|
|
'status' => 'fail',
|
|
|
|
|
'code' => 401,
|
|
|
|
|
'message' => $th->getMessage(),
|
|
|
|
|
'data' => [],
|
|
|
|
|
];
|
|
|
|
|
return response(json_encode($body, JSON_UNESCAPED_UNICODE), 401, ['Content-Type' => 'application/json']);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 不登陆&不鉴权
|
|
|
|
|
if (!in_array($request->action, $noNeedLogin)) { //如果需要登陆&鉴权
|
2025-07-09 19:11:57 +08:00
|
|
|
try {
|
2025-07-11 15:21:00 +08:00
|
|
|
$authHeader = $request->header('Fdesk-Auth-Token');
|
2025-07-09 19:11:57 +08:00
|
|
|
if (!$authHeader || !preg_match('/Bearer\s(\S+)/', $authHeader, $matches)) {
|
|
|
|
|
$body = [
|
|
|
|
|
'status' => 'fail',
|
|
|
|
|
'code' => 401,
|
|
|
|
|
'message' => '未授权',
|
|
|
|
|
'data' => [],
|
|
|
|
|
];
|
|
|
|
|
return response(json_encode($body, JSON_UNESCAPED_UNICODE), 401, ['Content-Type' => 'application/json']);
|
|
|
|
|
}
|
|
|
|
|
$token = $matches[1];
|
|
|
|
|
$request->user = (new Jwt())->verifyToken($token);
|
2025-07-18 17:32:42 +08:00
|
|
|
return $handler($request);
|
2025-07-09 19:11:57 +08:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
$body = [
|
|
|
|
|
'status' => 'fail',
|
|
|
|
|
'code' => 401,
|
|
|
|
|
'message' => $e->getMessage(),
|
|
|
|
|
'data' => [],
|
|
|
|
|
];
|
|
|
|
|
return response(json_encode($body, JSON_UNESCAPED_UNICODE), 401, ['Content-Type' => 'application/json']);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-18 17:32:42 +08:00
|
|
|
|
|
|
|
|
// 登陆&不鉴权
|
|
|
|
|
// if (!in_array($request->action, $noNeedAuth)) { //如果需要登陆&不鉴权
|
|
|
|
|
// try {
|
|
|
|
|
|
|
|
|
|
// return $handler($request);
|
|
|
|
|
// } catch (\Throwable $e) {
|
|
|
|
|
// $body = [
|
|
|
|
|
// 'status' => 'fail',
|
|
|
|
|
// 'code' => 401,
|
|
|
|
|
// 'message' => $e->getMessage(),
|
|
|
|
|
// 'data' => [],
|
|
|
|
|
// ];
|
|
|
|
|
// return response(json_encode($body, JSON_UNESCAPED_UNICODE), 401, ['Content-Type' => 'application/json']);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
return $handler($request);
|
2025-07-09 19:11:57 +08:00
|
|
|
}
|
|
|
|
|
}
|