UserTrafficAutoWarning.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 static $systemConfig;
  11. protected $signature = 'userTrafficAutoWarning';
  12. protected $description = '用户流量超过警告阈值自动发邮件提醒';
  13. public function __construct() {
  14. parent::__construct();
  15. self::$systemConfig = Helpers::systemConfig();
  16. }
  17. public function handle() {
  18. $jobStartTime = microtime(true);
  19. // 用户流量超过警告阈值自动发邮件提醒
  20. if(self::$systemConfig['traffic_warning']){
  21. $this->userTrafficWarning();
  22. }
  23. $jobEndTime = microtime(true);
  24. $jobUsedTime = round(($jobEndTime - $jobStartTime), 4);
  25. Log::info('---【'.$this->description.'】完成---,耗时'.$jobUsedTime.'秒');
  26. }
  27. // 用户流量超过警告阈值自动发邮件提醒
  28. private function userTrafficWarning() {
  29. $userList = User::query()->where('status', '>=', 0)->whereEnable(1)->where('transfer_enable', '>', 0)->get();
  30. foreach($userList as $user){
  31. // 用户名不是邮箱的跳过
  32. if(false === filter_var($user->email, FILTER_VALIDATE_EMAIL)){
  33. continue;
  34. }
  35. $usedPercent = round(($user->d + $user->u) / $user->transfer_enable, 2) * 100; // 已使用流量百分比
  36. if($usedPercent >= self::$systemConfig['traffic_warning_percent']){
  37. $title = '流量提醒';
  38. $content = '流量已使用:'.$usedPercent.'%,请保持关注。';
  39. $logId = Helpers::addNotificationLog($title, $content, 1, $user->email);
  40. Mail::to($user->email)->send(new userTrafficWarning($logId, $usedPercent));
  41. }
  42. }
  43. }
  44. }