You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
payment/app/Request/ApiRequest.php

55 lines
1.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Request;
use App\Exception\BusinessException;
use App\Helper\Signer;
use App\Model\App;
class ApiRequest extends Request
{
protected App $app;
public function __construct(array $params = [])
{
$this->checkApp($params['app_id'] ?? null);
$this->checkSign($params);
$data = json_decode($params['data'], true);
foreach ($this->defaults() as $key => $value) {
if (isset($data[$key])) {
if (empty($value) && empty($data[$key])) {
$data[$key] = $value;
}
} else {
$data[$key] = $value;
}
}
$this->_data = $data;
}
protected function checkSign($params) {
/* if ($params['timestamp'] < time() - 600) {
throw new BusinessException('请求已过期');
} */
if (!Signer::verify($params, $this->app->app_key)) {
throw new BusinessException('验签错误');
}
}
protected function checkApp($appId) {
if (is_null($appId)) {
throw new BusinessException('[app_id]错误');
}
$app = App::query()->where('app_id', $appId)->first();
if (is_null($app)) {
throw new BusinessException('APP错误');
}
$this->app = $app;
}
public function getApp() {
return $this->app;
}
}