From 99cb8ac923d5722838753e39264f7d0f3971e703 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fantaj=C4=AB=20=C3=97=20Neko?= Date: Fri, 14 Jun 2024 16:02:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=B5=81=E9=87=8F=E6=8F=90?= =?UTF-8?q?=E9=86=92=E9=82=AE=E4=BB=B6=E8=A7=84=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 重置周期内只发送一次邮件提醒 长期有效的一个月只提醒一次 --- app/Console/Commands/ResetTraffic.php | 12 ++++++++++++ app/Services/MailService.php | 21 +++++++++++++++++---- app/Services/OrderService.php | 1 + 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/app/Console/Commands/ResetTraffic.php b/app/Console/Commands/ResetTraffic.php index 157e2882a..829adb705 100644 --- a/app/Console/Commands/ResetTraffic.php +++ b/app/Console/Commands/ResetTraffic.php @@ -119,6 +119,9 @@ private function resetByExpireYear($builder):void 'u' => 0, 'd' => 0 ]); + foreach ($users as $user_id) { + Cache::forget(CacheKey::get('LAST_SEND_EMAIL_REMIND_TRAFFIC', $user_id)); //重置流量时清除 最后发送流量邮件提醒 标记 + } } private function resetByYearFirstDay($builder):void @@ -128,6 +131,9 @@ private function resetByYearFirstDay($builder):void 'u' => 0, 'd' => 0 ]); + foreach ($builder->get() as $item) { + Cache::forget(CacheKey::get('LAST_SEND_EMAIL_REMIND_TRAFFIC', $item->id)); //重置流量时清除 最后发送流量邮件提醒 标记 + } } } @@ -138,6 +144,9 @@ private function resetByMonthFirstDay($builder):void 'u' => 0, 'd' => 0 ]); + foreach ($builder->get() as $item) { + Cache::forget(CacheKey::get('LAST_SEND_EMAIL_REMIND_TRAFFIC', $item->id)); //重置流量时清除 最后发送流量邮件提醒 标记 + } } } @@ -160,5 +169,8 @@ private function resetByExpireDay($builder):void 'u' => 0, 'd' => 0 ]); + foreach ($users as $user_id) { + Cache::forget(CacheKey::get('LAST_SEND_EMAIL_REMIND_TRAFFIC', $user_id)); //重置流量时清除 最后发送流量邮件提醒 标记 + } } } diff --git a/app/Services/MailService.php b/app/Services/MailService.php index 8e11fe74b..f917db2f6 100644 --- a/app/Services/MailService.php +++ b/app/Services/MailService.php @@ -12,10 +12,22 @@ class MailService public function remindTraffic (User $user) { if (!$user->remind_traffic) return; - if (!$this->remindTrafficIsWarnValue($user->u, $user->d, $user->transfer_enable)) return; + if (!$this->remindTrafficIsWarnValue($user->u, $user->d, $user->transfer_enable, 80)) return; $flag = CacheKey::get('LAST_SEND_EMAIL_REMIND_TRAFFIC', $user->id); if (Cache::get($flag)) return; - if (!Cache::put($flag, 1, 24 * 3600)) return; + $permanently_flag = 9999999999; //长期有效 + $expired_at = $user->expired_at; + if ($expired_at == NULL) $expired_at = $permanently_flag; //长期有效 + $nowTime = time(); + if ($nowTime >= $expired_at ) return; + if ($expired_at == $permanently_flag) { //判断是否长期有效 + $expired_at = 30 * 24 *3600; //一个月提醒一次 + } + else{ + $expired_at = $expired_at - $nowTime; //过期时间内提醒一次 + } + + if (!Cache::put($flag, 1, $expired_at)) return; SendEmailJob::dispatch([ 'email' => $user->email, 'subject' => __('The traffic usage in :app_name has reached 80%', [ @@ -45,13 +57,14 @@ public function remindExpire(User $user) ]); } - private function remindTrafficIsWarnValue($u, $d, $transfer_enable) + //warnValue 流量阈值 80,90,95 + private function remindTrafficIsWarnValue($u, $d, $transfer_enable, $warnValue) { $ud = $u + $d; if (!$ud) return false; if (!$transfer_enable) return false; $percentage = ($ud / $transfer_enable) * 100; - if ($percentage < 80) return false; + if ($percentage < $warnValue) return false; if ($percentage >= 100) return false; return true; } diff --git a/app/Services/OrderService.php b/app/Services/OrderService.php index 0133222e0..158e7e71d 100644 --- a/app/Services/OrderService.php +++ b/app/Services/OrderService.php @@ -260,6 +260,7 @@ private function buyByResetTraffic() { $this->user->u = 0; $this->user->d = 0; + Cache::forget(CacheKey::get('LAST_SEND_EMAIL_REMIND_TRAFFIC', $this->user->id)); //重置流量时清除 最后发送流量邮件提醒 标记 } private function buyByPeriod(Order $order, Plan $plan)