/**
数据验证（表单验证）
*/
export default {
    error: '',
    check: function(data, rule) {
        for (let i = 0; i < rule.length; i++) {
            if (!rule[i].checkType) { return true }
            if (!rule[i].name) { return true }
            if (!rule[i].errorMsg) { return true }
            if (!data[rule[i].name] && data[rule[i].name !== 0]) { this.error = rule[i].errorMsg; return false }
            let reg, minMax
            switch (rule[i].checkType) {
                case 'string':
                    reg = new RegExp('^.{' + rule[i].checkRule + '}$')
                    if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false }
                    break
                case 'int':
                    reg = new RegExp('^(-[1-9]|[1-9])[0-9]{' + rule[i].checkRule + '}$')
                    if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false }
                    break
                case 'between':
                    if (!this.isNumber(data[rule[i].name])) {
                        this.error = rule[i].errorMsg
                        return false
                    }
                    minMax = rule[i].checkRule.split(',')
                    minMax[0] = Number(minMax[0])
                    minMax[1] = Number(minMax[1])
                    if (data[rule[i].name] > minMax[1] || data[rule[i].name] < minMax[0]) {
                        this.error = rule[i].errorMsg
                        return false
                    }
                    break
                case 'betweenD':
                    reg = /^-?[1-9][0-9]?$/
                    if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false }
                    minMax = rule[i].checkRule.split(',')
                    minMax[0] = Number(minMax[0])
                    minMax[1] = Number(minMax[1])
                    if (data[rule[i].name] > minMax[1] || data[rule[i].name] < minMax[0]) {
                        this.error = rule[i].errorMsg
                        return false
                    }
                    break
                case 'betweenF':
                    reg = /^-?[0-9][0-9]?.+[0-9]+$/
                    if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false }
                    minMax = rule[i].checkRule.split(',')
                    minMax[0] = Number(minMax[0])
                    minMax[1] = Number(minMax[1])
                    if (data[rule[i].name] > minMax[1] || data[rule[i].name] < minMax[0]) {
                        this.error = rule[i].errorMsg
                        return false
                    }
                    break
                case 'same':
                    if (data[rule[i].name] !== rule[i].checkRule) { this.error = rule[i].errorMsg; return false }
                    break
                case 'notsame':
                    if (data[rule[i].name] === rule[i].checkRule) { this.error = rule[i].errorMsg; return false }
                    break
                case 'email':
                    reg = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
                    if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false }
                    break
                case 'phoneno':
                    // 【数据卡】：14号段以前为上网卡专属号段，如中国联通的是145，中国移动的是147,中国电信的是149等等。
                    // 【虚拟运营商】：170[1700/1701/1702]、162(电信)，1703/1705/1706、165(移动)，1704/1707/1708/1709(联通)、171、167（联通）
                    // 【卫星通信】： 1740[0-5] (电信)，1349(移动)
                    // 【物联网网号】：10648、1440 (移动)，10646、146(联通)，10649、1410(电信)
                    // 【国家工信部应急通信】：1740[6-9]，1741[0-2]
                    // 手机号码: 13[0-9], 14[5,6,7,8,9], 15[0-3, 5-9], 16[2,5,6,7], 17[0-8], 18[0-9], 19[0-3, 5-9]
                    // 移动号段: 13[4-9],147,148,15[0-2,7-9],165,170[3,5,6],172,178,18[2-4,7-8],19[5,7,8]
                    // 联通号段: 130,131,132,145,146,155,156,166,167,170[4,7,8,9],171,175,176,185,186,196
                    // 电信号段: 133,149,153,162,170[0,1,2],173,174[0-5],177,180,181,189,19[0,1,3,9]
                    // 广电号段: 192
                    reg = /^1((3[\d])|(4[5-9])|(5[0-3,5-9])|(6[2,5,6,7])|(7[0-8])|(8[0-9])|(9[0-3,5-9]))\d{8}$/
                    // reg = /^1((3[\d])|(4[5,6,9])|(5[0-3,5-9])|(6[5-7])|(7[0-8])|(8[1-3,5-8])|(9[1,8,9]))\d{8}$/;
                    if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false }
                    break
                case 'ID_Num':
                    reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
                    if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false }
                    break
                case 'zipcode':
                    reg = /^[0-9]{6}$/
                    if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false }
                    break
                case 'reg':
                    reg = new RegExp(rule[i].checkRule)
                    if (!reg.test(data[rule[i].name])) { this.error = rule[i].errorMsg; return false }
                    break
                case 'in':
                    if (rule[i].checkRule.indexOf(data[rule[i].name]) == -1) {
                        this.error = rule[i].errorMsg; return false
                    }
                    break
                case 'notnull':
                    if (data[rule[i].name] == null || data[rule[i].name].length < 1) { this.error = rule[i].errorMsg; return false }
                    break
            }
        }
        return true
    },
    isNumber: function(checkVal) {
        const reg = /^-?[1-9][0-9]?.?[0-9]*$/
        return reg.test(checkVal)
    }
}
