UserExpireAutoWarning.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Components\Helpers;
  4. use Illuminate\Console\Command;
  5. use App\Http\Models\User;
  6. use App\Http\Models\EmailLog;
  7. use App\Mail\userExpireWarning;
  8. use App\Mail\userExpireWarningToday;
  9. use Mail;
  10. use Log;
  11. class UserExpireAutoWarning extends Command
  12. {
  13. protected $signature = 'userExpireAutoWarning';
  14. protected $description = '用户临近到期自动发邮件提醒';
  15. protected static $systemConfig;
  16. public function __construct()
  17. {
  18. parent::__construct();
  19. self::$systemConfig = Helpers::systemConfig();
  20. }
  21. public function handle()
  22. {
  23. $jobStartTime = microtime(true);
  24. // 用户临近到期自动发邮件提醒
  25. if (self::$systemConfig['expire_warning']) {
  26. $this->userExpireWarning();
  27. }
  28. $jobEndTime = microtime(true);
  29. $jobUsedTime = round(($jobEndTime - $jobStartTime), 4);
  30. Log::info('执行定时任务【' . $this->description . '】,耗时' . $jobUsedTime . '秒');
  31. }
  32. private function userExpireWarning()
  33. {
  34. // 只取SSR没被禁用的用户,其他不用管
  35. $userList = User::query()->where('enable', 1)->get();
  36. foreach ($userList as $user) {
  37. // 用户名不是邮箱的跳过
  38. if (false === filter_var($user->username, FILTER_VALIDATE_EMAIL)) {
  39. continue;
  40. }
  41. // 计算剩余可用时间
  42. $lastCanUseDays = ceil(round(strtotime($user->expire_time) - strtotime(date('Y-m-d H:i:s'))) / 3600 / 24);
  43. if ($lastCanUseDays == 0) {
  44. $title = '账号过期提醒';
  45. $content = '您的账号将于今天晚上【24:00】过期。';
  46. try {
  47. Mail::to($user->username)->send(new userExpireWarningToday(self::$systemConfig['website_name']));
  48. $this->sendEmailLog($user->id, $title, $content);
  49. } catch (\Exception $e) {
  50. $this->sendEmailLog($user->id, $title, $content, 0, $e->getMessage());
  51. }
  52. } elseif ($lastCanUseDays > 0 && $lastCanUseDays <= self::$systemConfig['expire_days']) {
  53. $title = '账号过期提醒';
  54. $content = '您的账号还剩' . $lastCanUseDays . '天即将过期。';
  55. try {
  56. Mail::to($user->username)->send(new userExpireWarning(self::$systemConfig['website_name'], $lastCanUseDays));
  57. $this->sendEmailLog($user->id, $title, $content);
  58. } catch (\Exception $e) {
  59. $this->sendEmailLog($user->id, $title, $content, 0, $e->getMessage());
  60. }
  61. }
  62. }
  63. }
  64. /**
  65. * 写入邮件发送日志
  66. *
  67. * @param int $user_id 用户ID
  68. * @param string $title 标题
  69. * @param string $content 内容
  70. * @param int $status 投递状态
  71. * @param string $error 投递失败时记录的异常信息
  72. */
  73. private function sendEmailLog($user_id, $title, $content, $status = 1, $error = '')
  74. {
  75. $emailLogObj = new EmailLog();
  76. $emailLogObj->user_id = $user_id;
  77. $emailLogObj->title = $title;
  78. $emailLogObj->content = $content;
  79. $emailLogObj->status = $status;
  80. $emailLogObj->error = $error;
  81. $emailLogObj->created_at = date('Y-m-d H:i:s');
  82. $emailLogObj->save();
  83. }
  84. }