UserExpireAutoWarning.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Components\Helpers;
  4. use App\Mail\userExpireWarning;
  5. use App\Mail\userExpireWarningToday;
  6. use App\Models\User;
  7. use Illuminate\Console\Command;
  8. use Log;
  9. use Mail;
  10. class UserExpireAutoWarning extends Command {
  11. protected $signature = 'userExpireAutoWarning';
  12. protected $description = '用户临近到期自动发邮件提醒';
  13. public function handle(): void {
  14. $jobStartTime = microtime(true);
  15. // 用户临近到期自动发邮件提醒
  16. if(sysConfig('expire_warning')){
  17. $this->userExpireWarning();
  18. }
  19. $jobEndTime = microtime(true);
  20. $jobUsedTime = round(($jobEndTime - $jobStartTime), 4);
  21. Log::info('---【'.$this->description.'】完成---,耗时'.$jobUsedTime.'秒');
  22. }
  23. private function userExpireWarning(): void {
  24. $expireDays = sysConfig('expire_days');
  25. // 只取SSR没被禁用的用户,其他不用管
  26. foreach(User::whereEnable(1)->get() as $user){
  27. // 用户名不是邮箱的跳过
  28. if(false === filter_var($user->email, FILTER_VALIDATE_EMAIL)){
  29. continue;
  30. }
  31. // 计算剩余可用时间
  32. $lastCanUseDays = Helpers::daysToNow($user->expired_at);
  33. if($lastCanUseDays == 0){
  34. $title = '账号过期提醒';
  35. $content = '您的账号将于今天晚上【24:00】过期。';
  36. $logId = Helpers::addNotificationLog($title, $content, 1, $user->email);
  37. Mail::to($user->email)->send(new userExpireWarningToday($logId));
  38. }elseif($lastCanUseDays > 0 && $lastCanUseDays <= $expireDays){
  39. $title = '账号过期提醒';
  40. $content = '您的账号还剩'.$lastCanUseDays.'天即将过期。';
  41. $logId = Helpers::addNotificationLog($title, $content, 1, $user->email);
  42. Mail::to($user->email)->send(new userExpireWarning($logId, $lastCanUseDays));
  43. }
  44. }
  45. }
  46. }