import { View, Text, Input, Button } from '@tarojs/components' import Taro from '@tarojs/taro' import { useState, useEffect } from 'react' import './index.scss' import { listAllUsers, switchUser, deleteUser, createUser, getUserInfo, verifyAdminPassword } from '../../utils/store' import { useThemeContext } from '../../context/ThemeContext' import { useSafeArea } from '../../hooks/useSafeArea' import { useStatusBar } from '../../hooks/useStatusBar' import ThemedPageMeta from '../../components/ThemedPageMeta' export default function UserDatabasePage() { const { theme, resolvedTheme } = useThemeContext() const safe = useSafeArea() useStatusBar(resolvedTheme) const [isUnlocked, setIsUnlocked] = useState(false) const [password, setPassword] = useState('') const [pwdError, setPwdError] = useState(false) const [users, setUsers] = useState([]) const [showAdd, setShowAdd] = useState(false) const [newNick, setNewNick] = useState('') const refresh = () => setUsers(listAllUsers()) useEffect(() => { refresh() }, []) const handleUnlock = () => { if (verifyAdminPassword(password)) { setIsUnlocked(true) setPwdError(false) refresh() } else { setPwdError(true) } } const handleSwitch = (openid: string) => { if (switchUser(openid)) { Taro.showToast({ title: '切换成功', icon: 'success' }) refresh() } else { Taro.showToast({ title: '切换失败', icon: 'none' }) } } const handleDelete = (openid: string) => { Taro.showModal({ title: '确认删除', content: '删除后将清空该用户所有数据,无法恢复', confirmColor: '#e64340', success: (res) => { if (res.confirm) { deleteUser(openid) refresh() Taro.showToast({ title: '已删除', icon: 'success' }) } } }) } const handleCreate = () => { if (!newNick.trim()) { Taro.showToast({ title: '请输入昵称', icon: 'none' }) return } createUser(newNick.trim()) setShowAdd(false) setNewNick('') refresh() Taro.showToast({ title: '创建成功', icon: 'success' }) } const activeOpenid = getUserInfo()?.openid || '' if (!isUnlocked) { return ( 🔐 管理员验证 请输入密码以访问用户数据库 setPassword(e.detail.value)} /> {pwdError && ( 密码错误,请重试 )} 进入数据库 ) } return ( 本地用户数据库 setShowAdd(true)}> + 新建账号 昵称 ID 设计/订单 操作 {users.map((u) => ( {u.nickName} {u.openid} {u.designCount} / {u.orderCount} {u.openid !== activeOpenid && ( handleSwitch(u.openid)}> 切换 )} {u.openid === activeOpenid && ( 当前 )} handleDelete(u.openid)}> 删除 ))} {users.length === 0 && ( 暂无用户数据 )} {/* 新建账号弹窗 */} {showAdd && ( 新建本地账号 setNewNick(e.detail.value)} /> setShowAdd(false)}> 取消 创建 )} ) }