From 417e0b8b1c8fe9df5fbdad5f3b4efdee6c709cc5 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 9 Nov 2020 14:17:07 +0800 Subject: [PATCH] =?UTF-8?q?workspace=20=E6=96=87=E4=BB=B6=E8=A7=A3?= =?UTF-8?q?=E9=99=A4=E5=B7=A5=E4=BD=9C=E4=B8=AD=E7=8A=B6=E6=80=81=E6=97=B6?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/services/system.js | 35 +++++++- src/views/main_web/workspace/workspace.vue | 98 ++++++++++++++++------ 2 files changed, 107 insertions(+), 26 deletions(-) diff --git a/src/services/system.js b/src/services/system.js index 577b6c1a..c573b687 100644 --- a/src/services/system.js +++ b/src/services/system.js @@ -4,6 +4,7 @@ */ import { fetchApi, wrapErrorHint } from '@/utils/request'; import { notify, firstCharToUpperCase } from '@/utils/tool'; +import { identity } from 'lodash'; export const isClient = !!global.electron; // process.env.IS_CLIENT; @@ -100,7 +101,7 @@ const system = { * + 文件上传至本地ipfs节点 * + 将文件的ipfsCid连同文件信息发送到远端服务器 */ - uploadFile: safeCall(async (projectId, projectName, folderId, folderName, levelId, fileList, onSuccessHandler) => { + uploadFile: safeCall(async (projectId, projectName, folderId, folderName, levelId, fileList, onSuccessHandler, onProgressHandler = identity) => { const { ipcRenderer } = global.electron; const res = await ipcRenderer.invoke('project-upload-file'); console.log('ipcRenderer project-selected-upload-file: ', res); @@ -119,6 +120,7 @@ const system = { console.log('receive download file message:', e); try { const { size, process, hash } = JSON.parse(e.data); + onProgressHandler(e); if(process !== 100 || !hash) return; socket.close(); // {"size":"88.69","currentSize":"88.69","unit":"KiB","process":100,"hash":""} @@ -176,6 +178,37 @@ const system = { } }); }), + /** + * 更新本地文件 + */ + updateFile: safeCall((file, localFilePathPrefix, projectName, folderName, onSuccessHandler, onProgressHandler = identity) => { + const socket = io('upload'); + const { archName, extension, id: fileId } = file; + const extensionedFileName = `${archName}${extension ? `.${extension}` : ''}`; + const filePath = `${localFilePathPrefix}\\${folderName}\\${extensionedFileName}`; + socket.on('open', () => { + const data = [filePath, extensionedFileName, projectName, folderName].join('|'); + socket.send(data); + }); + socket.on('message', async (e) => { + try { + const progressData = JSON.parse(e.data); + const { size, process, hash } = progressData; + onProgressHandler(progressData); + if(process !== 100 || !hash) return; + socket.close(); + const copyFile = firstCharToUpperCase({ ...file, ipfsCid: hash, ModifyUserId: sessionStorage.userId }); + + const res = await fetchApi('file/updateFile', copyFile); + wrapErrorHint(res); + if(res.Code === 0) { notify.success(`${archName} 更新成功`); } + onSuccessHandler(copyFile); + return; + } catch(err) { + console.error('socket-update-file parse data have error:', e); + } + }); + }), /** * 系统打开文件 */ diff --git a/src/views/main_web/workspace/workspace.vue b/src/views/main_web/workspace/workspace.vue index fd50216a..219af254 100644 --- a/src/views/main_web/workspace/workspace.vue +++ b/src/views/main_web/workspace/workspace.vue @@ -563,6 +563,20 @@
下载中 {{file.loadProgress}}
+
+ + +
上传中 {{file.loadProgress}}
+
+
file.downloadStatus === DownloadStatus.DOWNLOADED; @@ -2862,7 +2887,7 @@ export default { localWorkspacePrefix: '', // 本地工作目录前缀 socketIns: null, // 监听文件变化的socket实例 projectName: sessionStorage.projName, - fileDownloadStatusHash: {}, + fileDownloadStatusHash: {}, // 文件上传/下载状态存储 folderMap: {}, // 文件夹id哈希: { [folderId]: folder } fileLocalStateMap: {}, // 本地网关文件通信数据缓存: { [fileId]: { } } }; @@ -2969,7 +2994,7 @@ export default { file.downloadStatus = DownloadStatus.DOWNLOADED; ifModify = true; } - if(stateMap[file.id] && file.downloadStatus !== DownloadStatus.DOWNLOADED) { + if(stateMap[file.id]) { Object.assign(file, stateMap[file.id]); ifModify = true; } @@ -3303,27 +3328,50 @@ export default { // console.log(error); // }); // }, - async updateFileWorkStatus(file, workStatus, message) { - // const params = { - // FolderId: file.folderId, - // FileId: file.id, - // UserId: this.userId, - // FileStatus: file.status - // } - // file.loadingProgress = 0; - - const [resFlag] = await Promise.all([ - services.changeFileWorkStatus(file.id, workStatus), - // services.fileCoordinationChange(params) - ]); - if(resFlag.Code !== 0) return; - notify.success(message); - file.workStatus = workStatus; - this.$forceUpdate(); + async updateFileWorkStatus(file, nextWorkStatus, message) { + if(!this.isClient || nextWorkStatus === FileWorkStatus.WORKING && file.workStatus === FileWorkStatus.NOT_WORKING) { + const [resFlag] = await services.changeFileWorkStatus(file.id, nextWorkStatus); + if(resFlag.Code !== 0) return; + notify.success(message); + this.$forceUpdate(); + return; + } + // 仅当文件从工作中变成非工作状态时执行下面的代码 // todo 获取文件最新的hash并更新文件 - // if(workStatus === 1 && file.workStatus === 2) { - - // } + const { folderName, levelId, id: folderId } = this.nowFolder; + const { archName, extension, id: fileId } = file; + const stateMap = this.fileLocalStateMap; + const updateState = { + loadProgress: 0, + downloadStatus: DownloadStatus.UPLOADING, + isDownload: true, + }; + stateMap[fileId] = updateState; + this.refreshFileStatus(); + // const fileList = this.currentPageType === 0 ? this.folderFileList.listMyFiles : this.folderFileList.listOtherFiles; + const fileKey = `${folderName}\\${archName}${extension ? `.${extension}`:''}`; + system.updateFile( + file, this.localWorkspacePrefix, + this.projectName, folderName, + async (fileNewState) => { + const [resFlag] = await services.changeFileWorkStatus(file.id, nextWorkStatus); + notify.success(message); + const {IpfsCid} = fileNewState; + // 注入到文件下载检测表中 + this.fileDownloadStatusHash[fileKey] = IpfsCid; + this.refreshFileStatus(); + }, + (progressData) => { + const { process, hash } = progressData; + const updateState = { + loadProgress: process, + downloadStatus: process === 100 ? DownloadStatus.DOWNLOADED : DownloadStatus.UPLOADING, + isDownload: true, + }; + stateMap[fileId] = updateState; + this.refreshFileStatus(); + }, + ); }, /** * 更改文件状态 @@ -4755,7 +4803,7 @@ export default { const { folderName, levelId, id: folderId } = nowFolder; // console.log(folderName, levelId, folderId); system.uploadFile( - sessionStorage.projId,sessionStorage.projName, folderId, folderName, levelId, this.myFilesList, + sessionStorage.projId, sessionStorage.projName, folderId, folderName, levelId, this.myFilesList, (file) => { const {ArchName, IpfsCid} = file; // 注入到文件下载检测表中