This commit is contained in:
2026-06-24 17:45:24 +08:00
commit bc0223bf07
16 changed files with 1553 additions and 0 deletions
+49
View File
@@ -0,0 +1,49 @@
<?php
// 数据库连接和初始化
$db_path = __DIR__ . '/../data/database.db';
$is_new_db = !file_exists($db_path);
try {
$db = new PDO('sqlite:' . $db_path);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// 如果是新数据库,初始化表结构
if ($is_new_db) {
// 创建 apps 表
$db->exec("
CREATE TABLE IF NOT EXISTS apps (
id INTEGER PRIMARY KEY AUTOINCREMENT,
filename TEXT NOT NULL,
original_name TEXT NOT NULL,
version TEXT,
description TEXT,
upload_time DATETIME DEFAULT CURRENT_TIMESTAMP,
file_size INTEGER
)
");
// 创建 config 表
$db->exec("
CREATE TABLE IF NOT EXISTS config (
key TEXT PRIMARY KEY,
value TEXT
)
");
// 插入默认配置
$defaults = [
['site_title', 'OPS 运营管理系统'],
['site_description', '点击下方按钮下载移动端 APP'],
['logo_path', 'logo.png']
];
$stmt = $db->prepare("INSERT INTO config (key, value) VALUES (?, ?)");
foreach ($defaults as $config) {
$stmt->execute($config);
}
}
} catch (PDOException $e) {
die("数据库连接失败: " . $e->getMessage());
}
+51
View File
@@ -0,0 +1,51 @@
<?php
// 通用函数
/**
* 获取配置值
*/
function getConfig($key, $default = '') {
global $db;
$stmt = $db->prepare("SELECT value FROM config WHERE key = ?");
$stmt->execute([$key]);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
return $result ? $result['value'] : $default;
}
/**
* 更新配置值
*/
function updateConfig($key, $value) {
global $db;
$stmt = $db->prepare("INSERT OR REPLACE INTO config (key, value) VALUES (?, ?)");
return $stmt->execute([$key, $value]);
}
/**
* 格式化文件大小
*/
function formatFileSize($bytes) {
if ($bytes >= 1073741824) {
return number_format($bytes / 1073741824, 2) . ' GB';
} elseif ($bytes >= 1048576) {
return number_format($bytes / 1048576, 2) . ' MB';
} elseif ($bytes >= 1024) {
return number_format($bytes / 1024, 2) . ' KB';
} else {
return $bytes . ' B';
}
}
/**
* 检查是否已登录
*/
function isLoggedIn() {
return isset($_SESSION['admin_logged_in']) && $_SESSION['admin_logged_in'] === true;
}
/**
* 安全输出 HTML
*/
function e($string) {
return htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
}