UserExpireAutoWarning.php 1.9 KB

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