update: theme

This commit is contained in:
tokumeikoi
2022-05-09 00:26:38 +08:00
parent 8311722fda
commit db06001254
13 changed files with 230 additions and 179 deletions

View File

@ -8,6 +8,8 @@ use App\Services\TelegramService;
use Illuminate\Http\Request;
use App\Utils\Dict;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Mail;
class ConfigController extends Controller
@ -118,10 +120,6 @@ class ConfigController extends Controller
],
'frontend' => [
'frontend_theme' => config('v2board.frontend_theme', 'v2board'),
'frontend_theme_sidebar' => config('v2board.frontend_theme_sidebar', 'light'),
'frontend_theme_header' => config('v2board.frontend_theme_header', 'dark'),
'frontend_theme_color' => config('v2board.frontend_theme_color', 'default'),
'frontend_background_url' => config('v2board.frontend_background_url'),
'frontend_admin_path' => config('v2board.frontend_admin_path', 'admin'),
'frontend_customer_service_method' => config('v2board.frontend_customer_service_method', 0),
'frontend_customer_service_id' => config('v2board.frontend_customer_service_id'),
@ -172,15 +170,18 @@ class ConfigController extends Controller
public function save(ConfigSave $request)
{
$data = $request->validated();
$array = \Config::get('v2board');
foreach ($data as $k => $v) {
if (!in_array($k, array_keys($request->validated()))) {
abort(500, '参数' . $k . '不在规则内,禁止修改');
$config = config('v2board');
foreach ($config as $k => $v) {
if (!in_array($k, array_keys(ConfigSave::RULES))) {
unset($config[$k]);
continue;
}
if (isset($data[$k])) {
$config[$k] = $data[$k];
}
$array[$k] = $v;
}
$data = var_export($array, 1);
if (!\File::put(base_path() . '/config/v2board.php', "<?php\n return $data ;")) {
$data = var_export($config, 1);
if (!File::put(base_path() . '/config/v2board.php', "<?php\n return $data ;")) {
abort(500, '修改失败');
}
if (function_exists('opcache_reset')) {
@ -188,7 +189,7 @@ class ConfigController extends Controller
abort(500, '缓存清除失败请卸载或检查opcache配置状态');
}
}
\Artisan::call('config:cache');
Artisan::call('config:cache');
return response([
'data' => true
]);

View File

@ -20,6 +20,25 @@ class ThemeController extends Controller
}, glob($path . '*'));
}
private function initTheme($themeName, $configs)
{
$data = [];
foreach ($configs as $config) {
$data[$config['field_name']] = isset($config['default_value']) ? $config['default_value'] : '';
}
$data = var_export($data, 1);
if (!File::put(base_path() . "/config/theme/{$themeName}.php", "<?php\n return $data ;")) {
abort(500, "{$themeName}初始化失败");
}
try {
Artisan::call('config:cache');
} catch (\Exception $e) {
abort(500, "{$themeName}初始化失败");
}
}
public function getThemes()
{
$themeConfigs = [];
@ -28,12 +47,14 @@ class ThemeController extends Controller
if (!File::exists($themeConfigFile)) continue;
$themeConfig = include($themeConfigFile);
if (!isset($themeConfig['configs']) || !is_array($themeConfig)) continue;
$themeConfigs[$this->themes] = $themeConfig;
$themeConfigs[$theme] = $themeConfig;
if (config("theme.{$theme}")) continue;
$this->initTheme($theme, $themeConfig['configs']);
}
return response([
'data' => [
'themes' => $themeConfigs,
'active' => config('v2board.theme', 'v2board')
'active' => config('v2board.frontend_theme', 'v2board')
]
]);
}
@ -52,7 +73,7 @@ class ThemeController extends Controller
{
$payload = $request->validate([
'name' => 'required|in:' . join(',', $this->themes),
'configs' => 'required|array'
'config' => 'required|array'
]);
$themeConfigFile = public_path("theme/{$payload['name']}/config.php");
if (!File::exists($themeConfigFile)) abort(500, '主题不存在');
@ -60,8 +81,7 @@ class ThemeController extends Controller
$validateFields = array_column($themeConfig['configs'], 'field_name');
$config = [];
foreach ($validateFields as $validateField) {
if (!isset($payload['configs'][$validateField])) continue;
$config[$validateField] = $payload['configs'][$validateField];
$config[$validateField] = isset($payload['config'][$validateField]) ? $payload['config'][$validateField] : '';
}
File::ensureDirectoryExists(base_path() . '/config/theme/');
@ -78,7 +98,7 @@ class ThemeController extends Controller
}
return response([
'data' => config('theme.v2board')
'data' => $config
]);
}
}