SendRemindMail.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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\SendEmail;
  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. if (($user->expired_at - 86400) < time() && $user->expired_at > time()) {
  45. SendEmail::dispatch([
  46. 'email' => $user->email,
  47. 'subject' => '在' . config('v2board.app_name', 'V2board') . '的服务即将到期',
  48. 'template_name' => 'mail.sendRemindExpire',
  49. 'template_value' => [
  50. 'name' => config('v2board.app_name', 'V2Board')
  51. ]
  52. ])->onQueue('email_notify');
  53. }
  54. }
  55. private function remindTraffic ($user) {
  56. if ($this->remindTrafficIsWarnValue(($user->u + $user->d), $user->transfer_enable)) {
  57. $sendCount = MailLog::where('created_at', '>=', strtotime(date('Y-m-1')))
  58. ->where('template_name', 'mail.sendRemindTraffic')
  59. ->count();
  60. if ($sendCount > 0) return;
  61. SendEmail::dispatch([
  62. 'email' => $user->email,
  63. 'subject' => '在' . config('v2board.app_name', 'V2board') . '的流量使用已达到80%',
  64. 'template_name' => 'mail.sendRemindTraffic',
  65. 'template_value' => [
  66. 'name' => config('v2board.app_name', 'V2Board')
  67. ]
  68. ])->onQueue('email_notify');
  69. }
  70. }
  71. private function remindTrafficIsWarnValue ($ud, $transfer_enable) {
  72. if ($ud <= 0) return false;
  73. if (($ud / $transfer_enable * 100) < 80) return false;
  74. return true;
  75. }
  76. }