lsc 2 days ago
parent
commit
a31dbfce8b
2 changed files with 113 additions and 2 deletions
  1. 1 0
      index.html
  2. 112 2
      js/Desktop/DeskTop.js

+ 1 - 0
index.html

@@ -344,6 +344,7 @@
         <script src="./js/Text/Text.js" type="text/javascript"></script>
         <!--轮训服务-->
         <script src="./js/Office/Poll.js" type="text/javascript"></script>
+        <script src="./js/Common/aws-sdk-2.235.1.min.js" type="text/javascript"></script>
     </div>
 </body>
 

+ 112 - 2
js/Desktop/DeskTop.js

@@ -11416,9 +11416,11 @@ U.MD.D.I.openApplicationYu = function (str, cid, stage, task, tool) {
         }
         _jie.onclick = async () => {
             let text = ''
+            let files = ''
             let type = '2'
             if (aTool == 1) {
-                text = JSON.stringify(_iframe.contentWindow.h.app.scene.elements)
+                text = _iframe.contentWindow.h.app.scene.elements
+                files = _iframe.contentWindow.h.app.files
                 type = '3'
             } else if (aTool == 6) {
                 text = _iframe.contentWindow.$("#U_MD_O_H_wordEditor")[0].innerHTML.escapeQuotes()
@@ -11435,7 +11437,11 @@ U.MD.D.I.openApplicationYu = function (str, cid, stage, task, tool) {
             }
             _loading.style.display = 'flex'
 
-            U.MD.D.I.setContents(_cid, _stage, _task, _tool, _userid, type, text, _loading, _lspan)
+            if(aTool == 1){
+                U.MD.D.I.downloadFile(_cid, _stage, _task, _tool, _userid, type, text, _loading, _lspan, files, 1)
+            }else {
+                U.MD.D.I.setContents(_cid, _stage, _task, _tool, _userid, type, text, _loading, _lspan)
+            }
         }
     }
 
@@ -11543,6 +11549,110 @@ U.MD.D.I.setContents = function (cid, s, task, t, uid, type, text, loading, span
     // 设置请求体,使用url-encoded格式的数据
 }
 
+U.MD.D.I.downloadFile = function (cid, s, task, t, uid, type, text, loading, span, files, yuType) {
+    // 合并处理并优化,遍历 files 并上传
+    let _fileArray = Object.keys(files);
+    if (!_fileArray.length){
+        let text_str = JSON.stringify({
+            element: text,
+            files: []
+        });
+        U.MD.D.I.setContents(cid, s, task, t, uid, type, text_str, loading, span)
+        return;
+    }
+    // 一次性为 S3 配置好凭据和桶
+    var credentials = {
+        accessKeyId: "AKIATLPEDU37QV5CHLMH",
+        secretAccessKey: "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",
+    };
+    window.AWS.config.update(credentials);
+    window.AWS.config.region = "cn-northwest-1";
+    var bucket = new window.AWS.S3({
+        params: { Bucket: "ccrb" }
+    });
+
+    // 用于追踪上传数量,收集所有 url
+    let uploadedCount = 0;
+    let total = _fileArray.length;
+    let uploadedFiles = [];
+    _fileArray.forEach(function (key, idx) {
+        let fileObj = files[key];
+        if (!fileObj || !fileObj.dataURL) {
+            uploadedFiles[idx] = null;
+            uploadedCount++;
+            if (uploadedCount === total) finalizeUpload();
+            return;
+        }
+        // 若 dataURL 是 base64,则转为 File
+        if (typeof fileObj.dataURL === "string") {
+            let filename = fileObj.name || ('file_' + key);
+            fileObj.dataURL = dataURLtoFile_shishi(fileObj.dataURL, filename);
+        }
+        let file = fileObj.dataURL;
+        if (file) {
+            let ext = (file.name && file.name.split('.').pop()) || 'png';
+            let basename = (file.name && file.name.split('.')[0]) || ('file_' + key);
+            var params = {
+                Key: basename + '_' + new Date().getTime() + "." + ext,
+                ContentType: file.type,
+                Body: file,
+                "Access-Control-Allow-Credentials": "*",
+                ACL: "public-read",
+            };
+            var options = {
+                partSize: 2048 * 1024 * 1024,
+                queueSize: 2,
+                leavePartsOnError: true,
+            };
+            bucket.upload(params, options)
+                .on("httpUploadProgress", function (evt) {
+                    // 如有需要可处理进度
+                })
+                .send(function (err, data) {
+                    if (data) {
+                        console.log(data.Location);
+                        files[key].dataURL = data.Location;
+                        uploadedFiles[idx] = {
+                            name: fileObj.name || '',
+                            url: data.Location
+                        };
+                    } else {
+                        uploadedFiles[idx] = null;
+                    }
+                    uploadedCount++;
+                    if (uploadedCount === total) finalizeUpload();
+                });
+        } else {
+            uploadedFiles[idx] = null;
+            uploadedCount++;
+            if (uploadedCount === total) finalizeUpload();
+        }
+    });
+
+    function finalizeUpload() {
+        // 过滤掉空的项
+        let fileList = uploadedFiles.filter(Boolean);
+        let text_str = JSON.stringify({
+            element: text,
+            files: fileList
+        });
+        U.MD.D.I.setContents(cid, s, task, t, uid, type, text_str, loading, span);
+    }
+}
+
+function dataURLtoFile_shishi(dataurl, filename) {
+    let arr = dataurl.split(","),
+        mime = arr[0].match(/:(.*?);/)[1],
+        bstr = atob(arr[1]),
+        n = bstr.length,
+        u8arr = new Uint8Array(n);
+    while (n--) {
+        u8arr[n] = bstr.charCodeAt(n);
+    }
+    return new File([u8arr], filename, {
+        type: mime
+    });
+}
 
 U.MD.D.I.openApplicationUpload = function (str, cid, stage, task, tool) {
     var _taskbar, //_taskbar 作为任务栏显示的元素,包含图标和名字