From bbd5332ce5c79135d7b15eba188b8a13ed893d9d Mon Sep 17 00:00:00 2001 From: zhuhao <979263092@qq.com> Date: Tue, 16 Jul 2024 17:50:01 +0800 Subject: [PATCH 1/4] =?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 | 56 +++- .../src/layout/components/Uploader.vue | 13 +- src/renderer/src/utils/talkFile/index.js | 10 + src/renderer/src/views/prepare/index.vue | 274 ++++++++++++------ 4 files changed, 246 insertions(+), 107 deletions(-) create mode 100644 src/renderer/src/utils/talkFile/index.js diff --git a/src/main/file.js b/src/main/file.js index c0b8810..eecd08c 100644 --- a/src/main/file.js +++ b/src/main/file.js @@ -5,13 +5,27 @@ import { dialog } from 'electron' const manager = new ElectronDownloadManager() export default async function ({ app, shell, BrowserWindow, ipcMain }) { const userDataPath = app.getPath('userData') + const appRootFilePath = userDataPath + '\\selfFile\\' + 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) + return + } + e.reply('is-have-local-file-reply'+fileNewName, true) + }) + }) //默认浏览器打开url ipcMain.on('open-url-browser', (e, url) => { shell.openPath(url) }) //使用默认应用打开本地文件 - ipcMain.on('open-path-app', (e, path) => { - shell.openExternal(path) + ipcMain.on('open-path-app', (e, destination) => { + let path = appRootFilePath + destination + shell.openExternal(path).catch((error) => { + console.log(error) + }) }) //复制文件 @@ -21,32 +35,42 @@ export default async function ({ app, shell, BrowserWindow, ipcMain }) { }) }) + //获取应用文件目录 + ipcMain.on('get-root-file-path', (e) => { + e.reply('get-root-file-path-reply', appRootFilePath) + }) + //下载文件 - ipcMain.on('download-file-default', (e, url) => { + ipcMain.on('download-file-default', (e, { url, fileName }) => { createFolder('selfFile').then(async () => { const browserWindow = BrowserWindow.fromId(e.sender.id) const id = await manager.download({ window: browserWindow, url: url, - directory: userDataPath + '/selfFile/', + saveAsFilename: fileName, + directory: appRootFilePath, callbacks: { onDownloadStarted: async ({ id, item, webContents }) => { // Do something with the download id }, onDownloadProgress: async ({ id, item, percentCompleted }) => { - browserWindow.webContents.invoke('download-progress', { - id, - percentCompleted, - // Get the number of bytes received so far - bytesReceived: item.getReceivedBytes() - }) }, onDownloadCompleted: async ({ id, item }) => { - console.log(item) + console.log('完成') + e.reply('download-file-default'+fileName,true) }, - onDownloadCancelled: async () => {}, - onDownloadInterrupted: async () => {}, - onError: (err, data) => {} + onDownloadCancelled: async () => { + console.log('取消') + e.reply('download-file-default'+fileName,false) + }, + onDownloadInterrupted: async () => { + console.log('中断') + e.reply('download-file-default'+fileName,false) + }, + onError: (err, data) => { + console.log(err.toString()) + e.reply('download-file-default'+fileName,false) + } } }) }) @@ -59,7 +83,7 @@ export default async function ({ app, shell, BrowserWindow, ipcMain }) { * @param {String} fileName 文件名称包括后缀名,例如图1.png */ ipcMain.on('save-as', function (event, url, fileName) { - let win = BrowserWindow.getFocusedWindow(); + let win = BrowserWindow.getFocusedWindow() //通过扩展名识别文件类型 let filters = [{ name: '全部文件', extensions: ['*'] }] let ext = path.extname(fileName) //获取扩展名 @@ -101,7 +125,7 @@ export default async function ({ app, shell, BrowserWindow, ipcMain }) { }) function copyFile(source, destination, callback) { - let path = userDataPath + '\\selfFile\\' + destination + let path = appRootFilePath + destination createFolder('selfFile').then(() => { const readStream = fs.createReadStream(source) const writeStream = fs.createWriteStream(path) diff --git a/src/renderer/src/layout/components/Uploader.vue b/src/renderer/src/layout/components/Uploader.vue index a52f2cd..d622ddf 100644 --- a/src/renderer/src/layout/components/Uploader.vue +++ b/src/renderer/src/layout/components/Uploader.vue @@ -63,6 +63,7 @@ import uploaderState from '@/store/modules/uploader' import { getToken } from '@/utils/auth' import CryptoJS from 'crypto-js' import { DeleteFilled } from '@element-plus/icons-vue' +const { ipcRenderer } = window.electron || {} export default { name: 'Uploader', components: { DeleteFilled }, @@ -70,11 +71,6 @@ export default { return { timer: null, uploadDatas: { - textbookId: '123', - levelFirstId: '123', - levelSecondId: '123', - fileSource: '平台', - fileFlag: '课件' }, uploadUrl: import.meta.env.VITE_APP_BASE_API + '/smarttalk/file/upload', headers: { @@ -98,6 +94,12 @@ export default { this.runNowJob() }, 1000) }, + mounted() { + ipcRenderer.send('get-root-file-path'); + ipcRenderer.once('get-root-file-path-reply',(e, path)=>{ + window.rootTalkFilePath = path; + }) + }, methods: { formatFileSize(fileSize) { if (fileSize < 1024) { @@ -118,6 +120,7 @@ export default { }, onSuccess(res, file, files) { this.removeUploadFile(file.uid) + ipcRenderer.send('copy-file-default',{ source:file.raw.path, destination:res.resData.fileNewName}) file.callback(res) }, beforeUpload(file) { diff --git a/src/renderer/src/utils/talkFile/index.js b/src/renderer/src/utils/talkFile/index.js new file mode 100644 index 0000000..61fe003 --- /dev/null +++ b/src/renderer/src/utils/talkFile/index.js @@ -0,0 +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); + }) + }) +} diff --git a/src/renderer/src/views/prepare/index.vue b/src/renderer/src/views/prepare/index.vue index 95b991c..0bb1fb4 100644 --- a/src/renderer/src/views/prepare/index.vue +++ b/src/renderer/src/views/prepare/index.vue @@ -1,6 +1,6 @@ + diff --git a/src/renderer/src/views/prepare/container/file-oper-batch.vue b/src/renderer/src/views/prepare/container/file-oper-batch.vue new file mode 100644 index 0000000..e142527 --- /dev/null +++ b/src/renderer/src/views/prepare/container/file-oper-batch.vue @@ -0,0 +1,81 @@ + + + diff --git a/src/renderer/src/views/prepare/index.vue b/src/renderer/src/views/prepare/index.vue index 4d27b28..e03962e 100644 --- a/src/renderer/src/views/prepare/index.vue +++ b/src/renderer/src/views/prepare/index.vue @@ -1,10 +1,10 @@