Repository navigation
Expand file tree
/
Copy pathUserTrafficAutoWarning.php
More file actions
63 lines (52 loc) · 2.09 KB
/
Copy pathUserTrafficAutoWarning.php
File metadata and controls
63 lines (52 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
namespace App\Console\Commands;
use App\Components\Helpers;
use Illuminate\Console\Command;
use App\Http\Models\User;
use App\Mail\userTrafficWarning;
use Mail;
use Log;
class UserTrafficAutoWarning extends Command
{
protected $signature = 'userTrafficAutoWarning';
protected $description = '用户流量超过警告阈值自动发邮件提醒';
protected static $systemConfig;
public function __construct()
{
parent::__construct();
self::$systemConfig = Helpers::systemConfig();
}
public function handle()
{
$jobStartTime = microtime(true);
// 用户流量超过警告阈值自动发邮件提醒
if (self::$systemConfig['traffic_warning']) {
$this->userTrafficWarning();
}
$jobEndTime = microtime(true);
$jobUsedTime = round(($jobEndTime - $jobStartTime), 4);
Log::info('执行定时任务【' . $this->description . '】,耗时' . $jobUsedTime . '秒');
}
// 用户流量超过警告阈值自动发邮件提醒
private function userTrafficWarning()
{
$userList = User::query()->where('status', '>=', 0)->where('enable', 1)->where('transfer_enable', '>', 0)->get();
foreach ($userList as $user) {
// 用户名不是邮箱的跳过
if (false === filter_var($user->username, FILTER_VALIDATE_EMAIL)) {
continue;
}
$usedPercent = round(($user->d + $user->u) / $user->transfer_enable, 2) * 100; // 已使用流量百分比
if ($usedPercent >= self::$systemConfig['traffic_warning_percent']) {
$title = '流量提醒';
$content = '流量已使用:' . $usedPercent . '%,请保持关注。';
try {
Mail::to($user->username)->send(new userTrafficWarning($usedPercent));
Helpers::addEmailLog($user->username, $title, $content);
} catch (\Exception $e) {
Helpers::addEmailLog($user->username, $title, $content, 0, $e->getMessage());
}
}
}
}
}