230 lines
8.8 KiB
PHP
230 lines
8.8 KiB
PHP
<?php
|
|
namespace addon\theme_configurator\model;
|
|
|
|
use think\facade\Cache;
|
|
use think\facade\Config;
|
|
use think\facade\Db;
|
|
|
|
/**
|
|
* 主题配置模型
|
|
*/
|
|
class ThemeConfigModel
|
|
{
|
|
public const TABLE = 'addon_theme_configurator';
|
|
|
|
// 缓存配置
|
|
private const CACHE_KEY = 'theme_config_cache';
|
|
private const CACHE_TTL = 3600; // 缓存1小时
|
|
|
|
public static function tableName(): string
|
|
{
|
|
$connections = Config::get('database.connections', []);
|
|
$defaultConnection = Config::get('database.default', 'mysql');
|
|
$prefix = $connections[$defaultConnection]['prefix'] ?? Config::get('database.prefix', '');
|
|
return $prefix . self::TABLE;
|
|
}
|
|
|
|
protected function query()
|
|
{
|
|
return Db::name(self::TABLE);
|
|
}
|
|
|
|
/**
|
|
* 获取配置 (带缓存)
|
|
*/
|
|
public function getConfig(): array
|
|
{
|
|
// 临时禁用缓存读取用于调试
|
|
/*
|
|
// 尝试从缓存获取
|
|
try {
|
|
$config = Cache::get(self::CACHE_KEY);
|
|
|
|
if ($config !== false && $config !== null) {
|
|
// 缓存命中
|
|
error_log('ThemeConfigModel::getConfig() - 返回banner数: ' . count($config['banner'] ?? []));
|
|
return $config;
|
|
}
|
|
} catch (\Throwable $e) {
|
|
// 缓存系统异常,继续从数据库读取
|
|
}
|
|
*/
|
|
|
|
// 缓存未命中,从数据库读取
|
|
$row = $this->query()->order('id', 'desc')->find();
|
|
error_log('ThemeConfigModel::getConfig() - 查询结果: ' . ($row ? 'found' : 'not found'));
|
|
if ($row) {
|
|
error_log('config字段长度: ' . strlen($row['config']));
|
|
error_log('config前100字符: ' . substr($row['config'], 0, 100));
|
|
}
|
|
if (!$row) {
|
|
$config = $this->defaultConfig();
|
|
} else {
|
|
$config = json_decode($row['config'], true);
|
|
$config = is_array($config) ? $config : $this->defaultConfig();
|
|
}
|
|
|
|
// 临时禁用缓存写入用于调试
|
|
/*
|
|
// 写入缓存 (忽略缓存写入失败)
|
|
try {
|
|
Cache::set(self::CACHE_KEY, $config, self::CACHE_TTL);
|
|
} catch (\Throwable $e) {
|
|
// 忽略缓存写入失败
|
|
}
|
|
*/
|
|
|
|
return $config;
|
|
}
|
|
|
|
/**
|
|
* 保存配置
|
|
*/
|
|
public function saveConfig(array $config): array
|
|
{
|
|
$payload = [
|
|
'config' => json_encode($config, JSON_UNESCAPED_UNICODE),
|
|
'update_time' => time(),
|
|
];
|
|
|
|
// 查询是否有旧记录
|
|
$exists = $this->query()->find();
|
|
|
|
if ($exists) {
|
|
// 有旧记录:删除所有旧记录,确保只有一条最新记录
|
|
$this->query()->delete();
|
|
}
|
|
|
|
// 插入新记录
|
|
$this->query()->insert($payload);
|
|
|
|
// 清除缓存,确保下次读取最新数据
|
|
try {
|
|
Cache::delete(self::CACHE_KEY);
|
|
} catch (\Throwable $e) {
|
|
// 忽略缓存删除失败
|
|
}
|
|
|
|
return $config;
|
|
}
|
|
|
|
/**
|
|
* 安装时写入默认配置
|
|
*/
|
|
public function initConfig(): void
|
|
{
|
|
$exists = $this->query()->order('id', 'asc')->find();
|
|
if (!$exists) {
|
|
$this->saveConfig($this->defaultConfig());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 默认结构
|
|
*/
|
|
protected function defaultConfig(): array
|
|
{
|
|
return [
|
|
'seo' => [
|
|
'title' => 'BlackFruit-UI',
|
|
'keywords' => '云服务器,主题云,BlackFruit',
|
|
'description' => 'BlackFruit-UI 默认站点说明',
|
|
],
|
|
// 顶部/底部导航(第一个默认为产品中心,支持三级菜单)
|
|
'header_nav' => [
|
|
[
|
|
'name' => '产品中心',
|
|
'file_address' => '',
|
|
'blank' => false,
|
|
'children' => [
|
|
[
|
|
'name' => 'SAS轻量云服务器',
|
|
'file_address' => '/cloud.html',
|
|
'icon' => '/web/BlackFruit-web/assets/img/index/cloud-icon.png',
|
|
'description' => '高性能SSD云服务器',
|
|
'blank' => false,
|
|
'children' => [
|
|
[
|
|
'name' => '香港SAS',
|
|
'file_address' => '/cloud.html?region=hk',
|
|
'description' => '香港数据中心',
|
|
'blank' => false
|
|
],
|
|
[
|
|
'name' => '美国SAS',
|
|
'file_address' => '/cloud.html?region=us',
|
|
'description' => '美国数据中心',
|
|
'blank' => false
|
|
],
|
|
[
|
|
'name' => '日本SAS',
|
|
'file_address' => '/cloud.html?region=jp',
|
|
'description' => '日本数据中心',
|
|
'blank' => false
|
|
]
|
|
]
|
|
],
|
|
[
|
|
'name' => 'ECS精品云服务器',
|
|
'file_address' => '/dedicated.html',
|
|
'icon' => '/web/BlackFruit-web/assets/img/index/server-icon.png',
|
|
'description' => '企业级云服务器',
|
|
'blank' => false,
|
|
'children' => [
|
|
[
|
|
'name' => '宁波ECS',
|
|
'file_address' => '/dedicated.html?region=nb',
|
|
'description' => '宁波数据中心',
|
|
'blank' => false
|
|
],
|
|
[
|
|
'name' => '镇江ECS',
|
|
'file_address' => '/dedicated.html?region=zj',
|
|
'description' => '镇江数据中心',
|
|
'blank' => false
|
|
],
|
|
[
|
|
'name' => '北京ECS',
|
|
'file_address' => '/dedicated.html?region=bj',
|
|
'description' => '北京数据中心',
|
|
'blank' => false
|
|
]
|
|
]
|
|
]
|
|
]
|
|
]
|
|
],
|
|
'footer_nav' => [],
|
|
// 站点基础信息
|
|
'site_config' => [
|
|
'enterprise_name' => '主题云',
|
|
'enterprise_telephone' => '400-000-0000',
|
|
'enterprise_mailbox' => 'support@example.com',
|
|
'enterprise_qrcode' => '/upload/qrcode.png',
|
|
'official_website_logo' => '/upload/logo.png',
|
|
// Logo 链接配置(独立于导航数组)
|
|
'logo_link' => 'index.html',
|
|
// 在线客服链接,对应原主题中的 online_customer_service_link
|
|
'online_customer_service_link' => 'http://www.test.com',
|
|
'icp_info' => '京ICP备示例号',
|
|
'icp_info_link' => 'https://beian.miit.gov.cn/#/Integrated/index',
|
|
'public_security_network_preparation' => '京公网安备示例号',
|
|
'public_security_network_preparation_link' => 'https://beian.mps.gov.cn/#/query/webSearch',
|
|
'telecom_appreciation' => '增值电信业务经营许可证',
|
|
'copyright_info' => '© ' . date('Y') . ' 主题云',
|
|
'terms_service_url' => '/agreement/service.html',
|
|
'terms_privacy_url' => '/agreement/privacy.html',
|
|
'cloud_product_link' => '/cart/goods.htm?id=1',
|
|
'dcim_product_link' => '/cart/goods.htm?id=2',
|
|
],
|
|
// 友情链接、轮播、侧边浮窗等
|
|
'friendly_link' => [],
|
|
'banner' => [],
|
|
'side' => [],
|
|
// 用于 /console/v1/common 的扩展字段
|
|
'feedback_type' => [],
|
|
'honor' => [],
|
|
];
|
|
}
|
|
}
|