UserExpireWarning.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 UserExpireWarning extends Command
  8. {
  9. protected $signature = 'userExpireWarning';
  10. protected $description = '用户临近到期自动提醒';
  11. public function handle(): void
  12. {
  13. $jobTime = microtime(true);
  14. if (sysConfig('account_expire_notification')) {// 用户临近到期自动提醒
  15. $this->userExpireWarning();
  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 userExpireWarning(): void
  21. {
  22. // 只取没被禁用的用户,其他不用管
  23. User::whereEnable(1)
  24. ->where('expired_at', '<', date('Y-m-d', strtotime(sysConfig('expire_days').' days')))
  25. ->chunk(sysConfig('tasks_chunk'), function ($users) {
  26. foreach ($users as $user) {
  27. if (filter_var($user->username, FILTER_VALIDATE_EMAIL) === false) { // 用户账号不是邮箱的跳过
  28. continue;
  29. }
  30. $user->notify(new AccountExpire($user->expired_at->diffInDays()));
  31. }
  32. });
  33. }
  34. }