UserExpireAutoWarning.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 static $systemConfig;
  12. protected $signature = 'userExpireAutoWarning';
  13. protected $description = '用户临近到期自动发邮件提醒';
  14. public function __construct() {
  15. parent::__construct();
  16. self::$systemConfig = Helpers::systemConfig();
  17. }
  18. public function handle() {
  19. $jobStartTime = microtime(true);
  20. // 用户临近到期自动发邮件提醒
  21. if(self::$systemConfig['expire_warning']){
  22. $this->userExpireWarning();
  23. }
  24. $jobEndTime = microtime(true);
  25. $jobUsedTime = round(($jobEndTime - $jobStartTime), 4);
  26. Log::info('---【'.$this->description.'】完成---,耗时'.$jobUsedTime.'秒');
  27. }
  28. private function userExpireWarning() {
  29. // 只取SSR没被禁用的用户,其他不用管
  30. $userList = User::query()->whereEnable(1)->get();
  31. foreach($userList as $user){
  32. // 用户名不是邮箱的跳过
  33. if(false === filter_var($user->email, FILTER_VALIDATE_EMAIL)){
  34. continue;
  35. }
  36. // 计算剩余可用时间
  37. $lastCanUseDays = ceil(round(strtotime($user->expire_time) - strtotime(date('Y-m-d H:i:s'))) / Day);
  38. if($lastCanUseDays == 0){
  39. $title = '账号过期提醒';
  40. $content = '您的账号将于今天晚上【24:00】过期。';
  41. $logId = Helpers::addNotificationLog($title, $content, 1, $user->email);
  42. Mail::to($user->email)->send(new userExpireWarningToday($logId));
  43. }elseif($lastCanUseDays > 0 && $lastCanUseDays <= self::$systemConfig['expire_days']){
  44. $title = '账号过期提醒';
  45. $content = '您的账号还剩'.$lastCanUseDays.'天即将过期。';
  46. $logId = Helpers::addNotificationLog($title, $content, 1, $user->email);
  47. Mail::to($user->email)->send(new userExpireWarning($logId, $lastCanUseDays));
  48. }
  49. }
  50. }
  51. }