export default {
    /**
     * 获取url参数
     * @param {*} key 
     * @returns 
     */
     getUrlParam: function(key) {
        var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg); //匹配目标参数
        if (r != null) return decodeURI(r[2]);
        return null; //返回参数值
    },
    /**
     * @desc   判断是否为URL地址
     *  @param  {String} str
     *  @return {Boolean}
     * */
    isUrl(str) {
        return /[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/i.test(
            str
        );
    },
    /**
     * @desc   判断是否为手机号
     * @param  {String|Number} str
     * @return {Boolean}
     */
    isPhoneNum(str) {
        return /^(\+?0?86\-?)?1[3456789]\d{9}$/.test(str);
    },
    /**
     * @desc  判断是否为身份证号
     * @param  {String|Number} str
     * @return {Boolean}
     */
    isIdCard(str) {
        return /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/.test(
            str
        );
    },
    /**
     * @desc   判断是否为邮箱地址
     * @param  {String}  str
     * @return {Boolean}
     */
    isEmail(str) {
        return /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/.test(str);
    },
    /**
     * @desc 生成指定范围[min, max]的随机数
     * @param  {Number} min
     * @param  {Number} max
     * @return {Number}
     */
    randomNum(min, max) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    },
    /**
     * @desc 随机生成颜色
     * @return {String}
     */
    randomColor() {
        return (
            "#" + ("00000" + ((Math.random() * 0x1000000) << 0).toString(16)).slice(-6)
        );
    },
    /**
     * @desc   判断`obj`是否为空
     * @param  {Object} obj
     * @return {Boolean}
     */
    isEmptyObject(obj) {
        if (!obj || typeof obj !== "object" || Array.isArray(obj)) return false;
        return !Object.keys(obj).length;
    },
    /**
     * @desc   千分位数字
     * @param  {Number, Number}
     * @return {String}
     */
    fixedThousandsNumber(number, fixed = 0) {
        // 过滤参数
        if (Object.is(parseFloat(number), NaN)) {
            console.error("请检查入参。例如：fixedThousandsNumber(10000.1234, 2)");
            return;
        }
        // 参数预处理
        let numString = parseFloat(number).toFixed(fixed);
        // 提取整数部分和浮点数部分
        let [intNum, floatNum] = numString.split(".");
        // 判断是否需要进行千分位处理
        if (intNum.length <= 3) {
            return numString;
        }
        // 千分位转换
        let temp = [];
        for (let end = intNum.length; end >= 0; end -= 3) {
            let start = end - 3 > 0 ? end - 3 : 0;
            temp.push(intNum.slice(start, end));
        }
        // 判断是否需要保留浮点数
        let result = floatNum
            ? `${temp.reverse().join()}.${floatNum}`
            : temp.reverse().join();
        return result;
    },
    /**
     * 手机号码脱敏
     * 格式为：13966992457或'13966992457' => 139****2457
     */
    phoneEncrypt(number) {
        return String(number).replace(/(\d{3})\d{4}(\d{4})/, "$1****$2");
    },
    /**
     * 平铺树形结构
     * @param {*} tree
     * @param {*} children 子级字段
     * @returns
     */
    treeToList(tree, childrenName = "children") {
        var queen = [];
        var out = [];
        queen = queen.concat(tree);
        while (queen.length) {
            var first = queen.shift();
            if (first[childrenName]) {
                queen = queen.concat(first[childrenName]);
                delete first[childrenName];
            }

            out.push(first);
        }
        return out;
    },
    /**
     * 为树形结构设置层级
     * @param {*} tree
     * @param {*} levelName 层级字段
     * @param {*} childrenName 子级字段
     * @returns
     */
    arrayTreeSetLevel(tree, levelName = "level", childrenName = "children") {
        if (!Array.isArray(tree)) return [];
        const recursive = (tree, level = 0) => {
            level++;
            return tree.map((v) => {
                v[levelName] = level;
                const child = v[childrenName];
                if (child && child.length) recursive(child, level);
                return v;
            });
        };
        return recursive(array);
    },
}

var CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');

Math.uuid = function (len, radix) {
    var chars = CHARS, uuid = [], i;
    radix = radix || chars.length;

    if (len) {
        for (i = 0; i < len; i++) {
            uuid[i] = chars[0 | Math.random() * radix];
        }
    } else {
        var r;
        uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
        uuid[14] = '4';
        for (i = 0; i < 36; i++) {
            if (!uuid[i]) {
                r = 0 | Math.random() * 16;
                uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
            }
        }
    }

    return uuid.join('');
};

Math.uuidFast = function () {
    var chars = CHARS, uuid = new Array(36), rnd = 0, r;
    for (var i = 0; i < 36; i++) {
        if (i == 8 || i == 13 || i == 18 || i == 23) {
            uuid[i] = '-';
        } else if (i == 14) {
            uuid[i] = '4';
        } else {
            if (rnd <= 0x02) {
                rnd = 0x2000000 + (Math.random() * 0x1000000) | 0;
            }
            r = rnd & 0xf;
            rnd = rnd >> 4;
            uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
        }
    }
    return uuid.join('');
};

Math.uuidCompact = function () {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
};