/** * MSS后台主题配置系统 * Theme Configuration System * 版本: 1.0.0 */ (function(window) { 'use strict'; // 主题配置对象 const ThemeConfig = { // 当前主题 currentTheme: 'business-blue', // 可用主题列表 themes: { 'business-blue': { name: '深蓝商务', description: '简洁大气的深蓝色商务风格', cssFile: '/Public/css/theme-business-blue.css', preview: '/Public/images/theme-preview-blue.jpg', colors: { primary: '#1e3a8a', secondary: '#64748b', accent: '#f97316', success: '#059669', warning: '#d97706', danger: '#dc2626' } }, 'classic': { name: '经典风格', description: '传统的管理系统界面风格', cssFile: null, // 使用原始样式 preview: '/Public/images/theme-preview-classic.jpg', colors: { primary: '#409EFF', secondary: '#909399', accent: '#E6A23C', success: '#67C23A', warning: '#E6A23C', danger: '#F56C6C' } } }, // 初始化主题系统 init: function() { this.loadThemeFromStorage(); this.createThemeControls(); this.bindEvents(); this.applyTheme(this.currentTheme); }, // 从本地存储加载主题设置 loadThemeFromStorage: function() { const savedTheme = localStorage.getItem('mss-admin-theme'); if (savedTheme && this.themes[savedTheme]) { this.currentTheme = savedTheme; } }, // 保存主题设置到本地存储 saveThemeToStorage: function() { localStorage.setItem('mss-admin-theme', this.currentTheme); }, // 创建主题控制界面 createThemeControls: function() { // 检查是否已存在主题控制器 if (document.getElementById('theme-controls')) { return; } const controlsHTML = `

🎨 主题设置

选择主题风格
${Object.keys(this.themes).map(themeKey => { const theme = this.themes[themeKey]; return `
${theme.name}
${theme.description}
`; }).join('')}
`; document.body.insertAdjacentHTML('beforeend', controlsHTML); }, // 绑定事件 bindEvents: function() { // 主题切换器开关 document.addEventListener('click', (e) => { if (e.target.id === 'theme-toggle') { this.toggleThemeControls(); } // 主题选项点击 if (e.target.closest('.theme-option')) { const themeKey = e.target.closest('.theme-option').dataset.theme; this.switchTheme(themeKey); } // 重置主题 if (e.target.id === 'reset-theme') { this.resetTheme(); } // 点击其他区域关闭主题面板 if (!e.target.closest('#theme-controls')) { this.hideThemeControls(); } }); // 键盘快捷键 (Ctrl + T) document.addEventListener('keydown', (e) => { if (e.ctrlKey && e.key === 't') { e.preventDefault(); this.toggleThemeControls(); } }); }, // 切换主题控制面板显示 toggleThemeControls: function() { const controls = document.getElementById('theme-controls'); if (controls) { const isVisible = controls.style.right === '0px'; controls.style.right = isVisible ? '-300px' : '0px'; } }, // 隐藏主题控制面板 hideThemeControls: function() { const controls = document.getElementById('theme-controls'); if (controls && controls.style.right === '0px') { controls.style.right = '-300px'; } }, // 切换主题 switchTheme: function(themeKey) { if (!this.themes[themeKey]) { console.error('主题不存在:', themeKey); return; } this.currentTheme = themeKey; this.applyTheme(themeKey); this.updateThemeControls(); this.saveThemeToStorage(); // 显示切换成功提示 this.showNotification(`已切换到 ${this.themes[themeKey].name} 主题`); }, // 应用主题 applyTheme: function(themeKey) { const theme = this.themes[themeKey]; if (!theme) return; // 移除之前的主题样式 const existingThemeLinks = document.querySelectorAll('link[data-theme]'); existingThemeLinks.forEach(link => link.remove()); // 应用新主题样式 if (theme.cssFile) { const link = document.createElement('link'); link.rel = 'stylesheet'; link.href = theme.cssFile; link.setAttribute('data-theme', themeKey); document.head.appendChild(link); } // 更新CSS自定义属性 if (theme.colors) { const root = document.documentElement; Object.keys(theme.colors).forEach(colorKey => { root.style.setProperty(`--theme-${colorKey}`, theme.colors[colorKey]); }); } }, // 更新主题控制界面 updateThemeControls: function() { const options = document.querySelectorAll('.theme-option'); options.forEach(option => { const themeKey = option.dataset.theme; if (themeKey === this.currentTheme) { option.classList.add('active'); option.style.borderColor = '#1e3a8a'; } else { option.classList.remove('active'); option.style.borderColor = '#e2e8f0'; } }); }, // 重置主题 resetTheme: function() { this.switchTheme('business-blue'); }, // 显示通知 showNotification: function(message) { // 如果有layui的layer模块,使用layer if (window.layui && layui.layer) { layui.layer.msg(message, { icon: 1, time: 2000, skin: 'layui-layer-hui' }); } else { // 否则创建简单的通知 const notification = document.createElement('div'); notification.textContent = message; notification.style.cssText = ` position: fixed; top: 80px; right: 20px; background: linear-gradient(135deg, #059669 0%, #10b981 100%); color: white; padding: 12px 20px; border-radius: 8px; box-shadow: 0 4px 12px rgba(0,0,0,0.15); z-index: 10000; font-size: 14px; transform: translateX(100%); transition: transform 0.3s ease; `; document.body.appendChild(notification); // 动画显示 setTimeout(() => { notification.style.transform = 'translateX(0)'; }, 100); // 3秒后移除 setTimeout(() => { notification.style.transform = 'translateX(100%)'; setTimeout(() => { if (notification.parentNode) { notification.parentNode.removeChild(notification); } }, 300); }, 3000); } }, // 获取当前主题信息 getCurrentTheme: function() { return this.themes[this.currentTheme]; }, // 添加自定义主题 addCustomTheme: function(themeKey, themeConfig) { this.themes[themeKey] = themeConfig; this.createThemeControls(); // 重新创建控制界面 } }; // 自动初始化 if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function() { ThemeConfig.init(); }); } else { ThemeConfig.init(); } // 暴露到全局 window.ThemeConfig = ThemeConfig; })(window);