import {MessageBox, Message} from 'element-ui'
export function isPhone(phoneStr) {
let myreg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
if (!myreg.test(phoneStr)) {
return false;
} else {
return true;
}
}
export function isIdCard(idCardStr) {
let idcardReg = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
if (idcardReg.test(idCardStr)) {
return true;
} else {
return false;
}
}
export function isEmptyString(string) {
if (
string == undefined ||
typeof string == 'undefined' ||
!string ||
string == null ||
string == '' ||
/^\s+$/gi.test(string)
) {
return true;
} else {
return false;
}
}
export function getType(val) {
if (val == null) {
return val + "";
}
return typeof (val) === "object" ?
Object.prototype.toString.call(val).slice(8, -1).toLowerCase() :
typeof (val);
}
export function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
export function isArray(obj) {
return Object.prototype.toString.call(obj) === '[object Array]';
}
export function isArrayEmpty(val) {
if (val && val instanceof Array && val.length > 0) {
return false;
} else {
return true;
}
}
export function getQueryString(name) {
let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
let r = window.location.search.substr(1).match(reg);
if (r != null) {
return unescape(r[2]);
}
return null;
}
export function debounce(fn, delay) {
delay = delay || 1000;
let timer = null;
return function () {
let context = this;
let arg = arguments;
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(() => {
fn.apply(context, arg);
}, delay);
};
}
export function throttle2(fn, delay) {
let timer = null;
return function () {
let context = this;
let args = arguments;
if (!timer) {
timer = setTimeout(function () {
fn.apply(context, args);
clearTimeout(timer);
}, delay);
}
};
}
export function getDateTime(date) {
let timestamp = '';
if (date) {
date = date.substring(0, 19);
date = date.replace(/-/g, '/');
timestamp = new Date(date).getTime();
}
return timestamp;
}
export function getDates(data) {
let timeObj = {};
data = new Date(data);
let y = data.getFullYear();
let m =
data.getMonth() + 1 < 10
? '0' + (data.getMonth() + 1)
: data.getMonth() + 1;
let d = data.getDate() < 10 ? '0' + data.getDate() : data.getDate();
let w = data.getDay();
switch (w) {
case 1:
w = '星期一';
break;
case 2:
w = '星期二';
break;
case 3:
w = '星期三';
break;
case 4:
w = '星期四';
break;
case 5:
w = '星期五';
break;
case 6:
w = '星期六';
break;
case 7:
w = '星期日';
break;
}
let h = data.getHours() < 10 ? '0' + data.getHours() : data.getHours();
let mi = data.getMinutes() < 10 ? '0' + data.getMinutes() : data.getMinutes();
let s = data.getSeconds() < 10 ? '0' + data.getSeconds() : data.getSeconds();
timeObj = {
year: y + '',
month: m + '',
day: d + '',
week: w + '',
hour: h + '',
minute: mi + '',
second: s + ''
};
return timeObj;
}
export const formatDate = (fmt) => {
const date = new Date()
const o = {
'Y+': date.getFullYear(),
'M+': date.getMonth() + 1,
'D+': date.getDate(),
'h+': date.getHours(),
'm+': date.getMinutes(),
's+': date.getSeconds(),
W: date.getDay()
}
for (let k in o) {
if (new RegExp('(' + k + ')').test(fmt)) {
fmt = fmt.replace(RegExp.$1, () => {
if (k === 'W') {
const week = ['日', '一', '二', '三', '四', '五', '六']
return week[o[k]]
} else if (k === 'Y+' || RegExp.$1.length === 1) {
return o[k]
} else {
return ('00' + o[k]).substr(('' + o[k]).length)
}
})
}
}
return fmt
}
export function getTimer(data) {
var time = new Date();
var year = time.getFullYear();
var month = time.getMonth() + 1;
var dates = time.getDate();
var day = time.getDay();
var arr = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
var hours = time.getHours();
var minutes = time.getMinutes();
var seconds = time.getSeconds();
if (hours < 10) hours = "0" + hours;
if (minutes < 10) minutes = "0" + minutes;
if (seconds < 10) seconds = "0" + seconds;
if (data == 'year') return year;
if (data == 'date') return year + "-" + month + "-" + dates;
if (data == 'second') return year + "-" + month + "-" + dates + " " + hours + ':' + minutes + ':' + seconds;
if (data == 'week') return year + "-" + month + "-" + dates + " " + hours + ':' + minutes + ':' + seconds + ' ' + arr[day];
}
export function getMonthDates() {
var date1 = new Date();
var date2 = new Date(date1);
date2.setDate(date1.getDate() - 30);
var oldDay = `${date2.getFullYear()}-${date2.getMonth() + 1 < 10 ? `0${date2.getMonth() + 1}` : date2.getMonth() + 1}-${date2.getDate()}`;
var today = `${date1.getFullYear()}-${date1.getMonth() + 1 < 10 ? `0${date1.getMonth() + 1}` : date1.getMonth() + 1}-${date1.getDate()}`;
let date = [oldDay, today]
return date
}
export const unique = (arr) => {
if (Array.hasOwnProperty('from')) {
return Array.from(new Set(arr));
} else {
var n = {}, r = [];
for (var i = 0; i < arr.length; i++) {
if (!n[arr[i]]) {
n[arr[i]] = true;
r.push(arr[i]);
}
}
return r;
}
}
export function deWeight(arr, val) {
for (var i = 0; i < arr.length - 1; i++) {
for (var j = i + 1; j < arr.length; j++) {
if (arr[i][val] == arr[j][val]) {
arr.splice(j, 1);
j--;
}
}
}
return arr;
}
export const union = (a, b) => {
var newArr = a.concat(b);
return this.unique(newArr);
}
export const intersect = (a, b) => {
var _this = this;
a = this.unique(a);
return this.map(a, function (o) {
return _this.contains(b, o) ? o : null;
});
}
export const remove = (arr, ele) => {
var index = arr.indexOf(ele);
if (index > -1) {
arr.splice(index, 1);
}
return arr;
}
export const formArray = (ary) => {
var arr = [];
if (Array.isArray(ary)) {
arr = ary;
} else {
arr = Array.prototype.slice.call(ary);
}
;
return arr;
}
export const trim = (str, type) => {
type = type || 1
switch (type) {
case 1:
return str.replace(/\s+/g, "");
case 2:
return str.replace(/(^\s*)|(\s*$)/g, "");
case 3:
return str.replace(/(^\s*)/g, "");
case 4:
return str.replace(/(\s*$)/g, "");
default:
return str;
}
}
export function isObjectEmpty(val) {
if (JSON.stringify(val) === '{}') {
return true;
}
return false;
}
export function toTree(data) {
let result = [];
if (!Array.isArray(data)) {
return result
}
data.forEach(item => {
delete item.children;
});
let map = {};
data.forEach(item => {
map[item.id] = item;
});
data.forEach(item => {
let parent = map[item.parentId];
if (parent) {
(parent.children || (parent.children = [])).push(item);
} else {
result.push(item);
}
});
return result;
}
export function getBase64(file) {
return new Promise((resolve, reject) => {
let reader = new FileReader();
let fileResult = "";
reader.readAsDataURL(file);
reader.onload = () => {
fileResult = reader.result;
};
reader.onerror = (error) => {
reject(error);
};
reader.onloadend = () => {
resolve(fileResult);
};
});
}
export function IdCard(IdCard, type) {
if (type === 1) {
let birthday = IdCard.substring(6, 10) + "-" + IdCard.substring(10, 12) + "-" + IdCard.substring(12, 14)
return birthday
}
if (type === 2) {
if (parseInt(IdCard.substr(16, 1)) % 2 === 1) {
return "1"
} else {
return "2"
}
}
if (type === 3) {
var ageDate = new Date()
var month = ageDate.getMonth() + 1
var day = ageDate.getDate()
var age = ageDate.getFullYear() - IdCard.substring(6, 10) - 1
if (IdCard.substring(10, 12) < month || IdCard.substring(10, 12) === month && IdCard.substring(12, 14) <= day) {
age++
}
if (age <= 0) {
age = 1
}
return age
}
}