UserExpireAutoWarning.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. {
  12. protected $signature = 'userExpireAutoWarning';
  13. protected $description = '用户临近到期自动发邮件提醒';
  14. public function handle(): void
  15. {
  16. $jobStartTime = microtime(true);
  17. // 用户临近到期自动发邮件提醒
  18. if (sysConfig('expire_warning')) {
  19. $this->userExpireWarning();
  20. }
  21. $jobEndTime = microtime(true);
  22. $jobUsedTime = round(($jobEndTime - $jobStartTime), 4);
  23. Log::info(
  24. '---【' . $this->description . '】完成---,耗时' . $jobUsedTime . '秒'
  25. );
  26. }
  27. private function userExpireWarning(): void
  28. {
  29. $expireDays = sysConfig('expire_days');
  30. // 只取SSR没被禁用的用户,其他不用管
  31. foreach (User::whereEnable(1)->get() as $user) {
  32. // 用户名不是邮箱的跳过
  33. if (false === filter_var($user->email, FILTER_VALIDATE_EMAIL)) {
  34. continue;
  35. }
  36. // 计算剩余可用时间
  37. $lastCanUseDays = Helpers::daysToNow($user->expired_at);
  38. if ($lastCanUseDays == 0) {
  39. $title = '账号过期提醒';
  40. $content = '您的账号将于今天晚上【24:00】过期。';
  41. $logId = Helpers::addNotificationLog(
  42. $title,
  43. $content,
  44. 1,
  45. $user->email
  46. );
  47. Mail::to($user->email)->send(
  48. new userExpireWarningToday($logId)
  49. );
  50. } elseif ($lastCanUseDays > 0 && $lastCanUseDays <= $expireDays) {
  51. $title = '账号过期提醒';
  52. $content = '您的账号还剩' . $lastCanUseDays . '天即将过期。';
  53. $logId = Helpers::addNotificationLog(
  54. $title,
  55. $content,
  56. 1,
  57. $user->email
  58. );
  59. Mail::to($user->email)->send(
  60. new userExpireWarning($logId, $lastCanUseDays)
  61. );
  62. }
  63. }
  64. }
  65. }