// 四舍五入
export const numToFixed = (val, dafult) => {
if (!val) {
val = 0
};
if (!dafult) {
dafult = 2;
};
val = Math.round((val + Number.EPSILON) * 100) / 100;
return val.toFixed(dafult);
};
export const getObjType = obj => {
var toString = Object.prototype.toString
var map = {
'[object Boolean]': 'boolean',
'[object Number]': 'number',
'[object String]': 'string',
'[object Function]': 'function',
'[object Array]': 'array',
'[object Date]': 'date',
'[object RegExp]': 'regExp',
'[object Undefined]': 'undefined',
'[object Null]': 'null',
'[object Object]': 'object'
}
if (obj instanceof Element) {
return 'element'
}
return map[toString.call(obj)]
}
/**
- 对象深拷贝
*/
export const deepClone = data => {
var type = getObjType(data)
var obj
if (type === 'array') {
obj = []
} else if (type === 'object') {
obj = {}
} else {
// 不再具有下一层次
return data
}
if (type === 'array') {
for (var i = 0, len = data.length; i < len; i++) {
obj.push(deepClone(data[i]))
}
} else if (type === 'object') {
for (var key in data) {
obj[key] = deepClone(data[key])
}
}
return obj
}
/**
- 判断路由是否相等
*/
export const diff = (obj1, obj2) => {
delete obj1.close
var o1 = obj1 instanceof Object
var o2 = obj2 instanceof Object
if (!o1 || !o2) { /* 判断不是对象 */
return obj1 === obj2
}
if (Object.keys(obj1).length !== Object.keys(obj2).length) {
return false
// Object.keys() 返回一个由对象的自身可枚举属性(key值)组成的数组,例如:数组返回下表:let arr = ["a", "b", "c"];console.log(Object.keys(arr))->0,1,2;
}
for (var attr in obj1) {
var t1 = obj1[attr] instanceof Object
var t2 = obj2[attr] instanceof Object
if (t1 && t2) {
return diff(obj1[attr], obj2[attr])
} else if (obj1[attr] !== obj2[attr]) {
return false
}
}
return true
}
/**
- 设置灰度模式
*/
export const toggleGrayMode = (status) => {
if (status) {
document.body.className = document.body.className + ' grayMode'
} else {
document.body.className = document.body.className.replace(' grayMode', '')
}
}
/**
- 设置主题
*/
export const setTheme = (name) => {
document.body.className = name
}
/**
*加密处理
*/
export const encryption = (params) => {
let {
data,
type,
param,
key
} = params
const result = JSON.parse(JSON.stringify(data))
if (type === 'Base64') {
param.forEach(ele => {
result[ele] = btoa(result[ele])
})
} else {
param.forEach(ele => {
var data = result[ele]
key = CryptoJS.enc.Latin1.parse(key)
var iv = key
// 加密
var encrypted = CryptoJS.AES.encrypt(
data,
key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.ZeroPadding
})
result[ele] = encrypted.toString()
})
}
return result
}
/**
- 浏览器判断是否全屏
*/
export const fullscreenToggel = () => {
if (fullscreenEnable()) {
exitFullScreen()
} else {
reqFullScreen()
}
}
/**
- esc监听全屏
*/
export const listenfullscreen = (callback) => {
function listen() {
callback()
}
document.addEventListener('fullscreenchange', function() {
listen()
})
document.addEventListener('mozfullscreenchange', function() {
listen()
})
document.addEventListener('webkitfullscreenchange', function() {
listen()
})
document.addEventListener('msfullscreenchange', function() {
listen()
})
}
/**
- 浏览器判断是否全屏
*/
export const fullscreenEnable = () => {
return document.isFullScreen || document.mozIsFullScreen || document.webkitIsFullScreen
}
/**
- 浏览器全屏
*/
export const reqFullScreen = () => {
if (document.documentElement.requestFullScreen) {
document.documentElement.requestFullScreen()
} else if (document.documentElement.webkitRequestFullScreen) {
document.documentElement.webkitRequestFullScreen()
} else if (document.documentElement.mozRequestFullScreen) {
document.documentElement.mozRequestFullScreen()
}
}
/**
- 浏览器退出全屏
*/
export const exitFullScreen = () => {
if (document.documentElement.requestFullScreen) {
document.exitFullScreen()
} else if (document.documentElement.webkitRequestFullScreen) {
document.webkitCancelFullScreen()
} else if (document.documentElement.mozRequestFullScreen) {
document.mozCancelFullScreen()
}
}
/**
- 递归寻找子类的父类
*/
export const findParent = (menu, id) => {
for (let i = 0; i < menu.length; i++) {
if (menu[i].children.length !== 0) {
for (let j = 0; j < menu[i].children.length; j++) {
return menu[i]
} else {
if (menu[i].children[j].children.length !== 0) {
return findParent(menu[i].children[j].children, id)
}
}
}
}
}
}
/**
- 动态插入css
*/
export const loadStyle = url => {
const link = document.createElement('link')
link.type = 'text/css'
link.rel = 'stylesheet'
link.href = url
const head = document.getElementsByTagName('head')[0]
head.appendChild(link)
}
/**
- 判断路由是否相等
*/
export const isObjectValueEqual = (a, b) => {
let result = true
Object.keys(a).forEach(ele => {
const type = typeof (a[ele])
if (type === 'string' && a[ele] !== b[ele]) result = false
else if (type === 'object' && JSON.stringify(a[ele]) !== JSON.stringify(b[ele])) result = false
})
return result
}
/**
- 根据字典的value显示label
*/
export const findByvalue = (dic, value) => {
let result = ''
if (validatenull(dic)) return value
if (typeof (value) === 'string' || typeof (value) === 'number' || typeof (value) === 'boolean') {
let index = 0
index = findArray(dic, value)
if (index !== -1) {
result = dic[index].label
} else {
result = value
}
} else if (value instanceof Array) {
result = []
let index = 0
value.forEach(ele => {
index = findArray(dic, ele)
if (index !== -1) {
result.push(dic[index].label)
} else {
result.push(value)
}
})
result = result.toString()
}
return result
}
/**
- 根据字典的value查找对应的index
*/
export const findArray = (dic, value) => {
for (let i = 0; i < dic.length; i++) {
if (dic[i].value === value) {
return i
}
}
return -1
}
/**
- 生成随机len位数字
*/
export const randomLenNum = (len, date) => {
let random = ''
random = Math.ceil(Math.random() * 100000000000000).toString().substr(0, len || 4)
if (date) random = random + Date.now()
return random
}
/**
- 节流原理:在一定时间内,只能触发一次
- @param {Function} func 要执行的回调函数
- @param {Number} wait 延时的时间
- @param {Boolean} immediate 是否立即执行
- @return null
*/
let timer;
let flag
export function throttle(func, wait = 500, immediate = true) {
if (immediate) {
if (!flag) {
flag = true
// 如果是立即执行,则在wait毫秒内开始时执行
typeof func === 'function' && func()
timer = setTimeout(() => {
flag = false
}, wait)
}
} else if (!flag) {
flag = true
// 如果是非立即执行,则在wait毫秒内的结束处执行
timer = setTimeout(() => {
flag = false
typeof func === 'function' && func()
}, wait)
}
}
/**
- 防抖原理:一定时间内,只有最后一次操作,再过wait毫秒后才执行函数
- @param {Function} func 要执行的回调函数
- @param {Number} wait 延时的时间
- @param {Boolean} immediate 是否立即执行
- @return null
*/
let timeout = null
export function debounce(func, wait = 500, immediate = false) {
// 清除定时器
if (timeout !== null) clearTimeout(timeout)
// 立即执行,此类情况一般用不到
if (immediate) {
const callNow = !timeout
timeout = setTimeout(() => {
timeout = null
}, wait)
if (callNow) typeof func === 'function' && func()
} else {
// 设置定时器,当最后一次操作后,timeout不会再被清除,所以在延时wait毫秒后执行func回调方法
timeout = setTimeout(() => {
typeof func === 'function' && func()
}, wait)
}
}
// 处理金额
export function formatAmountWithUnit(amount) {
// 定义单位数组
const units = ['', '万', '亿', '兆'];
amount = String(amount);
// 将金额转换为字符串,并去除可能的逗号或其他字符
amount = parseFloat(amount.replace(/,/g, ''));
// 处理负数的情况(如果需要)
let isNegative = false;
if (amount < 0) {
isNegative = true;
amount = -amount;
}
// 计算单位的索引
let unitIndex = 0;
while (amount >= 10000 && unitIndex < units.length - 1) {
amount /= 10000;
unitIndex++;
}
// 保留两位小数
amount = amount.toFixed(2);
// 加上负号(如果需要)
let result = isNegative ? '-' + amount : amount;
// 加上单位
result += units[unitIndex];
return result;
}