From 90245dbb80aeb22cf99bafeedf298e46a42eaca8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=95=E7=BB=A7=E9=BE=99?= <1006325823@qq.com> Date: Tue, 15 Apr 2025 18:18:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AF=BC=E5=85=A5=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E9=87=8D=E5=A4=8D=E5=88=A4=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- electron-builder-config.json | 6 +- package.json | 3 +- src/App.js | 155 +++++++++++++++++++++-------------- src/services/ExcelService.js | 13 +-- 4 files changed, 101 insertions(+), 76 deletions(-) diff --git a/electron-builder-config.json b/electron-builder-config.json index 0be59ca..63af61e 100644 --- a/electron-builder-config.json +++ b/electron-builder-config.json @@ -12,10 +12,12 @@ "output": "release" }, "win": { - "target": "dir" + "target": "portable", + "sign": null, + "forceCodeSigning": false }, "npmRebuild": false, - "asar": false, + "asar": true, "extraMetadata": { "main": "main.js" } diff --git a/package.json b/package.json index 3ecf220..f65e1b8 100644 --- a/package.json +++ b/package.json @@ -9,8 +9,9 @@ "react-start": "react-scripts start", "react-build": "react-scripts build", "rebuild": "electron-rebuild", - "build": "electron-builder --publish never", + "build": "electron-builder --config electron-builder-config.json --publish never", "package": "npm run react-build && npm run rebuild && npm run build", + "package-portable": "npm run react-build && electron-packager . DataTools --platform=win32 --arch=x64 --out=release --overwrite --asar", "test": "react-scripts test", "eject": "react-scripts eject" }, diff --git a/src/App.js b/src/App.js index 32b409d..8f4040d 100644 --- a/src/App.js +++ b/src/App.js @@ -1,4 +1,4 @@ -import React, { useState, useEffect } from 'react'; +import React, { useState, useEffect, useRef } from 'react'; import { ConfigProvider, theme, Switch, Space, Modal, Progress, message } from 'antd'; import zhCN from 'antd/locale/zh_CN'; import dayjs from 'dayjs'; @@ -26,6 +26,7 @@ function App() { const [currentProject, setCurrentProject] = useState(null); const [loading, setLoading] = useState(false); const [isDarkMode] = useState(true); // 固定使用深色主题 + const [lastImportedFilePath, setLastImportedFilePath] = useState(null); // 记录最后导入的文件路径 // 初始化数据 useEffect(() => { @@ -203,68 +204,22 @@ function App() { const filePath = await window.electronAPI.selectExcelFile(); if (filePath) { - setLoading(true); - - // 创建进度对话框 - const progressModal = Modal.info({ - title: '导入Excel', - content: ( -
-

正在准备导入...

- -
- ), - icon: null, - okButtonProps: { style: { display: 'none' } }, - maskClosable: false, - closable: false, - }); - - // 监听导入进度 - window.electronAPI.onImportProgress((data) => { - // 更新进度条和消息 - const progressBar = document.getElementById('import-progress-bar'); - const progressMessage = document.getElementById('import-progress-message'); - - if (progressBar && progressMessage) { - // 更新进度条 - const antProgress = progressBar.querySelector('.ant-progress-bg'); - if (antProgress) { - antProgress.style.width = `${data.progress}%`; - antProgress.setAttribute('aria-valuenow', data.progress); + // 检查是否重复导入同一个文件 + if (lastImportedFilePath === filePath) { + // 显示确认对话框 + Modal.confirm({ + title: '文件已导入', + content: '该文件已经导入过,重新导入会覆盖之前的记录。确定要继续吗?', + okText: '确定', + cancelText: '取消', + onOk: () => { + // 用户确认后继续导入 + importFile(filePath); } - - // 更新文本 - progressMessage.textContent = data.message; - - // 如果导入完成,关闭对话框并重新加载数据 - if (data.status === 'complete') { - setTimeout(() => { - progressModal.destroy(); - message.success('Excel导入成功'); - // 重新加载数据 - loadData(); - setLoading(false); - }, 1000); - } else if (data.status === 'error') { - setTimeout(() => { - progressModal.destroy(); - message.error(`导入失败: ${data.message}`); - setLoading(false); - }, 1000); - } - } - }); - - // 导入Excel文件 - const result = await window.electronAPI.importExcel(filePath); - - // 如果导入失败且没有进度事件处理,则显示错误消息 - if (!result.success) { - progressModal.destroy(); - message.error(`导入失败: ${result.error}`); - console.error('导入Excel文件失败:', result.error); - setLoading(false); + }); + } else { + // 直接导入文件 + importFile(filePath); } } } catch (error) { @@ -274,6 +229,82 @@ function App() { } }; + // 导入文件的实际逻辑 + const importFile = async (filePath) => { + try { + setLoading(true); + + // 创建进度对话框 + const progressModal = Modal.info({ + title: '导入Excel', + content: ( +
+

正在准备导入...

+ +
+ ), + icon: null, + okButtonProps: { style: { display: 'none' } }, + maskClosable: false, + closable: false, + }); + + // 监听导入进度 + window.electronAPI.onImportProgress((data) => { + // 更新进度条和消息 + const progressBar = document.getElementById('import-progress-bar'); + const progressMessage = document.getElementById('import-progress-message'); + + if (progressBar && progressMessage) { + // 更新进度条 + const antProgress = progressBar.querySelector('.ant-progress-bg'); + if (antProgress) { + antProgress.style.width = `${data.progress}%`; + antProgress.setAttribute('aria-valuenow', data.progress); + } + + // 更新文本 + progressMessage.textContent = data.message; + + // 如果导入完成,关闭对话框并重新加载数据 + if (data.status === 'complete') { + setTimeout(() => { + progressModal.destroy(); + message.success('Excel导入成功'); + // 重新加载数据 + loadData(); + setLoading(false); + }, 1000); + } else if (data.status === 'error') { + setTimeout(() => { + progressModal.destroy(); + message.error(`导入失败: ${data.message}`); + setLoading(false); + }, 1000); + } + } + }); + + // 导入Excel文件 + const result = await window.electronAPI.importExcel(filePath); + + // 如果导入失败且没有进度事件处理,则显示错误消息 + if (!result.success) { + progressModal.destroy(); + message.error(`导入失败: ${result.error}`); + console.error('导入Excel文件失败:', result.error); + setLoading(false); + } else { + // 导入成功,更新最后导入的文件路径 + setLastImportedFilePath(filePath); + } + } catch (error) { + console.error('导入Excel文件失败:', error); + message.error(`导入失败: ${error.message}`); + setLoading(false); + } + }; + // 处理清除数据 const handleClearData = async () => { // 显示确认对话框 diff --git a/src/services/ExcelService.js b/src/services/ExcelService.js index 09c26f1..c36d28e 100644 --- a/src/services/ExcelService.js +++ b/src/services/ExcelService.js @@ -148,16 +148,8 @@ class ExcelService { // 将数据保存到数据库 saveDataToDatabase(data) { return new Promise((resolve, reject) => { - // 清空现有数据 - this.db.run('DELETE FROM projects', (err) => { - if (err) { - console.error('清空项目数据失败:', err); - reject(err); - return; - } - - // 插入新数据 - const stmt = this.db.prepare(` + // 不再清空现有数据,直接插入新数据 + const stmt = this.db.prepare(` INSERT INTO projects ( unit, project_number, @@ -303,7 +295,6 @@ class ExcelService { // 完成后关闭准备好的语句 stmt.finalize(); }); - }); } // 构建树状结构