SendRemindMail.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 > time()) return;
  45. if ($user->expired_at - 86400 > time()) return;
  46. SendEmail::dispatch([
  47. 'email' => $user->email,
  48. 'subject' => '在' . config('v2board.app_name', 'V2board') . '的服务即将到期',
  49. 'template_name' => 'mail.sendRemindExpire',
  50. 'template_value' => [
  51. 'name' => config('v2board.app_name', 'V2Board')
  52. ]
  53. ]);
  54. }
  55. private function remindTraffic ($user) {
  56. if (!$this->remindTrafficIsWarnValue(($user->u + $user->d), $user->transfer_enable)) return;
  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. ]);
  69. }
  70. private function remindTrafficIsWarnValue ($ud, $transfer_enable) {
  71. if ($ud <= 0) return false;
  72. if (($ud / $transfer_enable * 100) < 80) return false;
  73. return true;
  74. }
  75. }