UserTrafficAutoWarning.php 1.4 KB

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