This commit is contained in:
@@ -27,8 +27,9 @@ class ThemeConfigurator
|
|||||||
public function install()
|
public function install()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
|
$table = ThemeConfigModel::tableName();
|
||||||
Db::execute(
|
Db::execute(
|
||||||
"CREATE TABLE IF NOT EXISTS `addon_theme_configurator`(
|
"CREATE TABLE IF NOT EXISTS `{$table}`(
|
||||||
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
|
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||||
`config` longtext NOT NULL,
|
`config` longtext NOT NULL,
|
||||||
`update_time` int(11) DEFAULT 0,
|
`update_time` int(11) DEFAULT 0,
|
||||||
@@ -56,7 +57,8 @@ class ThemeConfigurator
|
|||||||
public function uninstall()
|
public function uninstall()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
Db::execute("DROP TABLE IF EXISTS `addon_theme_configurator`;");
|
$table = ThemeConfigModel::tableName();
|
||||||
|
Db::execute("DROP TABLE IF EXISTS `{$table}`;");
|
||||||
} catch (\Throwable $throwable) {
|
} catch (\Throwable $throwable) {
|
||||||
return [
|
return [
|
||||||
'status' => 400,
|
'status' => 400,
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace addon\theme_configurator\model;
|
namespace addon\theme_configurator\model;
|
||||||
|
|
||||||
|
use think\facade\Config;
|
||||||
use think\facade\Db;
|
use think\facade\Db;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -8,14 +9,27 @@ use think\facade\Db;
|
|||||||
*/
|
*/
|
||||||
class ThemeConfigModel
|
class ThemeConfigModel
|
||||||
{
|
{
|
||||||
protected $table = 'addon_theme_configurator';
|
public const TABLE = 'addon_theme_configurator';
|
||||||
|
|
||||||
|
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
|
public function getConfig(): array
|
||||||
{
|
{
|
||||||
$row = Db::name($this->table)->order('id', 'asc')->find();
|
$row = $this->query()->order('id', 'asc')->find();
|
||||||
if (!$row) {
|
if (!$row) {
|
||||||
return $this->defaultConfig();
|
return $this->defaultConfig();
|
||||||
}
|
}
|
||||||
@@ -34,11 +48,11 @@ class ThemeConfigModel
|
|||||||
'update_time' => time(),
|
'update_time' => time(),
|
||||||
];
|
];
|
||||||
|
|
||||||
$exists = Db::name($this->table)->order('id', 'asc')->find();
|
$exists = $this->query()->order('id', 'asc')->find();
|
||||||
if ($exists) {
|
if ($exists) {
|
||||||
Db::name($this->table)->where('id', $exists['id'])->update($payload);
|
$this->query()->where('id', $exists['id'])->update($payload);
|
||||||
} else {
|
} else {
|
||||||
Db::name($this->table)->insert($payload);
|
$this->query()->insert($payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $config;
|
return $config;
|
||||||
@@ -49,7 +63,7 @@ class ThemeConfigModel
|
|||||||
*/
|
*/
|
||||||
public function initConfig(): void
|
public function initConfig(): void
|
||||||
{
|
{
|
||||||
$exists = Db::name($this->table)->order('id', 'asc')->find();
|
$exists = $this->query()->order('id', 'asc')->find();
|
||||||
if (!$exists) {
|
if (!$exists) {
|
||||||
$this->saveConfig($this->defaultConfig());
|
$this->saveConfig($this->defaultConfig());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user