import { Catalog } from "@/api/monitoring/entity/monitoring";
import {CallStatus} from "@/api/entity/enums/callStatus";
//判斷是不是全數(shù)字
export function isDigit(value: string) {
return /^\d+$/.test(value);
}
/**
* 判斷是否為空
* @param value
* @returns
*/
export function isEmpty(value:any) {
return value == null || value === 'undefined' || value === '';
}
//判斷是否為null
export function isNull (value:any) {
return value == null || value === 'undefined'
}
/**
* 是否是中文
* @param value
* @returns
*/
export function isChinese(value:string){
let regExp = new RegExp('[\\u4E00-\\u9FFF]','g');
return regExp.test(value);
}
/**
* 文件大小
* @param value
* @returns
*/
export function renderSize (value:number) {
if (isEmpty(value) || value <= 0) {
return "0 Byte";
}
let unitArr = [" Byte", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"];
let index = 0;
let srcSize = parseFloat(value.toString());
index = Math.floor(Math.log(srcSize) / Math.log(1024));
let size:any = srcSize / Math.pow(1024, index);
size = size.toFixed(2);//保留的小數(shù)位數(shù)
return size + unitArr[index];
}
/**
* 是否是視頻
* @param fileName
* @returns
*/
export function checkSupportedVideoFormat(fileName:string){
return /\.(webm|mp4|m4v|mov|mkv)$/.test(fileName)
}
/**
* 是否是音頻
* @param fileName
* @returns
*/
export function checkSupportedAudioFormat(fileName:string){
return /\.(mp3|ogg|wav|flac|wma|opus|m4a|aac)$/.test(fileName)
}
export function saveImage (canvas:any, filename:String) {
let image = canvas.toDataURL('image/png').replace('image/png', 'image/octet-stream');
doSaveFile(image, (filename || 'file') + "_" + new Date().getTime() + '.png');
}
export function doSaveFile (objUrl:string, filename:string) {
let save_link:any = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
save_link.href = objUrl;
save_link.download = filename;
let event = document.createEvent('MouseEvents');
event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
save_link.dispatchEvent(event);
}
export function saveFile (data:any, filename:string) {
let blob:any = new Blob(data);
doSaveFile(URL.createObjectURL(blob), filename)
URL.revokeObjectURL(blob);
}
/**
* list轉(zhuǎn)樹結(jié)構(gòu)
* @param data //目標(biāo)數(shù)組
* @param id //id的屬性名
* @param pid //父id的屬性名
* @returns
*/
export function toTree (data:any[], id:string, pid:string,text?:string) {
let treeData:any[] = [];
if (!Array.isArray(data)) return treeData;
data.forEach(item => {
delete item.children; //刪除item下的children,以防多次調(diào)用
});
let map:any = {};
data.forEach((item:any) => {
map[item[id]] = item;
});
data.forEach(item => {
let parent = map[item[pid]]; //判斷item的pid是否是否存在map中
if (parent) { //如果存在則表示item不是最頂層的數(shù)據(jù)
(parent.children || (parent.children = [])).push(item)
}
else {
treeData.push(item) // 如果不存在 則是頂層數(shù)據(jù)
}
});
return treeData;
}
/**
* 去重
* @param data
* @param id
* @returns
*/
export function toRepeat (data:any[],id:any) {
let result:any[] = []
let targetIds:any[] = []
data.forEach((node:any) => {
if(targetIds.indexOf(node[id]) == -1) {
targetIds.push(node[id])
result.push(node)
}
})
return result
}
//獲取攝像頭總數(shù)和在線數(shù)量
export function forEachCameraCount (totalData:any, videoGroup:Catalog) {
if (isEmpty(videoGroup.children) || videoGroup.children!.length === 0) {
return
}
videoGroup.children!.forEach((item:Catalog) => {
if (item.chanType == 0) {
forEachCameraCount(totalData, item)
} else {
totalData.total += 1
if (item.state! == 'ON') {
totalData.online += 1
}
}
})
}
/**
* 獲取指定顏色的低透明度
* @param {string} color 指定顏色
* @param {number} opacity 透明度
* @returns
*/
export function getColorOpacity(color:any, opacity:any) {
let tempColor:any = null
// 是否是16進制
var regHex = /^[A-Fa-f0-9]{1,4}$/;
if (regHex.test(color)) {
tempColor = color
}else {
tempColor = rgbaToHex(color)
}
var theColor = tempColor.toLowerCase();
//十六進制顏色值的正則表達式
var r = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
// 如果是16進制顏色
if (theColor && r.test(theColor)) {
if (theColor.length === 4) {
var sColorNew = "#";
for (var i = 1; i < 4; i += 1) {
sColorNew += theColor.slice(i, i + 1).concat(theColor.slice(i, i + 1));
}
theColor = sColorNew;
}
//處理六位的顏色值
var sColorChange = [];
for (var i = 1; i < 7; i += 2) {
sColorChange.push(parseInt("0x" + theColor.slice(i, i + 2)));
}
return "rgba(" + sColorChange.join(",") + "," + opacity + ")";
}
return theColor;
}
/**
* rgba轉(zhuǎn)16進制
* @param color
* @returns
*/
export function rgbaToHex(color:any) {
var values = color
.replace(/rgba?\(/, '')
.replace(/\)/, '')
.replace(/[\s+]/g, '')
.split(',');
var a = parseFloat(values[3] || 1),
r = Math.floor(a * parseInt(values[0]) + (1 - a) * 255),
g = Math.floor(a * parseInt(values[1]) + (1 - a) * 255),
b = Math.floor(a * parseInt(values[2]) + (1 - a) * 255);
return "#" +
("0" + r.toString(16)).slice(-2) +
("0" + g.toString(16)).slice(-2) +
("0" + b.toString(16)).slice(-2);
}
/**
* 全屏
* @param ele
* @returns
*/
export function fullScreen(ele?: Element): Promise<void> {
if(ele) {
return ele.requestFullscreen()
} else {
return new Promise((resolve, reject) => {
reject("element is null")
})
}
}
常用工具方法(ts)
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。