Files
glt-taro/src/user/theme/index.tsx
赵忠林 0770eb1699 feat(home): 重构首页界面并更新API配置
- 移除底部导航栏中的"基地生活"选项卡
- 切换开发环境API地址为线上测试接口
- 添加完整的首页样式定义,包括英雄区域、商品卡片、快捷入口等
- 重构首页组件结构,集成商品列表、分类标签页和交互功能
- 更新主题管理逻辑,支持多种主题模式和用户ID兼容处理
- 添加商品数据获取和展示功能,实现首页内容动态加载
2026-01-15 10:12:49 +08:00

180 lines
6.1 KiB
TypeScript
Raw 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.

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<string>('')
const [currentTheme, setCurrentTheme] = useState<GradientTheme | null>(null)
// 获取当前主题
useEffect(() => {
const savedTheme = Taro.getStorageSync('user_theme') || 'nature'
setSelectedTheme(savedTheme)
if (savedTheme === 'auto') {
// 自动主题根据用户ID生成
const userId = Taro.getStorageSync('UserId') ?? Taro.getStorageSync('userId') ?? '1'
const theme = gradientUtils.getThemeByUserId(typeof userId === 'number' ? userId : parseInt(String(userId), 10))
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') ?? Taro.getStorageSync('userId') ?? '1'
const theme = gradientUtils.getThemeByUserId(typeof userId === 'number' ? userId : parseInt(String(userId), 10))
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') ?? Taro.getStorageSync('userId') ?? '1'
const theme = gradientUtils.getThemeByUserId(typeof userId === 'number' ? userId : parseInt(String(userId), 10))
setCurrentTheme(theme)
} else {
const theme = gradientThemes.find(t => t.name === themeName)
setCurrentTheme(theme || gradientThemes[0])
}
}
return (
<View className="min-h-screen bg-gray-50">
{/* 当前主题预览 */}
{currentTheme && (
<View
className="mx-4 mt-4 rounded-xl p-6 text-center"
style={{
background: currentTheme.background,
color: currentTheme.textColor
}}
>
<Text className="text-lg font-bold mb-2"></Text>
<Text className="text-sm opacity-90 px-2">{currentTheme.description}</Text>
<View className="mt-4 flex justify-center space-x-4">
<View
className="w-8 h-8 rounded-full"
style={{ backgroundColor: currentTheme.primary }}
></View>
{currentTheme.secondary && (
<View
className="w-8 h-8 rounded-full"
style={{ backgroundColor: currentTheme.secondary }}
></View>
)}
</View>
</View>
)}
{/* 主题选择 */}
<View className="mt-4">
<CellGroup>
<Cell
className="px-4 py-2"
title={
<View className="flex items-center justify-between w-full">
<View>
<Text className="font-medium"></Text>
<Text className="text-sm text-gray-500 mt-1">
ID自动选择个性化主题
</Text>
</View>
<Radio
checked={selectedTheme === 'auto'}
onChange={() => {
setSelectedTheme('auto')
previewTheme('auto')
}}
/>
</View>
}
onClick={() => {
setSelectedTheme('auto')
previewTheme('auto')
}}
/>
</CellGroup>
<View className="mt-4">
<Text className="text-sm text-gray-600 px-4 mb-2"></Text>
<CellGroup>
{gradientThemes.map((theme) => (
<Cell
key={theme.name}
className="px-4 py-3"
title={
<View className="flex items-center justify-between w-full">
<View className="flex items-center">
<View
className="w-6 h-6 rounded-full mr-3"
style={{ background: theme.background }}
></View>
<View>
<Text className="font-medium">{theme.description.split(' - ')[0]}</Text>
<Text className="text-sm text-gray-500 mt-1">
{theme.description.split(' - ')[1]}
</Text>
</View>
</View>
<Radio
checked={selectedTheme === theme.name}
onChange={() => {
setSelectedTheme(theme.name)
previewTheme(theme.name)
}}
/>
</View>
}
onClick={() => {
setSelectedTheme(theme.name)
previewTheme(theme.name)
}}
/>
))}
</CellGroup>
</View>
</View>
{/* 保存按钮 */}
<FixedButton text={'保存主题设置'} background={currentTheme?.background || '#1890ff'} onClick={() => saveTheme(selectedTheme)} />
{/* 底部安全区域 */}
<View className="h-20"></View>
</View>
)
}
export default ThemeSelector