生产监控前端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

341 lines
8.0 KiB

2 years ago
/**
* Author: Fu Guobin
* Date: 2020/06/28
* Last Modified by: Fu Guobin
* Last Modified time: 2023/05/29
* Copyright:Daniel(Fu Guobin)
* Description:封装函数工具
*/
import { useTransition, TransitionPresets } from '@vueuse/core';
/**
*
* typeOf('type')
*/
export function typeOf(obj: any) {
return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase();
}
/**
* uuid
*/
export function uuid() {
const temp_url = URL.createObjectURL(new Blob());
const uuid = temp_url.toString();
URL.revokeObjectURL(temp_url); //释放这个url
return uuid.substring(uuid.lastIndexOf('/') + 1);
}
/**
*
*/
export function numberRandom(min: number, max: number) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
/**
*
* @param {number} num
* @param {number} duration
*/
export function transitionNum(num: number, duration: number) {
const initial = ref(0);
const initialNum = useTransition(initial, {
duration: duration,
transition: TransitionPresets.easeInOutCubic
});
initial.value = num;
return initialNum;
}
/**
*
* 1: ios
* 2: android
* 3: 其它
*/
export function getOSType() {
let u = navigator.userAgent;
let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1;
let isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
if (isIOS) {
return 1;
}
if (isAndroid) {
return 2;
}
return 3;
}
/**
*
* @param {number} min
* @param {number} max
*/
export function random(min: number, max: number) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
/**
*
* @param {number} args (1, 2, 3, 4, 5)
*/
export function average(...args: any[]) {
return args.reduce((a, b) => a + b) / args.length;
}
/**
*
* @param {Date} date new Date("2023-05-18")
* @param {Date} otherDate new Date("2023-07-18")
*/
export function diffDays(date: any, otherDate: any) {
return Math.ceil(Math.abs(date - otherDate) / (1000 * 60 * 60 * 24));
}
/**
*
* @param {number} num
*/
export function isEven(num: number) {
return num % 2 === 0;
}
/**
*
*/
export function randomColor() {
return `#${Math.random().toString(16).slice(2, 8).padEnd(6, '0')}`;
}
/**
* RGB颜色转换为十六进制颜色值
* @param {number} rgb RGB(255, 255, 255)
*/
export function rgbToHex(r: number, g: number, b: number) {
return '#' + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
}
/**
*
* @param {string} str ('background-color')
*/
export function toCamelCase(str: string) {
return str.trim().replace(/[-_\s]+(.)?/g, (_: any, c: string) => (c ? c.toUpperCase() : ''));
}
/**
*
* @param {string} str ('hello world')
*/
export function uppercaseWords(str: string) {
return str.replace(/^(.)|\s+(.)/g, c => c.toUpperCase());
}
/**
*
* @param {array} arr
*/
export function uniqueArray(arr = []) {
return [...new Set(arr)];
}
/**
*
* @param {array} arr
* @param {string} key
*/
export function uniqueArrayObject(arr = [], key = 'id') {
if (arr.length === 0) return;
let list = [];
const map = {};
arr.forEach(item => {
if (!map[item[key]]) {
map[item[key]] = item;
}
});
list = Object.values(map);
return list;
}
/**
*
* @param {array} data
*/
export function getLastLevelNode(data: any) {
if (data.length === 0) {
return null; // 数组为空,返回null或自定义默认值
}
const lastItem = data[data.length - 1]; // 获取数组的最后一个元素
if (lastItem.children && lastItem.children.length > 0) {
return getLastLevelNode(lastItem.children); // 继续递归调用
}
return lastItem; // 返回最后一个层级
}
/**
*
* @param {array} data
*/
export function getFirstNodeLastLevel(data: any) {
if (data.length === 0) {
return null; // 数组为空,返回null或自定义默认值
}
const firstNode = data[0];
if (firstNode.children && firstNode.children.length > 0) {
return getFirstNodeLastLevel(firstNode.children);
}
return firstNode; // 返回第一个节点的最后一个层级的数据
}
/**
*
* @param {array} obj1 1
* @param {array} obj2 2
*/
export function isEqual(obj1: any, obj2: any) {
// 判断两个对象的类型是否相同
if (typeof obj1 !== typeof obj2) {
return false;
}
// 如果是基本类型数据,则直接比较它们的值
if (typeof obj1 !== 'object') {
return obj1 === obj2;
}
// 获取obj1和obj2的属性名称
const obj1Keys = Object.keys(obj1);
const obj2Keys = Object.keys(obj2);
// 如果两个对象的属性数量不同,则它们不相等
if (obj1Keys.length !== obj2Keys.length) {
return false;
}
// 递归比较obj1和obj2的属性和值
for (let key of obj1Keys) {
if (!isEqual(obj1[key], obj2[key])) {
return false;
}
}
// 如果所有属性和值都相等,则两个对象相等
return true;
}
2 years ago
/**
*
*/
export function numberPad(source: number, length = 2) {
let pre = '';
const negative = source < 0;
const string = String(Math.abs(source));
if (string.length < length) {
pre = new Array(length - string.length + 1).join('0');
}
return (negative ? '-' : '') + pre + string;
}
/**
*
*/
export function cutNumber(number: number, no = 2) {
if (typeof number != 'number') {
number = Number(number);
}
return Number(number.toFixed(no));
}
/**
*
* @param phone
* @returns
*/
export function hidePhone(phone: string) {
return phone.replace(/^(\d{3})\d{4}(\d{4})$/, '$1****$2');
}
/**
*
* @param idCard
* @returns
*/
export function hideIdCard(idCard: string) {
return idCard.replace(/^(.{6})(?:\d+)(.{4})$/, '$1****$2');
}
/**
* canvas画布是否为空函数
* @param {object} canvas
*/
export function isCanvasBlank(canvas: { width: number; height: number; toDataURL: () => string }) {
let blank = document.createElement('canvas'); //系统获取一个空canvas对象
blank.width = canvas.width;
blank.height = canvas.height;
return canvas.toDataURL() == blank.toDataURL(); //比较值相等则为空
}
/**
*
* @param hours
* @returns
*/
export function timePeriod(hours: number) {
if (hours >= 3 && hours < 8) {
return '早安!';
} else if (hours >= 8 && hours < 11) {
return '上午好!';
} else if (hours >= 11 && hours < 13) {
return '中午好!';
} else if (hours >= 13 && hours < 17) {
return '下午好!';
} else if (hours >= 17 && hours < 23) {
return '晚上好!';
} else if (hours >= 23 && hours < 3) {
return '晚安!';
}
}
/**
* Check if an element has a class
* @param {HTMLElement} elm
* @param {string} cls
* @returns {boolean}
*/
export function hasClass(ele: HTMLElement, cls: string) {
return !!ele.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
}
/**
* Add class to element
* @param {HTMLElement} elm
* @param {string} cls
*/
export function addClass(ele: HTMLElement, cls: string) {
if (!hasClass(ele, cls)) ele.className += ' ' + cls;
}
/**
* Remove class from element
* @param {HTMLElement} elm
* @param {string} cls
*/
export function removeClass(ele: HTMLElement, cls: string) {
if (hasClass(ele, cls)) {
const reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
ele.className = ele.className.replace(reg, ' ');
}
}
/**
* @param {string} path
* @returns {Boolean}
*/
export function isExternal(path: string) {
const isExternal = /^(https?:|http?:|mailto:|tel:)/.test(path);
return isExternal;
}