window.teaweb = { set: function (key, value) { localStorage.setItem(key, JSON.stringify(value)); }, get: function (key) { var item = localStorage.getItem(key); if (item == null || item.length == 0) { return null; } return JSON.parse(item); }, getString: function (key) { var value = this.get(key); if (typeof (value) == "string") { return value; } return ""; }, getBool: function (key) { return Boolean(this.get(key)); }, remove: function (key) { localStorage.removeItem(key) }, match: function (source, keyword) { if (source == null) { return false; } if (keyword == null) { return true; } source = source.trim(); keyword = keyword.trim(); if (keyword.length == 0) { return true; } if (source.length == 0) { return false; } var pieces = keyword.split(/\s+/); for (var i = 0; i < pieces.length; i++) { var pattern = pieces[i]; pattern = pattern.replace(/(\+|\*|\?|[|]|{|}|\||\\|\(|\)|\.)/g, "\\$1"); var reg = new RegExp(pattern, "i"); if (!reg.test(source)) { return false; } } return true; }, loadJS: function (file, callback) { let element = document.createElement("script") element.setAttribute("type", "text/javascript") element.setAttribute("src", file) if (typeof callback == "function") { element.addEventListener("load", callback) } document.head.append(element) }, loadCSS: function (file, callback) { let element = document.createElement("link") element.setAttribute("rel", "stylesheet") element.setAttribute("type", "text/css") element.setAttribute("href", file) if (typeof callback == "function") { element.addEventListener("load", callback) } document.head.append(element) }, datepicker: function (element, callback, bottomLeft) { // 加载 if (typeof Pikaday == "undefined") { let that = this this.loadJS("/js/moment.min.js", function () { that.loadJS("/js/pikaday.js", function () { that.datepicker(element, callback, bottomLeft) }) }) this.loadCSS("/js/pikaday.css") this.loadCSS("/js/pikaday.theme.css") this.loadCSS("/js/pikaday.triangle.css") return } if (typeof (element) == "string") { element = document.getElementById(element); } let year = new Date().getFullYear(); let picker = new Pikaday({ field: element, firstDay: 1, minDate: new Date(year - 1, 0, 1), maxDate: new Date(year + 10, 11, 31), yearRange: [year - 1, year + 10], format: "YYYY-MM-DD", i18n: { previousMonth: '上月', nextMonth: '下月', months: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'], weekdays: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'], weekdaysShort: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'] }, theme: 'triangle-theme', onSelect: function () { if (typeof (callback) == "function") { callback.call(Tea.Vue, picker.toString()); } }, reposition: !bottomLeft }) }, formatBytes: function (bytes) { bytes = Math.ceil(bytes); if (bytes < Math.pow(1024, 1)) { return bytes + "B"; } if (bytes < Math.pow(1024, 2)) { return (Math.round(bytes * 100 / Math.pow(1024, 1)) / 100) + "KB"; } if (bytes < Math.pow(1024, 3)) { return (Math.round(bytes * 100 / Math.pow(1024, 2)) / 100) + "MB"; } if (bytes < Math.pow(1024, 4)) { return (Math.round(bytes * 100 / Math.pow(1024, 3)) / 100) + "GB"; } if (bytes < Math.pow(1024, 5)) { return (Math.round(bytes * 100 / Math.pow(1024, 4)) / 100) + "TB"; } if (bytes < Math.pow(1024, 6)) { return (Math.round(bytes * 100 / Math.pow(1024, 5)) / 100) + "PB"; } return (Math.round(bytes * 100 / Math.pow(1024, 6)) / 100) + "EB"; }, formatNumber: function (x) { if (x == null) { return "null" } let s = x.toString() let dotIndex = s.indexOf(".") if (dotIndex >= 0) { return this.formatNumber(s.substring(0, dotIndex)) + "." + s.substring(dotIndex + 1) } if (s.length <= 3) { return s; } let result = [] for (let i = 0; i < Math.floor(s.length / 3); i++) { let start = s.length - (i + 1) * 3 result.push(s.substring(start, start + 3)) } if (s.length % 3 != 0) { result.push(s.substring(0, s.length % 3)) } return result.reverse().join(", ") }, formatCount: function (x) { let unit = "" let divider = "" if (x >= 1000 * 1000 * 1000) { unit = "B" divider = 1000 * 1000 * 1000 } else if (x >= 1000 * 1000) { unit = "M" divider = 1000 * 1000 } else if (x >= 1000) { unit = "K" divider = 1000 } if (unit.length == 0) { return x.toString() } return (Math.round(x * 100 / divider) / 100) + unit }, bytesAxis: function (stats, countFunc) { let max = Math.max.apply(this, stats.map(countFunc)) let divider = 1 let unit = "" if (max >= Math.pow(1024, 6)) { unit = "E" divider = Math.pow(1024, 6) } else if (max >= Math.pow(1024, 5)) { unit = "P" divider = Math.pow(1024, 5) } else if (max >= Math.pow(1024, 4)) { unit = "T" divider = Math.pow(1024, 4) } else if (max >= Math.pow(1024, 3)) { unit = "G" divider = Math.pow(1024, 3) } else if (max >= Math.pow(1024, 2)) { unit = "M" divider = Math.pow(1024, 2) } else if (max >= Math.pow(1024, 1)) { unit = "K" divider = Math.pow(1024, 1) } return { unit: unit, divider: divider } }, countAxis: function (stats, countFunc) { let max = Math.max.apply(this, stats.map(countFunc)) let divider = 1 let unit = "" if (max >= 1000 * 1000 * 1000) { unit = "B" divider = 1000 * 1000 * 1000 } else if (max >= 1000 * 1000) { unit = "M" divider = 1000 * 1000 } else if (max >= 1000) { unit = "K" divider = 1000 } return { unit: unit, divider: divider, max: max } }, popup: function (url, options) { if (url != null && url.length > 0 && url.substring(0, 1) == '.') { url = Tea.url(url) } if (options == null) { options = {}; } var width = "40em"; var height = "20em"; window.POPUP_CALLBACK = function () { Swal.close(); }; if (options["width"] != null) { width = options["width"]; } if (options["height"] != null) { height = options["height"]; } if (typeof (options["callback"]) == "function") { window.POPUP_CALLBACK = function () { Swal.close(); options["callback"].apply(Tea.Vue, arguments); }; } Swal.fire({ html: '', width: width, padding: "0.5em", showConfirmButton: false, showCloseButton: true, focusConfirm: false, onClose: function (popup) { if (typeof (options["onClose"]) == "function") { options["onClose"].apply(Tea.Vue, arguments) } } }); }, popupSuccess: function (url, width, height) { let options = {} if (width != null) { options["width"] = width } if (height != null) { options["height"] = height } options["callback"] = function () { teaweb.success("保存成功", function () { teaweb.reload() }) } this.popup(url, options) }, popupFinish: function () { if (window.POPUP_CALLBACK != null) { window.POPUP_CALLBACK.apply(window, arguments); } }, popupTip: function (html) { Swal.fire({ html: '