vue常用的时间、手机号等的格式化方法
export function phoneFilter(val) {
let str = "";
str = val.substr(0, 3) + "****" + val.substr(val.length - 4);
return str;
}
export function copyText(contentUrl) {
let oInput = document.createElement("input");
oInput.type = "text";
oInput.value = contentUrl;
document.body.appendChild(oInput);
oInput.select();
document.execCommand("Copy");
oInput.parentNode.removeChild(oInput);
}
export function checkPhone(value) {
var phone = value;
if (!/^1(3|4|5|6|7|8|9)\d{9}$/.test(phone)) {
return false;
} else {
return true;
}
}
export function format_date(dateDemo, format = "yyyy-MM-dd hh:mm:ss") {
let date = new Date(dateDemo * 1000);
if (date !== "Invalid Date") {
var o = {
"M+": date.getMonth() + 1,
"d+": date.getDate(),
"h+": date.getHours(),
"m+": date.getMinutes(),
"s+": date.getSeconds(),
"q+": Math.floor((date.getMonth() + 3) / 3),
S: date.getMilliseconds(),
};
if (/(y+)/.test(format)) {
format = format.replace(
RegExp.$1,
(date.getFullYear() + "").substr(4 - RegExp.$1.length)
);
}
for (var k in o) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(
RegExp.$1,
RegExp.$1.length === 1
? o[k]
: ("00" + o[k]).substr(("" + o[k]).length)
);
}
}
return format;
}
return "";
}
export function isCardNo(card) {
var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
if (reg.test(card) === false) {
return false;
} else {
return true;
}
}
export function getQueryString(name) {
let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
let url = window.location.hash.split('?')[1]? window.location.hash.split('?')[1].match(reg): null;
if (url != null) {
return decodeURI(url[2])
} else {
return null
}
}
function getAge(id){
const year = id.substr(6,4)
const month = id.substr(10,2)
const day = id.substr(12,2)
const timeBrth = new Date(`${year}/${month}/${day}`).getTime()
const timeNow = new Date().getTime()
const longTime = timeNow - timeBrth
const days = longTime / (1*24*60*60*1000)
let result = ''
if(days<31){
result = parseInt(days) + '天'
}else if(days<365){
result = `${parseInt(days/30)}月${parseInt(days%30)}天`
}else{
result = `${parseInt(days/365)}岁${parseInt(days%365/30)}月${parseInt(days%365%30)}天`
}
return result
}
function _typeof(item){
let res=Object.prototype.toString.call(item);
res = res.split(" ")[1];
res=res.substr(0,res.length-1);
return res;
}
function getSex(id){
const sex = id.substr(16,1)
return sex%2? '男': '女'
}
function throttle (func, wait ,type) {
if(type===1){
let previous = 0;
}else if(type===2){
let timeout;
}
return function() {
let context = this;
let args = arguments;
if(type===1){
let now = Date.now();
if (now - previous > wait) {
func.apply(context, args);
previous = now;
}
}else if(type===2){
if (!timeout) {
timeout = setTimeout(() => {
timeout = null;
func.apply(context, args)
}, wait)
}
}
}
}
function debounce (func, wait, immediate) {
let timeout;
return function() {
let context = this;
let args = arguments;
if (timeout) clearTimeout(timeout);
if (immediate) {
var callNow = !timeout;
timeout = setTimeout(() => {
timeout = null;
}, wait)
if (callNow) func.apply(context, args)
} else {
timeout = setTimeout(function() {
func.apply(context, args)
}, wait);
}
}
}
function numberToChinese (num) {
let AA = new Array('零', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十')
let BB = new Array('', '十', '百', '仟', '萬', '億', '点', '')
let a = ('' + num).replace(/(^0*)/g, '').split('.')
let k = 0
let re = ''
for (let i = a[0].length - 1; i >= 0; i--) {
switch (k) {
case 0:
re = BB[7] + re
break
case 4:
if (!new RegExp('0{4}//d{' + (a[0].length - i - 1) + '}$').test(a[0])) {
re = BB[4] + re
}
break
case 8:
re = BB[5] + re
BB[7] = BB[5]
k = 0
break
}
if (k % 4 === 2 && a[0].charAt(i + 2) !== 0 && a[0].charAt(i + 1) === 0) {
re = AA[0] + re
}
if (a[0].charAt(i) !== 0) {
re = AA[a[0].charAt(i)] + BB[k % 4] + re
}
k++
}
if (a.length > 1) {
re += BB[6]
for (let i = 0; i < a[1].length; i++) {
re += AA[a[1].charAt(i)]
}
}
if (re === '一十') {
re = '十'
}
if (re.match(/^一/) && re.length === 3) {
re = re.replace('一', '')
}
return re
}
function 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
}
}
function isDevice () {
var ua = navigator.userAgent.toLowerCase()
if (ua.match(/iPhone\sOS/i) === 'iphone os' || ua.match(/iPad/i) === 'ipad') {
return 'iOS'
}
if (ua.match(/Android/i) === 'android') {
return 'Android'
}
return 'Web'
}
function email(value) {
return /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(value);
}
function mobile(value) {
return /^1[3-9]\d{9}$/.test(value)
}
function url(value) {
return /http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w-.\/?%&=]*)?/.test(value)
}
function date(value) {
return !/Invalid|NaN/.test(new Date(value).toString())
}
function dateISO(value) {
return /^\d{4}[\/\-](0?[1-9]|1[012])[\/\-](0?[1-9]|[12][0-9]|3[01])$/.test(value)
}
function number(value) {
return /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/.test(value)
}
function digits(value) {
return /^\d+$/.test(value)
}
function idCard(value) {
return /^[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)$/.test(
value)
}
function carNo(value) {
const xreg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF]$)|([DF][A-HJ-NP-Z0-9][0-9]{4}$))/;
const creg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$/;
if (value.length === 7) {
return creg.test(value);
} else if (value.length === 8) {
return xreg.test(value);
} else {
return false;
}
}
function amount(value) {
return /^[1-9]\d*(,\d{3})*(\.\d{1,2})?$|^0\.\d{1,2}$/.test(value);
}
function chinese(value) {
let reg = /^[\u4e00-\u9fa5]+$/gi;
return reg.test(value);
}
function letter(value) {
return /^[a-zA-Z]*$/.test(value);
}
function enOrNum(value) {
let reg = /^[0-9a-zA-Z]*$/g;
return reg.test(value);
}
function contains(value, param) {
return value.indexOf(param) >= 0
}
function range(value, param) {
return value >= param[0] && value <= param[1]
}
function rangeLength(value, param) {
return value.length >= param[0] && value.length <= param[1]
}
function landline(value) {
let reg = /^\d{3,4}-\d{7,8}(-\d{3,4})?$/;
return reg.test(value);
}
function empty(value) {
switch (typeof value) {
case 'undefined':
return true;
case 'string':
if (value.replace(/(^[ \t\n\r]*)|([ \t\n\r]*$)/g, '').length == 0) return true;
break;
case 'boolean':
if (!value) return true;
break;
case 'number':
if (0 === value || isNaN(value)) return true;
break;
case 'object':
if (null === value || value.length === 0) return true;
for (var i in value) {
return false;
}
return true;
}
return false;
}
function jsonString(value) {
if (typeof value == 'string') {
try {
var obj = JSON.parse(value);
if (typeof obj == 'object' && obj) {
return true;
} else {
return false;
}
} catch (e) {
return false;
}
}
return false;
}
function array(value) {
if (typeof Array.isArray === "function") {
return Array.isArray(value);
} else {
return Object.prototype.toString.call(value) === "[object Array]";
}
}
function object(value) {
return Object.prototype.toString.call(value) === '[object Object]';
}
function code(value, len = 6) {
return new RegExp(`^\\d{${len}}$`).test(value);
}
export default {
throttle,
debounce,
numberToChinese,
trim,
isDevice,
email,
mobile,
url,
date,
dateISO,
number,
digits,
idCard,
carNo,
amount,
chinese,
letter,
enOrNum,
contains,
range,
rangeLength,
empty,
isEmpty: empty,
jsonString,
landline,
object,
array,
code
}