初始化:质量数据分析平台源码

This commit is contained in:
2026-10-22 15:58:57 +08:00
commit 1e8a655adc
280 changed files with 43922 additions and 0 deletions
+70
View File
@@ -0,0 +1,70 @@
import { useRef, useState } from 'react';
import { Alert, App, Button, Form, Input, Modal, Spin } from 'antd';
import { SettingOutlined } from '@ant-design/icons';
import { api } from './api';
export type AnalysisModelSettings = { baseUrl: string; model: string; hasApiKey: boolean; revision: string };
export type AnalysisModelSettingsInput = { baseUrl: string; model: string; apiKey?: string; revision?: string };
export function AnalysisApiSettings({ onUnauthorized }: { onUnauthorized: (error: unknown) => boolean }) {
const { message } = App.useApp();
const [form] = Form.useForm<AnalysisModelSettingsInput>();
const [open, setOpen] = useState(false);
const [loading, setLoading] = useState(false);
const [saving, setSaving] = useState(false);
const [testing, setTesting] = useState(false);
const [settings, setSettings] = useState<AnalysisModelSettings>();
const [notice, setNotice] = useState<{ type: 'success' | 'error'; text: string }>();
const sequence = useRef(0);
const close = () => { if (saving) return; sequence.current += 1; setOpen(false); form.resetFields(); setNotice(undefined); setTesting(false); };
const load = async () => {
const current = ++sequence.current;
setOpen(true); setLoading(true); setSettings(undefined); setNotice(undefined); form.resetFields();
try {
const value = await api.analysisSettings();
if (sequence.current !== current) return;
setSettings(value); form.setFieldsValue({ baseUrl: value.baseUrl, model: value.model, apiKey: '' });
} catch (error) {
if (sequence.current === current && !onUnauthorized(error)) setNotice({ type: 'error', text: error instanceof Error ? error.message : '读取配置失败' });
} finally { if (sequence.current === current) setLoading(false); }
};
const submit = async (test: boolean) => {
if (!settings) return;
let values: AnalysisModelSettingsInput;
try { values = await form.validateFields(); } catch { return; }
const current = sequence.current;
setNotice(undefined); test ? setTesting(true) : setSaving(true);
try {
const body = { ...values, revision: settings.revision };
if (test) {
const result = await api.testAnalysisSettings(body);
if (sequence.current === current) setNotice({ type: result.success ? 'success' : 'error', text: result.message });
} else {
const result = await api.saveAnalysisSettings(body);
if (sequence.current !== current) return;
setSettings(result); form.setFieldsValue({ apiKey: '' });
setNotice({ type: 'success', text: '已保存,下一轮分析使用新配置。' }); message.success('API 配置已保存');
}
} catch (error) {
if (sequence.current === current && !onUnauthorized(error)) setNotice({ type: 'error', text: error instanceof Error ? error.message : '操作失败,请重试' });
} finally { if (sequence.current === current) { setTesting(false); setSaving(false); } }
};
return <>
<Button type="text" size="small" icon={<SettingOutlined />} onClick={() => void load()}>API 配置</Button>
<Modal title="API 配置" open={open} width={540} onCancel={close} footer={<>
<Button onClick={close} disabled={saving}>关闭</Button>
<Button loading={testing} disabled={loading || saving || !settings} onClick={() => void submit(true)}>测试连接</Button>
<Button type="primary" loading={saving} disabled={loading || testing || !settings} onClick={() => void submit(false)}>保存配置</Button>
</>}>
{loading ? <div style={{ padding: 32, textAlign: 'center' }}><Spin /></div> : <>
<Form form={form} layout="vertical" disabled={saving || testing} onValuesChange={() => setNotice(undefined)}>
<Form.Item name="baseUrl" label="接口地址" rules={[{ required: true, whitespace: true, message: '请填写接口地址' }]} extra="支持 OpenAI 兼容接口,可填写含 /v1 的基础地址或完整的 /chat/completions 地址。"><Input placeholder="https://api.example.com/v1" autoComplete="off" maxLength={2048} /></Form.Item>
<Form.Item name="apiKey" label="API Key" extra={settings?.hasApiKey ? '已配置密钥;留空保留。更换接口地址时需重新填写。' : '密钥仅保存在服务端,保存后不回显。'} rules={settings?.hasApiKey ? [] : [{ required: true, whitespace: true, message: '请填写 API Key' }]}><Input.Password placeholder={settings?.hasApiKey ? '已配置,留空保留' : '请输入 API Key'} autoComplete="new-password" maxLength={4096} /></Form.Item>
<Form.Item name="model" label="模型名称" rules={[{ required: true, whitespace: true, message: '请填写模型名称' }]}><Input placeholder="例如 qwen3.8-27b-local" autoComplete="off" maxLength={160} /></Form.Item>
</Form>
{notice && <Alert showIcon type={notice.type} message={notice.text} />}
{!settings && notice && <Button style={{ marginTop: 12 }} onClick={() => void load()}>重新读取</Button>}
</>}
</Modal>
</>;
}