乐趣区

封装常用的插件

import {Message} from ‘element-ui’

/*

  • [Note description]
  • @param {object} prop [这个参数为 Message 的配置项目, 可直接传入需要提示的信息]

*/
const note = (prop) => {
prop.offset = 80;
Message.closeAll();
Message(prop);
}

// 数据的校验规则 传入数据
// this.$valid([
// {valid: this.strates_info.name, msg: ‘ 请输入策略组名称 ’},
// {valid: this.strates_info.ids.length, msg: ‘ 请至少选择一组策略 ’}
// ])
const valid = (props) => {
return new Promise((resolve, reject) => {

const valid_item = props.find((e) => !e.valid)

if (valid_item) {
  note({
    message: valid_item.msg,
    type: 'warning',
    duration: 1000
  })
} else {resolve()
}

})
}

const rules = {
// 各类数字字母下划线汉字的校验
name_rule: (str) => {

return new RegExp('^[\u4e00-\u9fa5_a-zA-Z0-9]+$', 'g').test(str)

},
// 纯数字的校验
num_rule: (str) => {

return new RegExp('^[0-9]*[1-9][0-9]*$', 'g').test(Number(str))

},

num_rule_time: (num, str) => {

const num_ = Number(num)
if (new RegExp('^[0-9]*[1-9][0-9]*$', 'g').test(num_)) {if (str == 's' && num_ <= 86400 || str == 'm' && num_ <= 1440 || str == 'h' && num_ <= 24) {return true}
  return false
}
return false

},

// 手机号校验
phone_rule: (str) => {

return new RegExp('^[1][3,4,5,7,8][0-9]{9}$').test(str)
// return new RegExp("^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$").test(str)

},

// 邮箱的校验
main_rule: (str) => {

return new RegExp('^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$').test(str)

},

// 密码校验
password_rule: (str) => {

return new RegExp('^(?=.*[0-9].*)(?=.*[a-zA-Z].*).{8,16}$').test(str)

},

// 大于 0 的数
num0_rule: (str) => {

return new RegExp('^(?!(0[0-9]{0,}$))[0-9]{1,}[.]{0,}[0-9]{0,}$').test(str)

},

num0_rule1: (str) => {

return new RegExp('^([1-9]\\d*(\\.[0-9]*[1-9])?)|(0\\.[0-9]*[1-9])|0$').test(str)

},

num0_rule2: (str) => {

const regu = /^[1-9]\d*$/;
if (str != '') {if (!regu.test(str)) {return false}
  return true
}
return false

},

num0_rule3: (str) => {

const regu = /^[1-9]\d*$/;
if (str != '') {if (!regu.test(str)) {return false}
  if (str > 100) {return false}
  return true
}
return false

},

time_rule: (str) => {

return new RegExp('^([01][0-9]|2[0-3])(:[0-5][0-9]){1,2}$').test(str)

},

// 身份证号校验
ID_card_rule: (idCode) => {

const vcity = {
  11: '北京',
  12: '天津',
  13: '河北',
  14: '山西',
  15: '内蒙古',
  21: '辽宁',
  22: '吉林',
  23: '黑龙江',
  31: '上海',
  32: '江苏',
  33: '浙江',
  34: '安徽',
  35: '福建',
  36: '江西',
  37: '山东',
  41: '河南',
  42: '湖北',
  43: '湖南',
  44: '广东',
  45: '广西',
  46: '海南',
  50: '重庆',
  51: '四川',
  52: '贵州',
  53: '云南',
  54: '西藏',
  61: '陕西',
  62: '甘肃',
  63: '青海',
  64: '宁夏',
  65: '新疆',
  71: '台湾',
  81: '香港',
  82: '澳门',
  91: '国外'
};
// 检查号码是否符合规范,包括长度,类型
const isCardNo = (card) => {
  // 身份证号码为 15 位或者 18 位,15 位时全为数字,18 位前 17 位为数字,最后一位是校验位,可能为数字或字符 X
  const reg = /(^\d{15}$)|(^\d{17}(\d|X)$)/;
  if (reg.test(card) === false) {return false;}
  return true;
};
// 取身份证前两位, 校验省份
const checkProvince = (card) => {const province = card.substr(0, 2);
  if (vcity[province] == undefined) {return false;}
  return true;
};
// 检查生日是否正确
const checkBirthday = (card) => {
  const len = card.length;
  // 身份证 15 位时,次序为省(3 位)市(3 位)年(2 位)月(2 位)日(2 位)校验位(3 位),皆为数字
  // 15 位身份证号码 使用 632123820927051
  if (len == '15') {const re_fifteen = /^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/;
    var arr_data = card.match(re_fifteen);
    var year = arr_data[2];
    var month = arr_data[3];
    var day = arr_data[4];
    var birthday = new Date('19' + year + '/' + month + '/' + day);
    return verifyBirthday('19' + year, month, day, birthday);
  }
  // 身份证 18 位时,次序为省(3 位)市(3 位)年(4 位)月(2 位)日(2 位)校验位(4 位),校验位末尾可能为 X
  if (len == '18') {const re_eighteen = /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/;
    var arr_data = card.match(re_eighteen);
    var year = arr_data[2];
    var month = arr_data[3];
    var day = arr_data[4];
    var birthday = new Date(year + '/' + month + '/' + day);
    return verifyBirthday(year, month, day, birthday);
  }
  return false;
};
// 校验日期
const verifyBirthday = (year, month, day, birthday) => {const now = new Date();
  const now_year = now.getFullYear();
  // 年月日是否合理
  if (birthday.getFullYear() == year && birthday.getMonth() + 1 == month && birthday.getDate() == day) {// 判断年份的范围(3 岁到 100 岁之间)
    const time = now_year - year;
    if (time >= 3 && time <= 100) {return true;}
    return false;
  }
  return false;
};
// 校验位的检测
const checkParity = (card) => {
  // 15 位转 18 位
  card = changeFivteenToEighteen(card);
  const len = card.length;
  if (len == '18') {const arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
    const arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
    let cardTemp = 0,
      i, valnum;
    for (i = 0; i < 17; i++) {cardTemp += card.substr(i, 1) * arrInt[i];
    }
    valnum = arrCh[cardTemp % 11];
    if (valnum == card.substr(17, 1)) {return true;}
    return false;
  }
  return false;
};
// 15 位转 18 位身份证号
const changeFivteenToEighteen = (card) => {if (card.length == '15') {const arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
    const arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
    let cardTemp = 0,
      i;
    card = card.substr(0, 6) + '19' + card.substr(6, card.length - 6);
    for (i = 0; i < 17; i++) {cardTemp += card.substr(i, 1) * arrInt[i];
    }
    card += arrCh[cardTemp % 11];
    return card;
  }
  return card;
};
// 是否为空
if (idCode === '') {return false;}
// 校验长度,类型
if (isCardNo(idCode) === false) {return false;}
// 检查省份
if (checkProvince(idCode) === false) {return false;}
// 校验生日
if (checkBirthday(idCode) === false) {return false;}
// 检验位的检测
if (checkParity(idCode) === false) {return false;}
return true;

}
}

// format 日期格式化
// (new Date()).format(“yyyy-MM-dd hh:mm:ss.S”) ==> 2006-07-02 08:09:04.423
const format = function(fmt) {
const o = {‘M+’: this.getMonth() + 1, // 月份

'd+': this.getDate(), // 日
'h+': this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, // 小时
'H+': this.getHours(), // 小时
'm+': this.getMinutes(), // 分
's+': this.getSeconds(), // 秒
'q+': Math.floor((this.getMonth() + 3) / 3), // 季度
'S': this.getMilliseconds() // 毫秒 

};
if (/(y+)/.test(fmt)) {

fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));

}
for (const 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;
}

const date_default = (() => {
const end = new Date();
const start = new Date();
if (!window.NeedLicense) {

start.setMonth(end.getMonth() - 6);
start.setTime(start.getTime() + 3600 * 1000 * 24 * 1);

}
return {

start_time: format.call(start, 'yyyy-MM-dd'),
end_time: format.call(end, 'yyyy-MM-dd')

}
})()

const downLoad = (url) => {
const downUrl = ‘/api/app’ + url;
window.open(downUrl, ‘_blank’);
}

export {
note,
valid,
rules,
format,
date_default,
downLoad
}

export const DomControl = {

$: function $(str) {

let Ele = false;
if (str.charAt(0) == '.') {const temstr = str.replace(/^\./, '');
  if (document.getElementsByClassName) { // 兼容 ie8
    Ele = document.getElementsByClassName(temstr);
  } else {Ele = [];
    const Arr1 = this.makeItArray(document.getElementsByTagName('*'));
    for (let i = 0, j = Arr1.length; i < j; i++) {if (this.hasClass(Arr1[i], temstr).a) {Ele.push(Arr1[i]);
        // console.log(Arr1[i]);
      }
    }
  }
} else if (str.charAt(0) == '#') {Ele = document.getElementById(str.replace(/^\#/, ''));
} else {Ele = document.getElementsByTagName(str);
}
return Ele;

},

hasClass: function hasClass(obj, clas) {

const temB = {a: false, b: false};
if (obj.className) {var Arr = obj.className.split(' ');
} else {return temB;}

// console.log(Arr);
for (const i in Arr) {if (Arr[i] == clas) {
    temB.a = true;
    temB.b = i;
  }
}
return temB;

},

makeItArray: function makeItArray(obj) {

const temArray = [];
const l = obj.length;
for (let i = 0; i < l; i++) {temArray.push(obj[i]);
}

return temArray;

},
toggleClass: function toggleClass(obj, clas) {

let temB = {};
if (typeof clas == 'string') {temB = this.hasClass(obj, clas);
  var Arr = obj.className.split(' ');
  if (temB.a) {Arr.splice(temB.b, 1);
  } else {Arr.push(clas);
  }

  obj.className = Arr.join(' ');
} else {temB = this.hasClass(obj, clas[0]);
  var Arr = obj.className.split(' ');
  if (temB.a) {Arr.splice(temB.b, 1);
    Arr.push(clas[1]);
  } else {temB = this.hasClass(obj, clas[1]);
    Arr.splice(temB.b, 1);
    Arr.push(clas[0]);
  }

  obj.className = Arr.join(' ');
}

},
addClass: function(obj, clas) {

const temB = this.hasClass(obj, clas);
const Arr = obj.className.split(' ');
if (temB.a) {return false;}
Arr.push(clas);
obj.className = Arr.join(' ');

},
removeClass: function(obj, clas) {// 删除 class

const temB = this.hasClass(obj, clas);
const Arr = obj.className.split(' ');
if (temB.a) {Arr.splice(temB.b, 1);
}
obj.className = Arr.join(' ');

},
parents: function(obj, selector) {

/*  这里 是找寻 具有 selector 的父级
 * selector
 * 分为  id
 *      class
 *      标签
 * */
let _obj = obj;
console.log(DomControl.hasClass(_obj, selector.slice(1)));

console.log(selector.charAt(0) === '.');
if (selector.charAt(0) === '#') { // id 找父集
  while (obj.id !== selector.slice(1)) { // 如果 obj 的 id 不等于 selector 的 ID
    _obj = _obj.parentNode;
  }
} else if (selector.charAt(0) === '.') { // 如果 obj 的 class 不等于 selector 的 Class
  while (_obj && _obj.nodeType !== 9 && !DomControl.hasClass(_obj, selector.slice(1)).a) { // 如果 obj 存在 并且 obj 不是 document
    _obj = _obj.parentNode;
    console.log(obj);
  }
} else {while (_obj && _obj.nodeType !== 9 && _obj.nodeName.toLowerCase() !== selector) {_obj = _obj.parentNode;}
}


// 找到 obj  返回 obj  没有找到 就返回 null
return _obj && _obj.nodeType === 9 ? null : _obj;

}
}

退出移动版