//公共js，主要做表单验证，以及基本方法封装
export function isNullOrEmpty(value) {
    //是否为空
    return (value === null || value === '' || value === undefined) ? true : false
}

export function trim(value) {
    //去空格
    return value.replace(/(^\s*)|(\s*$)/g, '')
}

export function isEmail(value) {
    //是否为邮箱
    return RegExp(
        /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/)
        .test(value)
}

export function isMobile(value) {
    //是否为手机号
    return RegExp(/^1[1-9][0-9]{9}$/).test(value)
}

export function isFloat(value) {
    //金额，只允许保留两位小数
    return RegExp(/^([0-9]*[.]?[0-9])[0-9]{0,1}$/).test(value)
}

export function isNum(value) {
    //是否全为数字
    return RegExp(/^[0-9]+$/).test(value)
}

export function isEmailCode(value) {
    return RegExp(/^\d{4}$/).test(value)
}

export function isMobileCode(value) {
    return RegExp(/^\d{6}$/).test(value)
}

export function isCredit(value) { //统一社会信用代码
    return RegExp(/^[0-9A-HJ-NPQRTUWXY]{2}\\d{6}[0-9A-HJ-NPQRTUWXY]{10}$/).test(value)
}

export function checkPwd(value) {
    //密码为8~20位数字和字母组合
    return RegExp(/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/).test(value)
}

export function hidePhone(value) {
    return value.replace(/^(\d{3})\d{4}(\d+)/, '$1****$2')
}

export function interceptHtmlFn(value) {
    return value.replace(/<[^>]+>/g, '')
}

export function desensitizationName(value) {
    if (null != value && value != undefined) {
        if (value.length <= 3) {
            let ruten = value.substring(1)
            return value.replace(ruten, '**')
        } else if (value.length > 3 && value.length <= 6) {
            let ruten = value.substring(2)
            return value.replace(ruten, '**')
        }
    } else {
        return ''
    }
}

export function formatNum(num) {
    //格式化手机号码
    if (utils.isMobile(num)) {
        num = num.replace(/^(\d{3})\d{4}(\d{4})$/, '$1****$2')
    }
    return num
}

export function rmoney(money) {
    //金额格式化
    return parseFloat(money).toFixed(2).toString().split('').reverse().join('').replace(/(\d{3})/g, '$1,')
        .replace(
            /\,$/, '').split('').reverse().join('')
}

export function bankRegex(num) {
    let bankNum = ''
    bankNum = num.replace(/\D/g, '').replace(/(....)(?=.)/g, '$1 ')
    return bankNum
}

export function checkData(value) {
    let data
    if (value == null || value == '') {
        data = ''
    } else {
        data = value
    }
    return data
}

export function formatDate(formatStr, fdate) {
    //日期格式化
    if (fdate) {
        if (~fdate.indexOf('.')) {
            fdate = fdate.substring(0, fdate.indexOf('.'))
        }
        fdate = fdate.toString().replace('T', ' ').replace(/\-/g, '/')
        var fTime, fStr = 'ymdhis'
        if (!formatStr)
            formatStr = 'y-m-d h:i:s'
        if (fdate)
            fTime = new Date(fdate)
        else
            fTime = new Date()
        var month = fTime.getMonth() + 1
        var day = fTime.getDate()
        var hours = fTime.getHours()
        var minu = fTime.getMinutes()
        var second = fTime.getSeconds()
        month = month < 10 ? '0' + month : month
        day = day < 10 ? '0' + day : day
        hours = hours < 10 ? ('0' + hours) : hours
        minu = minu < 10 ? '0' + minu : minu
        second = second < 10 ? '0' + second : second
        var formatArr = [
            fTime.getFullYear().toString(),
            month.toString(),
            day.toString(),
            hours.toString(),
            minu.toString(),
            second.toString()
        ]
        for (var i = 0; i < formatArr.length; i++) {
            formatStr = formatStr.replace(fStr.charAt(i), formatArr[i])
        }
        return formatStr
    } else {
        return ''
    }
}

export function throttle(fn, gapTime) {
    if (gapTime == null || gapTime == undefined) {
        gapTime = 1500
    }

    let _lastTime = null

    // 返回新的函数
    return function() {
        let _nowTime = +new Date()
        if (_nowTime - _lastTime > gapTime || !_lastTime) {
            fn.apply(this, arguments) //将this和参数传给原函数
            _lastTime = _nowTime
        }
    }
}

export function debounce(fn, delay, isImmediate) {
    var timer = null //初始化timer，作为计时清除依据
    return function() {
        var context = this //获取函数所在作用域this
        var args = arguments //取得传入参数
        clearTimeout(timer)
        if (isImmediate && timer === null) {
            //时间间隔外立即执行
            fn.apply(context, args)
            timer = 0
            return
        }
        timer = setTimeout(function() {
            fn.apply(context, args)
            timer = null
        }, delay)
    }
}



const install = Vue => {
    console.log('初始化挂载($reg)工具库 common.js')
    Vue.prototype.$reg = {
        isNullOrEmpty,
        trim,
        isMobile,
        isEmail,
        isFloat,
        isNum,
        isEmailCode,
        isMobileCode,
        checkPwd,
        formatNum,
        rmoney,
        formatDate,
        bankRegex,
        checkData,
        hidePhone,
        interceptHtmlFn,
        desensitizationName,
        throttle,
        debounce
    }
}

export default install
