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 (
setCompanyDraft((v) => ({ ...v, name: e.target.value }))} /> setCompanyDraft((v) => ({ ...v, industry: e.target.value }))} /> setCompanyDraft((v) => ({ ...v, contact: e.target.value }))} /> setCompanyDraft((v) => ({ ...v, email: e.target.value }))} /> setCompanyDraft((v) => ({ ...v, intro: e.target.value }))} />
)} {section === 'ai' && settings && (
{[ { key: 'autoGenerate', label: '自动生成回复建议' }, { key: 'requireConfirm', label: '发送前必须人工确认' }, { key: 'sentimentAnalysis', label: '启用情感分析' }, { key: 'messageIntentAnalysis', label: '逐条客户消息意图识别' }, { key: 'contextualTranslation', label: '翻译时参考会话上下文' }, { key: 'riskAlert', label: '启用风险预警' }, { key: 'cultureCheck', label: '启用文化敏感检测' }, { key: 'allowHistoryRef', label: '允许AI引用历史成功回复' }, ].map((item) => (
{item.label} updateMut.mutate({ ai: { [item.key]: v } })} />
))}
)} {section === 'notifications' && settings && (
{[ { key: 'newInquiry', label: '新询盘提醒' }, { key: 'highIntent', label: '高意向客户提醒' }, { key: 'riskAlert', label: '风险预警提醒' }, { key: 'taskDue', label: '跟进到期提醒' }, { key: 'taskOverdue', label: '跟进逾期提醒' }, { key: 'channelOffline', label: '渠道断开提醒' }, { key: 'planExpire', label: '套餐到期提醒' }, ].map((item) => (
{item.label} updateMut.mutate({ notification: { [item.key]: v } })} />
))}
)} {section === 'safety' && (
setPasswords((v) => ({ ...v, currentPassword: e.target.value }))} /> setPasswords((v) => ({ ...v, newPassword: e.target.value }))} /> setPasswords((v) => ({ ...v, confirmPassword: e.target.value }))} />
修改密码后,其他已登录设备需要重新登录。
)} {section === 'api' && (

密钥只在创建时完整显示一次,请立即复制并妥善保存。

setApiName(e.target.value)} placeholder="密钥名称" />
{newApiKey && (

请立即复制,关闭后无法再次查看

{newApiKey}
)}
{(apiKeys || []).map((key) => (

{key.name}

{key.prefix}•••••• · {key.revoked ? '已撤销' : '有效'}

{!key.revoked && }
))}
)} {section === 'lang' && (
setTimezoneDraft(e.target.value)} options={[{value:'Asia/Hong_Kong',label:'香港 (UTC+8)'},{value:'Asia/Shanghai',label:'上海 (UTC+8)'},{value:'Asia/Dubai',label:'迪拜 (UTC+4)'},{value:'America/New_York',label:'纽约 (UTC-5)'}]} />
)}
) }