import $ from 'jquery'; const CORS= 'http://localhost:8080/sensor/'; // 开启跨域请求(调试时开启) //const CORS=''; // 关闭跨域请求(发布时开启) //定义计时器 function Interval() { this.timer = null; this.time = ''; this.callback = ''; } // 开启计时器并添加 Interval.prototype.start = function(callback, time) { // 先关闭计时器 this.stop(); // 配置执行函数 if(typeof callback == 'function' && typeof time == 'number') { this.callback = callback; this.time = time; callback(); this.timer = setInterval(callback, time); }else { console.warn('未完整配置参数!'); } }; // 开启计时器 Interval.prototype.open = function() { var callback = this.callback; var time = this.time; this.start(callback, time); }; // 关闭计时器 Interval.prototype.stop = function() { clearInterval(this.timer); }; // 延时计时器 function Timeout() { this.timer = null; this.time = ''; this.callback = ''; } // 开启计时器并添加 Timeout.prototype.start = function(callback, time, exe) { // 先关闭计时器 this.stop(); // 配置执行函数 if(typeof callback == 'function' && typeof time == 'number') { this.callback = callback; this.time = time; if(exe != 'exe') { callback(); } this.timer = setTimeout(callback, time); }else { console.warn('未完整配置参数!'); } }; // 开启计时器 Timeout.prototype.open = function() { var callback = this.callback; var time = this.time; this.start(callback, time, 'exe'); }; // 关闭计时器 Timeout.prototype.stop = function() { clearTimeout(this.timer); }; /*从多维数组中获取最大值*/ function getMaxFromArr(arr) { var newArray = arr.join(",").split(","); return Math.max.apply({},newArray); } /*从多维数组中获取最小值*/ function getMinFromArr (arr) { var newArray = arr.join(",").split(","); return Math.min.apply({},newArray); } function ajax(option) { $.ajax({ type: 'post', async: true, url: CORS+option.url, data: option.data, dataType: 'json', success: option.success, error: option.error, complete: option.complete }); } // 格式化时间 Date.prototype.format =function(format) { var o = { "M+" : this.getMonth()+1, //month "d+" : this.getDate(), //day "h+" : this.getHours(), //hour "m+" : this.getMinutes(), //minute "s+" : this.getSeconds(), //second "q+" : Math.floor((this.getMonth()+3)/3), //quarter "S" : this.getMilliseconds() //millisecond }; if(/(y+)/.test(format)) format=format.replace(RegExp.$1, (this.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; }; // 根据对象获取url function getUrlStr(props) { var str = ""; Object.keys(props).forEach(function(key) { str+=props[key]+"/"; }); return str; } // 根据验证规则校验对象的属性值 function checkObjIsGood(obj, verify, callback) { var result = { code: 1, name: '', msg: '', data: obj }; Object.keys(verify).forEach(function(key) { // 上一笔数据是否有效 if(result.code && obj[key] != undefined) { var valueStatus = checkValueIsGood(obj[key], verify[key]); result.code = valueStatus.code; result.name = valueStatus.name; result.msg = valueStatus.msg; } }); // 如果code=1设置msg的值 if(result.code) { result.name = ''; result.msg = '数据匹配成功'; } // 执行callback函数 if(typeof callback == 'function') { callback(result); } } // 检测当前数据是否符合要求 function checkValueIsGood(val, option) { var result = { code: 0, name: option.name, msg: option.msg }; // 校验数据是否符合匹配规则 if(option.pattern.test(val)) { // 数据存在取值范围的校验 if(option.regVal) { if(val>=option.min && val<=option.max) { result.code =1; } }else { result.code =1; } } // 返回结果集 return result; } export{ CORS, // 跨域请求内容 Interval, // 计时器 Timeout, // 延时计时器 getMaxFromArr, // 获取数组中的最大值 getMinFromArr, // 获取数组中的最小值 ajax, // 请求 checkObjIsGood, // 验证对象数据 getUrlStr // 根据对象获取url };