Files
shengkao_pachong/app/service/ConfigService.php
杨志 a962e06a18 up
2026-01-21 08:53:45 +08:00

144 lines
4.0 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare (strict_types = 1);
namespace app\service;
use app\model\Config;
use think\facade\Db;
/**
* 配置服务类
*/
class ConfigService
{
/**
* 初始化数据库表
*/
private function initDatabase(): void
{
$dbPath = dirname(dirname(__DIR__)) . '/runtime/database.db';
$dbDir = dirname($dbPath);
if (!is_dir($dbDir)) {
mkdir($dbDir, 0755, true);
}
try {
$connection = Db::connect('sqlite');
// 创建配置表(如果不存在)
$sql = "CREATE TABLE IF NOT EXISTS configs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
config_key VARCHAR(100) NOT NULL UNIQUE,
config_value TEXT NOT NULL,
created_at DATETIME NOT NULL,
updated_at DATETIME NOT NULL
)";
$connection->execute($sql);
// 初始化BASE_URL配置如果不存在
try {
$defaultBaseUrl = 'http://gzrsks.oumakspt.com:62';
$exist = Config::where('config_key', 'BASE_URL')->find();
if (!$exist) {
$config = new Config();
$config->config_key = 'BASE_URL';
$config->config_value = $defaultBaseUrl;
$config->created_at = date('Y-m-d H:i:s');
$config->updated_at = date('Y-m-d H:i:s');
$config->save();
}
} catch (\Exception $e) {
// 忽略错误,可能在下次访问时创建
}
} catch (\Exception $e) {
// 忽略错误
}
}
/**
* 获取配置值
* @param string $key
* @param string $default
* @return string
*/
public function getConfig(string $key, string $default = ''): string
{
$this->initDatabase();
try {
$config = Config::where('config_key', $key)->find();
if ($config) {
return $config->config_value ?? $default;
}
} catch (\Exception $e) {
// 忽略错误
}
return $default;
}
/**
* 设置配置值
* @param string $key
* @param string $value
* @return array ['code' => 1|0, 'msg' => string]
*/
public function setConfig(string $key, string $value): array
{
$this->initDatabase();
try {
$config = Config::where('config_key', $key)->find();
if ($config) {
// 更新现有配置
$config->config_value = $value;
$config->updated_at = date('Y-m-d H:i:s');
$config->save();
} else {
// 创建新配置
$config = new Config();
$config->config_key = $key;
$config->config_value = $value;
$config->created_at = date('Y-m-d H:i:s');
$config->updated_at = date('Y-m-d H:i:s');
$config->save();
}
return ['code' => 1, 'msg' => '保存成功'];
} catch (\Exception $e) {
return ['code' => 0, 'msg' => '保存失败: ' . $e->getMessage()];
}
}
/**
* 获取BASE_URL
* @return string
*/
public function getBaseUrl(): string
{
return $this->getConfig('BASE_URL', 'http://gzrsks.oumakspt.com:62');
}
/**
* 设置BASE_URL
* @param string $url
* @return array
*/
public function setBaseUrl(string $url): array
{
if (empty($url)) {
return ['code' => 0, 'msg' => 'BASE_URL不能为空'];
}
// 验证URL格式
if (!filter_var($url, FILTER_VALIDATE_URL) && !preg_match('/^https?:\/\/[\w\.-]+(:\d+)?$/', $url)) {
return ['code' => 0, 'msg' => 'BASE_URL格式不正确'];
}
return $this->setConfig('BASE_URL', $url);
}
}