RedirectResponse.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace addons\epay\library;
  3. use Symfony\Component\HttpFoundation\RedirectResponse as BaseRedirectResponse;
  4. use JsonSerializable;
  5. class RedirectResponse extends BaseRedirectResponse implements JsonSerializable
  6. {
  7. public function __toString()
  8. {
  9. return $this->getContent();
  10. }
  11. public function setTargetUrl($url)
  12. {
  13. if ('' === ($url ?? '')) {
  14. throw new \InvalidArgumentException('无法跳转到空页面');
  15. }
  16. $this->targetUrl = $url;
  17. $this->setContent(
  18. sprintf('<!DOCTYPE html>
  19. <html>
  20. <head>
  21. <meta charset="UTF-8" />
  22. <meta http-equiv="refresh" content="0;url=\'%1$s\'" />
  23. <title>正在跳转支付 %1$s</title>
  24. </head>
  25. <body>
  26. <div id="redirect" style="display:none;">正在跳转支付 <a href="%1$s">%1$s</a></div>
  27. <script type="text/javascript">
  28. setTimeout(function(){
  29. document.getElementById("redirect").style.display = "block";
  30. }, 1000);
  31. </script>
  32. </body>
  33. </html>', htmlspecialchars($url, \ENT_QUOTES, 'UTF-8')));
  34. $this->headers->set('Location', $url);
  35. return $this;
  36. }
  37. #[\ReturnTypeWillChange]
  38. public function jsonSerialize()
  39. {
  40. return $this->getContent();
  41. }
  42. // 使用 PHP 8 兼容的新序列化方式
  43. public function __serialize(): array
  44. {
  45. return ['content' => $this->content];
  46. }
  47. public function __unserialize(array $data): void
  48. {
  49. $this->content = $data['content'] ?? '';
  50. }
  51. }