WebApp【公共组件库】@前端(For Git Submodule)
Tevin
2025-03-03 219fb684a854c63d51fdad6443e65693447b4f26
common/Bridge.js
@@ -38,12 +38,11 @@
import { $fileTrans } from '@components/common/FileTransform';
export class Bridge {
    constructor() {
        this._data = {
            count: 100,
            fileSaved: {},  // 已保存图片名称列表 { 'blob:***' : 'bridge:***' }
            fileLoaded: {},  // 已读取图片名称列表 { 'bridge:***' : 'blob:***' }
            fileSaved: {}, // 已保存图片名称列表 { 'blob:***' : 'bridge:***' }
            fileLoaded: {}, // 已读取图片名称列表 { 'bridge:***' : 'blob:***' }
        };
        this._receives = {};
        this._earlyInvok = [];
@@ -122,7 +121,7 @@
                };
            }, 5 * 60 * 1000);
            // 挂载到全局
            this[cbName] = (res) => {
            this[cbName] = res => {
                // res有值,正常通讯
                if (typeof res !== 'undefined') {
                    let data;
@@ -131,11 +130,12 @@
                        data = typeof res === 'string' ? JSON.parse(res) : res;
                        // 转换接收参数键名为驼峰
                        data = this.transKeyName('camel', data);
                        // 回传业务层
                        callback(data);
                    } catch (e) {
                        Tools.toast('跨端通讯异常:解析数据失败!');
                        return;
                    }
                    // 回传业务层,忽略来自业务层的报错
                    callback(data);
                }
                // res 无值,视为 java 层异常,由 java 层进行异常提醒
                else {
@@ -148,11 +148,13 @@
            };
        }
        // 发送
        window.aisim.linking(JSON.stringify({
            method,
            param,
            callback: cbName ? ('bridge.' + cbName) : '',
        }));
        window.aisim.linking(
            JSON.stringify({
                method,
                param,
                callback: cbName ? 'bridge.' + cbName : '',
            }),
        );
    }
    /**
@@ -185,15 +187,16 @@
     * @private
     */
    _initReceive() {
        window.telling = (res) => {
        window.telling = res => {
            const data = typeof res === 'string' ? JSON.parse(res) : res;
            const { method, param, marker } = data;
            // 转换接收参数键名为驼峰
            const param2 = this.transKeyName('camel', param);
            // 已注册协议
            if (this._receives[method]) {
                // 有通知回调
                if (marker) {
                    this._receives[method](param2, (param2) => {
                    this._receives[method](param2, param2 => {
                        this._sendTelling(method, param2 || {}, marker);
                    });
                }
@@ -201,6 +204,10 @@
                else {
                    this._receives[method](param2);
                }
            }
            // 未注册的协议
            else {
                console.warn('BridgeTelling:通讯协议【' + method + '】尚未注册!');
            }
        };
    }
@@ -221,11 +228,13 @@
        // 转换发送参数键名为下划线
        param = this.transKeyName('underline', param);
        // 发送
        window.aisim.linking(JSON.stringify({
            method,
            param,
            marker,
        }));
        window.aisim.linking(
            JSON.stringify({
                method,
                param,
                marker,
            }),
        );
    }
    /**
@@ -289,8 +298,11 @@
                            fileName: writeData.fileName,
                            currentIdx: writeData.currentIdx,
                            totalIdx: writeData.total,
                            data: (writeData.data || '').substr(0, 10)
                                + '...(共' + (writeData.data || '').length + '个base64字符)',
                            data:
                                (writeData.data || '').substr(0, 10) +
                                '...(共' +
                                (writeData.data || '').length +
                                '个base64字符)',
                        },
                        response: res,
                    });
@@ -335,7 +347,7 @@
        const baseArr = [];
        let totalSize = 0;
        let totalCount = 0;
        const loadFileChunk = (index) => {
        const loadFileChunk = index => {
            const loadData = {
                fileName,
                offset: chunkSize * index,
@@ -353,9 +365,12 @@
                        response: {
                            result: res.result,
                            msg: res.msg,
                            'total_size': res.totalSize,
                            data: (res.data || '').substr(0, 10)
                                + '...(共' + (res.data || '').length + '个base64字符)',
                            total_size: res.totalSize,
                            data:
                                (res.data || '').substr(0, 10) +
                                '...(共' +
                                (res.data || '').length +
                                '个base64字符)',
                        },
                    });
                    return;
@@ -390,12 +405,20 @@
                            response: {
                                result: res.result,
                                msg: res.msg,
                                'total_size': res.totalSize,
                                data: (res.data || '').substr(0, 10)
                                    + '...(共' + (res.data || '').length + '个base64字符)',
                                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字符)'),
                            base64Arr: baseData.baseArr.map(
                                baseItem =>
                                    (baseItem || []).substr(0, 10) +
                                    '...(共' +
                                    (res.data || '').length +
                                    '个base64字符)',
                            ),
                            message: 'Base64合并解析异常!',
                        });
                    }
@@ -441,21 +464,25 @@
        if (type === 'save') {
            const list = [];
            const save = index => {
                this.fileSave(names[index], bridgeName => {
                    list.push(bridgeName);
                    // 递归下一个
                    if (index < names.length - 1) {
                        setTimeout(() => {
                            save(index + 1);
                        }, 10);
                    }
                    // 完成
                    else {
                        callback && callback(list);
                    }
                }, err => {
                    callback && callback(null, err);
                });
                this.fileSave(
                    names[index],
                    bridgeName => {
                        list.push(bridgeName);
                        // 递归下一个
                        if (index < names.length - 1) {
                            setTimeout(() => {
                                save(index + 1);
                            }, 10);
                        }
                        // 完成
                        else {
                            callback && callback(list);
                        }
                    },
                    err => {
                        callback && callback(null, err);
                    },
                );
            };
            save(0);
        }
@@ -463,21 +490,25 @@
        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);
                    }
                }, err => {
                    callback && callback(null, err);
                });
                this.fileLoad(
                    names[index],
                    objUrl => {
                        list.push(objUrl);
                        // 递归下一个
                        if (index < names.length - 1) {
                            setTimeout(() => {
                                load(index + 1);
                            }, 10);
                        }
                        // 完成
                        else {
                            callback && callback(list);
                        }
                    },
                    err => {
                        callback && callback(null, err);
                    },
                );
            };
            load(0);
        }
@@ -490,7 +521,6 @@
            });
        }
    }
}
// 全局服务实例