WebApp【公共组件库】@前端(For Git Submodule)
Tevin
2023-11-23 fe7a9db56d8062ffd5e3e86576e781dbba3b3ece
common/Tools.js
@@ -1,5 +1,5 @@
/**
 * Tools
 * Tools - 工具集
 * @author Tevin
 */
@@ -44,20 +44,6 @@
    }
    /**
     * 读取文件 base64
     * @param {File} file
     * @return {Promise<string>}
     */
    static getFileBase64(file) {
        return new Promise((resolve, reject) => {
            const reader = new FileReader();
            reader.onload = () => resolve(reader.result);
            reader.onerror = error => reject(error);
            reader.readAsDataURL(file);
        });
    }
    /**
     * 生成 GUID
     * @return {string}
     */
@@ -83,34 +69,6 @@
            result += template.charAt(Math.floor(Math.random() * count));
        }
        return result;
    }
    /**
     * 检查网络状态
     */
    static checkOnlineState(callback) {
        // 网页模式
        if (process.env.TARO_ENV === 'h5') {
            // 网络已开启
            if (navigator.onLine) {
                // 用图片资源测试网络状态
                const img = new Image();
                img.onload = () => {
                    img.onload = null;
                    callback && callback(true);
                };
                img.onerror = () => {
                    img.onerror = null;
                    callback && callback(false);
                };
                img.src = 'http://gz.zhiheiot.com/disp/download/buildArchive/online.png?t=' + Date.now();
            }
            // 网络未开启
            else {
                callback && callback(false);
            }
        }
        // 补充:小程序模式用 getNetworkType
    }
    /**
@@ -349,6 +307,26 @@
    }
    /**
     * 数组元素交换位置
     * @param {array} arr 数组
     * @param {number} fromIndex 要交换项目的位置
     * @param {number} toIndex 被交换项目的位置
     */
    static swapArray(arr, fromIndex, toIndex) {
        // 先在目标位置新增一个和当前元素一样的元素,再把当前元素删除掉
        arr.splice(toIndex, 0, arr[fromIndex]);
        // 如果拖动元素上移动,当前元素下标为 fromIndex+1
        if (fromIndex > toIndex) {
            arr.splice(fromIndex + 1, 1);
        }
        // 如果拖动元素下移,当前元素下标还是 fromIndex
        else {
            arr.splice(fromIndex, 1);
        }
        return arr;
    }
    /**
     * 使用 moment.js 格式化时间戳
     * @param {Number|String} timestamp
     * @param {String} [type='date']
@@ -496,7 +474,7 @@
    /**
     * 求小数点后的数据长度
     * @param {Number|String} num
     * @private
     * @return {Number}
     */
    static getDecimalLength(num) {
        let t = 0;
@@ -517,47 +495,20 @@
    }
    /**
     * 显示调试面板(仅支持H5)
     * @param cssSelector
     * @param callback
     * 转换周数到日期
     * @param {Number} year
     * @param {Number} week
     * @param {Number} weekDay 需要输出星期几对应的日期 (1~7)
     * @return {String}
     */
    static showDevConsole(cssSelector, callback) {
        // 只支持 h5 编译
        if (process.env.TARO_ENV !== 'h5') {
            return;
        }
        const fileref = document.createElement('script');
        fileref.setAttribute('type', 'text/javascript');
        if (window.location.protocol === 'https:') {
            fileref.setAttribute('src', 'https://cdn.jsdelivr.net/npm/eruda');
        } else {
            fileref.setAttribute('src', 'http://cdn.jsdelivr.net/npm/eruda');
        }
        document.getElementsByTagName('head')[0].appendChild(fileref);
        const erudaTimer = setInterval(() => {
            if (window.eruda) {
                clearInterval(erudaTimer);
                const container = document.createElement('div');
                if (cssSelector) {
                    document.querySelector(cssSelector).appendChild(container);
                } else {
                    document.getElementsByTagName('body')[0].appendChild(container);
                }
                window.eruda.init({
                    container: container,
                    useShadowDom: false,
                });
                setTimeout(() => {
                    const devTool = document.querySelector('.eruda-dev-tools');
                    devTool.style.display = 'block';
                    devTool.style.opacity = '1';
                    devTool.style.height = '70%';
                    console.warn('=====【艾信App调试特别模式】=====');
                    console.warn('点击右下角悬浮工具箱图标可以折叠面板');
                }, 50);
                callback();
            }
        }, 100);
    static transWeekIndexToDate(year, week, weekDay) {
        const yearStart = moment([year, 0, 1]);
        const dayLong = 24 * 60 * 60 * 1000;
        const firstWeekLong = (7 - yearStart.day()) * dayLong;
        const weeksLong = (week - 1) * 7 * dayLong;
        const weekDayLong = weekDay * dayLong;
        const dayTimestamp = yearStart.valueOf() + firstWeekLong + weeksLong + weekDayLong;
        return moment(dayTimestamp).format('YYYY-MM-DD');
    }
}
@@ -575,4 +526,6 @@
        const px = (value - p1.x) * (p2.y - p1.y) / (p2.x - p1.x) + p1.y;
        console.info(Math.round(px) + 'px');
    };
}
}
global.Tools = Tools;