ApiEncrypt.php 704 B

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. // application/middleware/ApiEncrypt.php
  3. declare (strict_types=1);
  4. namespace app\common\middleware;
  5. class ApiEncrypt
  6. {
  7. public function handle($request, \Closure $next)
  8. {
  9. // 解密逻辑
  10. $data = $this->decrypt($request->param());
  11. $request->replace($data);
  12. $response = $next($request);
  13. // 加密逻辑
  14. $response->content(json_encode($this->encrypt($response->getData())));
  15. return $response;
  16. }
  17. private function decrypt($data)
  18. {
  19. // 实现解密逻辑
  20. // ...
  21. return $data;
  22. }
  23. private function encrypt($data)
  24. {
  25. // 实现加密逻辑
  26. // ...
  27. return $data;
  28. }
  29. }