feat : login wx
This commit is contained in:
82
app/Aspect/Api/ApiReturnAspect.php
Normal file
82
app/Aspect/Api/ApiReturnAspect.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace App\Aspect\Api;
|
||||
|
||||
use App\Extend\SystemUtil;
|
||||
use App\Lib\ApiReturn;
|
||||
use App\Lib\Crypto\CryptoFactory;
|
||||
use App\Lib\Log;
|
||||
use Hyperf\Context\Context;
|
||||
use Hyperf\Di\Annotation\Aspect;
|
||||
use Hyperf\Di\Aop\AbstractAspect;
|
||||
use Hyperf\Di\Aop\ProceedingJoinPoint;
|
||||
use Hyperf\Di\Exception\Exception;
|
||||
use Hyperf\HttpServer\Contract\RequestInterface;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
|
||||
#[
|
||||
Aspect(
|
||||
classes: [
|
||||
ApiReturn::class,
|
||||
],
|
||||
annotations: []
|
||||
)
|
||||
]
|
||||
class ApiReturnAspect extends AbstractAspect
|
||||
{
|
||||
/**
|
||||
* @param RequestInterface $request
|
||||
* @param CryptoFactory $CryptoFactory
|
||||
* @param Log $log
|
||||
* @param int $userId
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly RequestInterface $request,
|
||||
private readonly CryptoFactory $CryptoFactory,
|
||||
private readonly Log $log,
|
||||
private int $userId = 0
|
||||
){}
|
||||
|
||||
/**
|
||||
* @param ProceedingJoinPoint $proceedingJoinPoint
|
||||
* @return mixed
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
public function process(ProceedingJoinPoint $proceedingJoinPoint): mixed
|
||||
{
|
||||
// 在调用前进行处理
|
||||
$result = $proceedingJoinPoint->process();
|
||||
// 在调用后进行处理
|
||||
$this->userId = Context::get('user_id',0);
|
||||
|
||||
// 加密前写请求日志
|
||||
$this->writeResponseLog(json_encode($result));
|
||||
|
||||
//正式服加密 测试服不做处理
|
||||
if (SystemUtil::checkProEnv()) {
|
||||
$cryptoFactory = $this->CryptoFactory->cryptoClass('api', json_encode($result['data']));
|
||||
$result['data'] = $cryptoFactory->encrypt();
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求日志
|
||||
* @param string $result
|
||||
* @return void
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
private function writeResponseLog(string $result): void
|
||||
{
|
||||
$post = $this->request->all();
|
||||
$logParam = !$post ? '{}' : json_encode($post);
|
||||
$header = json_encode($this->request->getHeaders());
|
||||
|
||||
$this->log->requestApiLog("\napi==path:{$this->request->getPathInfo()}\nuserId:$this->userId\nrequestData:$logParam\nheader:$header\nresponseData:$result");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user