mirror of
https://gitee.com/niucloud-team/niucloud-admin.git
synced 2025-12-12 10:52:48 +00:00
60 lines
1.9 KiB
PHP
60 lines
1.9 KiB
PHP
<?php
|
||
// +----------------------------------------------------------------------
|
||
// | Niucloud-admin 企业快速开发的多应用管理平台
|
||
// +----------------------------------------------------------------------
|
||
// | 官方网址:https://www.niucloud.com
|
||
// +----------------------------------------------------------------------
|
||
// | niucloud团队 版权所有 开源版本可自由商用
|
||
// +----------------------------------------------------------------------
|
||
// | Author: Niucloud Team
|
||
// +----------------------------------------------------------------------
|
||
|
||
namespace app\api\middleware;
|
||
|
||
use app\dict\sys\AppTypeDict;
|
||
use app\Request;
|
||
use app\service\api\login\AuthService;
|
||
use app\service\api\login\LoginService;
|
||
use Closure;
|
||
use Exception;
|
||
use core\exception\AuthException;
|
||
|
||
|
||
/**
|
||
* 会员登录token验证
|
||
* Class ApiCheckToken
|
||
* @package app\api\middleware
|
||
*/
|
||
class ApiCheckToken
|
||
{
|
||
/**
|
||
* @param Request $request
|
||
* @param Closure $next
|
||
* @param bool $is_throw_exception 是否把错误抛出
|
||
* @return mixed
|
||
* @throws Exception
|
||
*/
|
||
public function handle(Request $request, Closure $next, bool $is_throw_exception = false)
|
||
{
|
||
$request->appType(AppTypeDict::API);
|
||
|
||
//通过配置来设置系统header参数
|
||
try {
|
||
$token = $request->apiToken();
|
||
$token_info = ( new LoginService() )->parseToken($token);
|
||
} catch (AuthException $e) {
|
||
//是否将登录错误抛出
|
||
if ($is_throw_exception)
|
||
return fail($e->getMessage(), [], $e->getCode());
|
||
}
|
||
if (!empty($token_info)) {
|
||
$request->memberId($token_info[ 'member_id' ]);
|
||
}
|
||
//校验会员和站点
|
||
( new AuthService() )->checkMember($request);
|
||
// 校验渠道
|
||
( new AuthService() )->checkChannel($request);
|
||
return $next($request);
|
||
}
|
||
}
|