// import config from './config/config'
// import CryptoJS from 'crypto-js'

var obj = {
        // 反转义字符串
        escape2Html(str) {
            var arrEntities = { lt: '<', gt: '>', nbsp: ' ', amp: '&', quot: '"' }
            return str ? str.replace(/&(lt|gt|nbsp|amp|quot);/gi, function(all, t) {
                return arrEntities[t]
            }) : ''
        },
        getparames(_u) {
            var url = decodeURIComponent(location.href).split('?')
            var pmsArr = url[1] || ''
            if (_u) {
                url = decodeURIComponent(_u).split('?')
            }
            var _obj = {}
            pmsArr = pmsArr.split('&')
            for (var i in pmsArr) {
                var o = pmsArr[i].split('=')
                _obj[o[0]] = o[1]
            }
            return _obj
        },
        // 改变bi设计器的组件内容层级，以uuid为唯一组件id
        changeBiGridItem(type, uuid) {
            var gridItems = document.getElementsByClassName('vue-grid-item')
            if (gridItems && gridItems.length > 0) {
                for (var gridItem of gridItems) {
                    var selectDom = gridItem.getElementsByClassName(uuid)
                    if (type && selectDom && selectDom.length > 0) { // 展开
                        // 存在
                        gridItem.style.zIndex = 99
                    } else {
                        gridItem.style.zIndex = 0
                    }
                }
            }
        },
        //  获取随机id，默认为16位
        uuid(num) {
            var s = [];
            var hexDigits = '0123456789abcdef'
            if (num) {
                var n = ''
                for (var i = 0; i < num; i++) {
                    n = n + 'x'
                }
                return n.replace(/[xy]/g, function(c) {
                    var r = Math.random() * 16 | 0
                    var v = c == 'x' ? r : (r & 0x3 | 0x8)
                    return v.toString(16)
                })
            }
            for (var i = 0; i < 36; i++) {
                s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1)
            }
            s[14] = '4'
            s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1)
            s[8] = s[13] = s[18] = s[23] = ''
            return s.join('')
        },
        // 数字格式化
        formMoney(number, decimals, dec_point, thousands_sep) {
            // return $.toThousands(number);
            /*
             * 参数说明：
             * number：要格式化的数字
             * decimals：保留几位小数
             * dec_point：小数点符号
             * thousands_sep：千分位符号
             * */
            decimals = decimals || 2;
            dec_point = dec_point || '.'
            thousands_sep = thousands_sep || ','
            number = (number + '').replace(/[^0-9+-Ee.]/g, '')
            var n = !isFinite(+number) ? 0 : +number
            var prec = !isFinite(+decimals) ? 0 : Math.abs(decimals)
            var sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep
            var dec = (typeof dec_point === 'undefined') ? '.' : dec_point
            var s = ''
            var toFixedFix = function(n, prec) {
                var k = Math.pow(10, prec);
                return '' + Math.round(n * k) / k
            }

            s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.')
            var re = /(-?\d+)(\d{3})/
            while (re.test(s[0])) {
                s[0] = s[0].replace(re, '$1' + sep + '$2')
            }

            if ((s[1] || '').length < prec) {
                s[1] = s[1] || ''
                s[1] += new Array(prec - s[1].length + 1).join('0')
            }
            return s.join(dec)
        },
        // 计算字符串长度(英文占1个字符，中文汉字占2个字符)
        getLengthByEnglishChinese(str) {
            var len = 0;
            for (var i = 0; i < str.length; i++) {
                if (str.charCodeAt(i) > 127 || str.charCodeAt(i) == 94) {
                    len += 2;
                } else {
                    len++;
                }
            }
            return len;
        },
        /**
         * 数字千分位
         */
        thousandsNum(num) {
            if (!num && num != 0 && num != '0.00') {
                return '-'
            }
            // 为数字
            if (!isNaN(Number(num))) {
                if (num.toString().indexOf('.') > -1) { // 存在小数点
                    return num ? Number(num).toFixed(2).replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g, '$1,') : 0.00
                } else {
                    return num ? (num + '').replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g, '$1,') : 0
                }
            }
            return num
        },
        // 复制
        copyText(_this, clipboard, e) {
            clipboard.on('success', e => {
                _this.$utils.msg(_this, '复制成功', 'success')
                    // 释放内存
                clipboard.off('error')
                clipboard.off('success')
                clipboard.destroy()
            })
            clipboard.on('error', e => {
                // 不支持复制
                _this.$utils.msg(_this, e.message || '复制失败', 'error')
                    // 释放内存
                clipboard.off('error')
                clipboard.off('success')
                clipboard.destroy()
            })
            clipboard.onClick(e)
        },
        // 加减计算
        countSue(a, b, type) {
            var c, d, e
            if (a == undefined || a == null || a == '' || isNaN(a)) {
                a = 0
            }
            if (b == undefined || b == null || b == '' || isNaN(b)) {
                b = 0
            }
            try {
                c = a.toString().split('.')[1].length
            } catch (f) {
                c = 0
            }
            try {
                d = b.toString().split('.')[1].length
            } catch (f) {
                d = 0
            }
            e = Math.pow(10, Math.max(c, d))
            if (type == 'add') {
                return (this.countMul(a, e) + this.countMul(b, e)) / e
            } else if (type == 'sub') {
                return (this.countMul(a, e) - this.countMul(b, e)) / e
            } else {
                return false
            }
        },
        // 乘法运算
        countMul(a, b) {
            var c = 0
            var d = a.toString()
            var e = b.toString()
            try {
                c += d.split('.')[1].length
            } catch (f) {}
            try {
                c += e.split('.')[1].length
            } catch (f) {}
            return Number(d.replace('.', '')) * Number(e.replace('.', '')) / Math.pow(10, c)
        },
        // 弹窗提示
        msg(_this, title, status) {
            _this.$XModal.message({ message: title, status: status || 'warning' })
        },
        // 获取当前日期
        getTodayDate(type) {
            return this.timestampToTime(type, new Date().getTime())
        },
        // 判断是否是JSON
        isJSON(str) {
            if (typeof str == 'string') {
                try {
                    var obj = JSON.parse(str)
                    if (typeof obj == 'object' && obj) {
                        return true
                    } else {
                        return false
                    }
                } catch (e) {
                    return false
                }
            }
        },
        // 格式化xml
        formateXml(xmlStr) {
            var _this = this
            var text = xmlStr;
            //使用replace去空格
            text = '\n' + text.replace(/(<\w+)(\s.*?>)/g, function($0, name, props) {
                return name + ' ' + props.replace(/\s+(\w+=)/g, " $1");
            }).replace(/>\s*?</g, ">\n<");
            //处理注释
            text = text.replace(/\n/g, '\r').replace(/<!--(.+?)-->/g, function($0, text) {
                var ret = '<!--' + escape(text) + '-->';
                return ret;
            }).replace(/\r/g, '\n');
            //调整格式  以压栈方式递归调整缩进
            var rgx = /\n(<(([^\?]).+?)(?:\s|\s*?>|\s*?(\/)>)(?:.*?(?:(?:(\/)>)|(?:<(\/)\2>)))?)/mg;
            var nodeStack = [];
            var output = text.replace(rgx, function($0, all, name, isBegin, isCloseFull1, isCloseFull2, isFull1, isFull2) {
                var isClosed = (isCloseFull1 == '/') || (isCloseFull2 == '/') || (isFull1 == '/') || (isFull2 == '/');
                var prefix = '';
                if (isBegin == '!') { //!开头
                    prefix = _this.setPrefix(nodeStack.length);
                } else {
                    ///开头
                    if (isBegin != '/') {
                        prefix = _this.setPrefix(nodeStack.length);
                        //非关闭标签
                        if (!isClosed) {
                            nodeStack.push(name);
                        }
                    } else {
                        nodeStack.pop(); //弹栈
                        prefix = _this.setPrefix(nodeStack.length);
                    }
                }
                var ret = '\n' + prefix + all;
                return ret;
            });
            var prefixSpace = -1;
            var outputText = output.substring(1);
            //还原注释内容
            outputText = outputText.replace(/\n/g, '\r').replace(/(\s*)<!--(.+?)-->/g, function($0, prefix, text) {
                if (prefix.charAt(0) == '\r')
                    prefix = prefix.substring(1);
                text = unescape(text).replace(/\r/g, '\n');
                var ret = '\n' + prefix + '<!--' + text.replace(/^\s*/mg, prefix) + '-->';
                return ret;
            });
            outputText = outputText.replace(/\s+$/g, '').replace(/\r/g, '\r\n');
            return outputText;

        },
        setPrefix(prefixIndex) {
            var result = '';
            //缩进长度
            var span = '    ';
            var output = [];
            for (var i = 0; i < prefixIndex; ++i) {
                output.push(span);
            }
            result = output.join('');
            return result;
        },
        // 整理时间戳为年月日
        timestampToTime(type, timestamp) {
            var date = new Date(timestamp || new Date().getTime())
            var y = date.getFullYear()
            var m = date.getMonth() + 1
            var d = date.getDate()
            var h = date.getHours()
            var minute = date.getMinutes()
            var second = date.getSeconds()
            m = m < 10 ? ('0' + m) : m
            d = d < 10 ? ('0' + d) : d
            h = h < 10 ? ('0' + h) : h
            minute = minute < 10 ? ('0' + minute) : minute
            second = second < 10 ? ('0' + second) : second
            if (type == 'time') {
                return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second
            } else {
                return y + '-' + m + '-' + d
            }
        },
        // 获取时间日期
        getDateBy8Site(date) {
            if (!date) {
                return ''
            }
            return (
                date.substring(0, 4) +
                '/' +
                date.substring(4, 6) +
                '/' +
                date.substring(6, 8) +
                (date.length > 10 ?
                    ' ' +
                    date.substring(8, 10) +
                    ':' +
                    date.substring(10, 12) +
                    ':' +
                    date.substring(12, 14) :
                    '')
            )
        },
        // 获取当前间隔时间后的时间
        getCurrentRangDate(range) {
            var date = new Date(); //1. js获取当前时间
            var min = date.getMinutes(); //2. 获取当前分钟
            date.setMinutes(min - range); //3. 设置当前时间+10分钟：把当前分钟数+10后的值重新设置为date对象的分钟数
            var y = date.getFullYear();
            var m = (date.getMonth() + 1) < 10 ? ('0' + (date.getMonth() + 1)) : (date.getMonth() + 1);
            var d = date.getDate() < 10 ? ('0' + date.getDate()) : date.getDate();
            var h = date.getHours() < 10 ? ('0' + date.getHours()) : date.getHours()
            var f = date.getMinutes() < 10 ? ('0' + date.getMinutes()) : date.getMinutes()
            var s = date.getSeconds() < 10 ? ('0' + date.getSeconds()) : date.getSeconds()
            var formatdate = y + '-' + m + '-' + d + ' ' + h + ':' + f + ':' + s;
            return formatdate
        },
        // 间隔几天前的时间计算
        intervalDate(date, num, type) {
            var now = new Date(date || '') // 当前日期
            var sjc = now.getTime() - parseInt(num) * 24 * 60 * 60 * 1000
            now = new Date(sjc)
            var y = now.getFullYear() // 当前年
            var m = now.getMonth() + 1 // 月
            var d = now.getDate() // 日
            var h = now.getHours()
            var minute = now.getMinutes()
            var second = now.getSeconds()
            m = m < 10 ? ('0' + m) : m
            d = d < 10 ? ('0' + d) : d
            h = h < 10 ? ('0' + h) : h
            minute = minute < 10 ? ('0' + minute) : minute
            second = second < 10 ? ('0' + second) : second
            if (type == 'time') {
                return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second
            } else {
                return y + '-' + m + '-' + d
            }
        },
        isHaveGroovy(value) {
            return new RegExp('<\\s*%\\s*=\\s*\\S*\\s*%\\s*>').test(value);
        },
        // 设置对象参数为空
        setObjEmp(data) {
            var d = {}
            if (Array.isArray(data) === true) {
                data.forEach((v, i) => {
                    v = this.setObjEmp(v)
                })
            } else {
                Object.keys(data).forEach((v, i) => {
                    if (typeof v === 'object') {
                        d[v] = this.setObjEmp(v)
                    } else {
                        d[v] = ''
                    }
                })
            }
            return d
        },
        // 
        splitNum(cellValue, targetName) {
            if (!cellValue) {
                return '-'
            }
            if (!isNaN(Number(cellValue))) {
                cellValue = this.thousandsNum(cellValue)
            }
            if (targetName.indexOf('人次') > -1) {
                cellValue = cellValue.split('.')[0]
            }
            return cellValue
        },

        // // 加密、解密key获取
        // md5Key(_str) {
        //     var myDate = new Date();
        //     var str = _str + '/'
        //     str += myDate.getFullYear()
        //     var m = myDate.getMonth() + 1
        //     if (m < 10) {
        //         str += '0'
        //     }
        //     str += m
        //     var d = myDate.getDate();
        //     if (d < 10) {
        //         str += '0'
        //     }
        //     str += d;
        //     return CryptoJS.MD5(str).toString();
        // },
        // key
        splitKeyAnIv(key) {
            if (!key) {
                key = ''
            }
            var k = '0000000000000000'
            var v = '0000000000000000'
            if (key.length >= 32) {
                k = key.substr(0, 16)
                v = key.substr(16, 16)
            } else {
                if (key.length >= 16) {
                    k = key.substr(0, 16)
                    var vv = key.substr(16, key.length - 16)
                    v = vv + v.substr(vv.length)
                } else {
                    k = key + k.substr(key.length)
                }
            }
            var a = []
            a[0] = k
            a[1] = v
            return a
        },
    }
    // 深度递归
function deepCopy(target) {
    if (typeof target == 'object') {
        var result = Array.isArray(target) ? [] : {}
        for (var key in target) {
            if (typeof target[key] == 'object') {
                result[key] = deepCopy(target[key])
            } else {
                result[key] = target[key]
            }
        }

        return result
    }

    return target
}
export default obj