mirror of
https://github.com/v2board/v2board.git
synced 2025-01-25 23:49:09 +08:00
114 lines
3.4 KiB
PHP
Executable File
114 lines
3.4 KiB
PHP
Executable File
<?php
|
||
|
||
namespace App\Http\Controllers\V1\Admin;
|
||
|
||
use App\Http\Controllers\Controller;
|
||
use App\Http\Requests\Admin\ConfigSave;
|
||
use App\Jobs\SendEmailJob;
|
||
use App\Services\ConfigService;
|
||
use App\Services\TelegramService;
|
||
use App\Utils\Dict;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Support\Facades\Artisan;
|
||
use Illuminate\Support\Facades\File;
|
||
use Illuminate\Support\Facades\Mail;
|
||
|
||
class ConfigController extends Controller
|
||
{
|
||
public function getEmailTemplate()
|
||
{
|
||
$path = resource_path('views/mail/');
|
||
$files = array_map(function ($item) use ($path) {
|
||
return str_replace($path, '', $item);
|
||
}, glob($path . '*'));
|
||
return response([
|
||
'data' => $files
|
||
]);
|
||
}
|
||
|
||
public function getThemeTemplate()
|
||
{
|
||
$path = public_path('theme/');
|
||
$files = array_map(function ($item) use ($path) {
|
||
return str_replace($path, '', $item);
|
||
}, glob($path . '*'));
|
||
return response([
|
||
'data' => $files
|
||
]);
|
||
}
|
||
|
||
public function testSendMail(Request $request)
|
||
{
|
||
$obj = new SendEmailJob([
|
||
'email' => $request->user['email'],
|
||
'subject' => 'This is v2board test email',
|
||
'template_name' => 'notify',
|
||
'template_value' => [
|
||
'name' => config('v2board.app_name', 'V2Board'),
|
||
'content' => 'This is v2board test email',
|
||
'url' => config('v2board.app_url')
|
||
]
|
||
]);
|
||
return response([
|
||
'data' => true,
|
||
'log' => $obj->handle()
|
||
]);
|
||
}
|
||
|
||
public function setTelegramWebhook(Request $request)
|
||
{
|
||
$hookUrl = url('/api/v1/guest/telegram/webhook?access_token=' . md5(config('v2board.telegram_bot_token', $request->input('telegram_bot_token'))));
|
||
$telegramService = new TelegramService($request->input('telegram_bot_token'));
|
||
$telegramService->getMe();
|
||
$telegramService->setWebhook($hookUrl);
|
||
return response([
|
||
'data' => true
|
||
]);
|
||
}
|
||
|
||
public function fetch(Request $request)
|
||
{
|
||
$key = $request->input('key');
|
||
$data = (new ConfigService)->getDefaultConfig();
|
||
if ($key && isset($data[$key])) {
|
||
return response([
|
||
'data' => [
|
||
$key => $data[$key]
|
||
]
|
||
]);
|
||
};
|
||
// TODO: default should be in Dict
|
||
return response([
|
||
'data' => $data
|
||
]);
|
||
}
|
||
|
||
public function save(ConfigSave $request)
|
||
{
|
||
$data = $request->validated();
|
||
$config = config('v2board');
|
||
foreach (ConfigSave::RULES as $k => $v) {
|
||
if (!in_array($k, array_keys(ConfigSave::RULES))) {
|
||
unset($config[$k]);
|
||
continue;
|
||
}
|
||
if (array_key_exists($k, $data)) {
|
||
$config[$k] = $data[$k];
|
||
}
|
||
}
|
||
$data = var_export($config, 1);
|
||
if (!File::put(base_path() . '/config/v2board.php', "<?php\n return $data ;")) {
|
||
abort(500, '修改失败');
|
||
}
|
||
if (function_exists('opcache_reset')) {
|
||
if (opcache_reset() === false) {
|
||
abort(500, '缓存清除失败,请卸载或检查opcache配置状态');
|
||
}
|
||
}
|
||
Artisan::call('config:cache');
|
||
return response([
|
||
'data' => true
|
||
]);
|
||
}
|
||
}
|