RedirectResponse.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. document.getElementById("redirect").style.display = "block";
  28. }, 1000);
  29. </script>
  30. </body>
  31. </html>', htmlspecialchars($url, \ENT_QUOTES, 'UTF-8')));
  32. $this->headers->set('Location', $url);
  33. return $this;
  34. }
  35. public function jsonSerialize()
  36. {
  37. return $this->getContent();
  38. }
  39. public function serialize()
  40. {
  41. return serialize($this->content);
  42. }
  43. public function unserialize($serialized)
  44. {
  45. return $this->content = unserialize($serialized);
  46. }
  47. }