123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739 |
- // 东八区时间差
- let _timezone = 8; //目标时区时间,东八区 东时区正数 西时区负数
- let _offset_GMT = new Date().getTimezoneOffset(); // 本地时间和格林威治的时间差,单位为分钟
- let diffTime = _offset_GMT * 60 * 1000 + _timezone * 60 * 60 * 1000; //0
- /**
- * 字符串是6-16位的大小写英文字母、数字组合。
- */
- export const reg = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{6,16}$/;
- /**
- * 字符串是6-16位的大小写英文字母、数字组合。
- */
- export const pwdCheck = (rule, value, callback) => {
- if (!reg.test(value)) {
- return callback(new Error("密码格式不正确!"));
- } else {
- callback();
- }
- };
- export const codeCheck = (rule, value, callback) => {
- if ((value && value.length !== 6) || !value) {
- return callback(new Error("请输入6位区域码!"));
- } else {
- callback();
- }
- };
- export const padDevices = [
- {
- pdorder: 1,
- pdtype: "摄像头",
- enabled: 1,
- icon: "ivu-icon iconfont icon-ball-camera"
- },
- {
- pdorder: 2,
- pdtype: "GPS",
- enabled: 1,
- icon: "ivu-icon iconfont icon-GPSxinxi"
- },
- {
- pdorder: 3,
- pdtype: "蓝牙",
- enabled: 2,
- icon: "ivu-icon iconfont icon-lanya"
- },
- {
- pdorder: 4,
- pdtype: "USB连接",
- enabled: 2,
- icon: "ivu-icon iconfont icon-USB"
- },
- {
- pdorder: 5,
- pdtype: "电话短信",
- enabled: 2,
- icon: "ivu-icon iconfont icon-dianhuaduanxin_line"
- },
- {
- pdorder: 6,
- pdtype: "数据流量",
- enabled: 2,
- icon: "ivu-icon iconfont icon-liuliang"
- },
- {
- pdorder: 7,
- pdtype: "Wi-Fi高级选项",
- enabled: 2,
- icon: "ivu-icon iconfont icon-wifiguanbi"
- },
- {
- pdorder: 8,
- pdtype: "SD卡",
- enabled: 2,
- icon: "ivu-icon iconfont icon-sdka-09"
- },
- {
- pdorder: 9,
- pdtype: "OTG",
- enabled: 2,
- icon: "ivu-icon iconfont icon-icon-OTG"
- }
- ];
- export const padViolates = [
- {
- title: "已ROOT",
- violateType: 1,
- checked: false,
- noticed: false,
- handleMethod: ""
- },
- {
- title: "安装非商店应用",
- violateType: 2,
- checked: false,
- noticed: false,
- handleMethod: ""
- },
- {
- title: "更换SIM卡",
- violateType: 3,
- checked: false,
- noticed: false,
- handleMethod: ""
- },
- {
- title: "USB连接电脑",
- violateType: 4,
- checked: false,
- noticed: false,
- handleMethod: ""
- }
- ];
- export const weekDay = [
- {
- label: "周日",
- alias: "星期日",
- value: 7
- },
- {
- label: "周一",
- alias: "星期一",
- value: 1
- },
- {
- label: "周二",
- alias: "星期二",
- value: 2
- },
- {
- label: "周三",
- alias: "星期三",
- value: 3
- },
- {
- label: "周四",
- alias: "星期四",
- value: 4
- },
- {
- label: "周五",
- alias: "星期五",
- value: 5
- },
- {
- label: "周六",
- alias: "星期六",
- value: 6
- }
- ];
- export const getWeekNameFromVal = (val, useAlias = false) => {
- const week = weekDay.filter((v) => {
- return v.value === val;
- });
- if (week.length > 0) {
- return useAlias ? week[0].alias : week[0].label;
- }
- return "";
- };
- export const getWeekDateName = (date, useAlias = false) => {
- const week = weekDay.map((v) => {
- return useAlias ? v.alias : v.label;
- });
- const index = new Date(date).getDay();
- return useAlias ? week[index].alias : week[index].label;
- };
- export const toChinesNum = (num) => {
- let changeNum = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
- let unit = ["", "十", "百", "千", "万"];
- num = parseInt(num);
- let getWan = (temp) => {
- let strArr = temp.toString().split("").reverse();
- let newNum = "";
- for (var 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;
- };
- let 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);
- };
- // 保留小数(四舍五入)
- export const fomatFloat = (src, pos) => {
- return Math.round(src * Math.pow(10, pos)) / Math.pow(10, pos);
- };
- export const setStringToDate = (timeStr) => {
- // IE不能识别字符串日期
- let splitArray = timeStr.split(" ");
- let dateArray = splitArray[0].split("-");
- let d = new Date();
- d.setFullYear(dateArray[0], dateArray[1] - 1, dateArray[2]);
- d.setHours(0);
- d.setMinutes(0);
- d.setSeconds(0);
- return d;
- };
- // 获取当前时间
- export const getNow = () => {
- var date = new Date();
- var year = date.getFullYear();
- var month = date.getMonth() + 1;
- var day = date.getDate();
- if (month < 10) {
- month = "0" + month;
- }
- if (day < 10) {
- day = "0" + day;
- }
- return year + month + day;
- };
- /**
- * 格式化秒
- * @param int value 总秒数
- * @return string result 格式化后的字符串
- */
- export const formatSeconds = (value) => {
- var theTime = parseInt(value); // 需要转换的时间秒
- var theTime1 = 0; // 分
- var theTime2 = 0; // 小时
- if (theTime > 60) {
- theTime1 = parseInt(theTime / 60);
- theTime = parseInt(theTime % 60);
- if (theTime1 > 60) {
- theTime2 = parseInt(theTime1 / 60);
- theTime1 = parseInt(theTime1 % 60);
- }
- }
- var result = "";
- if (theTime === 0 && theTime1 === 0 && theTime2 === 0) {
- result = "0秒";
- }
- if (theTime > 0) {
- result = "" + parseInt(theTime) + "秒";
- }
- if (theTime1 > 0) {
- result = "" + parseInt(theTime1) + "分" + result;
- }
- if (theTime2 > 0) {
- result = "" + parseInt(theTime2) + "小时" + result;
- }
- return result;
- };
- /**
- * 格式化秒
- * @param int value 总秒数
- * @return string result 格式化后的字符串 HH:mm:ss
- */
- export const format_seconds = (value) => {
- let theTime = parseInt(value); // 需要转换的时间秒
- let theTime1 = 0; // 分
- let theTime2 = 0; // 小时
- if (theTime > 60) {
- theTime1 = parseInt(theTime / 60);
- theTime = parseInt(theTime % 60);
- if (theTime1 > 60) {
- theTime2 = parseInt(theTime1 / 60);
- theTime1 = parseInt(theTime1 % 60);
- }
- }
- let t_hour = 0;
- let t_min = 0;
- let t_sec = 0;
- if (theTime > 0) {
- t_sec = parseInt(theTime);
- }
- if (theTime1 > 0) {
- t_min = parseInt(theTime1);
- }
- if (theTime2 > 0) {
- t_hour = parseInt(theTime2);
- }
- return t_hour > 0
- ? `${t_hour > 9 ? "" : 0}${t_hour}:${t_min > 9 ? "" : 0}${t_min}:${
- t_sec > 9 ? "" : 0
- }${t_sec}`
- : `${t_min > 9 ? "" : 0}${t_min}:${t_sec > 9 ? "" : 0}${t_sec}`;
- };
- /**
- * 获取对象类型
- */
- 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 date_format = (unixtimestamp, type) => {
- if (!unixtimestamp) {
- return "";
- }
- unixtimestamp = new Date(unixtimestamp * 1000 + diffTime);
- var year = 1900 + unixtimestamp.getYear();
- var month = "0" + (unixtimestamp.getMonth() + 1);
- var date = "0" + unixtimestamp.getDate();
- var hour = "0" + unixtimestamp.getHours();
- var minute = "0" + unixtimestamp.getMinutes();
- var second = "0" + unixtimestamp.getSeconds();
- return (
- year +
- "-" +
- month.substring(month.length - 2, month.length) +
- "-" +
- date.substring(date.length - 2, date.length) +
- " " +
- hour.substring(hour.length - 2, hour.length) +
- ":" +
- minute.substring(minute.length - 2, minute.length) +
- (type ? "" : ":" + second.substring(second.length - 2, second.length))
- );
- };
- //时间转化为时间戳
- export const date_time = (datestring) => {
- let time = new Date(datestring).getTime() / 1000;
- if (!time && datestring) {
- //兼容火狐浏览器
- datestring = datestring.replace(new RegExp(/-/gm), "/");
- time = new Date(datestring).getTime() / 1000;
- }
- return parseInt(time);
- };
- /**
- * 对象深拷贝
- */
- 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 function format_date(time, format = "yyyy-MM-dd hh:mm:ss") {
- let date = new Date(time * 1000 + diffTime);
- if (date !== "Invalid Date") {
- let o = {
- "M+": date.getMonth() + 1, // month
- "d+": date.getDate(), // day
- "h+": date.getHours(), // hour
- "m+": date.getMinutes(), // minute
- "s+": date.getSeconds(), // second
- "q+": Math.floor((date.getMonth() + 3) / 3), // quarter
- S: date.getMilliseconds() // millisecond
- };
- if (/(y+)/.test(format)) {
- format = format.replace(
- RegExp.$1,
- (date.getFullYear() + "").substr(4 - RegExp.$1.length)
- );
- }
- for (let k in o) {
- if (new RegExp("(" + k + ")").test(format)) {
- format = format.replace(
- RegExp.$1,
- RegExp.$1.length === 1
- ? o[k]
- : ("00" + o[k]).substr(("" + o[k]).length)
- );
- }
- }
- return format;
- }
- return "";
- }
- /**
- * 日期格式化
- */
- export function dateFormat(dateDemo, format = "yyyy-MM-dd hh:mm:ss") {
- let date = new Date(dateDemo);
- if (date !== "Invalid Date") {
- var o = {
- "M+": date.getMonth() + 1, // month
- "d+": date.getDate(), // day
- "h+": date.getHours(), // hour
- "m+": date.getMinutes(), // minute
- "s+": date.getSeconds(), // second
- "q+": Math.floor((date.getMonth() + 3) / 3), // quarter
- S: date.getMilliseconds() // millisecond
- };
- if (/(y+)/.test(format)) {
- format = format.replace(
- RegExp.$1,
- (date.getFullYear() + "").substr(4 - RegExp.$1.length)
- );
- }
- for (var k in o) {
- if (new RegExp("(" + k + ")").test(format)) {
- format = format.replace(
- RegExp.$1,
- RegExp.$1.length === 1
- ? o[k]
- : ("00" + o[k]).substr(("" + o[k]).length)
- );
- }
- }
- return format;
- }
- return "";
- }
-
- // 颜色渐变函数
- export function hexToRgba(hex, opacity) {
- let rgbaColor = "";
- let reg = /^#[\da-f]{6}$/i;
- if (reg.test(hex)) {
- rgbaColor = `rgba(${parseInt("0x" + hex.slice(1, 3))},${parseInt(
- "0x" + hex.slice(3, 5)
- )},${parseInt("0x" + hex.slice(5, 7))},${opacity})`;
- }
- return rgbaColor;
- }
- // 包含大小写字母和数字的随机组合
- export const generateRandomString = (n = 6) => {
- let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
- let result = "";
- let hasUpperCase = false;
- let hasLowerCase = false;
- let hasDigit = false;
- // 当n<3时,只需随机选择字符即可
- if (n < 3) {
- for (let i = 0; i < n; i++) {
- result += chars.charAt(Math.floor(Math.random() * chars.length));
- }
- return result;
- }
- // 当n>=3时,需要确保包含大写字母、小写字母和数字
- while (result.length < n) {
- let randomIndex = Math.floor(Math.random() * chars.length);
- let randomChar = chars.charAt(randomIndex);
-
- // 确保每种类型的字符至少被选中一次
- if (hasUpperCase && hasLowerCase && hasDigit) {
- result += randomChar;
- } else {
- // 如果还没包含大写字母,并且当前字符是大写字母,则添加
- if (!hasUpperCase && /[A-Z]/.test(randomChar)) {
- result += randomChar;
- hasUpperCase = true;
- }
- // 如果还没包含小写字母,并且当前字符是小写字母,则添加
- else if (!hasLowerCase && /[a-z]/.test(randomChar)) {
- result += randomChar;
- hasLowerCase = true;
- }
- // 如果还没包含数字,并且当前字符是数字,则添加
- else if (!hasDigit && /\d/.test(randomChar)) {
- result += randomChar;
- hasDigit = true;
- }
- // 否则跳过这个字符,重新选择
- }
- }
- // 如果字符串长度仍然小于n,则随机添加字符直到满足长度
- while (result.length < n) {
- result += chars.charAt(Math.floor(Math.random() * chars.length));
- }
- // 打乱结果字符串的顺序以确保随机性
- result = result
- .split("")
- .sort(function () {
- return 0.5 - Math.random();
- })
- .join("");
- return result;
- };
- /**
- * 提取html字符串中的文字
- */
- export const getHtmlPlainText = (html_str = "") => {
- let text = html_str.replace(/<[^<>]+>/g, "").trim();
- return text;
- };
- //判断html中是否有文字或图片
- export const htmlHasContent = (html_str = "") => {
- let text = html_str.replace(/<[^<>]+>/g, "").trim();
- let imgReg = /<img [^>]*src=['"]([^'"]+)[^>]*>/gi;
- let imgUrls = html_str.match(imgReg) || [];
- imgUrls.filter((url) => {
- return url && url.match(/src=['"]?([^'"]*)['"]?/i);
- });
- return Boolean(text) || imgUrls.length > 0;
- };
- /**
- * 压缩图片方法
- * @param {file} file 文件或文件数组
- * @param {Number} quality 图片质量(取值0-1之间默认0.92)
- */
- export const compressImg = (file, quality) => {
- if (Array.isArray(file)) {
- return Promise.all(Array.from(file).map((e) => compressImg(e, quality))); // 如果是 file 数组返回 Promise 数组
- } else {
- let qualitys = 1;
- //单位:MB
- let fileSize = parseInt((file.size / 1024 / 1024).toFixed(2));
- if (1 < fileSize && fileSize < 5) {
- qualitys = 0.92;
- }
- if (5 < fileSize && fileSize < 10) {
- qualitys = 0.85;
- }
- if (10 < fileSize) {
- qualitys = 0.52;
- }
- if (quality) {
- qualitys = quality;
- }
- return new Promise((resolve) => {
- if (qualitys >= 1) {
- resolve(file);
- } else {
- let fileType = file.type;
- if (!fileType || !fileType.includes("image/")) {
- resolve(file);
- return;
- }
- const fileReader = new FileReader(); // 创建 FileReader
- fileReader.onload = ({ target: { result: src } }) => {
- const image = new Image(); // 创建 img 元素
- image.onload = () => {
- const mCanvas = document.createElement("canvas"); // 创建 canvas 元素
- const mCtx = mCanvas.getContext("2d");
- let targetWidth = image.width;
- let targetHeight = image.height;
- let originWidth = image.width;
- let originHeight = image.height;
- if (1 <= fileSize) {
- let maxWidth = 1400;
- let maxHeight = 1400;
- if (5 < fileSize) {
- maxWidth = 1920;
- maxHeight = 1920;
- }
- targetWidth = originWidth;
- targetHeight = originHeight;
- // 图片尺寸超过的限制
- if (originWidth > maxWidth || originHeight > maxHeight) {
- if (originWidth / originHeight > maxWidth / maxHeight) {
- // 更宽,按照宽度限定尺寸
- targetWidth = maxWidth;
- targetHeight = Math.round(
- maxWidth * (originHeight / originWidth)
- );
- } else {
- targetHeight = maxHeight;
- targetWidth = Math.round(
- maxHeight * (originWidth / originHeight)
- );
- }
- }
- }
- mCanvas.width = targetWidth;
- mCanvas.height = targetHeight;
- mCtx.clearRect(0, 0, targetWidth, targetHeight);
- mCtx.drawImage(image, 0, 0, targetWidth, targetHeight); // 绘制 canvas
- const canvasURL = mCanvas.toDataURL(fileType, qualitys);
- const buffer = atob(canvasURL.split(",")[1]);
- let length = buffer.length;
- const bufferArray = new Uint8Array(new ArrayBuffer(length));
- while (length--) {
- bufferArray[length] = buffer.charCodeAt(length);
- }
- const miniFile = new File([bufferArray], file.name, {
- type: fileType
- });
- resolve(miniFile);
- };
- image.src = src;
- };
- fileReader.readAsDataURL(file);
- }
- });
- }
- };
- export function uuid() {
- const s = [];
- const hexDigits = "0123456789abcdef";
- for (let i = 0; i < 36; i++) {
- let start = Math.floor(Math.random() * 0x10);
- s[i] = hexDigits.substring(start, start + 1);
- }
- s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
- let start = (s[19] & 0x3) | 0x8;
- s[19] = hexDigits.substring(start, start + 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
- s[8] = s[13] = s[18] = s[23] = "-";
-
- return s.join("");
- }
-
- export function getPosInDom(outerDom, innerDom) {
- // 元素的坐标和大小
- const rect = outerDom.getBoundingClientRect();
- const rect2 = innerDom.getBoundingClientRect();
- const x = rect2.left - rect.left;
- const y = rect2.top - rect.top;
-
- return {
- x: x,
- y: y,
- width: rect2.width,
- height: rect2.height
- };
- }
-
- export function getPosInDomByEvent(outerDom, event) {
- // 元素的坐标和大小
- const rect = outerDom.getBoundingClientRect();
-
- // 鼠标坐标
- const x = event.clientX;
- const y = event.clientY;
-
- // 计算鼠标在元素中的坐标
- const elementX = x - rect.left;
- const elementY = y - rect.top;
-
- return {
- x: elementX,
- y: elementY
- };
- }
- /**
- * 返回开始日期(以0点开始)和结束日期(以24点结束)的秒数
- * @param beginDate 开始日期
- * @param endDate 结束日期
- */
- export const dateRangeToSecond = ([beginDate, endDate]) => {
- let _begintime = null;
- let _endtime = null;
- if (beginDate) {
- let _beginDate = new Date(beginDate);
- _begintime = new Date(
- _beginDate.getFullYear(),
- _beginDate.getMonth(),
- _beginDate.getDate(),
- 0,
- 0,
- 0
- ).getTime();
- _begintime = parseInt(_begintime / 1000);
- }
- if (endDate) {
- let _endDate = new Date(endDate);
- _endtime = new Date(
- _endDate.getFullYear(),
- _endDate.getMonth(),
- _endDate.getDate(),
- 23,
- 59,
- 59
- ).getTime();
- _endtime = parseInt(_endtime / 1000);
- }
- return {
- begintime: _begintime,
- endtime: _endtime
- };
- };
- /**
- * 所有设备事件日志
- */
- export const doEventInfo = {
- 101: "更换SIM卡",
- 102: "违规开启蓝牙",
- 103: "USB连接电脑",
- 104: "USB拔出电脑",
- 105: "插入SD卡",
- 106: "拔出SD卡",
- 201: "账号登录",
- 202: "首次使用",
- 203: "账号退出",
- 211: "安装应用(安装了非应用商店应用违规)",
- 212: "卸载应用(卸载强制安装应用违规)",
- 213: "启动应用"
- };
- /**
- * 所有违规设备事件日志
- */
- export const breakRuleDoEvent = {
- 101: "更换SIM卡",
- 102: "违规开启蓝牙",
- 103: "USB连接电脑",
- 104: "USB拔出电脑",
- 105: "插入SD卡",
- 106: "拔出SD卡",
- 211: "安装应用(安装了非应用商店应用违规)",
- 212: "卸载应用(卸载强制安装应用违规)"
- };
- /**
- * 所有设备推送类型
- */
- export const pushTypeInfo = {
- 1: "发送消息",
- 2: "解除锁定",
- 3: "锁定",
- 4: "更新策略",
- 5: "重启设备",
- 6: "恢复出厂",
- 7: "解除管控",
- 8: "解除绑定"
- };
|