UNPKG

fastchar-appjs

Version:

快速搭建VUE项目工具类的基本库,主要用于每个功能页面独立生成html,不使用vue单页面功能。

1,188 lines (1,187 loc) 45.9 kB
define(["require", "exports", "tslib"], function (require, exports, tslib_1) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FastHelper = void 0; /** * FastHelper 常用的工具类 * @author Janesen */ var FastHelper; (function (FastHelper) { /** * base64相关 */ var Base64 = /** @class */ (function () { function Base64() { } /** * 转码base64 * @param content */ Base64.encode = function (content) { var Base64 = require("js-base64"); return Base64.encode(content); }; /** * 解码base64 * @param content */ Base64.decode = function (content) { var Base64 = require("js-base64"); return Base64.decode(content); }; /** * 将base64转为file对象 * @param content base64内容 * @param fileName 文件名 */ Base64.base64ToFile = function (content, fileName) { var arr = content.split(','), mime = arr[0].match(/:(.*?);/)[1], bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n); while (n--) { u8arr[n] = bstr.charCodeAt(n); } var blob = new Blob([u8arr], { type: mime }); blob.lastModifiedDate = new Date(); blob.name = fileName; return blob; }; /** * 将file对象转为base64 * @param file */ Base64.fileToBase64 = function (file) { return tslib_1.__awaiter(this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { return [2 /*return*/, new Promise(function (resolved, rejected) { var fileReader = new FileReader(); fileReader.onload = function (readEvent) { var base64 = readEvent.target.result; resolved(base64); }; fileReader.readAsDataURL(file); })]; }); }); }; return Base64; }()); FastHelper.Base64 = Base64; /** * 对象操作类 */ var Objects = /** @class */ (function () { function Objects() { } /** * 判断目标是否为对象类型 * @param source */ Objects.isObject = function (source) { if ((toString.call(null) === '[object Object]')) { return source !== null && source !== undefined && toString.call(source) === '[object Object]' && source.ownerDocument === undefined; } return toString.call(source) === '[object Object]'; }; /** * 判断是否为空,包含undefined * @param source 目标值 */ Objects.isEmpty = function (source) { if (source === undefined) { return true; } if (source === null) { return true; } if (source.toString() === "") { return true; } return source.toString().length === 0; }; /** * 判断目标是否没有任何属性值,如果为空数组,则数组有实体属性length * @param source */ Objects.isEmptyProperty = function (source) { if (this.isEmpty(source)) { return true; } return Object.getOwnPropertyNames(source).length === 0; }; /** * 判断目标是否没有任何key * @param source */ Objects.isEmptyKey = function (source) { if (this.isEmpty(source)) { return true; } return Object.keys(source).length === 0; }; /** * 判断两个对象是否相同,深度判断属性中的值是否相同 * 如果比较的属性类型都为函数类型,则跳过比较 * @param first * @param second */ Objects.isSame = function (first, second) { if (!this.isObject(first)) { return false; } if (!this.isObject(second)) { return false; } var firstProps = Object.getOwnPropertyNames(first); var secondProps = Object.getOwnPropertyNames(second); if (firstProps.length !== secondProps.length) { return false; } for (var i = 0; i < firstProps.length; i++) { var propName = firstProps[i]; var propFist = first[propName]; var propSecond = second[propName]; if (FastHelper.Functions.isFunction(propFist) && FastHelper.Functions.isFunction(propSecond)) { continue; } if (this.isObject(propFist)) { if (!this.isSame(propFist, propSecond)) { return false; } } else if (FastHelper.Arrays.isArray(propFist)) { if (!FastHelper.Arrays.isSame(propFist, propSecond)) { return false; } } else if (propFist !== propSecond) { return false; } } return true; }; return Objects; }()); FastHelper.Objects = Objects; /** * 数组相关 */ var Arrays = /** @class */ (function () { function Arrays() { } /** * 判断目标是否为数组类型 * @param source */ Arrays.isArray = function (source) { if (source == null) { return false; } if (source instanceof Array) { return true; } return toString.call(source) == '[object Array]'; }; /** * 合并数组,返回一个新的数组 * @param items */ Arrays.merge = function () { var items = []; for (var _i = 0; _i < arguments.length; _i++) { items[_i] = arguments[_i]; } var newArray = []; items.forEach(function (item) { for (var i = 0; i < item.length; i++) { newArray.push(item[i]); } }); return newArray; }; /** * 批量追加 * @param source 原数组 * @param items 添加的选项,如果选项类型为数组,则合并到数组中 */ Arrays.append = function (source) { var items = []; for (var _i = 1; _i < arguments.length; _i++) { items[_i - 1] = arguments[_i]; } items.forEach(function (item) { if (item instanceof Array) { for (var i = 0; i < item.length; i++) { source.push(item[i]); } } else { source.push(item); } }); }; /** * 清空数组 * @param source */ Arrays.clear = function (source) { source.splice(0, source.length); }; /** * 判断是否存在于数组中 * @param source 数组 * @param value 值 */ Arrays.exists = function (source, value) { for (var i = 0; i < source.length; i++) { if (source[i] === value) { return true; } } return false; }; /** * 获取值在数组的下标 * @param source 数组 * @param value 值 */ Arrays.indexOf = function (source, value) { for (var i = 0; i < source.length; i++) { if (source[i] === value) { return i; } } return -1; }; /** * 将值插入指定的位置 * @param source 数组 * @param index 位置 * @param value 值 */ Arrays.insert = function (source, index, value) { source.splice(index, 0, value); }; /** * 将值替换到指定下标 * @param source 数组 * @param index 位置 * @param value 值 */ Arrays.replace = function (source, index, value) { source.splice(index, 1, value); }; /** * 删除指定位置的数据 * @param source 数组 * @param index 下标 */ Arrays.remove = function (source, index) { source.splice(index, 1); }; /** * 删除指定值 * @param source 数组 * @param value 值 */ Arrays.removeItem = function (source, value) { this.remove(source, this.indexOf(source, value)); }; /** * 判断两个数组是否相同 * 如果比较的值类型都为函数类型,则跳过比较 * @param first * @param second */ Arrays.isSame = function (first, second) { if (!this.isArray(first)) { return false; } if (!this.isArray(second)) { return false; } if (first.length !== second.length) { return false; } for (var i = 0; i < first.length; i++) { var firstValue = first[i]; var secondValue = second[i]; if (FastHelper.Functions.isFunction(firstValue) && FastHelper.Functions.isFunction(secondValue)) { continue; } if (FastHelper.Objects.isObject(firstValue)) { if (!FastHelper.Objects.isSame(firstValue, secondValue)) { return false; } } else if (this.isArray(firstValue)) { if (!this.isSame(firstValue, secondValue)) { return false; } } else if (firstValue !== secondValue) { return false; } } return true; }; return Arrays; }()); FastHelper.Arrays = Arrays; /** * 字符串相关 */ var Strings = /** @class */ (function () { function Strings() { } /** * 构建唯一标识符 * @param prefix 前缀 */ Strings.buildOnlyCode = function (prefix) { var md5 = require('md5'); var uuidv4 = require('uuid').v4; return prefix + md5(uuidv4()); }; /** * 判断目标值是否为字符串 * @param source */ Strings.isString = function (source) { if (!source) { return false; } if (source instanceof String) { return true; } return typeof source === 'string'; }; /** * 判断是否为空,包含undefined * @param source 目标值 */ Strings.isEmpty = function (source) { return FastHelper.Objects.isEmpty(source); }; /** * 判断是否不为空 * @param source 目标值 */ Strings.isNotEmpty = function (source) { return !this.isEmpty(source); }; /** * 获取字符串内容,如果为null或undefined则返回defaultValue * @param source 目标值 * @param defaultValue 默认值 */ Strings.defaultValue = function (source, defaultValue) { if (FastHelper.Strings.isEmpty(source)) { return defaultValue; } return source; }; /** * 判断字符串是否以某个字符开头 * @param source 字符串 * @param prefix 字符 */ Strings.startWith = function (source, prefix) { if (!prefix || prefix === "" || FastHelper.Strings.isEmpty(source) || prefix.length > source.length) return false; return source.substr(0, prefix.length) === prefix; }; /** * 判断字符串是否以某个字符结尾 * @param source 字符串 * @param suffix 字符 */ Strings.endWith = function (source, suffix) { if (!suffix || suffix === "" || FastHelper.Strings.isEmpty(source) || suffix.length > source.length) return false; return source.substring(source.length - suffix.length) === suffix; }; /** * 首字母大写 * @param source 字符串 */ Strings.firstUpperCase = function (source) { return source.replace(/^\S/, function (s) { return s.toUpperCase(); }); }; /** * 替换字符 * @param source 字符串 * @param oldStr 老的字符 * @param newStr 新的字符 */ Strings.replaceAll = function (source, oldStr, newStr) { return source.replace(new RegExp(oldStr, 'g'), newStr); }; /** * 数字补0 * @param source * @param length */ Strings.prefixInteger = function (source, length) { return (Array(length).join('0') + source).slice(-length); }; return Strings; }()); FastHelper.Strings = Strings; /** * 布尔值相关操作 */ var Booleans = /** @class */ (function () { function Booleans() { } /** * 判断目标值是否为布尔值 * @param source */ Booleans.isBoolean = function (source) { if (source == null) { return false; } if (source instanceof Boolean) { return true; } return typeof source === 'boolean'; }; /** * 转为布尔值 * @param source 目标值 * @param defaultValue 默认值,当为空或无效的布尔值时 返回 */ Booleans.parse = function (source, defaultValue) { if (FastHelper.Strings.isEmpty(defaultValue)) { defaultValue = false; } if (FastHelper.Strings.isEmpty(source)) { return defaultValue; } if (FastHelper.Strings.isString(source)) { if (source === "0" || source.toLowerCase() === "false") { return false; } if (source === "1" || source.toLowerCase() === "true") { return true; } return defaultValue; } if (FastHelper.Numbers.isNumber(source)) { if (source === 0) { return false; } if (source === 1) { return true; } return defaultValue; } if (this.isBoolean(source)) { return source; } return defaultValue; }; return Booleans; }()); FastHelper.Booleans = Booleans; /** * 数字相关 */ var Numbers = /** @class */ (function () { function Numbers() { } /** * 判断目标值是否为布尔值 * @param source */ Numbers.isNumber = function (source) { if (source == null) { return false; } if (source instanceof Number) { return true; } return typeof source === 'number' && isFinite(source); }; /** * 转为纯数字 * @param source * @param defaultValue */ Numbers.parse = function (source, defaultValue) { if (FastHelper.Strings.isEmpty(source)) { if (!defaultValue) { defaultValue = 0; } return defaultValue; } var value = parseFloat(source); if (isNaN(value)) { if (!defaultValue) { defaultValue = 0; } return defaultValue; } return value; }; /** * 提取纯数字[0-9],不包含小数点 * @param source 目标值 */ Numbers.getNumberValue = function (source) { if (FastHelper.Strings.isEmpty(source)) { return 0; } return parseFloat(source.toString().replace(/[^0-9]/ig, "")); }; return Numbers; }()); FastHelper.Numbers = Numbers; /** * 日期相关 */ var Dates = /** @class */ (function () { function Dates() { } /** * 判断目标值是否是日期类型 * @param source */ Dates.isDate = function (source) { if (source == null) { return false; } if (source instanceof Date) { return true; } return toString.call(source) === '[object Date]'; }; /** * 格式化日期 * @param source 日期 * @param pattern 格式类型,例如:yyyy-MM-DD HH:mm:ss,更多查看:https://momentjs.com/docs/#/displaying/ */ Dates.format = function (source, pattern) { if (FastHelper.Strings.isEmpty(pattern)) { return null; } if (FastHelper.Strings.isEmpty(source)) { return null; } var moment = require('moment'); return moment(source).format(pattern); }; /** * 将字符串日期转换为Date对象 * @param source 日期值 */ Dates.parse = function (source) { if (FastHelper.Strings.isEmpty(source)) { return null; } var moment = require('moment'); return moment(source).toDate(); }; /** * 格式化日期的友好展示,例如:1分钟前,3分钟前 * @param source * @param level 精确级别 1 分钟 2 小时 3 天 4 周 5 月 */ Dates.formatNice = function (source, level) { if (FastHelper.Strings.isEmpty(source)) { return null; } if (level === undefined) { level = 2; } var seconds = 1000; var minute = seconds * 60; var hour = minute * 60; var day = hour * 24; var week = day * 7; var month = day * 30; var time1 = new Date().getTime(); //当前的时间戳 var sourceDate = this.parse(source); if (!sourceDate) { return null; } var time2 = sourceDate.getTime(); var time = time1 - time2; if (time <= minute) { return "刚刚"; } if (time / month >= 3) { return this.format(sourceDate, "yyyy-MM-DD HH:mm"); } var nice = true; if (time / month >= 1) { if (level >= 5) { return parseInt(String(time / month)) + "个月前"; } nice = false; } if (nice && time / week >= 1) { if (level >= 4) { return parseInt(String(time / week)) + "周前"; } nice = false; } if (nice && time / day >= 1) { if (level >= 3) { return parseInt(String(time / day)) + "天前"; } nice = false; } if (nice && time / hour >= 1) { if (level >= 2) { return parseInt(String(time / hour)) + "小时前"; } nice = false; } if (nice && time / minute >= 1) { if (level >= 1) { return parseInt(String(time / minute)) + "分钟前"; } } return this.format(sourceDate, "yyyy-MM-DD HH:mm"); }; /** * 将总时间转换为中文描述,最高描述到:天,例如:2天03时45分09秒 * @param timestamp 时间戳 单位:毫秒 * @param chinese 是否用中文表达,默认:true */ Dates.toDescription = function (timestamp, chinese) { if (chinese === undefined) { chinese = true; } var seconds = 1000, minuteUnit = 1000 * 60, hourUnit = 1000 * 60 * 60, dayUnit = 1000 * 60 * 60 * 24; var descriptions = []; if (timestamp < seconds) { descriptions.push("00" + (chinese ? '秒' : '')); return descriptions.join(""); } else if (timestamp < minuteUnit) { descriptions.push(FastHelper.Strings.prefixInteger(parseInt(String(timestamp / seconds)), 2) + (chinese ? '秒' : '')); return descriptions.join(""); } if (timestamp < hourUnit) { var minute = parseInt(String(timestamp / minuteUnit)); descriptions.push(FastHelper.Strings.prefixInteger(minute, 2) + (chinese ? '分' : ':')); var nextTimestamp_1 = timestamp - parseInt(String(minute * minuteUnit)); if (nextTimestamp_1 === 0) { descriptions.push("00" + (chinese ? '秒' : '')); } else { descriptions.push(FastHelper.Dates.toDescription(nextTimestamp_1, chinese)); } return descriptions.join(""); } if (timestamp < dayUnit) { var hour = parseInt(String(timestamp / hourUnit)); descriptions.push(FastHelper.Strings.prefixInteger(hour, 2) + (chinese ? '时' : ':')); var nextTimestamp_2 = timestamp - parseInt(String(hour * hourUnit)); if (nextTimestamp_2 === 0) { descriptions.push("00" + (chinese ? '分' : ':')); descriptions.push("00" + (chinese ? '秒' : '')); } else { descriptions.push(FastHelper.Dates.toDescription(nextTimestamp_2, chinese)); } return descriptions.join(""); } var day = parseInt(String(timestamp / dayUnit)); descriptions.push(day + (chinese ? '天' : 'day ')); var nextTimestamp = timestamp - parseInt(String(day * dayUnit)); if (nextTimestamp === 0) { descriptions.push("00" + (chinese ? '时' : ':')); descriptions.push("00" + (chinese ? '分' : ':')); descriptions.push("00" + (chinese ? '秒' : '')); } else { descriptions.push(FastHelper.Dates.toDescription(nextTimestamp, chinese)); } return descriptions.join(""); }; return Dates; }()); FastHelper.Dates = Dates; /** * 函数相关操作 */ var Functions = /** @class */ (function () { function Functions() { } /** * 判断目标类型是否为函数 * @param source */ Functions.isFunction = function (source) { if (source == null) { return false; } return !!source && typeof source === 'function'; }; /** * 批量执行函数并获取返回值 * @param source 单个函数或函数数组 * @param params 执行函数携带的参数 */ Functions.run = function (source) { var params = []; for (var _i = 1; _i < arguments.length; _i++) { params[_i - 1] = arguments[_i]; } var result = []; if (this.isFunction(source)) { result.push(source.apply(this, params)); } else if (FastHelper.Arrays.isArray(source)) { for (var i = 0; i < source.length; i++) { result.push(source[i].apply(this, params)); } } else if (FastHelper.Strings.isString(source)) { result.push(eval(source)); } if (result.length == 1) { return result[0]; } else if (result.length > 1) { return result; } return null; }; /** * 按顺序同步执行函数,当其中一个函数返回false时,则终止后续执行 * @param source 单个函数或函数数组 * @param params 执行函数携带的参数 */ Functions.syncRun = function (source) { var params = []; for (var _i = 1; _i < arguments.length; _i++) { params[_i - 1] = arguments[_i]; } return tslib_1.__awaiter(this, void 0, void 0, function () { var i, result; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: if (!FastHelper.Arrays.isArray(source)) return [3 /*break*/, 5]; i = 0; _a.label = 1; case 1: if (!(i < source.length)) return [3 /*break*/, 4]; if (!FastHelper.Functions.isFunction(source[i])) return [3 /*break*/, 3]; return [4 /*yield*/, source[i].apply(this, params)]; case 2: result = _a.sent(); if (!FastHelper.Booleans.parse(result, false)) { return [2 /*return*/]; } _a.label = 3; case 3: i++; return [3 /*break*/, 1]; case 4: return [3 /*break*/, 6]; case 5: if (this.isFunction(source)) { source.apply(this, params); } else if (FastHelper.Strings.isString(source)) { eval(source); } _a.label = 6; case 6: return [2 /*return*/]; } }); }); }; return Functions; }()); FastHelper.Functions = Functions; /** * 节点相关 */ var Elements = /** @class */ (function () { function Elements() { } /** * 绑定节点属性变动的监听 * @param source 节点对象 * @param attrs 需要监听的属性集合 * @param callBack 回调 */ Elements.onAttributesChange = function (source, attrs, callBack) { if (source) { var observe = new MutationObserver(function (mutationsList) { mutationsList.forEach(function (item, index) { if (callBack) { callBack(item); } }); }); observe.observe(source, { attributes: true, attributeFilter: attrs }); return observe; } return null; }; /** * 绑定当前节点子节点的变动的监听 * @param source 节点对象 * @param callBack 回调 */ Elements.onChildrenChange = function (source, callBack) { if (source) { var observe = new MutationObserver(function (mutationsList) { mutationsList.forEach(function (item, index) { if (callBack) { callBack(item); } }); }); observe.observe(source, { childList: true, subtree: true }); return observe; } return null; }; /** * 在目标节点后追加新的节点 * @param targetEl 目标节点 * @param newEl 新的节点 */ Elements.insertAfter = function (targetEl, newEl) { var parent = targetEl.parentNode; if (parent.lastChild === targetEl) { parent.appendChild(newEl); } else { parent.insertBefore(newEl, targetEl.nextSibling); } }; /** * 在目标节点前追加新的节点 * @param targetEl 目标节点 * @param newEl 新的节点 */ Elements.insertBefore = function (targetEl, newEl) { var parent = targetEl.parentNode; if (parent.lastChild === targetEl) { parent.appendChild(newEl); } else { parent.insertBefore(newEl, targetEl); } }; /** * 获取目标节点的实际占位空间,包含的内外边距 * @param targetEl */ Elements.getRealSize = function (targetEl) { var childStyle = window.getComputedStyle(targetEl); var height = FastHelper.Numbers.parse(childStyle.height); var width = FastHelper.Numbers.parse(childStyle.width); var vSpace = FastHelper.Numbers.parse(childStyle.marginTop) + FastHelper.Numbers.parse(childStyle.marginBottom) + FastHelper.Numbers.parse(childStyle.paddingTop) + FastHelper.Numbers.parse(childStyle.paddingBottom); var hSpace = FastHelper.Numbers.parse(childStyle.marginLeft) + FastHelper.Numbers.parse(childStyle.marginRight) + FastHelper.Numbers.parse(childStyle.paddingLeft) + FastHelper.Numbers.parse(childStyle.paddingRight); return { width: width + hSpace, height: height + vSpace }; }; /** * 获取目标节点的selector * @param targetEl */ Elements.getPath = function (targetEl) { var currEl = targetEl; var domPath = []; if (currEl.id) { domPath.unshift('#' + currEl.id); } else { while (currEl.nodeName.toLowerCase() !== "html") { if (currEl.id) { domPath.unshift('#' + currEl.id); break; } else if (currEl.tagName.toLocaleLowerCase() === "body") { domPath.unshift(currEl.tagName.toLowerCase()); } else { for (var i = 0; i < currEl.parentNode.childElementCount; i++) { if (currEl.parentNode.children[i] === currEl) { var className = currEl.getAttribute('class'); if (className) { var selectors = currEl.className.split(/\s/g), array = []; for (var j = 0; j < selectors.length; ++j) { if (selectors[j].length > 0) { array.push('.' + selectors[j]); } } className = array.join(""); } else { className = ""; } domPath.unshift(currEl.tagName.toLowerCase() + className + ':nth-child(' + (i + 1) + ')'); } } } currEl = currEl.parentNode; } } return domPath.join(' > '); }; /** * 判断目标元素的滚动条 是否已滚动到底部 * @param targetEl * @param scrollNimble 检测的灵敏度,越大越灵敏 */ Elements.isScrollBottom = function (targetEl, scrollNimble) { var scrollTop = targetEl.scrollTop; var elHeight = targetEl.getBoundingClientRect().height; var scrollHeight = targetEl.scrollHeight; return scrollTop + elHeight >= (scrollHeight - scrollNimble); }; return Elements; }()); FastHelper.Elements = Elements; /** * css样式相关代码 */ var Styles = /** @class */ (function () { function Styles() { } /** * 动态加载css代码 * @param style css代码 */ Styles.loadCssCode = function (style) { var oHead = document.getElementsByTagName('head').item(0); var oStyle = document.createElement("style"); oStyle.type = "text/css"; if (oStyle["styleSheet"]) { oStyle["styleSheet"].cssText = style; } else { oStyle.innerHTML = style; } oHead.appendChild(oStyle); }; return Styles; }()); FastHelper.Styles = Styles; /** * 事件相关工具 */ var Events = /** @class */ (function () { function Events() { } /** * 获取异常信息的堆栈信息 * @param event */ Events.geErrorInfo = function (event) { if (event) { if (event.error && event.error.stack) { return event.error.stack; } else if (event.stack) { return event.stack; } else if (event.message) { return event.message; } else if (event.reason) { return event.reason; } return event.toString(); } return ""; }; return Events; }()); FastHelper.Events = Events; /** * 高德地图操作工具类 */ var AMap = /** @class */ (function () { function AMap() { } /** * 加载高德地图的js文件,具体查看 https://lbs.amap.com/api/jsapi-v2/guide/abc/load * @param options */ AMap.load = function (options) { var AMapLoader = require("fastchar-appjs/src/amap/amap-loader.js"); return AMapLoader.load(options); }; return AMap; }()); FastHelper.AMap = AMap; /** * 窗口window操作对象 */ var Windows = /** @class */ (function () { function Windows() { } /** * 复制文本到剪贴板里 * @param content 内容 */ Windows.copyToBoard = function (content) { var oInput = document.createElement('textarea'); oInput.value = content; document.body.appendChild(oInput); oInput.select(); document.execCommand("Copy"); oInput.style.display = 'none'; oInput.remove(); }; /** * 添加window的onLoad事件,如果存在onLoad函数名则进行合并 * @param callBack */ Windows.addOnLoad = function (callBack) { var oldOnload = window.onload; if (FastHelper.Functions.isFunction(oldOnload)) { window.onload = function () { oldOnload(); callBack(); }; } else { window.onload = callBack; } }; /** * 添加window的函数,如果存在相同的函数名则进行合并 * @param functionName 函数名 * @param functionBody 函数执行的代码 */ Windows.addFunction = function (functionName, functionBody) { var targetFunction = window[functionName]; if (FastHelper.Functions.isFunction(targetFunction)) { window[functionName] = function () { targetFunction.apply(this, arguments); functionBody.apply(this, arguments); }; } else { window[functionName] = functionBody; } }; /** * 安全的获取窗口宽度,有的手机渲染后document.body.clientWidth 莫明会返回0 */ Windows.safeGetClientWidth = function () { return Math.max(document.documentElement.clientWidth, document.body.clientWidth); }; /** * 安全的获取窗口高度,有的手机渲染后document.body.clientHeight 莫明会返回0 */ Windows.safeGetClientHeight = function () { return Math.max(document.documentElement.clientHeight, document.body.clientHeight); }; return Windows; }()); FastHelper.Windows = Windows; /** * JSON相关功能 */ var Json = /** @class */ (function () { function Json() { } /** * 将json字符串转成对象 * @param jsonStr json字符串 * @returns {Object} */ Json.jsonToObject = function (jsonStr) { try { return JSON.parse(jsonStr); } catch (e) { } return null; }; /** * 将对象转成json字符串 * @param jsonObj 待转换的对象 * @returns {string} */ Json.objectToJson = function (jsonObj) { try { return JSON.stringify(jsonObj); } catch (e) { } return null; }; /** * 将对象转成json字符串,注意此方法将转换函数对象,慎用! * @param jsonObj 待转换的对象 * @return {string} */ Json.objectToJsonUnsafe = function (jsonObj) { return JSON.stringify(jsonObj, function (key, val) { if (typeof val === 'function') { return val.toString(); } return val; }); }; /** * 将json字符串转成对象 * @param jsonStr json字符串 * @returns {Object} */ Json.jsonToObjectUnsafe = function (jsonStr) { try { return JSON.parse(jsonStr, function (k, v) { if (v.indexOf && v.indexOf('function') > -1) { // noinspection UnnecessaryReturnStatementJS return eval("(function(){return " + v + " })()"); } return v; }); } catch (e) { } return null; }; /** * 合并两个json对象 * @param jsonData1 json对象 * @param jsonData2 json对象 * @return 合并后的新对象 */ Json.mergeJson = function (jsonData1, jsonData2) { var newJsonData = {}; if (!Objects.isEmpty(jsonData1)) { for (var property in jsonData1) { newJsonData[property] = jsonData1[property]; } } if (!Objects.isEmpty(jsonData2)) { for (var property in jsonData2) { newJsonData[property] = jsonData2[property]; } } return newJsonData; }; return Json; }()); FastHelper.Json = Json; })(FastHelper = exports.FastHelper || (exports.FastHelper = {})); });