RedirectResponse.php 1.4 KB

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