tp/app/admin/controller/ThemeController.php
2026-03-09 09:40:36 +08:00

121 lines
2.6 KiB
PHP

<?php
declare(strict_types=1);
namespace app\admin\controller;
use app\admin\BaseController;
use app\service\ThemeService;
use think\facade\Request;
/**
* 模板管理控制器
*/
class ThemeController extends BaseController
{
private ThemeService $themeService;
public function __construct()
{
$this->themeService = new ThemeService();
}
/**
* 获取模板列表(后台管理)
* @return \think\response\Json
*/
public function index()
{
$themes = $this->themeService->getThemeList();
$currentTheme = $this->themeService->getCurrentTheme();
return json([
'code' => 200,
'msg' => 'success',
'data' => [
'list' => $themes,
'currentTheme' => $currentTheme
]
]);
}
/**
* 切换模板
* @return \think\response\Json
*/
public function switch()
{
$themeKey = Request::post('theme_key', '');
if (empty($themeKey)) {
return json([
'code' => 400,
'msg' => '模板标识不能为空'
]);
}
$result = $this->themeService->switchTheme($themeKey);
if ($result) {
return json([
'code' => 200,
'msg' => '切换成功'
]);
}
return json([
'code' => 400,
'msg' => '切换失败,模板不存在'
]);
}
/**
* 获取模板字段数据
* @return \think\response\Json
*/
public function getData()
{
$themeKey = Request::get('theme_key', '');
$themeData = $this->themeService->getThemeData($themeKey ?: null);
return json([
'code' => 200,
'msg' => 'success',
'data' => $themeData
]);
}
/**
* 保存模板字段数据
* @return \think\response\Json
*/
public function saveData()
{
$themeKey = Request::post('theme_key', '');
$fieldKey = Request::post('field_key', '');
$fieldValue = Request::post('field_value', '');
if (empty($themeKey) || empty($fieldKey)) {
return json([
'code' => 400,
'msg' => '参数不完整'
]);
}
$result = $this->themeService->saveThemeField($themeKey, $fieldKey, $fieldValue);
if ($result) {
return json([
'code' => 200,
'msg' => '保存成功'
]);
}
return json([
'code' => 400,
'msg' => '保存失败'
]);
}
}