|
@@ -20,6 +20,7 @@ use App\Services\IM\Telegram;
|
|
|
use App\Utils\Tools;
|
|
|
use DateTime;
|
|
|
use Exception;
|
|
|
+use GuzzleHttp\Exception\GuzzleException;
|
|
|
use Psr\Http\Client\ClientExceptionInterface;
|
|
|
use Telegram\Bot\Exceptions\TelegramSDKException;
|
|
|
use function array_map;
|
|
@@ -31,7 +32,7 @@ use function strtotime;
|
|
|
use function time;
|
|
|
use const PHP_EOL;
|
|
|
|
|
|
-final class CronJob
|
|
|
+final class Cron
|
|
|
{
|
|
|
public static function addTrafficLog(): void
|
|
|
{
|
|
@@ -97,13 +98,9 @@ final class CronJob
|
|
|
' 检测到 ' . User::where('is_inactive', 1)->count() . ' 个账户处于闲置状态' . PHP_EOL;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * @throws TelegramSDKException
|
|
|
- */
|
|
|
public static function detectNodeOffline(): void
|
|
|
{
|
|
|
$nodes = Node::where('type', 1)->get();
|
|
|
- $adminUsers = User::where('is_admin', 1)->get();
|
|
|
|
|
|
foreach ($nodes as $node) {
|
|
|
if ($node->getNodeOnlineStatus() >= 0 && $node->online === 1) {
|
|
@@ -111,26 +108,29 @@ final class CronJob
|
|
|
}
|
|
|
|
|
|
if ($node->getNodeOnlineStatus() === -1 && $node->online === 1) {
|
|
|
- foreach ($adminUsers as $user) {
|
|
|
- echo 'Send Node Offline Email to admin user: ' . $user->id . PHP_EOL;
|
|
|
- $user->sendMail(
|
|
|
+ echo 'Send Node Offline Email to admin users' . PHP_EOL;
|
|
|
+
|
|
|
+ try {
|
|
|
+ Notification::notifyAdmin(
|
|
|
$_ENV['appName'] . '-系统警告',
|
|
|
- 'warn.tpl',
|
|
|
- [
|
|
|
- 'text' => '管理员你好,系统发现节点 ' . $node->name . ' 掉线了,请你及时处理。',
|
|
|
- ],
|
|
|
- [],
|
|
|
- false
|
|
|
+ '管理员你好,系统发现节点 ' . $node->name . ' 掉线了,请你及时处理。'
|
|
|
);
|
|
|
+ } catch (GuzzleException|ClientExceptionInterface|TelegramSDKException $e) {
|
|
|
+ echo $e->getMessage() . PHP_EOL;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (Setting::obtain('telegram_node_offline')) {
|
|
|
$notice_text = str_replace(
|
|
|
'%node_name%',
|
|
|
$node->name,
|
|
|
Setting::obtain('telegram_node_offline_text')
|
|
|
);
|
|
|
- }
|
|
|
|
|
|
- if (Setting::obtain('telegram_node_offline')) {
|
|
|
- (new Telegram())->send(0, $notice_text);
|
|
|
+ try {
|
|
|
+ (new Telegram())->send(0, $notice_text);
|
|
|
+ } catch (TelegramSDKException $e) {
|
|
|
+ echo $e->getMessage();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
$node->online = 0;
|
|
@@ -140,32 +140,36 @@ final class CronJob
|
|
|
}
|
|
|
|
|
|
if ($node->getNodeOnlineStatus() === 1 && $node->online === 0) {
|
|
|
- foreach ($adminUsers as $user) {
|
|
|
- echo 'Send Node Online Email to admin user: ' . $user->id . PHP_EOL;
|
|
|
- $user->sendMail(
|
|
|
+ echo 'Send Node Online Email to admin user' . PHP_EOL;
|
|
|
+
|
|
|
+ try {
|
|
|
+ Notification::notifyAdmin(
|
|
|
$_ENV['appName'] . '-系统提示',
|
|
|
- 'warn.tpl',
|
|
|
- [
|
|
|
- 'text' => '管理员你好,系统发现节点 ' . $node->name . ' 恢复上线了。',
|
|
|
- ],
|
|
|
- [],
|
|
|
- false
|
|
|
+ '管理员你好,系统发现节点 ' . $node->name . ' 恢复上线了。'
|
|
|
);
|
|
|
+ } catch (GuzzleException|ClientExceptionInterface|TelegramSDKException $e) {
|
|
|
+ echo $e->getMessage() . PHP_EOL;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (Setting::obtain('telegram_node_online')) {
|
|
|
$notice_text = str_replace(
|
|
|
'%node_name%',
|
|
|
$node->name,
|
|
|
Setting::obtain('telegram_node_online_text')
|
|
|
);
|
|
|
- }
|
|
|
|
|
|
- if (Setting::obtain('telegram_node_online')) {
|
|
|
- (new Telegram())->send(0, $notice_text);
|
|
|
+ try {
|
|
|
+ (new Telegram())->send(0, $notice_text);
|
|
|
+ } catch (TelegramSDKException $e) {
|
|
|
+ echo $e->getMessage();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
$node->online = 1;
|
|
|
$node->save();
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
echo Tools::toDateTime(time()) . ' 节点离线检测完成' . PHP_EOL;
|
|
|
}
|
|
|
|
|
@@ -180,18 +184,14 @@ final class CronJob
|
|
|
|
|
|
if ($reset_traffic >= 0) {
|
|
|
$user->transfer_enable = Tools::toGB($reset_traffic);
|
|
|
- $text .= '流量已经被重置为' . $reset_traffic . 'GB';
|
|
|
+ $text .= '流量已经被重置为' . $reset_traffic . 'GB。';
|
|
|
}
|
|
|
|
|
|
- $user->sendMail(
|
|
|
- $_ENV['appName'] . '-你的账户等级已经过期了',
|
|
|
- 'warn.tpl',
|
|
|
- [
|
|
|
- 'text' => $text,
|
|
|
- ],
|
|
|
- [],
|
|
|
- true
|
|
|
- );
|
|
|
+ try {
|
|
|
+ Notification::notifyUser($user, $_ENV['appName'] . '-你的账号等级已经过期了', $text);
|
|
|
+ } catch (GuzzleException|ClientExceptionInterface|TelegramSDKException $e) {
|
|
|
+ echo $e->getMessage() . PHP_EOL;
|
|
|
+ }
|
|
|
|
|
|
$user->u = 0;
|
|
|
$user->d = 0;
|
|
@@ -439,23 +439,23 @@ final class CronJob
|
|
|
$freeUsers = User::where('class', 0)->where('auto_reset_day', date('d'))->get();
|
|
|
|
|
|
foreach ($freeUsers as $user) {
|
|
|
+ try {
|
|
|
+ Notification::notifyUser(
|
|
|
+ $user,
|
|
|
+ $_ENV['appName'] . '-免费流量重置通知',
|
|
|
+ '你好,你的免费流量已经被重置为' . $user->auto_reset_bandwidth . 'GB。'
|
|
|
+ );
|
|
|
+ } catch (GuzzleException|ClientExceptionInterface|TelegramSDKException $e) {
|
|
|
+ echo $e->getMessage() . PHP_EOL;
|
|
|
+ }
|
|
|
+
|
|
|
$user->u = 0;
|
|
|
$user->d = 0;
|
|
|
$user->transfer_enable = $user->auto_reset_bandwidth * 1024 * 1024 * 1024;
|
|
|
$user->save();
|
|
|
-
|
|
|
- $user->sendMail(
|
|
|
- $_ENV['appName'] . '-你的免费流量被重置了',
|
|
|
- 'warn.tpl',
|
|
|
- [
|
|
|
- 'text' => '你好,你的免费流量已经被重置为' . $user->auto_reset_bandwidth . 'GB',
|
|
|
- ],
|
|
|
- [],
|
|
|
- true
|
|
|
- );
|
|
|
}
|
|
|
|
|
|
- echo Tools::toDateTime(time()) . ' 重设免费用户流量完成' . PHP_EOL;
|
|
|
+ echo Tools::toDateTime(time()) . ' 免费用户流量重置完成' . PHP_EOL;
|
|
|
}
|
|
|
|
|
|
public static function sendDailyFinanceMail(): void
|
|
@@ -475,19 +475,16 @@ final class CronJob
|
|
|
|
|
|
$text_html .= '</table>';
|
|
|
$text_html .= '<br>昨日总收入笔数:' . count($paylists) . '<br>昨日总收入金额:' . $paylists->sum('total');
|
|
|
- $adminUser = User::where('is_admin', '=', '1')->get();
|
|
|
+ echo 'Sending daily finance email to admin user' . PHP_EOL;
|
|
|
|
|
|
- foreach ($adminUser as $user) {
|
|
|
- echo 'Sending daily finance email to admin user: ' . $user->id . PHP_EOL;
|
|
|
- $user->sendMail(
|
|
|
- $_ENV['appName'] . '-财务日报',
|
|
|
- 'finance.tpl',
|
|
|
- [
|
|
|
- 'title' => '财务日报',
|
|
|
- 'text' => $text_html,
|
|
|
- ],
|
|
|
- []
|
|
|
+ try {
|
|
|
+ Notification::notifyAdmin(
|
|
|
+ '财务日报',
|
|
|
+ $text_html,
|
|
|
+ 'finance.tpl'
|
|
|
);
|
|
|
+ } catch (GuzzleException|ClientExceptionInterface|TelegramSDKException $e) {
|
|
|
+ echo $e->getMessage() . PHP_EOL;
|
|
|
}
|
|
|
|
|
|
echo Tools::toDateTime(time()) . ' 成功发送财务日报' . PHP_EOL;
|
|
@@ -501,19 +498,16 @@ final class CronJob
|
|
|
->get();
|
|
|
|
|
|
$text_html = '<br>上周总收入笔数:' . count($paylists) . '<br>上周总收入金额:' . $paylists->sum('total');
|
|
|
- $adminUser = User::where('is_admin', '=', '1')->get();
|
|
|
+ echo 'Sending weekly finance email to admin user' . PHP_EOL;
|
|
|
|
|
|
- foreach ($adminUser as $user) {
|
|
|
- echo 'Sending weekly finance email to admin user: ' . $user->id . PHP_EOL;
|
|
|
- $user->sendMail(
|
|
|
- $_ENV['appName'] . '-财务周报',
|
|
|
- 'finance.tpl',
|
|
|
- [
|
|
|
- 'title' => '财务周报',
|
|
|
- 'text' => $text_html,
|
|
|
- ],
|
|
|
- []
|
|
|
+ try {
|
|
|
+ Notification::notifyAdmin(
|
|
|
+ '财务周报',
|
|
|
+ $text_html,
|
|
|
+ 'finance.tpl'
|
|
|
);
|
|
|
+ } catch (GuzzleException|ClientExceptionInterface|TelegramSDKException $e) {
|
|
|
+ echo $e->getMessage() . PHP_EOL;
|
|
|
}
|
|
|
|
|
|
echo Tools::toDateTime(time()) . ' 成功发送财务周报' . PHP_EOL;
|
|
@@ -527,19 +521,16 @@ final class CronJob
|
|
|
->get();
|
|
|
|
|
|
$text_html = '<br>上月总收入笔数:' . count($paylists) . '<br>上月总收入金额:' . $paylists->sum('total');
|
|
|
- $adminUser = User::where('is_admin', '=', '1')->get();
|
|
|
+ echo 'Sending monthly finance email to admin user' . PHP_EOL;
|
|
|
|
|
|
- foreach ($adminUser as $user) {
|
|
|
- echo 'Sending monthly finance email to admin user: ' . $user->id . PHP_EOL;
|
|
|
- $user->sendMail(
|
|
|
- $_ENV['appName'] . '-财务月报',
|
|
|
- 'finance.tpl',
|
|
|
- [
|
|
|
- 'title' => '财务月报',
|
|
|
- 'text' => $text_html,
|
|
|
- ],
|
|
|
- []
|
|
|
+ try {
|
|
|
+ Notification::notifyAdmin(
|
|
|
+ '财务月报',
|
|
|
+ $text_html,
|
|
|
+ 'finance.tpl'
|
|
|
);
|
|
|
+ } catch (GuzzleException|ClientExceptionInterface|TelegramSDKException $e) {
|
|
|
+ echo $e->getMessage() . PHP_EOL;
|
|
|
}
|
|
|
|
|
|
echo Tools::toDateTime(time()) . ' 成功发送财务月报' . PHP_EOL;
|
|
@@ -567,19 +558,20 @@ final class CronJob
|
|
|
}
|
|
|
|
|
|
if ($under_limit && ! $user->traffic_notified) {
|
|
|
- $result = $user->sendMail(
|
|
|
- $_ENV['appName'] . '-你的剩余流量过低',
|
|
|
- 'warn.tpl',
|
|
|
- [
|
|
|
- 'text' => '你好,系统发现你剩余流量已经低于 ' . $_ENV['notify_limit_value'] . $unit_text . ' 。',
|
|
|
- ],
|
|
|
- [],
|
|
|
- true
|
|
|
- );
|
|
|
- if ($result) {
|
|
|
+ try {
|
|
|
+ Notification::notifyUser(
|
|
|
+ $user,
|
|
|
+ $_ENV['appName'] . '-你的剩余流量过低',
|
|
|
+ '你好,系统发现你剩余流量已经低于 ' . $_ENV['notify_limit_value'] . $unit_text . ' 。',
|
|
|
+ );
|
|
|
+
|
|
|
$user->traffic_notified = true;
|
|
|
- $user->save();
|
|
|
+ } catch (GuzzleException|ClientExceptionInterface|TelegramSDKException $e) {
|
|
|
+ $user->traffic_notified = false;
|
|
|
+ echo $e->getMessage() . PHP_EOL;
|
|
|
}
|
|
|
+
|
|
|
+ $user->save();
|
|
|
} elseif (! $under_limit && $user->traffic_notified) {
|
|
|
$user->traffic_notified = false;
|
|
|
$user->save();
|