WebApp【公共组件库】@前端(For Git Submodule)
Tevin
2022-07-16 c4df98c26cde5e7e4da0bf0b72e019a01a2386c7
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 {
@@ -218,6 +219,99 @@
        return Fetcher.prototype.transKeyName(type, json);
    }
    /* ----- 文件系统 ----- */
    /**
     * 保存文件 base64 到 java
     * @param objUrl
     * @param callback
     */
    fileSave(objUrl, callback) {
        // 分段存储
        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('');
            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 (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);
    }
    fileRemove(fileName) {
        if (fileName.indexOf('bridge:') < 0) {
            callback('');
            return;
        }
        fileName = fileName.split(':')[1];
        this.invoking('img_del', { fileName });
    }
}
// 全局服务实例