From 3f13fa42876867c4a7ca723a4d0df18b03587f03 Mon Sep 17 00:00:00 2001 From: Shu Guang <61069967+shuguangnet@users.noreply.github.com> Date: Sat, 19 Apr 2025 01:16:11 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=87=E4=BB=B6=E5=88=86=E6=9E=90?= =?UTF-8?q?=E6=94=AF=E6=8C=81csv=20excel=20img?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 3 +- src/pages/Report/index.tsx | 129 +++++++++++++++++++++++-------------- 2 files changed, 83 insertions(+), 49 deletions(-) diff --git a/package.json b/package.json index 7b9b9c3..0ace9b5 100644 --- a/package.json +++ b/package.json @@ -70,7 +70,8 @@ "react-echarts": "^0.1.1", "react-helmet-async": "^1.3.0", "react-intl": "^7.1.6", - "react-markdown": "^10.1.0" + "react-markdown": "^10.1.0", + "xlsx": "^0.18.5" }, "devDependencies": { "@ant-design/pro-cli": "^3.3.0", diff --git a/src/pages/Report/index.tsx b/src/pages/Report/index.tsx index 572ef2b..a4b40cf 100644 --- a/src/pages/Report/index.tsx +++ b/src/pages/Report/index.tsx @@ -8,6 +8,7 @@ import { marked } from 'marked'; import styles from './index.less'; import { Document, Packer, Paragraph as DocxParagraph, TextRun } from 'docx'; import { saveAs } from 'file-saver'; +import * as XLSX from 'xlsx'; const { Dragger } = Upload; const { TextArea } = Input; @@ -49,59 +50,91 @@ const ReportPage: React.FC = () => { const handleUpload = async (file: File) => { setLoading(true); try { - // 读取文件为 base64 - const reader = new FileReader(); - reader.readAsDataURL(file); - reader.onload = async () => { - const base64Image = reader.result as string; - - // 调用 NewAPI 接口 - try { - const response = await fetch('https://aizex.top/v1/chat/completions', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'Authorization': 'Bearer sk-Bp4AtAw19a6lENrPUQeqfiS9KP46Z5A43j4QkNeX4NRnGKMU' - }, - body: JSON.stringify({ - model: 'gpt-4o', - messages: [ - { - role: 'user', - content: [ - { - type: 'text', - text: '请分析这张图表中的数据趋势和关键信息,并给出专业的分析见解。' - }, - { - type: 'image_url', - image_url: { - url: base64Image - } - } - ] - } - ], - max_tokens: 1000 - }) - }); + const isImage = file.type.startsWith('image/'); + const isExcel = file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' || + file.type === 'application/vnd.ms-excel'; + const isCsv = file.type === 'text/csv'; - const data: AnalysisResponse = await response.json(); - - setPreviewData({ - columns: ['分析结果'], - data: [[data.choices[0].message.content]] + if (isImage) { + // 处理图片文件 + const reader = new FileReader(); + reader.readAsDataURL(file); + reader.onload = async () => { + const base64Image = reader.result as string; + await analyzeData({ + type: 'image_url', + image_url: { + url: base64Image + } }); + }; + } else if (isExcel || isCsv) { + // 处理 Excel/CSV 文件 + const reader = new FileReader(); + reader.onload = async (e) => { + const data = e.target?.result; + const workbook = XLSX.read(data, { type: 'array' }); + const firstSheet = workbook.Sheets[workbook.SheetNames[0]]; + const jsonData = XLSX.utils.sheet_to_json(firstSheet, { header: 1 }); - message.success('图片分析成功'); - setCurrentStep(1); - } catch (error) { - message.error('图片分析失败'); - console.error('API调用失败:', error); - } - }; + // 将数据转换为字符串 + const textContent = jsonData.map(row => row.join('\t')).join('\n'); + + await analyzeData({ + type: 'text', + text: textContent + }); + }; + reader.readAsArrayBuffer(file); + } else { + message.error('不支持的文件格式'); + setLoading(false); + } } catch (error) { message.error('文件处理失败'); + console.error('文件处理失败:', error); + setLoading(false); + } + }; + + const analyzeData = async (content: any) => { + try { + const response = await fetch('https://aizex.top/v1/chat/completions', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Authorization': 'Bearer sk-Bp4AtAw19a6lENrPUQeqfiS9KP46Z5A43j4QkNeX4NRnGKMU' + }, + body: JSON.stringify({ + model: 'gpt-4o', + messages: [ + { + role: 'user', + content: [ + { + type: 'text', + text: '请分析这些数据的趋势和关键信息,并给出专业的分析见解。' + }, + content + ] + } + ], + max_tokens: 1000 + }) + }); + + const data: AnalysisResponse = await response.json(); + + setPreviewData({ + columns: ['分析结果'], + data: [[data.choices[0].message.content]] + }); + + message.success('数据分析成功'); + setCurrentStep(1); + } catch (error) { + message.error('数据分析失败'); + console.error('API调用失败:', error); } finally { setLoading(false); }