重构初始化

This commit is contained in:
技术老胡
2026-04-15 11:45:46 +08:00
parent 72d72d735b
commit 7612026773
381 changed files with 28287 additions and 14717 deletions

View File

@@ -0,0 +1,63 @@
<?php
namespace app\http\mer\middleware;
use app\service\merchant\auth\MerchantAuthService;
use support\Context;
use Webman\Http\Request;
use Webman\Http\Response;
use Webman\MiddlewareInterface;
/**
* 商户认证中间件。
*
* 负责读取商户 token并把商户身份写入请求上下文。
*/
class MerchantAuthMiddleware implements MiddlewareInterface
{
/**
* 构造函数,注入对应依赖。
*/
public function __construct(
protected MerchantAuthService $merchantAuthService
) {
}
/**
* 处理请求。
*/
public function process(Request $request, callable $handler): Response
{
$token = trim((string) ($request->header('authorization', '') ?: $request->header('x-merchant-token', '')));
$token = preg_replace('/^Bearer\s+/i', '', $token) ?: $token;
if ($token === '') {
if ((int) env('AUTH_MIDDLEWARE_STRICT', 1) === 1) {
return json([
'code' => 401,
'msg' => 'merchant unauthorized',
'data' => null,
]);
}
} else {
$result = $this->merchantAuthService->authenticateToken(
$token,
$request->getRealIp(),
$request->header('user-agent', '')
);
if (!$result) {
return json([
'code' => 401,
'msg' => 'merchant unauthorized',
'data' => null,
]);
}
Context::set('auth.merchant_id', (int) $result['merchant']->id);
Context::set('auth.merchant_no', (string) $result['merchant']->merchant_no);
}
return $handler($request);
}
}