Files
anxinyan/server-api/app/middleware/AppAuthMiddleware.php
2026-05-25 14:53:59 +08:00

62 lines
1.7 KiB
PHP

<?php
namespace app\middleware;
use app\support\AppAuthService;
use Webman\Http\Request;
use Webman\Http\Response;
use Webman\MiddlewareInterface;
class AppAuthMiddleware implements MiddlewareInterface
{
public function process(Request $request, callable $handler): Response
{
$path = $request->path();
if (strpos($path, '/api/app') !== 0) {
return $handler($request);
}
if ($request->method() === 'OPTIONS') {
return $handler($request);
}
$authService = new AppAuthService();
$userInfo = $authService->current($request);
if ($userInfo) {
$request->appUser = $userInfo;
}
if ($this->isPublicPath($path)) {
return $handler($request);
}
if (!$userInfo) {
return api_error('未登录或登录已过期', 401);
}
return $handler($request);
}
private function isPublicPath(string $path): bool
{
return in_array($path, [
'/api/app/home/index',
'/api/app/content/page-visuals',
'/api/app/catalog/brands',
'/api/app/help-center',
'/api/app/help-article/detail',
'/api/app/report/detail',
'/api/app/report/anti-counterfeit/verify',
'/api/app/verify',
'/api/app/material-tag',
'/api/app/material-tag/verify',
'/api/app/auth/send-code',
'/api/app/auth/login/code',
'/api/app/auth/login/password',
'/api/app/auth/wechat/config',
'/api/app/auth/wechat/exchange',
'/api/app/auth/wechat/bind-mobile',
], true);
}
}