Files
huangjingfen/pro_v3.5.1/app/http/middleware/api/AuthTokenMiddleware.php
panchengyong c1e74d8e68 chore(php): 统一 ScottPan 文件头与注释域名替换
- 按 docs/renew-code-comment.md 将 PHP 文件头改为带边框的 Author 注释\n- 注释中的 crmeb.com 替换为 uj345.cn(代码字符串中的外链未改)\n- 新增 docs/renew-code-comment.md 说明

Made-with: Cursor
2026-03-29 11:22:58 +08:00

57 lines
2.0 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
// +----------------------------------------------------------------------
// | Author: ScottPan Team
// +----------------------------------------------------------------------
namespace app\http\middleware\api;
use app\Request;
use app\services\community\CommunityUserServices;
use app\services\user\UserAuthServices;
use crmeb\exceptions\AuthException;
use crmeb\interfaces\MiddlewareInterface;
use think\exception\DbException;
/**
* Class AuthTokenMiddleware
* @package app\api\middleware
*/
class AuthTokenMiddleware implements MiddlewareInterface
{
public function handle(Request $request, \Closure $next, bool $force = true)
{
$authInfo = null;
$token = trim(ltrim($request->header('Authori-zation'), 'Bearer'));
if (!$token) $token = trim(ltrim($request->header('Authorization'), 'Bearer'));//正式版删除此行某些服务器无法获取到token调整为 Authori-zation
try {
/** @var UserAuthServices $service */
$service = app()->make(UserAuthServices::class);
$authInfo = $service->parseToken($token);
} catch (AuthException $e) {
if ($force)
return app('json')->make($e->getCode(), $e->getMessage());
}
if (!is_null($authInfo)) {
$request->user = function (string $key = null) use (&$authInfo) {
if ($key) {
return $authInfo['user'][$key] ?? '';
}
return $authInfo['user'];
};
$request->tokenData = $authInfo['tokenData'];
}
$request->isLogin = !is_null($authInfo);
$request->uid = is_null($authInfo) ? 0 : (int)$authInfo['user']->uid;
if ($request->uid) {
/** @var CommunityUserServices $communityUserServices */
$communityUserServices = app()->make(CommunityUserServices::class);
$communityUserServices->hasUser($request->uid);
}
return $next($request);
}
}