Skip to content

Commit

Permalink
更新流量提醒邮件规则
Browse files Browse the repository at this point in the history
重置周期内只发送一次邮件提醒
长期有效的一个月只提醒一次
  • Loading branch information
FantajiNeko committed Jun 14, 2024
1 parent 86e5cbc commit 99cb8ac
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
12 changes: 12 additions & 0 deletions app/Console/Commands/ResetTraffic.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)); //重置流量时清除 最后发送流量邮件提醒 标记
}
}
}

Expand All @@ -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)); //重置流量时清除 最后发送流量邮件提醒 标记
}
}
}

Expand All @@ -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)); //重置流量时清除 最后发送流量邮件提醒 标记
}
}
}
21 changes: 17 additions & 4 deletions app/Services/MailService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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%', [
Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions app/Services/OrderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 99cb8ac

Please sign in to comment.