From 0e17a2d9a54208daa0ebaeeddc5cffc00f26218d Mon Sep 17 00:00:00 2001 From: augushong Date: Mon, 28 Feb 2022 22:20:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=AE=9A=E6=97=B6=E5=99=A8?= =?UTF-8?q?=E7=9A=84=E9=98=B2=E5=88=B7=E6=9C=BA=E5=88=B6=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/command/Timer.php | 26 +++++++++++---- app/common/controller/TimerController.php | 35 ++++++++++++++++++++ app/tools/controller/timer/ResetPassword.php | 12 +++++-- 3 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 app/common/controller/TimerController.php diff --git a/app/common/command/Timer.php b/app/common/command/Timer.php index 2284e75..994d6a8 100644 --- a/app/common/command/Timer.php +++ b/app/common/command/Timer.php @@ -28,11 +28,19 @@ class Timer extends Command // 指令输出 $output->writeln('start timer'); + + $site_domain = sysconfig('site', 'site_domain'); + + if (empty($site_domain)) { + $output->writeln('请前往后台设置站点域名(site_domain)配置项'); + return; + } + $client = new Client([ - 'base_uri' => sysconfig('site', 'site_domain'), + 'base_uri' => $site_domain, 'verify' => false, ]); - + while (true) { try { @@ -48,6 +56,10 @@ class Timer extends Command $name = $config_item['name']; + if ($name == 'http_demo' && !env('adminsystem.is_demo', false)) { + continue; + } + $cache_key = 'timer_' . $name; $cache_tag = 'system_timer'; @@ -74,10 +86,10 @@ class Timer extends Command } } - if (empty($list_promises)){ - + if (empty($list_promises)) { + $output->writeln(date('Y-m-d H:i:s') . ' no request'); - }else{ + } else { $results = Utils::unwrap($list_promises); $output->writeln(date('Y-m-d H:i:s') . ': request all finished'); } @@ -106,8 +118,8 @@ class Timer extends Command $data = array_merge($default, $config); - if ($data['frequency'] < 1) { - $data['frequency'] = 1; + if ($data['frequency'] < 0) { + $data['frequency'] = 0; } return $data; diff --git a/app/common/controller/TimerController.php b/app/common/controller/TimerController.php new file mode 100644 index 0000000..40bed2f --- /dev/null +++ b/app/common/controller/TimerController.php @@ -0,0 +1,35 @@ +frequency)) { + $this->protectVisit($this->frequency); + } + } + + protected function protectVisit(int $frequency) + { + + $cache_tag = 'timer_protect'; + + $cache_key = 'timer_protect_' . $this->request->url(); + + $last_exec_time = Cache::get($cache_key, 0); + + if ($last_exec_time >= time() - $frequency) { + + return $this->error('请不要频繁请求'); + } + + Cache::tag($cache_tag)->set($cache_key, time()); + } +} diff --git a/app/tools/controller/timer/ResetPassword.php b/app/tools/controller/timer/ResetPassword.php index 5269d89..cc3a0dc 100644 --- a/app/tools/controller/timer/ResetPassword.php +++ b/app/tools/controller/timer/ResetPassword.php @@ -4,13 +4,21 @@ declare(strict_types=1); namespace app\tools\controller\timer; -use app\common\controller\ToolsController; +use app\common\controller\TimerController; use think\facade\Console; -class ResetPassword extends ToolsController +class ResetPassword extends TimerController { + + protected $frequency = 600; + public function do() { + + if (!env('adminsystem.is_demo', false)) { + return $this->error('本功能只有在演示环境下在能使用', '', '/'); + } + $output = Console::call('admin:resetPassword', [ '--password=123456' ]);