fuc-new/src/Services/Notification.php
2026-05-08 16:08:40 +08:00

143 lines
4.3 KiB
PHP
Executable File

<?php
declare(strict_types=1);
namespace App\Services;
use App\Models\EmailQueue;
use App\Models\User;
use GuzzleHttp\Exception\GuzzleException;
use Psr\Http\Client\ClientExceptionInterface;
use Telegram\Bot\Exceptions\TelegramSDKException;
use App\Services\MailHub;
final class Notification
{
/**
* @throws GuzzleException
* @throws TelegramSDKException
* @throws ClientExceptionInterface
*/
// public static function notifyAdmin($title = '', $msg = '', $template = 'warn.tpl'): void
// {
// $admins = (new User())->where('is_admin', 1)->get();
//
// foreach ($admins as $admin) {
// if ($admin->contact_method === 1 || $admin->im_type === 0) {
// Mail::send(
// $admin->email,
// $title,
// $template,
// [
// 'user' => $admin,
// 'title' => $title,
// 'text' => $msg,
// ]
// );
// } else {
// IM::send($admin->im_value, $msg, $admin->im_type);
// }
// }
// }
public static function notifyAdmin($title = '', $msg = '', $template = 'warn.tpl', $scene = ''): void
{
$admins = (new User())->where('is_admin', 1)->get();
foreach ($admins as $admin) {
if ($admin->contact_method === 1 || $admin->im_type === 0) {
MailHub::admin($title, [
'user' => $admin,
'title' => $title,
'text' => $msg,
])->to($admin->email);
} else {
IM::send($admin->im_value, $msg, $admin->im_type);
}
}
}
/**
* @throws GuzzleException$scene
* @throws TelegramSDKException
* @throws ClientExceptionInterface
*/
// public static function notifyUser($user, $title = '', $msg = '', $template = 'warn.tpl'): void
// {
// if ($user->contact_method === 1 || $user->im_type === 0) {
// $array = [
// 'user' => $user,
// 'title' => $title,
// 'text' => $msg,
// ];
//
// (new EmailQueue())->add($user->email, $title, $template, $array);
// } else {
// IM::send($user->im_value, $msg, $user->im_type);
// }
// }
public static function notifyUser($user, $title = '', $msg = '', $template = 'warn.tpl', $scene = ''): void
{
if ($user->contact_method === 1 || $user->im_type === 0) {
$array = [
'user' => $user,
'title' => $title,
'text' => $msg,
];
(new EmailQueue())->add($user->email, $title, $template, $array, $scene);
} else {
IM::send($user->im_value, $msg, $user->im_type);
}
}
/**
* @throws GuzzleException
* @throws TelegramSDKException
*/
// public static function notifyAllUser($title = '', $msg = '', $template = 'warn.tpl', $scene = ''): void
// {
// $users = User::all();
//
// foreach ($users as $user) {
// if ($user->contact_method === 1 || $user->im_type === 0) {
// $array = [
// 'user' => $user,
// 'title' => $title,
// 'text' => $msg,
// ];
//
// (new EmailQueue())->add($user->email, $title, $template, $array);
// } else {
// IM::send($user->im_value, $msg, $user->im_type);
// }
// }
// }
public static function notifyAllUser($title = '', $msg = '', $template = 'warn.tpl', $scene = ''): void
{
$users = User::all();
foreach ($users as $user) {
if ($user->contact_method === 1 || $user->im_type === 0) {
$array = [
'user' => $user,
'title' => $title,
'text' => $msg,
];
(new EmailQueue())->add($user->email, $title, $template, $array, $scene);
} else {
IM::send($user->im_value, $msg, $user->im_type);
}
}
}
}