mirror of
https://github.com/lkddi/Xboard.git
synced 2026-04-14 19:40:53 +08:00
refactor: move subscribe templates to dedicated database table
This commit is contained in:
@@ -4,20 +4,12 @@ namespace App\Http\Controllers\V2\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Admin\ConfigSave;
|
||||
use App\Protocols\Clash;
|
||||
use App\Protocols\ClashMeta;
|
||||
use App\Protocols\SingBox;
|
||||
use App\Protocols\Stash;
|
||||
use App\Protocols\Surfboard;
|
||||
use App\Protocols\Surge;
|
||||
use App\Models\SubscribeTemplate;
|
||||
use App\Services\MailService;
|
||||
use App\Services\TelegramService;
|
||||
use App\Services\ThemeService;
|
||||
use App\Utils\Dict;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Illuminate\Support\Facades\File;
|
||||
|
||||
class ConfigController extends Controller
|
||||
{
|
||||
@@ -57,18 +49,6 @@ class ConfigController extends Controller
|
||||
'data' => $mailLog,
|
||||
]);
|
||||
}
|
||||
/**
|
||||
* 获取规则模板内容
|
||||
*
|
||||
* @param string $file 文件路径
|
||||
* @return string 文件内容
|
||||
*/
|
||||
private function getTemplateContent(string $file): string
|
||||
{
|
||||
$path = base_path($file);
|
||||
return File::exists($path) ? File::get($path) : '';
|
||||
}
|
||||
|
||||
public function setTelegramWebhook(Request $request)
|
||||
{
|
||||
$hookUrl = $this->resolveTelegramWebhookUrl();
|
||||
@@ -214,14 +194,14 @@ class ConfigController extends Controller
|
||||
],
|
||||
'subscribe_template' => [
|
||||
'subscribe_template_singbox' => $this->formatTemplateContent(
|
||||
admin_setting('subscribe_template_singbox', $this->getDefaultTemplate('singbox')),
|
||||
subscribe_template('singbox') ?? '',
|
||||
'json'
|
||||
),
|
||||
'subscribe_template_clash' => admin_setting('subscribe_template_clash', $this->getDefaultTemplate('clash')),
|
||||
'subscribe_template_clashmeta' => admin_setting('subscribe_template_clashmeta', $this->getDefaultTemplate('clashmeta')),
|
||||
'subscribe_template_stash' => admin_setting('subscribe_template_stash', $this->getDefaultTemplate('stash')),
|
||||
'subscribe_template_surge' => admin_setting('subscribe_template_surge', $this->getDefaultTemplate('surge')),
|
||||
'subscribe_template_surfboard' => admin_setting('subscribe_template_surfboard', $this->getDefaultTemplate('surfboard'))
|
||||
'subscribe_template_clash' => subscribe_template('clash') ?? '',
|
||||
'subscribe_template_clashmeta' => subscribe_template('clashmeta') ?? '',
|
||||
'subscribe_template_stash' => subscribe_template('stash') ?? '',
|
||||
'subscribe_template_surge' => subscribe_template('surge') ?? '',
|
||||
'subscribe_template_surfboard' => subscribe_template('surfboard') ?? ''
|
||||
]
|
||||
];
|
||||
}
|
||||
@@ -230,7 +210,20 @@ class ConfigController extends Controller
|
||||
{
|
||||
$data = $request->validated();
|
||||
|
||||
$templateKeys = [
|
||||
'subscribe_template_singbox' => 'singbox',
|
||||
'subscribe_template_clash' => 'clash',
|
||||
'subscribe_template_clashmeta' => 'clashmeta',
|
||||
'subscribe_template_stash' => 'stash',
|
||||
'subscribe_template_surge' => 'surge',
|
||||
'subscribe_template_surfboard' => 'surfboard',
|
||||
];
|
||||
|
||||
foreach ($data as $k => $v) {
|
||||
if (isset($templateKeys[$k])) {
|
||||
SubscribeTemplate::setContent($templateKeys[$k], $v);
|
||||
continue;
|
||||
}
|
||||
if ($k == 'frontend_theme') {
|
||||
$themeService = app(ThemeService::class);
|
||||
$themeService->switch($v);
|
||||
@@ -273,53 +266,6 @@ class ConfigController extends Controller
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取默认模板内容
|
||||
*
|
||||
* @param string $type 模板类型
|
||||
* @return string 默认模板内容
|
||||
*/
|
||||
private function getDefaultTemplate(string $type): string
|
||||
{
|
||||
$fileMap = [
|
||||
'singbox' => [SingBox::CUSTOM_TEMPLATE_FILE, SingBox::DEFAULT_TEMPLATE_FILE],
|
||||
'clash' => [Clash::CUSTOM_TEMPLATE_FILE, Clash::DEFAULT_TEMPLATE_FILE],
|
||||
'clashmeta' => [
|
||||
ClashMeta::CUSTOM_TEMPLATE_FILE,
|
||||
ClashMeta::CUSTOM_CLASH_TEMPLATE_FILE,
|
||||
ClashMeta::DEFAULT_TEMPLATE_FILE
|
||||
],
|
||||
'stash' => [
|
||||
Stash::CUSTOM_TEMPLATE_FILE,
|
||||
Stash::CUSTOM_CLASH_TEMPLATE_FILE,
|
||||
Stash::DEFAULT_TEMPLATE_FILE
|
||||
],
|
||||
'surge' => [Surge::CUSTOM_TEMPLATE_FILE, Surge::DEFAULT_TEMPLATE_FILE],
|
||||
'surfboard' => [Surfboard::CUSTOM_TEMPLATE_FILE, Surfboard::DEFAULT_TEMPLATE_FILE],
|
||||
];
|
||||
|
||||
if (!isset($fileMap[$type])) {
|
||||
return '';
|
||||
}
|
||||
|
||||
// 按优先级查找可用的模板文件
|
||||
foreach ($fileMap[$type] as $file) {
|
||||
$content = $this->getTemplateContent($file);
|
||||
if (!empty($content)) {
|
||||
// 对于 SingBox,需要格式化 JSON
|
||||
if ($type === 'singbox') {
|
||||
$decoded = json_decode($content, true);
|
||||
if (json_last_error() === JSON_ERROR_NONE) {
|
||||
return json_encode($decoded, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
}
|
||||
}
|
||||
return $content;
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
private function getTelegramWebhookBaseUrl(): ?string
|
||||
{
|
||||
$customUrl = trim((string) admin_setting('telegram_webhook_url', ''));
|
||||
|
||||
Reference in New Issue
Block a user