From ff52ea040c49f33c6cb84cdda2deb75c637bc545 Mon Sep 17 00:00:00 2001
From: Tevin <tingquanren@163.com>
Date: Wed, 20 Jul 2022 11:37:35 +0800
Subject: [PATCH] 变更网络检测图片地址

---
 common/Bridge.js |  113 +++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 102 insertions(+), 11 deletions(-)

diff --git a/common/Bridge.js b/common/Bridge.js
index 8514c83..71335b2 100644
--- a/common/Bridge.js
+++ b/common/Bridge.js
@@ -31,6 +31,8 @@
     constructor() {
         this._data = {
             count: 100,
+            fileSaved: {},  // 已保存图片名称列表 { 'blob:***' : 'bridge:***' }
+            fileLoaded: {},  // 已读取图片名称列表 { 'bridge:***' : 'blob:***' }
         };
         this._receives = {};
         this._earlyInvok = [];
@@ -222,11 +224,20 @@
     /* ----- 文件系统 ----- */
 
     /**
-     * 保存文件 base64 到 java
+     * 保存文件到 java 端,并返回文件名
      * @param objUrl
      * @param callback
      */
-    fileSave(objUrl, callback) {
+    fileSave(objUrl = '', callback) {
+        // 非 ObjectURL 跳过
+        if (objUrl.indexOf('blob:') < 0) {
+            callback(objUrl);
+            return;
+        }
+        if (this._data.fileSaved[objUrl]) {
+            callback(this._data.fileSaved[objUrl]);
+            return;
+        }
         // 分段存储
         const saveFileChunk = (baseData, index) => {
             const writeData = {
@@ -253,21 +264,27 @@
             });
         };
         $fileTrans.transObjUrlToBaseData(objUrl, baseData => {
+            this._data.fileSaved[objUrl] = 'bridge:' + baseData.fileName;
             saveFileChunk(baseData, 0);
         });
     }
 
     /**
      * 从 java 读取文件 base64
-     * @param fileName
+     * @param bridgeName
      * @param callback
      */
-    fileLoad(fileName, callback) {
-        if (fileName.indexOf('bridge:') < 0) {
-            callback('');
+    fileLoad(bridgeName = '', callback) {
+        // 非存储地址,跳过
+        if (bridgeName.indexOf('bridge:') < 0) {
+            callback(bridgeName);
             return;
         }
-        fileName = fileName.split(':')[1];
+        if (this._data.fileLoaded[bridgeName]) {
+            callback(this._data.fileLoaded[bridgeName]);
+            return;
+        }
+        const fileName = bridgeName.split(':')[1];
         const chunkSize = $fileTrans.getChunkSize();
         const baseArr = [];
         let totalSize = 0;
@@ -279,6 +296,10 @@
                 length: chunkSize,
             };
             this.invoking('img_read', loadData, res => {
+                if (res.result === false) {
+                    Tools.toast(res.msg);
+                    return;
+                }
                 if (totalSize === 0) {
                     totalSize = res.totalSize;
                     totalCount = Math.ceil(res.totalSize / chunkSize);
@@ -295,6 +316,7 @@
                         fileName,
                     };
                     $fileTrans.transBaseDataToObjUrl(baseData, objUrl => {
+                        this._data.fileLoaded[bridgeName] = objUrl;
                         callback && callback(objUrl);
                     });
                 }
@@ -303,13 +325,82 @@
         loadFileChunk(0);
     }
 
-    fileRemove(fileName) {
-        if (fileName.indexOf('bridge:') < 0) {
-            callback('');
+    /**
+     * 从 java 端移除文件
+     * @param bridgeName
+     */
+    fileRemove(bridgeName = '') {
+        if (bridgeName.indexOf('bridge:') < 0) {
             return;
         }
-        fileName = fileName.split(':')[1];
+        const fileName = bridgeName.split(':')[1];
         this.invoking('img_del', { fileName });
+        // 移除
+        Object.keys(this._data.fileSaved).forEach(objUrl => {
+            if (bridgeName === this._data.fileSaved[objUrl]) {
+                delete this._data.fileSaved[objUrl];
+            }
+        });
+    }
+
+    /**
+     * 文件存储批量操作服务
+     * @param type
+     * @param names
+     * @param callback
+     */
+    fileStore(type, names = [], callback) {
+        if (!names || names.length === 0) {
+            callback && callback([]);
+        }
+        // 保存
+        if (type === 'save') {
+            const list = [];
+            const save = index => {
+                this.fileSave(names[index], bridgeName => {
+                    list.push(bridgeName);
+                    // 递归下一个
+                    if (index < names.length - 1) {
+                        setTimeout(() => {
+                            save(index + 1);
+                        }, 10);
+                    }
+                    // 完成
+                    else {
+                        callback && callback(list);
+                    }
+                });
+            };
+            save(0);
+        }
+        // 读取
+        else if (type === 'load') {
+            const list = [];
+            const load = index => {
+                this.fileLoad(names[index], objUrl => {
+                    list.push(objUrl);
+                    // 递归下一个
+                    if (index < names.length - 1) {
+                        setTimeout(() => {
+                            load(index + 1);
+                        }, 10);
+                    }
+                    // 完成
+                    else {
+                        callback && callback(list);
+                    }
+                });
+            };
+            load(0);
+        }
+        // 移除
+        else if (type === 'remove') {
+            names.forEach((name, index) => {
+                setTimeout(() => {
+                    this.fileRemove(name);
+                }, 10 * index);
+            });
+        }
     }
 
 }

--
Gitblit v1.9.1