No more than code.
常用校验/判断
// 加密手机号
function dealPhoneNum(phone) {
return phone && phone.length == 11
? phone.replace(/^([0-9]{3})[0-9]{4}([0-9]{4})$/, '$1****$2')
: '';
}
// 生成26个字母+数字
function generateAZ() {
let start = 'a';
let result = '';
for (let i = 0; i < 26; i++) {
result += String.fromCharCode(start.charCodeAt(0) + i);
}
for (let i = 0; i < 10; i++) {
result += i;
}
return result;
}
// 生成UUID
export const generateUUID = () => {
/** 设置64位随机数,数字/字母 */
let result = '';
for (let i = 0; i < 64; i++) {
result += generateAZ()[Math.floor(Math.random() * 36)];
}
return result;
};
// 阿拉伯数字转化汉字
function toChinesNum (num) {
const changeNum = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'] // changeNum[0] = "零"
const unit = ['', '十', '百', '千', '万']
num = parseInt(num)
const getWan = (temp) => {
const strArr = temp.toString().split('').reverse()
let newNum = ''
for (let i = 0; i < strArr.length; i++) {
newNum = (i === 0 && strArr[i] === '0' ? '' : (i > 0 && strArr[i] === '0' && strArr[i - 1] === '0' ? '' : changeNum[strArr[i]] + (strArr[i] === '0' ? unit[0] : unit[i]))) + newNum
}
return newNum
}
const overWan = Math.floor(num / 10000)
let noWan = num % 10000
if (noWan.toString().length < 4) noWan = '0' + noWan
return overWan ? getWan(overWan) + '万' + getWan(noWan) : getWan(num)
}
// 保留两位小数
function toDecimal2 (x) {
let f = parseFloat(x)
if (isNaN(f)) {
return false
}
f = Math.round(x * 100) / 100
let s = f.toString()
let rs = s.indexOf('.')
if (rs < 0) {
rs = s.length
s += '.'
}
while (s.length <= rs + 2) {
s += '0'
}
return s
}
// 微信内置
export function isWeiXin () {
const ua = window.navigator.userAgent.toLowerCase()
return ua.match(/MicroMessenger/i)
}
// 安卓操作系统
export function isAndroid () {
const u = navigator.userAgent
return u.indexOf('Android') > -1 || u.indexOf('Linux') > -1
}
// ios操作系统
export function isIos () {
const u = navigator.userAgent
return !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)
}
// 获取url参数
export default function getQueryString (name) {
const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
const r = window.location.search.substr(1).match(reg)
if (r != null) return unescape(r[2])
return null
}