import { useState } from 'react' import { Form, Input, Button, Card, Typography, Space, Spin } from 'antd' import { ArrowRightOutlined, ClearOutlined } from '@ant-design/icons' import type { FetchStreamOptions } from '@copilotkit/react-core' const { Title, Text } = Typography interface DifyResponse { message: string } export default function NumberToChinese() { const [form] = Form.useForm() const [loading, setLoading] = useState(false) const [result, setResult] = useState('') const [error, setError] = useState('') const handleConvert = async (values: { number: string }) => { setLoading(true) setError('') setResult('') try { const response = await fetch(`${import.meta.env.VITE_DIFY_API_BASE}/chat-messages`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${import.meta.env.VITE_DIFY_APP_KEY}` }, body: JSON.stringify({ inputs: { number: values.number }, query: '1', response_mode: 'streaming' }) }) if (!response.ok) { throw new Error('请求失败') } const reader = response.body?.getReader() if (!reader) { throw new Error('无法读取响应流') } const decoder = new TextDecoder() let fullContent = '' while (true) { const { done, value } = await reader.read() if (done) break const chunk = decoder.decode(value) const lines = chunk.split('\n') for (const line of lines) { if (line.startsWith('data: ')) { try { const data = JSON.parse(line.slice(6)) as DifyResponse if (data.message) { fullContent += data.message setResult(fullContent) } } catch { // 忽略解析错误 } } } } } catch (err) { setError(err instanceof Error ? err.message : '转换失败,请稍后重试') } finally { setLoading(false) } } const handleReset = () => { form.resetFields() setResult('') setError('') } return (
{/* 页面标题区 */}
数字转中文大写转换器
{/* 参数输入区 */}
{/* 内容展示区 */} {loading && (
转换中...
)} {!loading && error && (
{error}
)} {!loading && !error && result && (
转换结果:
{result}
)} {!loading && !error && !result && (
输入数字后点击转换按钮,结果将显示在这里
)}
) }