|
|
|
@ -4,8 +4,12 @@ declare(strict_types=1); |
|
|
|
|
|
|
|
namespace App\Middleware\Auth; |
|
|
|
|
|
|
|
use App\Model\v3\User; |
|
|
|
use App\TaskWorker\SSDBTask; |
|
|
|
use Hashids\Hashids; |
|
|
|
use Hyperf\HttpServer\Contract\RequestInterface as HttpRequest; |
|
|
|
use Hyperf\HttpServer\Contract\ResponseInterface as HttpResponse; |
|
|
|
use Hyperf\Utils\ApplicationContext; |
|
|
|
use Psr\Container\ContainerInterface; |
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
|
|
@ -38,18 +42,17 @@ class ApiMiddleware implements MiddlewareInterface |
|
|
|
|
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
|
|
{ |
|
|
|
|
|
|
|
if (env('APP_ENV') == 'dev' || env('APP_ENV') == 'local') { |
|
|
|
return $handler->handle($request); |
|
|
|
} |
|
|
|
|
|
|
|
// 签名校验
|
|
|
|
|
|
|
|
# 获取参数
|
|
|
|
$params = $this->request->all(); |
|
|
|
|
|
|
|
# 必须参数,签名、时间戳、随机数
|
|
|
|
if (!(isset($params['sign'])&&isset($params['timestamp'])&&isset($params['rand']))) { |
|
|
|
if (!( |
|
|
|
isset($params['sign']) |
|
|
|
&&isset($params['timestamp']) |
|
|
|
&&isset($params['rand']) |
|
|
|
) && env('APP_ENV') == 'prod') { |
|
|
|
|
|
|
|
$content = [ |
|
|
|
"status" => 'ok', |
|
|
|
@ -73,11 +76,27 @@ class ApiMiddleware implements MiddlewareInterface |
|
|
|
return $this->response->json($content); |
|
|
|
} |
|
|
|
|
|
|
|
$this->request->user = null; |
|
|
|
$userToken = $params['user_token'] ?? ''; |
|
|
|
|
|
|
|
if ($userToken) { |
|
|
|
$ssdb = ApplicationContext::getContainer()->get(SSDBTask::class); |
|
|
|
$exists = $ssdb->exec('exists', $userToken); |
|
|
|
if ($exists) { |
|
|
|
$hashIds = ApplicationContext::getContainer()->get(Hashids::class); |
|
|
|
$user = $hashIds->decode($userToken); |
|
|
|
$this->request->user = User::query()->find($user[0]); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return $handler->handle($request); |
|
|
|
} |
|
|
|
|
|
|
|
private function checkSign($params) |
|
|
|
{ |
|
|
|
if (env('APP_ENV') != 'prod') { |
|
|
|
return true; |
|
|
|
} |
|
|
|
$sign = $params['sign']; |
|
|
|
unset($params['sign']); |
|
|
|
$timestamp = $params['timestamp']; |
|
|
|
|