From f243505462264715ddb38d3300a359f0c25f98cc Mon Sep 17 00:00:00 2001
From: Tevin <tingquanren@163.com>
Date: Mon, 18 Jul 2022 11:09:22 +0800
Subject: [PATCH] 优化文件存储服务

---
 forms/imagePicker/CImagePicker.vue |   82 +--------------------------
 common/Bridge.js                   |   45 +++++++++++----
 2 files changed, 36 insertions(+), 91 deletions(-)

diff --git a/common/Bridge.js b/common/Bridge.js
index cf2b376..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 = [];
@@ -232,6 +234,10 @@
             callback(objUrl);
             return;
         }
+        if (this._data.fileSaved[objUrl]) {
+            callback(this._data.fileSaved[objUrl]);
+            return;
+        }
         // 分段存储
         const saveFileChunk = (baseData, index) => {
             const writeData = {
@@ -258,22 +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) {
+    fileLoad(bridgeName = '', callback) {
         // 非存储地址,跳过
-        if (fileName.indexOf('bridge:') < 0) {
-            callback(fileName);
+        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;
@@ -305,6 +316,7 @@
                         fileName,
                     };
                     $fileTrans.transBaseDataToObjUrl(baseData, objUrl => {
+                        this._data.fileLoaded[bridgeName] = objUrl;
                         callback && callback(objUrl);
                     });
                 }
@@ -315,14 +327,20 @@
 
     /**
      * 从 java 端移除文件
-     * @param fileName
+     * @param bridgeName
      */
-    fileRemove(fileName = '') {
-        if (fileName.indexOf('bridge:') < 0) {
+    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];
+            }
+        });
     }
 
     /**
@@ -331,13 +349,16 @@
      * @param names
      * @param callback
      */
-    fileStorehouse(type, names = [], callback) {
+    fileStore(type, names = [], callback) {
+        if (!names || names.length === 0) {
+            callback && callback([]);
+        }
         // 保存
         if (type === 'save') {
             const list = [];
             const save = index => {
-                this.fileSave(names[index], fileName => {
-                    list.push(fileName);
+                this.fileSave(names[index], bridgeName => {
+                    list.push(bridgeName);
                     // 递归下一个
                     if (index < names.length - 1) {
                         setTimeout(() => {
diff --git a/forms/imagePicker/CImagePicker.vue b/forms/imagePicker/CImagePicker.vue
index 4d741be..32d9017 100644
--- a/forms/imagePicker/CImagePicker.vue
+++ b/forms/imagePicker/CImagePicker.vue
@@ -167,6 +167,9 @@
             this.showImg = false;
         },
         handleFail(msg) {
+            if (typeof msg === 'object') {
+                msg = msg.message;
+            }
             Taro.showToast({
                 title: msg,
                 icon: 'none',
@@ -193,85 +196,6 @@
                     callback(state, res);
                 }
             });
-            // let url = $fetchCommon.getUploadImgURL();
-            // if (url.indexOf('http') < 0) {
-            //     url = $hostBoot.getHost() + url;
-            // }
-            // const uploadTeam = [];
-            // const imgs = [];
-            // this.files.forEach(file => {
-            //     if (file.type === 'btn') {
-            //         return;
-            //     }
-            //     // 临时文件才上传
-            //     if (
-            //         file.url.indexOf('blob') >= 0 ||
-            //         file.url.indexOf('wxfile') >= 0 ||
-            //         file.url.indexOf('http://tmp/') >= 0
-            //     ) {
-            //         let header = {};
-            //         if (process.env.TARO_ENV === 'weapp') {
-            //             const localCookies = JSON.parse(
-            //                 Taro.getStorageSync('cookies') || '{}'
-            //             );
-            //             const cookiesArr = [];
-            //             Object.keys(localCookies).forEach(key => {
-            //                 cookiesArr.push(key + '=' + localCookies[key]);
-            //             });
-            //             header['Cookie'] = cookiesArr.join('; ');
-            //         }
-            //         uploadTeam.push(
-            //             new Promise((resolve, reject) => {
-            //                 Taro.uploadFile({
-            //                     url,
-            //                     header,
-            //                     filePath: file.url,
-            //                     fileName: this.fileNames[file.url],
-            //                     name: 'file',
-            //                     formData: {},
-            //                     success(res) {
-            //                         const res2 =
-            //                             typeof res.data === 'string'
-            //                                 ? JSON.parse(res.data)
-            //                                 : res.data;
-            //                         if (res2.state.code === 2000) {
-            //                             resolve(
-            //                                 $fetchCommon.transImgPath(
-            //                                     'fix',
-            //                                     res2.data.src ||
-            //                                         res2.data.file ||
-            //                                         res2.data.url
-            //                                 )
-            //                             );
-            //                         } else {
-            //                             reject({ message: res2.state.msg });
-            //                         }
-            //                     },
-            //                     cancel() {
-            //                         reject({ message: '上传图片已取消!' });
-            //                     },
-            //                     fail() {
-            //                         reject({ message: '上传图片失败!' });
-            //                     },
-            //                 });
-            //             })
-            //         );
-            //     }
-            //     // 其他类型视为 url,忽略
-            //     else {
-            //         uploadTeam.push(Promise.resolve(file.url));
-            //     }
-            // });
-            // Promise.all(uploadTeam)
-            //     .then(res => {
-            //         this.itemRes.onChange(res);
-            //         setTimeout(() => {
-            //             callback('success');
-            //         }, 0);
-            //     })
-            //     .catch(err => {
-            //         callback('error', err);
-            //     });
         },
     },
     mounted() {

--
Gitblit v1.9.1