UNPKG

sohelp-ele

Version:

SohelpEle Library

384 lines (383 loc) 12.3 kB
function digit(value, length = 2) { if (typeof value === "undefined" || value === null) { return ""; } if (String(value).length >= length) { return String(value); } return (Array(length).join("0") + value).slice(-length); } function toDateString(time, format = "yyyy-MM-dd HH:mm:ss") { if (!time) { return ""; } const transformStr = (str) => { if (typeof str === "string") { if (str.includes("T")) { return str.replace(/\//g, "-"); } else { return str.replace(/-/g, "/"); } } else if (typeof str === "number" && String(str).length === 10) { return str * 1e3; } return str; }; const date = new Date(transformStr(time)); const ymd = [ digit(date.getFullYear(), 4), digit(date.getMonth() + 1), digit(date.getDate()) ]; const hms = [ digit(date.getHours()), digit(date.getMinutes()), digit(date.getSeconds()) ]; return format.replace(/yyyy/g, ymd[0]).replace(/MM/g, ymd[1]).replace(/dd/g, ymd[2]).replace(/HH/g, hms[0]).replace(/mm/g, hms[1]).replace(/ss/g, hms[2]).replace(/YYYY/g, ymd[0]).replace(/DD/g, ymd[2]); } function timeAgo(time, onlyDate) { if (!time) { return ""; } const arr = [[], []]; const stamp = new Date().getTime() - new Date(time).getTime(); if (stamp < 0 || stamp > 1e3 * 60 * 60 * 24 * 31) { const date = new Date(time); arr[0][0] = digit(date.getFullYear(), 4); arr[0][1] = digit(date.getMonth() + 1); arr[0][2] = digit(date.getDate()); if (!onlyDate) { arr[1][0] = digit(date.getHours()); arr[1][1] = digit(date.getMinutes()); arr[1][2] = digit(date.getSeconds()); } return arr[0].join("-") + " " + arr[1].join(":"); } if (stamp >= 1e3 * 60 * 60 * 24) { return (stamp / 1e3 / 60 / 60 / 24 | 0) + "\u5929\u524D"; } if (stamp >= 1e3 * 60 * 60) { return (stamp / 1e3 / 60 / 60 | 0) + "\u5C0F\u65F6\u524D"; } if (stamp >= 1e3 * 60 * 3) { return (stamp / 1e3 / 60 | 0) + "\u5206\u949F\u524D"; } return "\u521A\u521A"; } function toTreeData(option) { var _a, _b; if (Array.isArray(option)) { option = { data: arguments[0], idField: arguments[1], parentIdField: arguments[2], childrenField: arguments[3], parentId: arguments[4], addParentIds: arguments[5], parentIdsField: arguments[6], parentIds: arguments[7] }; } const data = option.data; const idField = option.idField || option.idKey || "id"; const parentIdField = option.parentIdField || option.pidKey || "parentId"; const childrenField = option.childrenField || option.childKey || "children"; const parentIdIsNull = typeof option.parentId === "undefined" && typeof option.pid === "undefined"; const parentId = parentIdIsNull ? [] : (_a = option.parentId) != null ? _a : option.pid; const addParentIds = (_b = option.addParentIds) != null ? _b : option.addPIds; const parentIdsField = option.parentIdsField || option.parentsKey || "parentIds"; const parentIds = option.parentIds; if (parentIdIsNull) { data.forEach((d) => { let flag = true; for (let i = 0; i < data.length; i++) { if (d[parentIdField] == data[i][idField]) { flag = false; break; } } if (flag) { parentId.push(d[parentIdField]); } }); } const result = []; data.forEach((d) => { if (d[idField] == d[parentIdField]) { throw new Error( [ "data error: {", idField + ": ", JSON.stringify(d[idField]), parentIdField + ": ", JSON.stringify(d[parentIdField]), "}" ].join("") ); } const isArr = Array.isArray(parentId); const isParent = isArr ? parentId.includes(d[parentIdField]) : d[parentIdField] == parentId; if (isParent) { const r = Object.assign({}, d); const children = toTreeData({ data, idField, parentIdField, childrenField, parentId: d[idField], addParentIds, parentIdsField, parentIds: (parentIds != null ? parentIds : []).concat([d[idField]]) }); if (children.length > 0) { r[childrenField] = children; } if (addParentIds) { r[parentIdsField] = parentIds != null ? parentIds : []; } result.push(r); } }); return result; } function eachTreeData(data, callback, childrenField = "children", parent) { if (data) { data.forEach((d, i) => { var _a; if (callback && callback(d, i, parent) !== false && ((_a = d[childrenField]) == null ? void 0 : _a.length)) { eachTreeData(d[childrenField], callback, childrenField, d); } }); } } function formatTreeData(data, formatter, childrenField = "children", resultChildrenField = "children", parent) { const result = []; data == null ? void 0 : data.forEach((d, i) => { var _a; const item = formatter(d, i, parent); if (item) { if ((_a = d[childrenField]) == null ? void 0 : _a.length) { item[resultChildrenField] = formatTreeData( d[childrenField], formatter, childrenField, resultChildrenField, item ); } result.push(item); } }); return result; } function uuid(length = 32, radix) { const num = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; let result = ""; for (let i = 0; i < length; i++) { result += num.charAt(Math.floor(Math.random() * (radix || num.length))); } return result; } function random(m, n) { return Math.floor(Math.random() * (m - n) + n); } function formatNumber(num) { return String(num != null ? num : "").replace(/(\d{1,3})(?=(\d{3})+(?:$|\.))/g, "$1,"); } function escape(html) { return (html != null ? html : "").replace(/&(?!#?[a-zA-Z0-9]+;)/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/'/g, "&#39;").replace(/"/g, "&quot;"); } function htmlToText(html) { return (html != null ? html : "").replace(/<[^>]+>/g, ""); } function deepClone(obj) { let result; if (Array.isArray(obj)) { result = []; } else if (typeof obj === "object") { result = {}; } else { return obj; } Object.keys(obj).forEach((key) => { if (typeof obj[key] === "object") { result[key] = deepClone(obj[key]); } else { result[key] = obj[key]; } }); return result; } function assignObject(target, source, excludes) { Object.keys(target).forEach((key) => { if (!(excludes == null ? void 0 : excludes.includes(key))) { target[key] = source[key]; } }); return target; } function isExternalLink(url) { return !!(url && (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("//"))); } function bd09ToGcj02(point) { const x_pi = 3.141592653589793 * 3e3 / 180; const x = point.lng - 65e-4; const y = point.lat - 6e-3; const z = Math.sqrt(x * x + y * y) - 2e-5 * Math.sin(y * x_pi); const theta = Math.atan2(y, x) - 3e-6 * Math.cos(x * x_pi); return { lng: z * Math.cos(theta), lat: z * Math.sin(theta) }; } function gcj02ToBd09(point) { const x_pi = 3.141592653589793 * 3e3 / 180; const x = point.lng; const y = point.lat; const z = Math.sqrt(x * x + y * y) + 2e-5 * Math.sin(y * x_pi); const theta = Math.atan2(y, x) + 3e-6 * Math.cos(x * x_pi); return { lng: z * Math.cos(theta) + 65e-4, lat: z * Math.sin(theta) + 6e-3 }; } function play(url) { const audio = new Audio(url); audio.play(); return audio; } function device(key) { const agent = navigator.userAgent.toLowerCase(); const result = { os: null, ie: false }; const getVersion = function(label) { const exp = new RegExp(label + "/([^\\s\\_\\-]+)"); return (agent.match(exp) || [])[1] || false; }; if (/windows/.test(agent)) { result.os = "windows"; } else if (/linux/.test(agent)) { result.os = "linux"; } else if (/iphone|ipod|ipad|ios/.test(agent)) { result.os = "ios"; } else if (/mac/.test(agent)) { result.os = "mac"; } else if (/android/.test(agent)) { result.os = "android"; } if (!!window["ActiveXObject"] || "ActiveXObject" in window) { result.ie = (agent.match(/msie\s(\d+)/) || [])[1] || "11"; } result.weixin = getVersion("micromessenger"); if (key && !result[key]) { result[key] = getVersion(key); } result.android = /android/.test(agent); result.ios = result.os === "ios"; result.mobile = result.android || result.ios; return result; } function countdown(endTime, serverTime, callback) { if (typeof serverTime === "function") { callback = serverTime; serverTime = void 0; } const end = new Date(endTime).getTime(); const now = new Date(serverTime != null ? serverTime : new Date()).getTime(); const count = end - now; const time = [ Math.floor(count / (1e3 * 60 * 60 * 24)), Math.floor(count / (1e3 * 60 * 60)) % 24, Math.floor(count / (1e3 * 60)) % 60, Math.floor(count / 1e3) % 60 ]; const timer = setTimeout(() => { countdown(endTime, now + 1e3, callback); }, 1e3); callback && callback(count > 0 ? time : [0, 0, 0, 0], serverTime, timer); if (count <= 0) { clearTimeout(timer); } return timer; } function toggleFullscreen(el, fullscreen) { if (typeof el === "undefined" || el === null) { el = document.documentElement; } if (typeof fullscreen === "undefined" || fullscreen === null) { fullscreen = !isFullscreen(); } if (fullscreen) { const rfs = el.requestFullscreen || el.requestFullScreen || el.webkitRequestFullScreen || el.mozRequestFullScreen || el.msRequestFullScreen; if (rfs) { rfs.call(el); } else { throw new Error("\u60A8\u7684\u6D4F\u89C8\u5668\u4E0D\u652F\u6301\u5168\u5C4F\u6A21\u5F0F"); } } else { const cfs = document.exitFullscreen || document.exitFullScreen || document.webkitCancelFullScreen || document.mozCancelFullScreen || document.msExitFullscreen; cfs && cfs.call(document); } return fullscreen; } function isFullscreen() { return !!(document.fullscreenElement || document.webkitFullscreenElement || document.mozFullScreenElement || document.msFullscreenElement); } function screenWidth() { return document.documentElement.clientWidth || document.body.clientWidth; } function screenHeight() { return document.documentElement.clientHeight || document.body.clientHeight; } function contentWidth() { var _a, _b; return (_b = (_a = document.querySelector(".ele-admin-content-view")) == null ? void 0 : _a.clientWidth) != null ? _b : screenWidth(); } function contentHeight() { var _a, _b; return (_b = (_a = document.querySelector(".ele-admin-content-view")) == null ? void 0 : _a.clientHeight) != null ? _b : screenHeight(); } function exportSheet(XLSX, sheet, sheetName = "sheet1", type = "xlsx") { if (Array.isArray(sheet)) { sheet = XLSX.utils.aoa_to_sheet(sheet); } const workbook = { SheetNames: [sheetName], Sheets: {} }; workbook.Sheets[sheetName] = sheet; XLSX.writeFile(workbook, sheetName + "." + type); } function combineArrays(arrays, separator = "") { let result = [""]; for (let i = 0; i < arrays.length; i++) { const temp = []; for (let j = 0; j < result.length; j++) { for (let k = 0; k < arrays[i].length; k++) { temp.push(result[j] + separator + arrays[i][k]); } } result = temp; } return result; } function dataType(data) { let type = Object.prototype.toString.call(data); if (type === "[object String]") { type = "String"; } else if (type === "[object Number]") { type = "Number"; } else if (type === "[object Null]") { type = "Null"; } else if (type === "[object Boolean]") { type = "Boolean"; } else if (type === "[object Array]") { type = "Array"; } else if (type === "[object Object]") { type = "Object"; } else { type = "\u672A\u8FDB\u884C\u5224\u65AD\u7684\u7C7B\u578B:" + type; } return type; } export { assignObject, bd09ToGcj02, combineArrays, contentHeight, contentWidth, countdown, dataType, deepClone, device, digit, eachTreeData, escape, exportSheet, formatNumber, formatTreeData, gcj02ToBd09, htmlToText, isExternalLink, isFullscreen, play, random, screenHeight, screenWidth, timeAgo, toDateString, toTreeData, toggleFullscreen, uuid };