(function(win) { let Patch = win.Patch = { version: '0.0.1', description: '第三方库文件补丁包,避免库文件升级引起的兼容性问题' }; Patch.msWidget = { description: 'avalon framework widget patch' }; Patch.msWidget.getParams = function(data, vmodels) { let args = data.value.split(','); let optsName = args[2]; for (let i = 0; i < vmodels.length; i++) { if (vmodels[i][optsName] && typeof vmodels[i][optsName] === "object") { return vmodels[i][optsName]; } } } Patch.msWidget.initProcess = function(action, vmodel) { if (action) { action.call(this, vmodel); } } if (!String.prototype.startsWith) { String.prototype.startsWith = function(str) { if (str == null || str == undefined || str.length > this.length) return false; if (str.length == 0) { return true; } return this.substr(0, str.length) == str }; String.prototype.endsWith = function(str) { if (str == null || str == undefined || str.length > this.length) return false; if (str.length == 0) { return true; } return this.substring(this.length - str.length) == str }; if (!String.prototype.hasOwnProperty('trim')) { String.prototype.trim = function() { this.replace(/^\s+|\s+$/g, ''); } } } Date.prototype.format = function(fmt) { //author: meizz let o = { "M+": this.getMonth() + 1, //月份 "d+": this.getDate(), //日 "h+": this.getHours(), //小时 "m+": this.getMinutes(), //分 "s+": this.getSeconds(), //秒 "q+": Math.floor((this.getMonth() + 3) / 3), //季度 "S": this.getMilliseconds() //毫秒 }; if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); for (let k in o) if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); return fmt; } Number.prototype.round = function(precision) { let arg1 = this let m = Math.pow(10, precision); arg1 = Math.round(arg1 * m); return arg1 / m; } win.TypeUtil = { toString: function(obj) { if (!obj && obj !== 0) { return ''; } return obj.toString(); }, toBoolean: function(obj) { if (typeof s === 'string') { return s === '1' || s === 't' || s === 'true' || s === 'T' || s === 'True' || s === 'TRUE'; } return !!s; }, toDate: function(obj) { if (!obj && obj !== 0) { return null; } let value = Date.parse(obj); return isNaN(value) ? null : new Date(value); }, toDateString: function(val) { if (!val) { return '' } if (val instanceof Date) { return val.format('yyyy-MM-dd hh:mm:ss') } return "N/A" }, toInt: function(obj) { if (!obj && obj !== 0) { return 0; } let value = parseInt(obj); return isNaN(value) ? 0 : value; }, toFloat: function(obj) { if (!obj && obj !== 0) { return 0.0; } let value = parseFloat(obj); return isNaN(value) ? 0 : value; }, isNullOrEmpty: function() { return this !== 0 && !this } } win.BigNumber = { /** * 加法函数,用来得到精确的加法结果 * javascript的加法结果会有误差,在两个浮点数相加的时候会比较明显。这个函数返回较为精确的加法结果 * @param {*} arg1 * @param {*} arg2 */ add(arg1, arg2) { var r1, r2, m, c; try { r1 = arg1.toString().split('.')[1].length; } catch (e) { r1 = 0; } try { r2 = arg2.toString().split('.')[1].length; } catch (e) { r2 = 0; } c = Math.abs(r1 - r2); m = Math.pow(10, Math.max(r1, r2)); if (c > 0) { var cm = Math.pow(10, c); if (r1 > r2) { arg1 = Number(arg1.toString().replace('.', '')); arg2 = Number(arg2.toString().replace('.', '')) * cm; } else { arg1 = Number(arg1.toString().replace('.', '')) * cm; arg2 = Number(arg2.toString().replace('.', '')); } } else { arg1 = Number(arg1.toString().replace('.', '')); arg2 = Number(arg2.toString().replace('.', '')); } return (arg1 + arg2) / m; }, /** * 减法函数,用来得到精确的减法结果 * javascript的减法结果会有误差,在两个浮点数相减的时候会比较明显。这个函数返回较为精确的减法结果 * @param {*} arg1 * @param {*} arg2 */ sub(arg1, arg2) { var r1, r2, m, n; try { r1 = arg1.toString().split('.')[1].length; } catch (e) { r1 = 0; } try { r2 = arg2.toString().split('.')[1].length; } catch (e) { r2 = 0; } m = Math.pow(10, Math.max(r1, r2)); // last modify by deeka //动态控制精度长度 n = (r1 >= r2) ? r1 : r2; return ((arg1 * m - arg2 * m) / m).toFixed(n); }, /** * 乘法函数,用来得到精确的乘法结果 * javascript的乘法结果会有误差,在两个浮点数相乘的时候会比较明显。这个函数返回较为精确的乘法结果 * @param {*} arg1 * @param {*} arg2 * @param {Number} precision */ mul(arg1, arg2, precision) { let m = 0; const s1 = arg1.toString(); const s2 = arg2.toString(); try { m += s1.split('.')[1].length; } catch (e) { console.log(e); } try { m += s2.split('.')[1].length; } catch (e) { console.log(e); } const result = Number(s1.replace('.', '')) * Number(s2.replace('.', '')) / Math.pow(10, m); if (!Number.isInteger(precision)) { return result; } return Math.round(Math.pow(10, precision) * result) / Math.pow(10, precision); }, /** * 除法函数,用来得到精确的除法结果 * javascript的除法结果会有误差,在两个浮点数相除的时候会比较明显。这个函数返回较为精确的除法结果 * @param {*} arg1 * @param {*} arg2 * @param {Number} precision */ div(arg1, arg2, precision) { let t1 = 0; let t2 = 0; try { t1 = arg1.toString().split('.')[1].length; } catch (e) { console.log(e); } try { t2 = arg2.toString().split('.')[1].length; } catch (e) { console.log(e); } const r1 = Number(arg1.toString().replace('.', '')); const r2 = Number(arg2.toString().replace('.', '')); const result = (r1 / r2) * Math.pow(10, t2 - t1); if (!Number.isInteger(precision)) { return result; } return Math.round(Math.pow(10, precision) * result) / Math.pow(10, precision); } }; })(window);