vue+elementui中IP,掩码,网关的校验以及子网掩码转换器
程序员文章站
2022-06-03 23:17:23
...
IP,掩码,网关的校验
/**
* 判断是否为空
*/
export function validatenull(val) {
if (typeof val === 'boolean') {
return false
}
if (typeof val === 'number') {
return false
}
if (val instanceof Array) {
if (val.length === 0) {
return true
}
} else if (val instanceof Object) {
if (JSON.stringify(val) === '{}') {
return true
}
} else {
if (val === 'null' || val === null || val === 'undefined' || val === undefined || val === '') {
return true
}
return false
}
return false
}
/**
* 验证IP地址格式
* @param {String} value IP地址
*/
export function validateIP(value) {
const reg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
return reg.test(value)
}
export function ipToNumber(ip) {
var num = 0
if (ip === '') {
return num
}
var aNum = ip.split('.')
if (aNum.length !== 4) {
return num
}
num += parseInt(aNum[0]) << 24
num += parseInt(aNum[1]) << 16
num += parseInt(aNum[2]) << 8
num += parseInt(aNum[3]) << 0
num = num >>> 0 // 这个很关键,不然可能会出现负数的情况
return num
}
/**
* 校验子网掩码
* @param {String} mask 子网掩码
*/
export function validateMask(mask) {
var umask = ipToNumber(mask)
umask = umask.toString(2)
return umask.indexOf('01') < 0
}
/**
* 校验网关是否在指定网段内
* @param {String} ip IP
* @param {String} mask 子网掩码
* @param {String} gate 网关
*/
export function validateGate(ip, mask, gate) {
// console.log(ip, mask, gate)
var uip = ipToNumber(ip)
var umask = ipToNumber(mask)
var ugate = ipToNumber(gate)
// console.log(uip, umask, ugate)
var min = uip & umask
var max = min + (~umask)
// console.log(min, ugate, max)
return ugate >= min && ugate <= max
}
//使用
data() {
var checkVCIp = (rule, ip, callback) => {
if (!validateIP(ip)) {
callback(new Error('输入格式不合法!'))
return
}
callback()
}
var validcodeip = (rule, mask, callback) => {
if (!validateIP(mask)) {
callback(new Error('输入格式不合法!'))
return
}
if (!validateMask(mask)) {
callback(new Error('输入并非子网掩码!'))
return
}
callback()
}
var validGate = (rule, gate, callback) => {
if (gate) {
if (!validateIP(gate)) {
callback(new Error('输入格式不合法!'))
return
}
}
callback()
},
rules: {
mask: [
{ required: true, message: '请设置掩码', trigger: 'blur' },
{ validator: validcodeip, trigger: 'blur' }
],
ip: [
{ required: true, message: '请设置IP', trigger: 'blur' },
{ validator: checkVCIp, trigger: 'blur' }
],
'ethernet.gateway4': [
{ required: true, message: '请设置网关', trigger: 'blur' },
{ validator: validGate, trigger: 'blur' }
]
}
}
/**
* 子网掩码转换器 掩码转位数
* @param {String} mask 掩码IP
*/
export const computeSNMA = (mask) => {
var maskItem = mask.split('.')
let bits = 0
for (let i = 0; i < maskItem.length; i++) {
const n = Number(maskItem[i])
for (let i = 0; i < 32; i++) {
if ((n & (1 << i)) === (1 << i)) {
bits++
}
}
}
// console.log(bits, mask, maskFormat(bits))
return bits
}
/**
* 子网掩码转换器 位数转掩码
* @param {Number} num 掩码的位数
*/
export const maskFormat = (num) => {
let mask = ''
const n1 = parseInt(num / 8)
for (let i = 0; i < n1; i++) {
if (i > 0) {
mask += '.'
}
mask += '255'
}
if (n1 < 4) {
const n2 = num % 8
let sn2 = '0'
for (let i = 0; i < n2; i++) {
if (i === 0) {
sn2 = ''
}
sn2 += '1'
}
if (n1 > 0) { mask += '.' }
mask += parseInt(sn2, 2)
const n3 = 4 - n1 - 1
for (let i = 0; i < n3; i++) {
mask += '.0'
}
}
// console.log(mask, num, computeSNMA(mask))
return mask
}