WebApp【公共组件库】@前端(For Git Submodule)
Tevin
2022-07-17 bab852804ba2aa1dc23385e62f6873ccdbd73ccf
common/Bridge.js
@@ -222,11 +222,16 @@
    /* ----- 文件系统 ----- */
    /**
     * 保存文件 base64 到 java
     * 保存文件到 java 端,并返回文件名
     * @param objUrl
     * @param callback
     */
    fileSave(objUrl, callback) {
    fileSave(objUrl = '', callback) {
        // 非 ObjectURL 跳过
        if (objUrl.indexOf('blob:') < 0) {
            callback(objUrl);
            return;
        }
        // 分段存储
        const saveFileChunk = (baseData, index) => {
            const writeData = {
@@ -262,9 +267,10 @@
     * @param fileName
     * @param callback
     */
    fileLoad(fileName, callback) {
    fileLoad(fileName = '', callback) {
        // 非存储地址,跳过
        if (fileName.indexOf('bridge:') < 0) {
            callback('');
            callback(fileName);
            return;
        }
        fileName = fileName.split(':')[1];
@@ -279,6 +285,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);
@@ -303,15 +313,75 @@
        loadFileChunk(0);
    }
    fileRemove(fileName) {
    /**
     * 从 java 端移除文件
     * @param fileName
     */
    fileRemove(fileName = '') {
        if (fileName.indexOf('bridge:') < 0) {
            callback('');
            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);
            });
        }
    }
}
// 全局服务实例