WaitGo.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace UCore\DcatAdmin\Metrics\Examples;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Widgets\Widget;
  5. /**
  6. * 倒计时跳转
  7. *
  8. */
  9. class WaitGo extends Widget
  10. {
  11. protected $title = 'WaitGo';
  12. protected $view = 'admin_core.metrics.waitgo';
  13. /**
  14. * 等待秒数
  15. *
  16. * @var int
  17. */
  18. protected $sWait = 30;
  19. /**
  20. * 目标网址
  21. *
  22. * @var string
  23. */
  24. public $go_url = '';
  25. /**
  26. * 弹出提示
  27. *
  28. * @var bool
  29. */
  30. public $notice = false;
  31. public function __construct($go_url = '', $sWait = 30)
  32. {
  33. if ($go_url) {
  34. $this->go_url = $go_url;
  35. }
  36. if ($sWait) {
  37. $this->sWait = $sWait;
  38. }
  39. $this->variables['sWait'] = $this->sWait;
  40. $this->variables['notice'] = $this->notice;
  41. $this->variables['go_url'] = $this->go_url;
  42. $script = <<<JS
  43. // js
  44. if(typeof waitindex == 'undefined'){
  45. window.waitindex_waits = {$this->sWait};
  46. var go_url = '{$this->go_url}';
  47. var notice = '{$this->notice}';
  48. var waitindex = setInterval(function(){
  49. if(waitindex_waits < 0){
  50. clearInterval(waitindex);
  51. window.location.replace(go_url);
  52. }
  53. window.waitindex_waits --;
  54. $('.wait_go').html(window.waitindex_waits);
  55. console.log('waitindex :', window.waitindex_waits,go_url)
  56. }, 1000);
  57. if(notice){
  58. Dcat.warning('页面将在 {$this->sWait} 秒后前往'+go_url, null, {
  59. timeOut: 5000 // 5秒后自动消失
  60. });
  61. }
  62. }
  63. JS;
  64. Admin::script($script);
  65. }
  66. }