UserExpireAutoWarning.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\User;
  4. use App\Notifications\AccountExpire;
  5. use Illuminate\Console\Command;
  6. use Log;
  7. class UserExpireAutoWarning extends Command
  8. {
  9. protected $signature = 'userExpireAutoWarning';
  10. protected $description = '用户临近到期自动发邮件提醒';
  11. public function handle(): void
  12. {
  13. $jobStartTime = microtime(true);
  14. // 用户临近到期自动发邮件提醒
  15. if (sysConfig('account_expire_notification')) {
  16. $this->userExpireWarning();
  17. }
  18. $jobEndTime = microtime(true);
  19. $jobUsedTime = round(($jobEndTime - $jobStartTime), 4);
  20. Log::info('---【'.$this->description.'】完成---,耗时'.$jobUsedTime.'秒');
  21. }
  22. private function userExpireWarning(): void
  23. {
  24. // 只取SSR没被禁用的用户,其他不用管
  25. foreach (User::whereEnable(1)->where('expired_at', '<', date('Y-m-d H:i:s', strtotime(sysConfig('expire_days').' days')))->get() as $user) {
  26. // 用户名不是邮箱的跳过
  27. if (filter_var($user->email, FILTER_VALIDATE_EMAIL) === false) {
  28. continue;
  29. }
  30. $user->notify(new AccountExpire($user->expired_at));
  31. }
  32. }
  33. }