SendRemindMail.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\User;
  5. use App\Models\MailLog;
  6. use App\Jobs\SendEmailJob;
  7. class SendRemindMail extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'send:remindMail';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = '发送提醒邮件';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return mixed
  34. */
  35. public function handle()
  36. {
  37. $users = User::all();
  38. foreach ($users as $user) {
  39. if ($user->remind_expire) $this->remindExpire($user);
  40. if ($user->remind_traffic) $this->remindTraffic($user);
  41. }
  42. }
  43. private function remindExpire($user)
  44. {
  45. if ($user->expired_at !== NULL && ($user->expired_at - 86400) < time() && $user->expired_at > time()) {
  46. SendEmailJob::dispatch([
  47. 'email' => $user->email,
  48. 'subject' => '在' . config('v2board.app_name', 'V2board') . '的服务即将到期',
  49. 'template_name' => 'remindExpire',
  50. 'template_value' => [
  51. 'name' => config('v2board.app_name', 'V2Board'),
  52. 'url' => config('v2board.app_url')
  53. ]
  54. ]);
  55. }
  56. }
  57. private function remindTraffic($user)
  58. {
  59. if ($this->remindTrafficIsWarnValue(($user->u + $user->d), $user->transfer_enable)) {
  60. $sendCount = MailLog::where('created_at', '>=', strtotime(date('Y-m-1')))
  61. ->where('template_name', 'like', '%remindTraffic%')
  62. ->count();
  63. if ($sendCount > 0) return;
  64. SendEmailJob::dispatch([
  65. 'email' => $user->email,
  66. 'subject' => '在' . config('v2board.app_name', 'V2board') . '的流量使用已达到80%',
  67. 'template_name' => 'remindTraffic',
  68. 'template_value' => [
  69. 'name' => config('v2board.app_name', 'V2Board'),
  70. 'url' => config('v2board.app_url')
  71. ]
  72. ]);
  73. }
  74. }
  75. private function remindTrafficIsWarnValue($ud, $transfer_enable)
  76. {
  77. if ($ud <= 0) return false;
  78. if (($ud / $transfer_enable * 100) < 80) return false;
  79. return true;
  80. }
  81. }