From bab852804ba2aa1dc23385e62f6873ccdbd73ccf Mon Sep 17 00:00:00 2001
From: Tevin <tingquanren@163.com>
Date: Sun, 17 Jul 2022 10:23:55 +0800
Subject: [PATCH] 优化文件存储服务,提供列队批量操作

---
 common/Bridge.js |  178 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 174 insertions(+), 4 deletions(-)

diff --git a/common/Bridge.js b/common/Bridge.js
index 6664716..cf2b376 100644
--- a/common/Bridge.js
+++ b/common/Bridge.js
@@ -24,6 +24,7 @@
 
 import { Fetcher } from '@components/bases/Fetcher';
 import { Tools } from '@components/common/Tools';
+import { $fileTrans } from '@components/common/FileTransform';
 
 export class Bridge {
 
@@ -98,9 +99,16 @@
         this[name] = (res) => {
             if (callback && Tools.isFunction(callback)) {
                 if (res) {
-                    let data = typeof res === 'string' ? JSON.parse(res) : res;
-                    // 转换接收参数键名为驼峰
-                    data = this.transKeyName('camel', data);
+                    let data;
+                    try {
+                        // 转对象
+                        data = typeof res === 'string' ? JSON.parse(res) : res;
+                        // 转换接收参数键名为驼峰
+                        data = this.transKeyName('camel', data);
+                    } catch (e) {
+                        Tools.toast('跨端通讯异常:解析数据失败!');
+                        return;
+                    }
                     callback(data);
                 } else {
                     callback();
@@ -151,7 +159,6 @@
                 // 有通知回调
                 if (marker) {
                     this._receives[method](param2, (param2) => {
-                        debugger;
                         this._sendTelling(method, param2 || {}, marker);
                     });
                 }
@@ -212,6 +219,169 @@
         return Fetcher.prototype.transKeyName(type, json);
     }
 
+    /* ----- 文件系统 ----- */
+
+    /**
+     * 保存文件到 java 端,并返回文件名
+     * @param objUrl
+     * @param callback
+     */
+    fileSave(objUrl = '', callback) {
+        // 非 ObjectURL 跳过
+        if (objUrl.indexOf('blob:') < 0) {
+            callback(objUrl);
+            return;
+        }
+        // 分段存储
+        const saveFileChunk = (baseData, index) => {
+            const writeData = {
+                fileName: baseData.fileName,
+                currentIdx: index,
+                totalIdx: baseData.total,
+                data: baseData.baseArr[index],
+            };
+            this.invoking('img_write', writeData, res => {
+                if (res.result === false) {
+                    Tools.toast(res.msg);
+                    return;
+                }
+                // 按分段递归保存
+                if (index < baseData.total - 1) {
+                    setTimeout(() => {
+                        saveFileChunk(baseData, index + 1);
+                    }, 10);
+                }
+                // 已完成
+                else {
+                    callback && callback('bridge:' + baseData.fileName);
+                }
+            });
+        };
+        $fileTrans.transObjUrlToBaseData(objUrl, baseData => {
+            saveFileChunk(baseData, 0);
+        });
+    }
+
+    /**
+     * 从 java 读取文件 base64
+     * @param fileName
+     * @param callback
+     */
+    fileLoad(fileName = '', callback) {
+        // 非存储地址,跳过
+        if (fileName.indexOf('bridge:') < 0) {
+            callback(fileName);
+            return;
+        }
+        fileName = fileName.split(':')[1];
+        const chunkSize = $fileTrans.getChunkSize();
+        const baseArr = [];
+        let totalSize = 0;
+        let totalCount = 0;
+        const loadFileChunk = (index) => {
+            const loadData = {
+                fileName,
+                offset: chunkSize * index,
+                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);
+                }
+                baseArr.push(res.data);
+                // 按分段递归读取
+                if (totalCount > 1 && totalCount - 1 > index) {
+                    loadFileChunk(index + 1);
+                }
+                // 读取完成
+                else {
+                    const baseData = {
+                        baseArr,
+                        fileName,
+                    };
+                    $fileTrans.transBaseDataToObjUrl(baseData, objUrl => {
+                        callback && callback(objUrl);
+                    });
+                }
+            });
+        };
+        loadFileChunk(0);
+    }
+
+    /**
+     * 从 java 端移除文件
+     * @param fileName
+     */
+    fileRemove(fileName = '') {
+        if (fileName.indexOf('bridge:') < 0) {
+            return;
+        }
+        fileName = fileName.split(':')[1];
+        this.invoking('img_del', { fileName });
+    }
+
+    /**
+     * 文件存储批量操作服务
+     * @param type
+     * @param names
+     * @param callback
+     */
+    fileStorehouse(type, names = [], callback) {
+        // 保存
+        if (type === 'save') {
+            const list = [];
+            const save = index => {
+                this.fileSave(names[index], fileName => {
+                    list.push(fileName);
+                    // 递归下一个
+                    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