UserTrafficAutoWarning.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. protected $signature = 'userTrafficAutoWarning';
  11. protected $description = '用户流量超过警告阈值自动发邮件提醒';
  12. public function handle(): void {
  13. $jobStartTime = microtime(true);
  14. // 用户流量超过警告阈值自动发邮件提醒
  15. if(sysConfig('traffic_warning')){
  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. $trafficWarningPercent = sysConfig('traffic_warning_percent');
  25. foreach(User::activeUser()->where('transfer_enable', '>', 0)->get() as $user){
  26. // 用户名不是邮箱的跳过
  27. if(false === filter_var($user->email, FILTER_VALIDATE_EMAIL)){
  28. continue;
  29. }
  30. $usedPercent = round(($user->d + $user->u) / $user->transfer_enable, 2) * 100; // 已使用流量百分比
  31. if($usedPercent >= $trafficWarningPercent){
  32. $logId = Helpers::addNotificationLog("流量提醒", '流量已使用:'.$usedPercent.'%,请保持关注。', 1, $user->email);
  33. Mail::to($user->email)->send(new userTrafficWarning($logId, $usedPercent));
  34. }
  35. }
  36. }
  37. }