/**
 * 获取url参数值
 * @param {*} url
 * @param {*} name
 */
export function getUrlParameter(url, name) {
    var regexSearch = '[\\?&#]' + name + '=([^&#]*)'
    var regex = new RegExp(regexSearch)
    var results = regex.exec(url)
    return results ? window.decodeURIComponent(results[1]) : ''
}
export function showToast(e) {
    uni.showToast({
        title: e,
        icon: 'none',
        mask: true,
        duration: 1000
    });
}
export default {

    transDate: function (val, pattern) {
        if (val) {
            let time = new Date()
            time.setTime(val)
            if (Date.parse(val)) {
                time = new Date(val)
            }
            if (time instanceof Date) {
                return this.formatDate((pattern) || 'yyyy-MM-dd', time)
            }
        }
        return ''
    },
    transBaseDateTime: function (val, pattern) {
        if (val) {
            let time = new Date()
            time.setTime(val)
            if (Date.parse(val)) {
                time = new Date(val)
            }
            if (time instanceof Date) {
                return this.formatDate((pattern) || 'yyyy年MM月dd日 hh:mm', time)
            }
        }
        return ''
    },
    transBaseToDateTime: function (val, pattern) {
        if (val) {
            let time = new Date()
            time.setTime(val)
            if (Date.parse(val)) {
                time = new Date(val)
            }
            if (time instanceof Date) {
                return this.formatDate((pattern) || 'MM月dd日 hh:mm', time)
            }
        }
        return ''
    },
    transServDate: function (val) {
        return this.transDate(val, 'yyyy-MM-dd hh:mm:ss')
    },
    transServDay: function (val) {
        return this.transDate(val, 'yyyy-MM-dd')
    },
    isArrayFn: function (value) {
        if (typeof Array.isArray === 'function') {
            return Array.isArray(value)
        } else {
            return Object.prototype.toString.call(value) === '[object Array]'
        }
    },

    transDcMap: function (arr) {
        const tMap = {}
        arr.forEach(item => {
            tMap[item.value] = item.label
        })
        return tMap
    },

    castEval: function (val) {
        // return JSON.parse(val)
        if (val) {
            return eval('(' + val + ')')
        } else {
            return ''
        }
    },

    castString: function (val) {
        return JSON.stringify(val)
    },

    // 金额格式化，整数部分每3位用逗号分隔，支持带有正负号以及小数部分
    formatMoney: function (amt) {
        if (!amt) return ''
        if (amt.length <= 3) {
            return amt
        }

        if (!/^(\+|-)?(\d+)(\.\d+)?$/.test(amt)) {
            return amt
        }

        var a = RegExp.$1;
        var b = RegExp.$2;
        var c = RegExp.$3
        var re = new RegExp()
        re.compile('(\\d)(\\d{3})(,|$)')
        while (re.test(b)) {
            b = b.replace(re, '$1,$2$3')
        }
        return a + '' + b + '' + c
    },

    formatDate: function (fmt, date) { // author: meizz
        var o = {
            'M+': date.getMonth() + 1, // 月份
            'd+': date.getDate(), // 日
            'h+': date.getHours(), // 小时
            'm+': date.getMinutes(), // 分
            's+': date.getSeconds(), // 秒
            'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
            'S': date.getMilliseconds() // 毫秒
        }
        if (/(y+)/.test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
        }
        for (var k in o) {
            if (new RegExp('(' + k + ')').test(fmt)) {
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
            }
        }
        return fmt
    },
    phoneNumber(phone) {
        return phone.replace(/(\d{3})\d{4}(\d{4})/, "$1****$2")
    },

    replaceThumbnail: function (urlPath) {
        if (urlPath != null) {
            urlPath = urlPath.replace('/server/FileController/download/', this.$constant.BASE_URI + '/wx/fileController/downloadThumbnail/')
        }
        if (urlPath != null) {
            urlPath = urlPath.replace('/h5Server/FileController/download/', this.$constant.BASE_URI + '/wx/fileController/downloadThumbnail/')
        }
        return urlPath
    },

    goBack: function () {
        // window.history.go(-1)
        // self.location = document.referrer
        history.back()
    },

    uuid: function (len, radix) {
        const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
        const uuid = []
        let i
        radix = radix || chars.length

        if (len) {
            // Compact form
            for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix]
        } else {
            // rfc4122, version 4 form
            let r

            // rfc4122 requires these characters
            uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'
            uuid[14] = '4'

            // Fill in random data.  At i==19 set the high bits of clock sequence as
            // per rfc4122, sec. 4.1.5
            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('')
    },

}