128 lines
2.9 KiB
PHP
128 lines
2.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace app\admin\controller\Cms\Theme;
|
|
|
|
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()
|
|
{
|
|
$tid = Request::get('tid', 0, 'int');
|
|
|
|
$themes = $this->themeService->getThemeList();
|
|
$currentTheme = $this->themeService->getCurrentTheme($tid);
|
|
|
|
return json([
|
|
'code' => 200,
|
|
'msg' => 'success',
|
|
'data' => [
|
|
'list' => $themes,
|
|
'currentTheme' => $currentTheme
|
|
]
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 切换模板
|
|
* @return \think\response\Json
|
|
*/
|
|
public function switch()
|
|
{
|
|
$tid = Request::post('tid', 0, 'int');
|
|
$themeKey = Request::post('theme_key', '');
|
|
|
|
if (empty($themeKey)) {
|
|
return json([
|
|
'code' => 400,
|
|
'msg' => '模板标识不能为空'
|
|
]);
|
|
}
|
|
|
|
$result = $this->themeService->switchTheme($tid, $themeKey);
|
|
|
|
if ($result) {
|
|
return json([
|
|
'code' => 200,
|
|
'msg' => '切换成功'
|
|
]);
|
|
}
|
|
|
|
return json([
|
|
'code' => 400,
|
|
'msg' => '切换失败,模板不存在'
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 获取模板字段数据
|
|
* @return \think\response\Json
|
|
*/
|
|
public function getData()
|
|
{
|
|
$tid = Request::get('tid', 0, 'int');
|
|
$themeKey = Request::get('theme_key', '');
|
|
|
|
$themeData = $this->themeService->getThemeData($tid, $themeKey ?: null);
|
|
|
|
return json([
|
|
'code' => 200,
|
|
'msg' => 'success',
|
|
'data' => $themeData
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 保存模板字段数据
|
|
* @return \think\response\Json
|
|
*/
|
|
public function saveData()
|
|
{
|
|
$tid = Request::post('tid', 0, 'int');
|
|
$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($tid, $themeKey, $fieldKey, $fieldValue);
|
|
|
|
if ($result) {
|
|
return json([
|
|
'code' => 200,
|
|
'msg' => '保存成功'
|
|
]);
|
|
}
|
|
|
|
return json([
|
|
'code' => 400,
|
|
'msg' => '保存失败'
|
|
]);
|
|
}
|
|
}
|