hyperf 中间件带参数到控制器

2024年4月29日09:21:30

<?php

declare(strict_types=1);

namespace App\Middleware;

use App\Model\Admin;
use App\Service\Admin\CommonService;
use App\Util\GlobalCode;
use App\Util\ResponseTrait;
use Exception;
use Hyperf\Context\Context;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface as HttpResponse;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Throwable;

class AdminCheckMiddleware implements MiddlewareInterface
{
    use ResponseTrait;

    protected ContainerInterface $container;

    protected RequestInterface $request;

    protected HttpResponse $response;

    public function __construct(ContainerInterface $container, HttpResponse $response, RequestInterface $request)
    {
        $this->container = $container;
        $this->response = $response;
        $this->request = $request;
    }

    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
//        p(self::class);
        try {
            $token = $this->request->header(GlobalCode::API_TOKEN, '');

            if (empty($token)) {
                //兼容文件下载文件验证 token
                $token = $this->request->input(GlobalCode::API_TOKEN);
                if (empty($token)) {
                    throw new Exception('token为空,请重新登录');
                }
            }

            $admin = Admin::where('token', $token)->first();
            if ($admin == null) {
                throw new Exception('token不存在');
            }
            if ((int)time() > ((int)strtotime($admin->token_time) + (int)GlobalCode::TOKEN_TIME)) {
                throw new Exception('token过期,请重新登录');
            }
            //方案一:直接属性复制,不推荐
            $this->request->admin_id = $admin->id;
            $this->request->token = $token;
			//方案二:使用上下文工具带过去,
			//官方的建议:https://hyperf.wiki/3.1/#/zh-cn/controller?id=%e9%81%bf%e5%85%8d%e5%8d%8f%e7%a8%8b%e9%97%b4%e6%95%b0%e6%8d%ae%e6%b7%b7%e6%b7%86
            Context::set('admin_id', $admin->id);
            Context::set('token', $token);

            $request_url = $this->request->getUri()->getPath();


        } catch (Throwable $e) {
            return $this->grant($e);
        }

        return $handler->handle($request);
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在.NET Core中,中间件负责处理HTTP请求和响应。中间件可以在请求到达控制器之前对请求进行操作,或在响应离开控制器之前对响应进行操作。 如果中间件无法正确读取请求参数,可能有几个原因导致这种情况。以下是一些可能的原因和解决方法: 1. 请求参数绑定问题:中间件可能无法正确绑定请求参数控制器的操作方法参数上。可以检查控制器的操作方法参数的类型和名称是否与请求参数匹配,以确保正确绑定。 2. 请求体读取问题:如果请求中包含了请求体,中间件需要正确读取请求体才能获取请求参数。可以使用`Request.Body`对象来读取请求体,并使用适当的方法(如`StreamReader`)将其转换为字符串或JSON对象。 3. 请求头问题:某些请求参数可能包含在请求头中而不是请求体中。可以通过`Request.Headers`对象来读取请求头,并使用适当的方法(如`Get(key)`)获取特定请求头的值。 4. 中间件顺序问题:如果在中间件管道中使用了多个中间件,确保它们的顺序正确。有时,其他中间件可能已经处理了请求参数,导致当前中间件无法正确读取参数。可以尝试调整中间件的顺序,确保它在其他可能影响请求参数中间件之前执行。 5. 异常处理问题:如果中间件在读取请求参数时发生异常,可能会阻止其他中间件正确处理请求参数。可以通过在中间件代码中添加适当的错误处理逻辑来捕获并处理异常,以确保请求参数可以正确读取。 请根据具体情况检查以上可能的原因,并相应地调整中间件代码,以确保能正确读取请求参数

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值