v2board/app/Services/MailService.php

59 lines
1.9 KiB
PHP
Raw Normal View History

2020-05-10 19:04:16 +08:00
<?php
namespace App\Services;
2020-09-23 22:40:20 +08:00
use App\Jobs\SendEmailJob;
use App\Models\User;
2020-09-23 23:15:43 +08:00
use App\Utils\CacheKey;
use Illuminate\Support\Facades\Cache;
2020-09-23 22:40:20 +08:00
2020-05-10 19:04:16 +08:00
class MailService
{
2020-09-23 22:40:20 +08:00
public function remindTraffic (User $user)
{
if (!$user->remind_traffic) return;
2021-08-25 17:06:08 +08:00
if (!$this->remindTrafficIsWarnValue($user->u, $user->d, $user->transfer_enable)) return;
2020-09-23 23:15:43 +08:00
$flag = CacheKey::get('LAST_SEND_EMAIL_REMIND_TRAFFIC', $user->id);
if (Cache::get($flag)) return;
if (!Cache::put($flag, 1, 24 * 3600)) return;
2020-09-23 22:40:20 +08:00
SendEmailJob::dispatch([
'email' => $user->email,
2021-08-28 14:32:38 +08:00
'subject' => __('The traffic usage in :app_name has reached 80%', [
'app_name' => config('v2board.app_name', 'V2board')
]),
2020-09-23 22:40:20 +08:00
'template_name' => 'remindTraffic',
'template_value' => [
'name' => config('v2board.app_name', 'V2Board'),
'url' => config('v2board.app_url')
]
]);
}
2021-08-28 14:39:10 +08:00
public function remindExpire(User $user)
{
2021-08-29 11:56:55 +08:00
if (!($user->expired_at !== NULL && ($user->expired_at - 86400) < time() && $user->expired_at > time())) return;
2021-08-28 14:39:10 +08:00
SendEmailJob::dispatch([
'email' => $user->email,
'subject' => __('The service in :app_name is about to expire', [
'app_name' => config('v2board.app_name', 'V2board')
]),
'template_name' => 'remindExpire',
'template_value' => [
'name' => config('v2board.app_name', 'V2Board'),
'url' => config('v2board.app_url')
]
]);
}
2021-08-25 17:06:08 +08:00
private function remindTrafficIsWarnValue($u, $d, $transfer_enable)
2020-09-23 22:40:20 +08:00
{
2021-08-25 17:06:08 +08:00
$ud = $u + $d;
2020-11-07 16:04:42 +08:00
if (!$ud) return false;
2020-11-07 15:44:57 +08:00
if (!$transfer_enable) return false;
$percentage = ($ud / $transfer_enable) * 100;
2020-09-23 23:15:43 +08:00
if ($percentage < 80) return false;
if ($percentage >= 100) return false;
2020-09-23 22:40:20 +08:00
return true;
}
2020-05-10 19:04:16 +08:00
}