javascript公共⽅法1、获取字符的长度
getByteLen(val) {
var len = 0;
for (var i = 0; i < val.length; i++) {
var a = val.charAt(i);
if (a.match(/[^\x00-\xff]/ig) != null) {
len += 2;
} el {
len += 1;
}
}
return len;
},
2、格式化⾦额,单位:分(eg:430分=4.30元)
formatFee(fee, suffix = '') {
if (!fee) {
return 0;
}
return Number(fee).toFixed(2) + suffix;
},
3、格式化公⾥(eg:3000 = 3公⾥)
formatMileage(mileage, text) {
if (!mileage) {
return 0;
}
if (mileage >= 1000) {
text = text || " km";
return Math.floor(mileage / 100) / 10 + text;
} el {
text = text || " m";
return mileage + text;
}
},
4、隐藏⼿机号中间4位
formatPhone(phone) {
phone += '';
place(/(\d{3})\d*(\d{4})/g, '$1***$2')
},
5、隐藏⾝份证号中11位
演讲稿英语formatIdentity(number) {
number += '';
place(/(\d{3})\d*(\d{4})/g, '$1***********$2')
},
6、moment格式化时间
momentFormat(time,format='YYYY-MM-DD HH:mm:ss') {
return moment(time).format(format)
},
7、 listToTree
listToTree(data = []) {
data = data.map(d => ({...d, key: d._id || d.cat_id}));
const isHasChild = id => data.some(m => m.parent_id === id);
const loopMenus = (menus) => {
menus.forEach(it => {
if (isHasChild(it._id || it.cat_id)) {
it.children = [];
data.forEach(it2 => {
if (it2.parent_id === (it._id || it.cat_id)) {
if (it2.parent_id === (it._id || it.cat_id)) {
it.children.push(it2);
}
});
loopMenus(it.children);
}
})
}
const getTopLevel = m => m.parent_id == 0;
let menus = data.filter(getTopLevel);
loopMenus(menus);
return menus
}
8、设置头部信息
tMetaAboutSeoFun(data) {
let description = document.querySelector('meta[name="description"]'), keywords = document.querySelector('meta[name="keywords"]');
description["content"] = data.summary;
keywords["content"] = data.keyword;
if (data.title) {
document.title = data.title;
}
},
9、对象转为formData格式
obj2FormData(param) {
const formData = new FormData();
Object.keys(param).forEach((key) => {
formData.append(key, param[key]);
});
return formData;
},
10、去除数组中的值为fal值
bouncer(arr) {
return arr.filter(function (val) {
return !(!val || val === "");
});
},
11、限制输⼊框的字符长度
compositionInput(id, that) {
let inputLock = fal;
that.timer = tTimeout(() => {
let nickNameId = ElementById(id);
if (nickNameId) {
nickNameId.addEventListener('compositionstart', () => {
inputLock = true;
});
给你我所有的爱nickNameId.addEventListener('compositionend', () => {
inputLock = fal;
var value = nickNameId.value,
maxlength = Attribute('maxlength');
if (this.byteLen(value) > maxlength) {
nickNameId.value = Maxlen(value, maxlength);
}
});
nickNameId.addEventListener('input', () => {
if (!inputLock) {
var value = nickNameId.value,
maxlength = Attribute('maxlength');
工作调动
maxlength = Attribute('maxlength');
if (this.byteLen(value) > maxlength) {
nickNameId.value = Maxlen(value, maxlength);
}
}
});
}
}, 1000);
},
byteLen(str) {
//正则取到中⽂的个数,然后len*count+原来的长度。不⽤replace
str += '';
var tmp = str.match(/[^\x00-\xff]/g) || [];
return str.length + tmp.length;
},
getMaxlen(str, maxlen) {
var sResult = '', L = 0, i = 0, stop = fal, sChar;
if (place(/[^\x00-\xff]/g, 'xxx').length <= maxlen) {
return str;
}
while (!stop) {
sChar = str.charAt(i);
L += sChar.match(/[^\x00-\xff]/) ? 2 : 1;
if (L > maxlen) {
stop = true;
} el {
sResult += sChar;
i++;
}
}
return sResult;
},
使⽤⽅法
<Input maxLength={30} type="text" onBlur={this.checkoutNicknameBlur}/>
compositionInput('nick_name', this);
12、promi的使⽤
registerCheckHttp = (key, type) => { // 检查⽤户昵称、⼿机、邮箱是否已存在
return new Promi((resolve, reject) => {
axiosHttp(`api/WebSite/Register/Check?key=${key}&keyType=${type}`, '', 'GET').then((res) => { resolve(res);
}).catch(e => {
reject(e)
})
});
};
}).catch(e => {
});
13、ba64封装的加密和解密函数
ba64Function() {
// private property
let _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";圆周角定理
/
/ public method for encoding
var output = "";
var output = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
input = _utf8_encode(input);
while (i < input.length) {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} el if (isNaN(chr3)) {
enc4 = 64;
}
output = output +
_keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
_keyStr.charAt(enc3) + _keyStr.charAt(enc4);
}
return output;
};
// public method for decoding
this.decode = function (input) {
var output = "";
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var i = 0;
input = place(/[^A-Za-z0-9\+\/\=]/g, "");
不到黄昏梦未成打一字
while (i < input.length) {
enc1 = _keyStr.indexOf(input.charAt(i++));
enc2 = _keyStr.indexOf(input.charAt(i++));
enc3 = _keyStr.indexOf(input.charAt(i++));
enc4 = _keyStr.indexOf(input.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output = output + String.fromCharCode(chr1);
if (enc3 != 64) {
output = output + String.fromCharCode(chr2);
忍俊不禁意思
}
if (enc4 != 64) {
output = output + String.fromCharCode(chr3);
}
}
output = _utf8_decode(output);
return output;
};
// private method for UTF-8 encoding
let _utf8_encode = function (string) {
string = place(/\r\n/g, "\n");
var utftext = "";
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
} el if ((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
} el {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128); utftext += String.fromCharCode((c & 63) | 128);
}
}
return utftext;
};
// private method for UTF-8 decoding
let _utf8_decode = function (utftext) {
var string = "";
var i = 0;
var c = 0, c1 = 0, c2 = 0, c3 = 0;
while (i < utftext.length) {
c = utftext.charCodeAt(i);
家与国的关系if (c < 128) {
string += String.fromCharCode(c);
i++;
} el if ((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i + 1);
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
} el {
c2 = utftext.charCodeAt(i + 1);
c3 = utftext.charCodeAt(i + 2);
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}
}
return string;
}
}
使⽤⽅法
this.ba64 = new ba64Function();
this.ba64.decode(pass_word),//解密
de(pass_word),//加密
14、替换地址栏中的url
placeState(null, null, '/?keyword='+item)
15、cookie设置和获取
const {cookieExpires, cookieName, cookieValue} = res.data;
kieValue = cookieValue;
var exp = new Date();
exp.Time() + cookieExpires * 60 * 1000);
男士职业装
16、antd中表单中的应⽤
hasErrors(fieldsError) {
// console.log(fieldsError);
return Object.keys(fieldsError).some(field => fieldsError[field]);
},
hasValues(value) {
// console.log(value);
if (JSON.stringify(value) === "{}") {
return true
}
let item = null;