添加主题切换功能:支持暗色/亮色玻璃主题

功能说明:
- 管理员可在系统设置中配置全局默认主题
- 普通用户可在设置页面选择:跟随全局/暗色/亮色
- 分享页面自动继承分享者的主题偏好
- 主题设置实时保存,刷新后保持

技术实现:
- 后端:数据库添加theme_preference字段,新增主题API
- 前端:CSS变量实现主题切换,localStorage缓存避免闪烁
- 分享页:加载时获取分享者主题设置

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-11-27 23:02:48 +08:00
parent 138bda9ae5
commit f12b9b7291
5 changed files with 374 additions and 8 deletions

View File

@@ -446,7 +446,7 @@ const ShareDB = {
});
const result = db.prepare(`
SELECT s.*, u.username, u.ftp_host, u.ftp_port, u.ftp_user, u.ftp_password, u.http_download_base_url
SELECT s.*, u.username, u.ftp_host, u.ftp_port, u.ftp_user, u.ftp_password, u.http_download_base_url, u.theme_preference
FROM shares s
JOIN users u ON s.user_id = u.id
WHERE s.share_code = ?
@@ -616,6 +616,26 @@ function initDefaultSettings() {
if (!SettingsDB.get('max_upload_size')) {
SettingsDB.set('max_upload_size', '10737418240'); // 10GB in bytes
}
// 默认全局主题为暗色
if (!SettingsDB.get('global_theme')) {
SettingsDB.set('global_theme', 'dark');
}
}
// 数据库迁移 - 主题偏好字段
function migrateThemePreference() {
try {
const columns = db.prepare("PRAGMA table_info(users)").all();
const hasThemePreference = columns.some(col => col.name === 'theme_preference');
if (!hasThemePreference) {
console.log('[数据库迁移] 添加主题偏好字段...');
db.exec(`ALTER TABLE users ADD COLUMN theme_preference TEXT DEFAULT NULL`);
console.log('[数据库迁移] ✓ 主题偏好字段已添加');
}
} catch (error) {
console.error('[数据库迁移] 主题偏好迁移失败:', error);
}
}
// 数据库版本迁移 - v2.0 本地存储功能
@@ -798,6 +818,7 @@ initDatabase();
createDefaultAdmin();
initDefaultSettings();
migrateToV2(); // 执行数据库迁移
migrateThemePreference(); // 主题偏好迁移
module.exports = {
db,