From 3369f393745e81691ea9021bbd786ca39899416f Mon Sep 17 00:00:00 2001 From: zhuhao <979263092@qq.com> Date: Mon, 15 Jul 2024 20:05:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9F=BA=E7=A1=80=E6=96=87=E4=BB=B6=E4=B8=8A?= =?UTF-8?q?=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 | 66 ++++++++++++++++--- src/renderer/src/api/file/index.js | 9 +++ .../src/layout/components/Uploader.vue | 32 +++++++-- src/renderer/src/views/prepare/index.vue | 46 +++++++++++-- 4 files changed, 131 insertions(+), 22 deletions(-) diff --git a/src/main/file.js b/src/main/file.js index 1551855..c0b8810 100644 --- a/src/main/file.js +++ b/src/main/file.js @@ -1,9 +1,9 @@ const fs = require('fs') const path = require('path') -import { ElectronDownloadManager } from 'electron-dl-manager'; -const manager = new ElectronDownloadManager(); +import { ElectronDownloadManager } from 'electron-dl-manager' +import { dialog } from 'electron' +const manager = new ElectronDownloadManager() export default async function ({ app, shell, BrowserWindow, ipcMain }) { - const userDataPath = app.getPath('userData') //默认浏览器打开url ipcMain.on('open-url-browser', (e, url) => { @@ -22,8 +22,8 @@ export default async function ({ app, shell, BrowserWindow, ipcMain }) { }) //下载文件 - ipcMain.on('download-file-default', (e,url) => { - createFolder('selfFile').then(async ()=>{ + ipcMain.on('download-file-default', (e, url) => { + createFolder('selfFile').then(async () => { const browserWindow = BrowserWindow.fromId(e.sender.id) const id = await manager.download({ window: browserWindow, @@ -38,17 +38,65 @@ export default async function ({ app, shell, BrowserWindow, ipcMain }) { id, percentCompleted, // Get the number of bytes received so far - bytesReceived: item.getReceivedBytes(), - }); + bytesReceived: item.getReceivedBytes() + }) }, onDownloadCompleted: async ({ id, item }) => { console.log(item) }, onDownloadCancelled: async () => {}, onDownloadInterrupted: async () => {}, - onError: (err, data) => {}, + onError: (err, data) => {} } - }); + }) + }) + }) + + /**另存为... + * 接收渲染进程 保存文件的 的通知 + * @param {Object} event + * @param {String} url 下载链接 + * @param {String} fileName 文件名称包括后缀名,例如图1.png + */ + ipcMain.on('save-as', function (event, url, fileName) { + let win = BrowserWindow.getFocusedWindow(); + //通过扩展名识别文件类型 + let filters = [{ name: '全部文件', extensions: ['*'] }] + let ext = path.extname(fileName) //获取扩展名 + if (ext && ext !== '.') { + const name = ext.slice(1, ext.length) + if (name) { + filters.unshift({ + name: '', + extensions: [name] + }) + } + } + let filePath = null //用户选择存放文件的路径 + + //1- 弹出另存为弹框,用于获取保存路径 + dialog + .showSaveDialog(win, { + title: '另存为', + filters, + defaultPath: fileName + }) + .then((result) => { + //点击保存后开始下载 + filePath = result.filePath + if (filePath) { + win.webContents.downloadURL(url) // 触发will-download事件 + } + }) + .catch(() => { + console.log('另存为--catch') + }) + + //2- 准备下载的时候触发 + win.webContents.session.once('will-download', (event, item, webContents) => { + if (!filePath) return + //设置下载项的保存文件路径 + item.setSavePath(filePath) }) }) diff --git a/src/renderer/src/api/file/index.js b/src/renderer/src/api/file/index.js index 8b03c3b..c4b877f 100644 --- a/src/renderer/src/api/file/index.js +++ b/src/renderer/src/api/file/index.js @@ -8,9 +8,18 @@ export const getSmarttalkPage = (params) => { params }) } + export function deleteSmarttalk(id) { return request({ url: '/smarttalk/file/' + id, method: 'delete' }) } + +export const updateSmarttalk = (params) => { + return request({ + url: '/smarttalk/file/updateSmarttalk', + method: 'post', + params + }) +} diff --git a/src/renderer/src/layout/components/Uploader.vue b/src/renderer/src/layout/components/Uploader.vue index 21488a6..a52f2cd 100644 --- a/src/renderer/src/layout/components/Uploader.vue +++ b/src/renderer/src/layout/components/Uploader.vue @@ -1,5 +1,5 @@