新增模板,修复模板选择bug

This commit is contained in:
2026-03-19 15:47:01 +08:00
parent bf21006d4e
commit a7908e0677
22 changed files with 964 additions and 2301 deletions
+20 -26
View File
@@ -7,7 +7,6 @@ namespace app\service;
use think\facade\Db;
use think\facade\Config;
use app\model\Cms\TemplateSiteConfig;
use app\model\Cms\TemplateThemeData;
/**
* 模板服务类
@@ -120,19 +119,12 @@ class ThemeService
* @param int $tid 租户ID
* @return string
*/
public function getCurrentTheme(int $tid = 0): string
public function getCurrentTheme(int $tid)
{
try {
$where = [['key', '=', 'current_theme'], ['delete_time', '=', null]];
if ($tid > 0) {
$where[] = ['tid', '=', $tid];
}
$config = TemplateSiteConfig::where($where)
->find();
return $config['value'] ?? 'default';
} catch (\Exception $e) {
return 'default';
}
// 直接通过 tid 查询对应的 value
$value = TemplateSiteConfig::where('tid', $tid)->value('value');
return $value;
}
/**
@@ -175,6 +167,7 @@ class ThemeService
]);
} else {
TemplateSiteConfig::insert([
'tid' => $tid,
'key' => 'current_theme',
'value' => $themeKey,
'create_time' => $now,
@@ -199,17 +192,18 @@ class ThemeService
$themeKey = $themeKey ?? $this->getCurrentTheme($tid);
try {
$where = [['theme_key', '=', $themeKey], ['delete_time', '=', null]];
if ($tid > 0) {
$where[] = ['tid', '=', $tid];
}
$themeData = TemplateThemeData::where($where)
$configData = TemplateSiteConfig::where($where)
->select()
->toArray();
$data = [];
foreach ($themeData as $item) {
$data[$item['field_key']] = $item['field_value'];
foreach ($configData as $item) {
// 去掉 field_ 前缀
$fieldKey = substr($item['key'], 6);
$data[$fieldKey] = $item['value'];
}
return [
@@ -237,32 +231,32 @@ class ThemeService
public function saveThemeField(int $tid, string $themeKey, string $fieldKey, $fieldValue): bool
{
try {
// 使用 TemplateSiteConfig 表,key 格式为 field_xxx
$configKey = 'field_' . $fieldKey;
$where = [
['theme_key', '=', $themeKey],
['field_key', '=', $fieldKey],
['key', '=', $configKey],
['delete_time', '=', null]
];
if ($tid > 0) {
$where[] = ['tid', '=', $tid];
}
$existing = TemplateThemeData::where($where)
$existing = TemplateSiteConfig::where($where)
->find();
$value = is_array($fieldValue) ? json_encode($fieldValue, JSON_UNESCAPED_UNICODE) : $fieldValue;
$now = date('Y-m-d H:i:s');
if ($existing) {
TemplateThemeData::where('id', $existing['id'])
TemplateSiteConfig::where('id', $existing['id'])
->update([
'field_value' => $value,
'value' => $value,
'update_time' => $now
]);
} else {
TemplateThemeData::insert([
TemplateSiteConfig::insert([
'tid' => $tid,
'theme_key' => $themeKey,
'field_key' => $fieldKey,
'field_value' => $value,
'key' => $configKey,
'value' => $value,
'create_time' => $now,
'update_time' => $now
]);