WebApp【公共组件库】@前端(For Git Submodule)
Tevin
2022-08-24 a64da2da9e94583e2c8da1711a762107d5682ea1
common/Bridge.js
@@ -31,6 +31,8 @@
    constructor() {
        this._data = {
            count: 100,
            fileSaved: {},  // 已保存图片名称列表 { 'blob:***' : 'bridge:***' }
            fileLoaded: {},  // 已读取图片名称列表 { 'bridge:***' : 'blob:***' }
        };
        this._receives = {};
        this._earlyInvok = [];
@@ -225,11 +227,16 @@
     * 保存文件到 java 端,并返回文件名
     * @param objUrl
     * @param callback
     * @param onError
     */
    fileSave(objUrl = '', callback) {
    fileSave(objUrl = '', callback, onError) {
        // 非 ObjectURL 跳过
        if (objUrl.indexOf('blob:') < 0) {
            callback(objUrl);
            return;
        }
        if (this._data.fileSaved[objUrl]) {
            callback(this._data.fileSaved[objUrl]);
            return;
        }
        // 分段存储
@@ -242,7 +249,18 @@
            };
            this.invoking('img_write', writeData, res => {
                if (res.result === false) {
                    Tools.toast(res.msg);
                    Tools.toast('离线图片存储:' + res.msg);
                    onError({
                        method: 'img_write',
                        request: {
                            fileName: writeData.fileName,
                            currentIdx: writeData.currentIdx,
                            totalIdx: writeData.total,
                            data: (writeData.data || '').substr(0, 10)
                                + '...(共' + (writeData.data || '').length + '个base64字符)',
                        },
                        response: res,
                    });
                    return;
                }
                // 按分段递归保存
@@ -258,22 +276,28 @@
            });
        };
        $fileTrans.transObjUrlToBaseData(objUrl, baseData => {
            this._data.fileSaved[objUrl] = 'bridge:' + baseData.fileName;
            saveFileChunk(baseData, 0);
        });
    }
    /**
     * 从 java 读取文件 base64
     * @param fileName
     * @param bridgeName
     * @param callback
     * @param onError
     */
    fileLoad(fileName = '', callback) {
    fileLoad(bridgeName = '', callback, onError) {
        // 非存储地址,跳过
        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;
@@ -286,7 +310,21 @@
            };
            this.invoking('img_read', loadData, res => {
                if (res.result === false) {
                    Tools.toast(res.msg);
                    Tools.toast('离线图片读取:' + res.msg);
                    onError({
                        method: 'img_read',
                        request: {
                            ...loadData,
                            totalCount,
                        },
                        response: {
                            result: res.result,
                            msg: res.msg,
                            'total_size': res.totalSize,
                            data: (res.data || '').substr(0, 10)
                                + '...(共' + (res.data || '').length + '个base64字符)',
                        },
                    });
                    return;
                }
                if (totalSize === 0) {
@@ -304,9 +342,30 @@
                        baseArr,
                        fileName,
                    };
                    $fileTrans.transBaseDataToObjUrl(baseData, objUrl => {
                        callback && callback(objUrl);
                    });
                    try {
                        $fileTrans.transBaseDataToObjUrl(baseData, objUrl => {
                            this._data.fileLoaded[bridgeName] = objUrl;
                            callback && callback(objUrl);
                        });
                    } catch (e) {
                        onError({
                            method: 'img_read@merge_after_base64_loaded',
                            request: {
                                ...loadData,
                                totalCount,
                            },
                            response: {
                                result: res.result,
                                msg: res.msg,
                                'total_size': res.totalSize,
                                data: (res.data || '').substr(0, 10)
                                    + '...(共' + (res.data || '').length + '个base64字符)',
                            },
                            base64Arr: baseData.baseArr.map(baseItem => (baseItem || []).substr(0, 10)
                                + '...(共' + (res.data || '').length + '个base64字符)'),
                            message: 'Base64合并解析异常!',
                        });
                    }
                }
            });
        };
@@ -315,14 +374,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 +396,20 @@
     * @param names
     * @param callback
     */
    fileStorehouse(type, names = [], callback) {
    fileStore(type, names = [], callback) {
        if (!names || names.length === 0) {
            callback && callback([]);
            return;
        }
        if (typeof names === 'string') {
            names = names.split(',');
        }
        // 保存
        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(() => {
@@ -348,6 +420,8 @@
                    else {
                        callback && callback(list);
                    }
                }, err => {
                    callback && callback(null, err);
                });
            };
            save(0);
@@ -368,6 +442,8 @@
                    else {
                        callback && callback(list);
                    }
                }, err => {
                    callback && callback(null, err);
                });
            };
            load(0);