import React from 'react'
const utils = {
isObject: params => Object.prototype.toString.call(params) === '[object Object]',
isArray: params => Object.prototype.toString.call(params) === '[object Array]',
isFunction: params => Object.prototype.toString.call(params) === '[object Function]',
toSizeText: size => {
let num = 0
let unit = ''
if (size / 1024 < 1000) {
num = size / 1024
unit = 'KB'
} else if (size / 1024 / 1024 < 1000) {
num = size / 1024 / 1024
unit = 'MB'
} else {
num = size / 1024 / 1024 / 1024
unit = 'GB'
}
num = Math.floor(num * 100) / 100
return `${num}${unit}`
},
// 获取localStorage的已存容量
getLocalSurplus: () => {
if (!window.localStorage) {
console.log('浏览器不支持localStorage');
}
var size = 0;
for (let item in window.localStorage) {
if (window.localStorage.hasOwnProperty(item)) {
size += window.localStorage.getItem(item).length;
}
}
if (parseInt((size / 1024).toFixed(2)) > 4000) {
window.localStorage.clear()
}
console.log('当前localStorage已存容量为' + (size / 1024).toFixed(2) + 'KB');
},
// 获取local的剩余最大容量
// getMaxLocalSpace: () => {
// if(!window.localStorage) {
// console.log('当前浏览器不支持localStorage!')
// }
// var test = '0123456789';
// var add = function(num) {
// num += num;
// if(num.length == 10240) {
// test = num;
// return;
// }
// add(num);
// }
// add(test);
// var sum = test;
// var show = setInterval(function(){
// sum += test;
// try {
// window.localStorage.removeItem('test');
// window.localStorage.setItem('test', sum);
// console.log(sum.length / 1024 + 'KB');
// } catch(e) {
// console.log(sum.length / 1024 + 'KB超出最大限制');
// clearInterval(show);
// }
// }, 0.1)
// },
parseFormatNum: (number) => {
let v = number.split('').reverse()
let vn = ''
for (let i = 0; i < v.length; i++) {
vn += v[i] + ((i + 1) % 3 == 0 && (i + 1) != v.length ? ',' : '')
}
return vn.split('').reverse().join('')
},
idCardTest: (value) => {
let re = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
return re.test(value)
},
imsiTest: (value) => {
let re = /(^\d{15}$)/
return re.test(value)
},
phoneTest: (value) => {
let re = /^(13[0-9]|14[0-9]|15[0-9]|166|17[0-9]|18[0-9]|19[8|9])\d{8}$/
return re.test(value)
},
getQueryString: (h, name) => {
let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
let r = h.substr(1).match(reg);
if (r != null) return decodeURI(r[2]);
return null;
},
updateTreeData: (data) => {
let item = [];
data.map(({ id, text, nodes }) => {
item.push({
key: id,
value: id,
title: text,
children: nodes ? utils.updateTreeData(nodes) : []
})
})
return item;
},
changeObjToQuery: (obj, ignoreArr = []) => {
let arr = []
Object.keys(obj).filter(item => !ignoreArr.includes(item)).forEach(elem => {
if (obj[elem] && Array.isArray(obj[elem])) {
arr.push(`${elem}=${obj[elem].join(',')}`)
} else {
arr.push(`${elem}=${obj[elem] ? obj[elem] : ''}`)
}
})
return arr.join('&')
},
openNewWindow: (url) => {
let newWindow = window.open()
newWindow.opener = null
newWindow.target = '_blank'
newWindow.location = url
},
toFindRegion: (pro, city, county) => {
let cityData = JSON.parse(localStorage.getItem('_cityData'))
if (cityData) {
let region = []
for (let i = 0; i < cityData.length; i++) {
let elem = cityData[i]
if (elem.label === pro) {
region.push(elem.value)
if (elem.children) {
if (city.length && county) {
for (let j = 0; j < elem.children.length; j++) {
let node = elem.children[j]
if (node.label === city) {
region.push(node.value)
if (node.children) {
for (let k = 0; k < node.children.length; k++) {
let child = node.children[k]
if (child.label === county) {
region.push(child.value)
break
}
}
}
}
}
} else if (!city.length && county) {
let node = elem.children[0]
region.push(node.value)
if (node.children) {
for (let k = 0; k < node.children.length; k++) {
let child = node.children[k]
if (child.label === county) {
region.push(child.value)
break
}
}
}
}
}
}
}
return region
} else {
return []
}
},
// 生成列表序号
toSerialNumber: number => {
let result = number
switch (number.toString().length) {
case 1:
result = `00${number}`
break
case 2:
result = `0${number}`
break
default:
result += ''
break
}
return result
},
// 获取地址栏搜索参数 search 格式,如: ?deal_status=2
generateParams: search => {
let result = {}
let searchParamsStr = search.split('?')[1]
if (searchParamsStr) {
let searchParams = searchParamsStr.split('&')
searchParams.forEach(elem => {
let arr = elem.split('=')
let key = arr[0]
let value = arr[1]
if (key) {
result[key] = value
}
})
}
return result
},
// 判断视频、图片数组(data:媒体链接数组,type:img,video)
handleMedio: (data, type = "") => {
let _video = []
let _image = []
data && data.map(i => {
let suffixArr = i.split('.')
let fileSuffix = suffixArr[suffixArr.length - 1]
const imgList = ['png', 'jpg', 'jpeg', 'bmp', 'gif']
const videoList = ['mp4', 'rmvb', 'avi', 'flv', 'm2v', 'mkv', 'wmv', 'mp3', 'wav'];
if (imgList.indexOf(fileSuffix) > -1) {
_image.push(i)
} else if (videoList.indexOf(fileSuffix) > -1) {
_video.push(i)
} else {
return
}
})
if (type == 'img') return _image
else if (type == 'video') return _video
else return []
},
// 格式化数字
// 格式化数字展示
numberFormat:(num = '') => {
let str = typeof num == 'string' ? num : JSON.stringify(num);
if (!str) {
return "--"
}
let len = str.length;
let tem = null;
if (len > 8) {
let str1 = str.substring(0, len - 8);
let str2 = str.substring(str1.length, str1.length + 2);
str = str1 + "." + str2;
tem = (
<label className="wrap-num">
<label>{str}</label>
<label className="unit"> 亿</label>
</label>
);
} else if (len > 4 && len <= 8) {
let str1 = str.substring(0, len - 4);
let str2 = str.substring(str1.length, str1.length + 2);
str = str1 + "." + str2;
tem = (
<label className="wrap-num">
<label>{str}</label>
<label className="unit"> 万</label>
</label>
);
} else {
tem = (
<label className="wrap-num">
<label>{str}</label>
</label>
);
}
return tem;
}
}
export default utils
react 常用方法整理
最新推荐文章于 2024-06-18 17:59:21 发布