2024-07-11 00:10:33 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace app\middleware;
|
|
|
|
|
|
|
|
use app\common\model\AdminUserModel;
|
|
|
|
use app\common\model\MchTerminalModel;
|
|
|
|
use app\common\model\Teacher;
|
|
|
|
use app\constant\ResponseCode;
|
|
|
|
use ReflectionClass;
|
|
|
|
use think\Exception;
|
|
|
|
use think\model\Collection;
|
|
|
|
use Tinywan\Jwt\Exception\JwtCacheTokenException;
|
|
|
|
use Tinywan\Jwt\Exception\JwtTokenException;
|
|
|
|
use Tinywan\Jwt\Exception\JwtTokenExpiredException;
|
|
|
|
use Tinywan\Jwt\JwtToken;
|
|
|
|
use Webman\Http\Request;
|
|
|
|
use Webman\Http\Response;
|
|
|
|
use Webman\MiddlewareInterface;
|
|
|
|
|
|
|
|
class ApiAuthCheckMiddleware implements MiddlewareInterface
|
|
|
|
{
|
|
|
|
public function process(Request $request, callable $handler): Response
|
|
|
|
{
|
|
|
|
|
2024-07-21 18:40:09 +08:00
|
|
|
$request->student = new \stdClass();
|
2024-07-11 00:10:33 +08:00
|
|
|
$request->teacher = new \stdClass();
|
2024-07-27 10:35:57 +08:00
|
|
|
$request->parent = new \stdClass();
|
2024-07-11 00:10:33 +08:00
|
|
|
|
|
|
|
// 通过反射获取控制器哪些方法不需要登录和鉴权
|
|
|
|
$controller = new ReflectionClass($request->controller);
|
|
|
|
$noNeedLogin = $controller->getDefaultProperties()['noNeedLogin'] ?? [];
|
|
|
|
|
|
|
|
// $res = JwtToken::getExtend();
|
|
|
|
|
|
|
|
$is_need_login = 0;
|
|
|
|
$msg = '';
|
|
|
|
try {
|
|
|
|
$extend = JwtToken::getExtend();
|
2024-07-27 10:35:57 +08:00
|
|
|
|
2024-07-11 00:10:33 +08:00
|
|
|
if ($extend['role'] == 'student') {
|
2024-07-21 18:40:09 +08:00
|
|
|
$request->student = \support\Db::table('student')
|
|
|
|
->where('id', $extend['id'])
|
2024-07-27 10:35:57 +08:00
|
|
|
->select('id', 'student_name', 'account', 'openid')
|
2024-07-21 18:40:09 +08:00
|
|
|
->first();
|
2024-07-11 00:10:33 +08:00
|
|
|
} elseif ($extend['role'] == 'teacher') {
|
|
|
|
$request->teacher = \support\Db::table('teacher')
|
|
|
|
->where('id', $extend['id'])
|
2024-07-27 10:35:57 +08:00
|
|
|
->select('id', 'account', 'teacher_name', 'openid', 'time_zone_name', 'time_zone_abbr', 'time_zone_offset')
|
2024-07-11 00:10:33 +08:00
|
|
|
->first();
|
|
|
|
|
2024-07-27 10:35:57 +08:00
|
|
|
} elseif ($extend['role'] == 'parent') {
|
|
|
|
$request->parent = \support\Db::table('student_parent')
|
2024-07-21 18:40:09 +08:00
|
|
|
->where('id', $extend['id'])
|
2024-07-27 10:35:57 +08:00
|
|
|
->select('id', 'parent_name', 'account', 'openid')
|
2024-07-21 18:40:09 +08:00
|
|
|
->first();
|
2024-07-11 00:10:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// $request->user = JwtToken::getUser();
|
|
|
|
} catch (JwtTokenExpiredException $e) {
|
|
|
|
$is_need_login = 1;
|
|
|
|
$msg = $e->getMessage();
|
|
|
|
} catch (JwtTokenException $e) {
|
|
|
|
$is_need_login = 1;
|
|
|
|
$msg = $e->getMessage();
|
|
|
|
} catch (JwtCacheTokenException $e) {
|
|
|
|
$is_need_login = 1;
|
|
|
|
$msg = $e->getMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 访问的方法需要登录
|
|
|
|
if (!in_array($request->action, $noNeedLogin) && !in_array('*', $noNeedLogin) && $is_need_login) {
|
|
|
|
return json([
|
|
|
|
'code' => ResponseCode::NEED_LOGIN,
|
|
|
|
'msg' => $msg,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
return $handler($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|