import { useEffect, useState } from 'react' import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' import { ShieldCheck, KeyRound, Bell, Globe, Languages, UserCog, Copy, Trash2 } from 'lucide-react' import { toast, errorMessage } from '@/app/store/toast' import { PageHeader } from '@/components/common/PageHeader' import { Button } from '@/components/ui/button' import { Card, CardHeader } from '@/components/ui/card' import { Input, Field } from '@/components/ui/input' import { Select } from '@/components/ui/select' import { Switch } from '@/components/ui/switch' import { getSettings, updateSettings } from '@/services/api/org' import { useI18n } from '@/i18n' import { changePassword, createApiKey, getApiKeys, revokeApiKey } from '@/services/api/auth' import { useAuthStore } from '@/app/store/auth' const SECTIONS = [ { key: 'company', label: '企业资料', icon: UserCog }, { key: 'lang', label: '语言与时区', icon: Globe }, { key: 'notifications', label: '消息通知', icon: Bell }, { key: 'ai', label: '智能回复', icon: Languages }, { key: 'safety', label: '安全设置', icon: ShieldCheck }, { key: 'api', label: 'API Key', icon: KeyRound }, ] export function SettingsPage() { const queryClient = useQueryClient() const [section, setSection] = useState('company') const { data: settings, isLoading } = useQuery({ queryKey: ['settings'], queryFn: getSettings }) const { locale, setLocale, t } = useI18n() const logout = useAuthStore((state) => state.logout) const [companyDraft, setCompanyDraft] = useState({ name: '', industry: '', contact: '', email: '', intro: '' }) const [passwords, setPasswords] = useState({ currentPassword: '', newPassword: '', confirmPassword: '' }) const [localeDraft, setLocaleDraft] = useState(locale) const [timezoneDraft, setTimezoneDraft] = useState('Asia/Hong_Kong') const [apiName, setApiName] = useState('业务集成') const [newApiKey, setNewApiKey] = useState('') const { data: apiKeys } = useQuery({ queryKey: ['api-keys'], queryFn: getApiKeys, enabled: section === 'api' }) useEffect(() => { if (settings?.company) setCompanyDraft({ name: settings.company.name || '', industry: settings.company.industry || '', contact: settings.company.contact || '', email: settings.company.email || '', intro: settings.company.intro || '', }) if (settings?.language) setLocaleDraft(settings.language.startsWith('zh') ? 'zh' : settings.language as any) if (settings?.timezone) setTimezoneDraft(settings.timezone) }, [settings]) const updateMut = useMutation({ mutationFn: (payload: any) => updateSettings(payload), onSuccess: () => { toast.success('设置已保存'); queryClient.invalidateQueries({ queryKey: ['settings'] }) }, onError: (e) => toast.error(errorMessage(e)), }) return (
设置分类
{SECTIONS.map((s) => ( ))}密钥只在创建时完整显示一次,请立即复制并妥善保存。
请立即复制,关闭后无法再次查看
{newApiKey}
{key.name}
{key.prefix}•••••• · {key.revoked ? '已撤销' : '有效'}