工具方法
import moment from 'moment'
moment.locale('zh-cn')
//获取日期
export const momentDate= function (date,format="YYYY-MM-DD"){
return moment(date).format(format)
}
export const throwError=function(msg="非法参数"){
throw new Error(msg)
}
/**
* 数字转带单位数字
* @param integer 要转换的数字,必须是整数
* @param compareVal 比较值 10000,1000,100...
* @param unit 单位 万 万元 千人...
*/
export const num2UnitNum=function(integer,unit,compareVal=10000){
let res,num=0,float=0,len=0
switch(Number(compareVal)){
case 10000:
len=4
break
case 1000:
len=3
break
case 100:
len=2
break
}
if(integer>compareVal){
num=String(integer).slice(0,-len)
float=String(integer).slice(-len)
res=parseFloat(num+"."+float).toFixed(2)+unit
}else{
res=integer
}
return res
}
/**
* 获取文件格式
*
*/
export function getFileFormat(filename) {
let arr=(filename || '').split(".")
let len=arr.length
return arr[len-1]
}
/**
*
*遮挡手机号码
*@param tel
*/
export function shelterMobile(tel){
tel=String(tel)
var reg = /^(\d{3})\d{6}(\d{2})$/
return tel.replace(reg, "$1******$2")
}
/**
* 多少时间前
* @param {number} time
* @param {string} label
* @return {string}
*/
function pluralize(time, label) {
if (time === 1) {
return time + label
}
return time + label + 's'
}
/**
* @param {number} time
*/
export function timeAgo(time) {
const between = Date.now() / 1000 - Number(time)
if (between < 3600) {
return pluralize(~~(between / 60), ' minute')
} else if (between < 86400) {
return pluralize(~~(between / 3600), ' hour')
} else {
return pluralize(~~(between / 86400), ' day')
}
}
/**
* 10000 => "10,000"
* @param {number} num
*/
export function toThousandFilter(num) {
return (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
}
/**
* 首字母大写
* @param {String} string
*/
export function uppercaseFirst(string) {
return string.charAt(0).toUpperCase() + string.slice(1)
}
/* *
用途:js中字符串超长作固定长度加省略号(...)处理
参数说明:
str:需要进行处理的字符串,可含汉字
len:需要显示多少个汉字,两个英文字母相当于一个汉字。
*/
export function beautySub(str, len) {
var reg = /[\u4e00-\u9fa5]/g, //专业匹配中文
slice = str.substring(0, len),
chineseCharNum = (~~(slice.match(reg) && slice.match(reg).length)),
realen = slice.length*2 - chineseCharNum;
return str.substr(0, realen) + (realen < str.length ? "..." : "");
}
/**
* 从某个字符串第一次出现的位置开始切割字符串
*/
export function subStrByStr(targetStr,handlerStr){
let index=targetStr.indexOf(handlerStr)
return targetStr.substring(index+1)
}
/**
*
*遮挡中间字符
*@param tel
*/
shelterStr : function(tel){
tel=String(tel)
var reg = /^([\w\D]{4})[\w\D]+([\w\D]{5})$/
return tel.replace(reg, "$1******$2")
}
//清空对象数据
export function clearInfo(obj){
Object.keys(obj).map(item=>{
console.log(obj[item],item,obj[item].constructor);
if(obj[item].constructor == Array){
obj[item] = []
}
if(typeof obj[item].constructor == String){
obj[item] = ""
}
if(typeof obj[item].constructor == Number){
obj[item] = 0
}
if(typeof obj[item].constructor == Object){
this.clearInfo(obj[item])
}
})
}
验证方法:
/**
* @description 判读是否为外链
* @param path
* @returns {boolean}
*/
export function isExternal(path) {
return /^(https?:|mailto:|tel:|\/\/)/.test(path)
}
/**
* @description 校验密码是否小于6位
* @param value
* @returns {boolean}
*/
export function isPassword(value) {
return value.length >= 6
}
/**
* @description 判断是否为数字
* @param value
* @returns {boolean}
*/
export function isNumber(value) {
const reg = /^[0-9]*$/
return reg.test(value)
}
/**
* @description 判断是否是名称
* @param value
* @returns {boolean}
*/
export function isName(value) {
const reg = /^[\u4e00-\u9fa5a-zA-Z0-9]+$/
return reg.test(value)
}
/**
* @description 判断是否为IP
* @param ip
* @returns {boolean}
*/
export function isIP(ip) {
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(ip)
}
/**
* @description 判断是否是传统网站
* @param url
* @returns {boolean}
*/
export function isUrl(url) {
const reg =
/^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
return reg.test(url)
}
/**
* @description 判断是否是小写字母
* @param value
* @returns {boolean}
*/
export function isLowerCase(value) {
const reg = /^[a-z]+$/
return reg.test(value)
}
/**
* @description 判断是否是大写字母
* @param value
* @returns {boolean}
*/
export function isUpperCase(value) {
const reg = /^[A-Z]+$/
return reg.test(value)
}
/**
* @description 判断是否是大写字母开头
* @param value
* @returns {boolean}
*/
export function isAlphabets(value) {
const reg = /^[A-Za-z]+$/
return reg.test(value)
}
/**
* @description 判断是否是字符串
* @param value
* @returns {boolean}
*/
export function isString(value) {
return typeof value === 'string' || value instanceof String
}
/**
* @description 判断是否是数组
* @param arg
*/
export function isArray(arg) {
if (typeof Array.isArray === 'undefined') {
return Object.prototype.toString.call(arg) === '[object Array]'
}
return Array.isArray(arg)
}
/**
* @description 判断是否是端口号
* @param value
* @returns {boolean}
*/
export function isPort(value) {
const reg =
/^([0-9]|[1-9]\d|[1-9]\d{2}|[1-9]\d{3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$/
return reg.test(value)
}
/**
* @description 验证手机号
* @param value
* @returns {boolean}
*/
export function checkPhone(phone){
if(!(/^1[34578]\d{9}$/.test(phone))){
return false;
}
return true
}
/**
* @description 判断是否是身份证号(第二代)
* @param value
* @returns {boolean}
*/
export function isIdCard(value) {
const reg =
/^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
return reg.test(value)
}
/**
* @description 判断是否是邮箱
* @param value
* @returns {boolean}
*/
export function isEmail(value) {
const reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
return reg.test(value)
}
/**
* @description 判断是否中文
* @param value
* @returns {boolean}
*/
export function isChina(value) {
const reg = /^[\u4E00-\u9FA5]{2,4}$/
return reg.test(value)
}
/**
* @description 判断是否为空
* @param value
* @returns {boolean}
*/
export function isBlank(value) {
return (
value === null ||
false ||
value === '' ||
value.trim() === '' ||
value.toLocaleLowerCase().trim() === 'null'
)
}
/**
* @description 判断是否为固话
* @param value
* @returns {boolean}
*/
export function isTel(value) {
const reg =
/^(400|800)([0-9\\-]{7,10})|(([0-9]{4}|[0-9]{3})([- ])?)?([0-9]{7,8})(([- 转])*([0-9]{1,4}))?$/
return reg.test(value)
}
/**
* @description 判断是否为数字且最多两位小数
* @param value
* @returns {boolean}
*/
export function isNum(value) {
const reg = /^\d+(\.\d{1,2})?$/
return reg.test(value)
}
/**
* @description 判断是否为json
* @param value
* @returns {boolean}
*/
export function isJson(value) {
if (typeof value === 'string') {
const obj = JSON.parse(value)
return !!(typeof obj === 'object' && obj)
}
return false
}
//----------------------------------------自己后写的---------------------------------------------------------------------
/**
* @description 判断是否为空
* @param value
* @returns {boolean}
*/
export function isNullOrEmpty(value){
console.log('value',value);
if(value==null||value==undefined||value=='')
return true;
return false;
}
export function isSpecialCharacter(str){
let pattern = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>《》/?~!@#¥……&*()——|{}【】‘;:”“'。,、? ]");
return pattern.test(str) ? true : false
}
//清空对象数据
export function clearInfo(obj){
Object.keys(obj).map(item=>{
if(obj[item]!=null){
if(obj[item].constructor == Array){
obj[item] = []
}
if(obj[item].constructor == String){
obj[item] = ""
}
if(obj[item].constructor == Number){
obj[item] = 0
}
if(obj[item].constructor == Object){
this.clearInfo(obj[item])
}
}
})
}