114 lines
4.7 KiB
PHP
114 lines
4.7 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Admin;
|
||
|
||
use App\Http\Controllers\Controller;
|
||
use App\Services\SettingsService;
|
||
use App\Services\TelegramNotifier;
|
||
use Illuminate\Http\Request;
|
||
|
||
class SystemSettingsController extends Controller
|
||
{
|
||
public function index(SettingsService $settings, TelegramNotifier $telegram)
|
||
{
|
||
$form = $settings->effectiveForForm();
|
||
$token = trim((string) ($form['telegram.bot_token'] ?? ''));
|
||
$username = trim((string) ($form['telegram.bot_username'] ?? ''));
|
||
if ($token !== '' && $username === '') {
|
||
$fetched = $telegram->fetchBotUsername($token);
|
||
if ($fetched !== null) {
|
||
$username = ltrim($fetched, '@');
|
||
$settings->putMany(['telegram.bot_username' => $username]);
|
||
$form['telegram.bot_username'] = $username;
|
||
}
|
||
}
|
||
|
||
return view('admin.system.settings', [
|
||
'form' => $form,
|
||
'botUsername' => $telegram->formatBotUsername($form['telegram.bot_username'] ?? ''),
|
||
'botInviteUrl' => $telegram->inviteUrl($form['telegram.bot_username'] ?? ''),
|
||
]);
|
||
}
|
||
|
||
public function update(Request $request, SettingsService $settings, TelegramNotifier $telegram)
|
||
{
|
||
$data = $request->validate([
|
||
'telegram_bot_token' => ['nullable', 'string', 'max:255'],
|
||
'telegram_owner_chat_id' => ['nullable', 'string', 'max:64'],
|
||
'channels_max_per_agent' => ['nullable', 'integer', 'min:1', 'max:100'],
|
||
'channels_domains' => ['nullable', 'string', 'max:4000'],
|
||
'panel_admin_hosts' => ['nullable', 'string', 'max:4000'],
|
||
'panel_agent_hosts' => ['nullable', 'string', 'max:4000'],
|
||
]);
|
||
|
||
$domains = SettingsService::parseDomains($data['channels_domains'] ?? null);
|
||
$adminHosts = SettingsService::parseDomains($data['panel_admin_hosts'] ?? null);
|
||
$agentHosts = SettingsService::parseDomains($data['panel_agent_hosts'] ?? null);
|
||
|
||
$token = trim((string) ($data['telegram_bot_token'] ?? ''));
|
||
$username = '';
|
||
$note = '';
|
||
if ($token !== '') {
|
||
$fetched = $telegram->fetchBotUsername($token);
|
||
if ($fetched !== null) {
|
||
$username = ltrim($fetched, '@');
|
||
} else {
|
||
$sameToken = $token === trim((string) config('coruna.telegram.bot_token', ''));
|
||
$previous = ltrim((string) config('coruna.telegram.bot_username', ''), '@');
|
||
if ($sameToken && $previous !== '') {
|
||
$username = $previous;
|
||
} else {
|
||
$note = ';未能从 Token 读取 Bot 用户名,请确认 Token 有效';
|
||
}
|
||
}
|
||
}
|
||
|
||
$settings->putMany([
|
||
'telegram.bot_token' => $token !== '' ? $token : null,
|
||
'telegram.bot_username' => $username !== '' ? $username : null,
|
||
'telegram.owner_chat_id' => $data['telegram_owner_chat_id'] ?? null,
|
||
'channels.max_per_agent' => isset($data['channels_max_per_agent'])
|
||
? (string) $data['channels_max_per_agent']
|
||
: null,
|
||
// Persist normalized comma-separated list (empty clears override to []).
|
||
'channels.domains' => $domains === [] ? '' : implode(',', $domains),
|
||
// Empty = no Host restriction for that panel.
|
||
'panel.admin_hosts' => $adminHosts === [] ? '' : implode(',', $adminHosts),
|
||
'panel.agent_hosts' => $agentHosts === [] ? '' : implode(',', $agentHosts),
|
||
]);
|
||
|
||
$display = $telegram->formatBotUsername($username);
|
||
|
||
return response()->json([
|
||
'code' => 0,
|
||
'msg' => '已保存'.($display !== '' ? '(Bot '.$display.')' : '').$note,
|
||
'data' => [
|
||
'bot_username' => $display,
|
||
'bot_invite_url' => $telegram->inviteUrl($display),
|
||
],
|
||
]);
|
||
}
|
||
|
||
public function testTelegram(Request $request, TelegramNotifier $telegram)
|
||
{
|
||
$data = $request->validate([
|
||
'chat_id' => ['nullable', 'string', 'max:64'],
|
||
'bot_token' => ['nullable', 'string', 'max:255'],
|
||
]);
|
||
|
||
$chatId = trim((string) ($data['chat_id'] ?? ''));
|
||
if ($chatId === '') {
|
||
$chatId = trim((string) config('coruna.telegram.owner_chat_id', ''));
|
||
}
|
||
$token = trim((string) ($data['bot_token'] ?? ''));
|
||
$token = $token !== '' ? $token : null;
|
||
|
||
$result = $telegram->sendTest($chatId, '官方通知群推送正常。', $token);
|
||
|
||
return response()->json([
|
||
'code' => $result['ok'] ? 0 : 1,
|
||
'msg' => $result['ok'] ? '已发送测试消息,请查看对应群' : ($result['error'] ?: '发送失败'),
|
||
]);
|
||
}
|
||
}
|