import React, { useState, useEffect } from 'react' import { View, Text } from '@tarojs/components' import { Cell, CellGroup, Radio } from '@nutui/nutui-react-taro' import { gradientThemes, GradientTheme, gradientUtils } from '@/styles/gradients' import Taro from '@tarojs/taro' import FixedButton from "@/components/FixedButton"; const ThemeSelector: React.FC = () => { const [selectedTheme, setSelectedTheme] = useState('') const [currentTheme, setCurrentTheme] = useState(null) // 获取当前主题 useEffect(() => { const savedTheme = Taro.getStorageSync('user_theme') || 'auto' setSelectedTheme(savedTheme) if (savedTheme === 'auto') { // 自动主题:根据用户ID生成 const userId = Taro.getStorageSync('userId') || '1' const theme = gradientUtils.getThemeByUserId(userId) setCurrentTheme(theme) } else { // 手动选择的主题 const theme = gradientThemes.find(t => t.name === savedTheme) setCurrentTheme(theme || gradientThemes[0]) } }, []) // 保存主题设置 const saveTheme = (themeName: string) => { try { Taro.setStorageSync('user_theme', themeName) setSelectedTheme(themeName) if (themeName === 'auto') { const userId = Taro.getStorageSync('userId') || '1' const theme = gradientUtils.getThemeByUserId(userId) setCurrentTheme(theme) } else { const theme = gradientThemes.find(t => t.name === themeName) setCurrentTheme(theme || gradientThemes[0]) } Taro.showToast({ title: '主题已保存', icon: 'success', }) // 延迟返回,让用户看到效果 setTimeout(() => { Taro.navigateBack() }, 1000) } catch (error) { Taro.showToast({ title: '保存失败', icon: 'error', }) } } // 预览主题 const previewTheme = (themeName: string) => { if (themeName === 'auto') { const userId = Taro.getStorageSync('userId') || '1' const theme = gradientUtils.getThemeByUserId(userId) setCurrentTheme(theme) } else { const theme = gradientThemes.find(t => t.name === themeName) setCurrentTheme(theme || gradientThemes[0]) } } return ( {/* 当前主题预览 */} {currentTheme && ( 当前主题预览 {currentTheme.description} {currentTheme.secondary && ( )} )} {/* 主题选择 */} 智能主题 根据您的用户ID自动选择个性化主题 { setSelectedTheme('auto') previewTheme('auto') }} /> } onClick={() => { setSelectedTheme('auto') previewTheme('auto') }} /> 手动选择主题 {gradientThemes.map((theme) => ( {theme.description.split(' - ')[0]} {theme.description.split(' - ')[1]} { setSelectedTheme(theme.name) previewTheme(theme.name) }} /> } onClick={() => { setSelectedTheme(theme.name) previewTheme(theme.name) }} /> ))} {/* 保存按钮 */} saveTheme(selectedTheme)} /> {/* 底部安全区域 */} ) } export default ThemeSelector