From a30d2945a0a63a0d4a210d58b85e8db66f099450 Mon Sep 17 00:00:00 2001 From: zhuhao <979263092@qq.com> Date: Thu, 18 Jul 2024 15:17:15 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=9F=BA=E7=A1=80=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=A0=B8=E5=BF=83=E5=BC=80=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/file.js | 147 +++++++++++++++++- src/renderer/src/utils/talkFile/index.js | 31 +++- .../prepare/container/file-list-item.vue | 4 +- .../prepare/container/file-oper-batch.vue | 26 +++- src/renderer/src/views/prepare/index.vue | 16 +- 5 files changed, 203 insertions(+), 21 deletions(-) diff --git a/src/main/file.js b/src/main/file.js index eecd08c..170d134 100644 --- a/src/main/file.js +++ b/src/main/file.js @@ -2,18 +2,22 @@ const fs = require('fs') const path = require('path') import { ElectronDownloadManager } from 'electron-dl-manager' import { dialog } from 'electron' +import axios from 'axios' +const uploadUrl = import.meta.env.VITE_APP_BASE_API + '/smarttalk/file/upload' +console.log(uploadUrl) const manager = new ElectronDownloadManager() export default async function ({ app, shell, BrowserWindow, ipcMain }) { const userDataPath = app.getPath('userData') const appRootFilePath = userDataPath + '\\selfFile\\' + const appTempFilePath = userDataPath + '\\tempFile\\' ipcMain.on('is-have-local-file', (e, fileNewName) => { let filePath = appRootFilePath + fileNewName fs.access(filePath, fs.constants.F_OK, (err) => { if (err) { - e.reply('is-have-local-file-reply'+fileNewName, false) + e.reply('is-have-local-file-reply' + fileNewName, false) return } - e.reply('is-have-local-file-reply'+fileNewName, true) + e.reply('is-have-local-file-reply' + fileNewName, true) }) }) //默认浏览器打开url @@ -35,6 +39,69 @@ export default async function ({ app, shell, BrowserWindow, ipcMain }) { }) }) + //复制文件 + ipcMain.on('export-file-default', (e, list) => { + exportFile(list, (res) => { + e.reply('export-file-default-reply', res) + }) + }) + + ipcMain.on('creat-file-default', (e, { name, uploadData, cookie }) => { + createFolder('tempFile').then(() => { + let path = appTempFilePath + name + fs.writeFileSync(path, '', 'utf-8') + // 读取文件 + fs.readFile(path, (err, data) => { + if (err) { + return console.error(err) + } + console.log(cookie) + // 配置上传的请求 + // const uploadUrl = 'http://192.168.2.52:7863/smarttalk/file/upload' // 你的上传服务URL + const config = { + headers: { + 'Content-Type': 'application/octet-stream', // 或者其他适合上传文件的Content-Type + Authorization: 'Bearer ' + cookie + } + } + + // 使用axios上传文件 + axios + .post(uploadUrl, { ...uploadData, data }, config) + .then((response) => { + console.log('File uploaded successfully:', response.data) + }) + .catch((error) => { + console.error('Error uploading file:', error) + }) + }) + /*fs.readFile(path, (err, data) => { + if (err) { + return console.error(err) + } + let file = new File([data], '111.pptx', { + type: 'application/vnd.openxmlformats-officedocument.presentationml.presentation' + }) + const headers = { + 'Content-Type': 'multipart/form-data', + Authorization: 'Bearer ' + cookie + } + axios + .post( + import.meta.env.VITE_APP_BASE_API + '/smarttalk/file/upload', + { + file + }, + { headers } + ) + .then((res) => { + console.log(res) + }) + // e.reply('creat-file-default-reply', res) + })*/ + }) + }) + //获取应用文件目录 ipcMain.on('get-root-file-path', (e) => { e.reply('get-root-file-path-reply', appRootFilePath) @@ -53,23 +120,22 @@ export default async function ({ app, shell, BrowserWindow, ipcMain }) { onDownloadStarted: async ({ id, item, webContents }) => { // Do something with the download id }, - onDownloadProgress: async ({ id, item, percentCompleted }) => { - }, + onDownloadProgress: async ({ id, item, percentCompleted }) => {}, onDownloadCompleted: async ({ id, item }) => { console.log('完成') - e.reply('download-file-default'+fileName,true) + e.reply('download-file-default' + fileName, true) }, onDownloadCancelled: async () => { console.log('取消') - e.reply('download-file-default'+fileName,false) + e.reply('download-file-default' + fileName, false) }, onDownloadInterrupted: async () => { console.log('中断') - e.reply('download-file-default'+fileName,false) + e.reply('download-file-default' + fileName, false) }, onError: (err, data) => { console.log(err.toString()) - e.reply('download-file-default'+fileName,false) + e.reply('download-file-default' + fileName, false) } } }) @@ -124,6 +190,71 @@ export default async function ({ app, shell, BrowserWindow, ipcMain }) { }) }) + function exportFile(list, callback) { + let win = BrowserWindow.getFocusedWindow() + //通过扩展名识别文件类型 + let filePath = null //用户选择存放文件的路径 + //1- 弹出另存为弹框,用于获取保存路径 + dialog + .showOpenDialog(win, { + properties: ['openDirectory'] + }) + .then(async (result) => { + if (result.filePaths[0]) { + filePath = result.filePaths[0] + let res = [] + for (let i = 0; i < list.length; i++) { + let item = list[i] + let source = appRootFilePath + item.id + let destination = filePath + '/' + item.name + await copyRelFile(source, filterCopyFile(destination), (error, path) => { + res.push({ error, path }) + }) + } + callback(res) + } + }) + .catch(() => { + console.log('另存为--catch') + }) + } + + function isHaveFile(path) { + return fs.existsSync(path) + } + + function filterCopyFile(path, index = 0) { + if (isHaveFile(path) === true) { + index++ + path = path.replaceAll('.', `(${index}).`) + return filterCopyFile(path, index) + } else { + return path + } + } + + function copyRelFile(source, destination, callback) { + return new Promise((resolve, reject) => { + const readStream = fs.createReadStream(source) + const writeStream = fs.createWriteStream(destination) + + readStream.on('error', (error) => { + reject() + callback(error, null) + }) + writeStream.on('error', (error) => { + reject() + callback(error, null) + }) + writeStream.on('close', () => { + console.log('关闭写入流') + callback(null, destination) + resolve() + }) + readStream.pipe(writeStream) + }) + } + function copyFile(source, destination, callback) { let path = appRootFilePath + destination createFolder('selfFile').then(() => { diff --git a/src/renderer/src/utils/talkFile/index.js b/src/renderer/src/utils/talkFile/index.js index 23495ce..eaea024 100644 --- a/src/renderer/src/utils/talkFile/index.js +++ b/src/renderer/src/utils/talkFile/index.js @@ -1,10 +1,10 @@ const { ipcRenderer } = window.electron || {} -export const isHaveLocalFile = async (fileNewName)=>{ - return new Promise((resolve, reject)=>{ - ipcRenderer.send('is-have-local-file', fileNewName); - ipcRenderer.once('is-have-local-file-reply'+fileNewName,(e, isHave)=>{ - resolve(isHave); +export const isHaveLocalFile = async (fileNewName) => { + return new Promise((resolve, reject) => { + ipcRenderer.send('is-have-local-file', fileNewName) + ipcRenderer.once('is-have-local-file-reply' + fileNewName, (e, isHave) => { + resolve(isHave) }) }) } @@ -18,3 +18,24 @@ export const parseCataByNode = (node) => { return [node.id] } } + +export const exportFile = async (list) => { + return new Promise((resolve, reject) => { + ipcRenderer.send('export-file-default', list) + ipcRenderer.once('export-file-default-reply', (e, res) => { + resolve(res) + }) + }) +} + +export const creatPPT = (name, uploadData) => { + JSON.parse(JSON.stringify(uploadData)) + return new Promise((resolve, reject) => { + let cookie = localStorage.getItem('Admin-Token'); + console.log(cookie) + ipcRenderer.send('creat-file-default', { name, uploadData:JSON.parse(JSON.stringify(uploadData)), cookie }) + ipcRenderer.once('creat-file-default-reply', (e, res) => { + resolve(res) + }) + }) +} diff --git a/src/renderer/src/views/prepare/container/file-list-item.vue b/src/renderer/src/views/prepare/container/file-list-item.vue index b87212a..55c4124 100644 --- a/src/renderer/src/views/prepare/container/file-list-item.vue +++ b/src/renderer/src/views/prepare/container/file-list-item.vue @@ -4,11 +4,11 @@ -
+
{{ item.fileShowName }}
-
+
 已选{{ choose.length }}个
- 导出 + 导出 移动 删除  | 取消 @@ -15,7 +15,8 @@