From b775c4043773e2eac816cb271f16e056e6d5f8dd Mon Sep 17 00:00:00 2001 From: Vibe Studio Date: Wed, 4 Feb 2026 09:39:59 +0000 Subject: [PATCH] Update from Vibe Studio --- .env.development | 5 +- src/api/number-to-chinese.ts | 29 +++++ src/pages/number-to-chinese/index.tsx | 173 ++++++++++++++++++++++++++ src/router/index.tsx | 4 + 4 files changed, 210 insertions(+), 1 deletion(-) create mode 100644 src/api/number-to-chinese.ts create mode 100644 src/pages/number-to-chinese/index.tsx diff --git a/.env.development b/.env.development index c08fb06..32c9bb4 100644 --- a/.env.development +++ b/.env.development @@ -15,4 +15,7 @@ VITE_API_BASE_VOICE=/voice-api # AI后端请求地址 VITE_AI_API_BASE=/v1 # Xiren-Lite SDK 配置 - 开发环境使用代理 -VITE_XIREN_LITE_BASE_URL=/xiren-api \ No newline at end of file +VITE_XIREN_LITE_BASE_URL=/xiren-api +# Dify AI 配置 +VITE_DIFY_API_BASE=https://copilot.sino-bridge.com/v1 +VITE_DIFY_APP_KEY=app-1YVGGD6IgKByI4CwHqe68pOB \ No newline at end of file diff --git a/src/api/number-to-chinese.ts b/src/api/number-to-chinese.ts new file mode 100644 index 0000000..8e607d1 --- /dev/null +++ b/src/api/number-to-chinese.ts @@ -0,0 +1,29 @@ +/** + * 数字转中文大写转换器 API + */ + +/** + * 转换数字为中文大写 + * @param number - 要转换的数字 + * @returns Promise - 流式响应 + */ +export async function convertNumberToChinese(number: string): Promise { + 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 }, + query: '1', + response_mode: 'streaming' + }) + }) + + if (!response.ok) { + throw new Error('转换请求失败') + } + + return response.body! +} diff --git a/src/pages/number-to-chinese/index.tsx b/src/pages/number-to-chinese/index.tsx new file mode 100644 index 0000000..c63de02 --- /dev/null +++ b/src/pages/number-to-chinese/index.tsx @@ -0,0 +1,173 @@ +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 && ( +
+ 输入数字后点击转换按钮,结果将显示在这里 +
+ )} +
+
+
+ ) +} diff --git a/src/router/index.tsx b/src/router/index.tsx index 231190a..3e58e7d 100644 --- a/src/router/index.tsx +++ b/src/router/index.tsx @@ -30,6 +30,10 @@ const router: RouteObject[] = [ children: [ { path: '/', + element: LazyLoad(lazy(() => import('@/pages/number-to-chinese'))) + }, + { + path: '/home', element: LazyLoad(lazy(() => import('@/pages/home'))) }, {