SendRemindMail.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. 'url' => config('v2board.app_url')
  52. ]
  53. ]);
  54. }
  55. }
  56. private function remindTraffic ($user) {
  57. if ($this->remindTrafficIsWarnValue(($user->u + $user->d), $user->transfer_enable)) {
  58. $sendCount = MailLog::where('created_at', '>=', strtotime(date('Y-m-1')))
  59. ->where('template_name', 'mail.sendRemindTraffic')
  60. ->count();
  61. if ($sendCount > 0) return;
  62. SendEmail::dispatch([
  63. 'email' => $user->email,
  64. 'subject' => '在' . config('v2board.app_name', 'V2board') . '的流量使用已达到80%',
  65. 'template_name' => 'mail.sendRemindTraffic',
  66. 'template_value' => [
  67. 'name' => config('v2board.app_name', 'V2Board'),
  68. 'url' => config('v2board.app_url')
  69. ]
  70. ]);
  71. }
  72. }
  73. private function remindTrafficIsWarnValue ($ud, $transfer_enable) {
  74. if ($ud <= 0) return false;
  75. if (($ud / $transfer_enable * 100) < 80) return false;
  76. return true;
  77. }
  78. }