UserTrafficAutoWarning.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Components\Helpers;
  4. use App\Mail\userTrafficWarning;
  5. use App\Models\User;
  6. use Illuminate\Console\Command;
  7. use Log;
  8. use Mail;
  9. class UserTrafficAutoWarning extends Command
  10. {
  11. protected $signature = 'userTrafficAutoWarning';
  12. protected $description = '用户流量超过警告阈值自动发邮件提醒';
  13. public function handle(): void
  14. {
  15. $jobStartTime = microtime(true);
  16. // 用户流量超过警告阈值自动发邮件提醒
  17. if (sysConfig('traffic_warning')) {
  18. $this->userTrafficWarning();
  19. }
  20. $jobEndTime = microtime(true);
  21. $jobUsedTime = round(($jobEndTime - $jobStartTime), 4);
  22. Log::info('---【'.$this->description.'】完成---,耗时'.$jobUsedTime.'秒');
  23. }
  24. // 用户流量超过警告阈值自动发邮件提醒
  25. private function userTrafficWarning(): void
  26. {
  27. $trafficWarningPercent = sysConfig('traffic_warning_percent');
  28. foreach (User::activeUser()->where('transfer_enable', '>', 0)->get() as $user) {
  29. // 用户名不是邮箱的跳过
  30. if (false === filter_var($user->email, FILTER_VALIDATE_EMAIL)) {
  31. continue;
  32. }
  33. $usedPercent = $user->used_traffic_percentage * 100; // 已使用流量百分比
  34. if ($usedPercent >= $trafficWarningPercent) {
  35. $logId = Helpers::addNotificationLog('流量提醒', '流量已使用:'.$usedPercent.'%,请保持关注。', 1, $user->email);
  36. Mail::to($user->email)->send(new userTrafficWarning($logId, $usedPercent));
  37. }
  38. }
  39. }
  40. }