UserTrafficWarning.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 UserTrafficWarning extends Command
  8. {
  9. protected $signature = 'userTrafficWarning';
  10. protected $description = '用户流量超过警告阈值自动发邮件提醒';
  11. public function handle(): void
  12. {
  13. $jobTime = microtime(true);
  14. if (sysConfig('data_exhaust_notification')) { // 用户流量超过警告阈值提醒
  15. $this->userTrafficWarning();
  16. }
  17. $jobTime = round(microtime(true) - $jobTime, 4);
  18. Log::info(__('----「:job」Completed, Used :time seconds ----', ['job' => $this->description, 'time' => $jobTime]));
  19. }
  20. private function userTrafficWarning(): void
  21. { // 用户流量超过警告阈值提醒
  22. $trafficWarningPercent = sysConfig('traffic_warning_percent');
  23. User::activeUser()->where('transfer_enable', '>', 0)->chunk(config('tasks.chunk'), function ($users) use ($trafficWarningPercent) {
  24. foreach ($users as $user) {
  25. // 用户账号不是邮箱的跳过
  26. if (filter_var($user->username, FILTER_VALIDATE_EMAIL) === false) {
  27. continue;
  28. }
  29. $usedPercent = $user->used_traffic_percentage * 100; // 已使用流量百分比
  30. if ($usedPercent >= $trafficWarningPercent) {
  31. $user->notify(new DataExhaust($usedPercent));
  32. }
  33. }
  34. });
  35. }
  36. }