/** * common.js * @param {Object} val */ var Prodog = { version: "3.4", // 动态 setting: { // 是否显示注册按钮 allowRegister: false } }; Array.prototype.myIndexOf = function (val) { for (var i = 0; i < this.length; i++) { if (this[i] == val) return i; } return -1; }; Array.prototype.myIndexOf2 = function (val) { for (var i = 0; i < this.length; i++) { if (this[i].name == val.name) return i; } return -1; }; Array.prototype.myIndexOf3 = function (val) { for (var i = 0; i < this.length; i++) { if (JSON.stringify(this[i]) == JSON.stringify(val)) return i; } return -1; }; Array.prototype.indexOfProperty = function (val, propertyName) { for (var i = 0; i < this.length; i++) { if (this[i][propertyName] == val) return i; } return -1; }; Array.prototype.remove = function (val) { var index = this.myIndexOf(val); if (index > -1) { this.splice(index, 1); } }; String.prototype.startsWith = function (str) { var reg = new RegExp("^" + str); return reg.test(this); } String.prototype.endWith = function (str) { var reg = new RegExp(str + "$"); return reg.test(this); } Date.prototype.format = function (fmt) { var 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 (var 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; } // 给请求头中加上account和token信息 function addTokenToRequest(request) { // var token = Cookies.get('token'); // var account = Cookies.get('account'); // 修改在Android上Cookies无效 var token = localStorage.getItem("token"); var account = localStorage.getItem("account"); console.log("token" + token); request.setRequestHeader("account", account); request.setRequestHeader("token", token); } // 时间格式转换 function dateFormat(date) { var year = date.getFullYear(); var m = date.getMonth() + 1; var d = date.getDate(); if (m < 10) { m = "0" + m; } if (d < 10) { d = "0" + d; } return year + "-" + m + "-" + d; } /** * 获取ECharts饼图数据 * @param {Object} data */ function getEChartsPieOption(data, title) { var option = { title: { text: title, x: 'center', }, legend: { orient: 'horizontal', x: 'left', data: data, top: '20' }, series: [{ name: '资产统计', type: 'pie', radius: '45%', center: ['50%', '60%'], data: data, itemStyle: { emphasis: { shadowBlur: 10, shadowOffsetX: 0, shadowColor: 'rgba(0, 0, 0, 0.5)' }, normal: { label: { show: true, textStyle: { fontSize: '0.2em', fontWeight: 'bolder' }, formatter: '{b}({c}) ' }, labelLine: { show: true, length: 12 } } } }] }; return option; } /** * 获取ECharts柱状图数据 * @param {Object} data */ function getEChartsBarOption(data, title) { var xData = new Array(); var yData = new Array(); data.forEach(function (item, index, arr) { xData.push(item.name); yData.push(item.value); }); var option = { tooltip: { trigger: 'axis', axisPointer: { // 坐标轴指示器,坐标轴触发有效 type: 'shadow' // 默认为直线,可选为:'line' | 'shadow' } }, title: { text: title, x: 'center', }, legend: { x: 'left', top: '20', data: ['数量'], }, grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true }, xAxis: [{ type: 'value' }], yAxis: [{ type: 'category', axisTick: { show: false }, data: xData }], series: [{ name: '数量', type: 'bar', label: { normal: { show: true, position: 'inside' } }, data: yData }] }; return option; } // 未找到图片 function imgNotFound(event) { var img = event; console.log("imgNotFound。。。"); img.src = "../../img/null.jpg"; img.onerror = null; } var Common = {}; /** * 从URL里面获取UUID */ Common.getUuidFromUrl = function (param) { var reg = new RegExp("(^|&)" + param + "=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if (r != null) { return unescape(r[2]); } else { return null; } } /** * 从URL里面获取UUID */ Common.getParamFromUrl = function (param) { var reg = new RegExp("(^|&)" + param + "=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if (r != null) { return unescape(r[2]); } else { return null; } } /** * 获取主机地址 */ Common.getRootPath = function () { //获取当前网址,如: http://localhost:8083/myproj/view/my.jsp var curWwwPath = window.document.location.href; //获取主机地址之后的目录,如: myproj/view/my.jsp var pathName = window.document.location.pathname; var pos = curWwwPath.indexOf(pathName); //获取主机地址,如: http://localhost:8083 var localhostPaht = curWwwPath.substring(0, pos); return localhostPaht; } /** * 获取IP地址 */ Common.getIp = function () { return localStorage.getItem("ipServer"); } // 文件服务器 Common.getFileServerUrl = function (api) { return this.getRootPath() + api; }, /** * 获取API的地址 * @param {Object} api */ Common.getApi = function (api) { // return 'http://192.168.1.107:10026' + api; return localStorage.getItem("ipServer") + api; } Common.getUrlApi = function (api) { return Common.getIp() + api; } Common.getApiURL = function (api) { if (api.indexOf("/") == 0) { return Common.getIp() + "/api" + api; } else { return Common.getIp() + "/api/" + api } } /** * 获取URL中的参数 * 适应以下两种模式,来获取url参数值: * /User/vip_card_manager/useless/219/id/18 * /User/vip_card_manager?useless=219&id=18 * @param {Object} name */ Common.getQueryString = function (name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); var reg_rewrite = new RegExp("(^|/)" + name + "/([^/]*)(/|$)", "i"); var r = window.location.search.substr(1).match(reg); var q = window.location.pathname.substr(1).match(reg_rewrite); if (r != null) { return unescape(r[2]); } else if (q != null) { return unescape(q[2]); } else { return null; } } // 获取图片相对路径 Common.getimgUrl = function (imageUrl) { return imageUrl; } // 获取图片路径 Common.getImageSrc = function (className, imageName) { var accountId = localStorage.getItem("account"); //return "http://prodog.leanwo.com/Files/Images/" + className + "/" + imageName; return Common.getRootPath() + "/Files/" + accountId + "/Images/" + className + "/thumbnail/" + imageName; //本地测试用 //return Common.getRootPath() + "/Files/Images/" + className + "/" + imageName; //本地测试用 } // 获取大图片路径 Common.getBigImageSrc = function (className, imageName) { var accountId = localStorage.getItem("account"); //return "http://prodog.leanwo.com/Files/Images/" + className + "/" + imageName; //return Common.getRootPath() + "/Files/Images/" + className + "/thumbnail/" + imageName; //本地测试用 return Common.getRootPath() + "/Files/" + accountId + "/Images/" + className + "/" + imageName; //本地测试用 } Common.addTokenToRequest = addTokenToRequest; // 异常处理 Common.processException = function (XMLHttpRequest) { var _self = this; console.log(XMLHttpRequest); if (XMLHttpRequest.status == 400) { // 400 Bad Request mui.toast("400:" + XMLHttpRequest.responseText); } else if (XMLHttpRequest.status == 401) { var currentUrl = window.location; // 系统未登录 if (window.location.href.indexOf('redirectUrl=') < 0) { window.location = Common.getRedirectUrl("index.html?redirectUrl=" + currentUrl); } } else if (XMLHttpRequest.status == 500) { // 500 Internal Server Error mui.toast("500:" + XMLHttpRequest.responseText); if (XMLHttpRequest.responseText.indexOf("登录超时") > 0) { // 如果异常信息包含“登录超时”,则2秒后跳转到登录页面 setTimeout(function () { window.location = Common.getRedirectUrl("index.html"); }, 2 * 1000); } } else { mui.toast("服务器异常:" + XMLHttpRequest.responseText); } } // 异常处理 Common.processExceptionVant = function (XMLHttpRequest) { var _self = this; console.log(XMLHttpRequest); if (XMLHttpRequest.status == 400) { vant.Dialog.alert({ title: 400, message: XMLHttpRequest.responseText, theme: 'round-button', }); } else if (XMLHttpRequest.status == 401) { var currentUrl = window.location; // 系统未登录 if (window.location.href.indexOf('redirectUrl=') < 0) { window.location = Common.getRedirectUrl("index.html?redirectUrl=" + currentUrl); } } else if (XMLHttpRequest.status == 500) { // 500 Internal Server Error vant.Dialog.alert({ title: 500, message: XMLHttpRequest.responseText, theme: 'round-button', }); if (XMLHttpRequest.responseText.indexOf("登录超时") > 0) { // 如果异常信息包含“登录超时”,则2秒后跳转到登录页面 setTimeout(function () { window.location = Common.getRedirectUrl("index.html"); }, 2 * 1000); } } else { vant.Dialog.alert({ title: "服务器异常:", message: XMLHttpRequest.responseText, theme: 'round-button', }); } } /** * 获取跳转的路径 * @param {*} url */ Common.getRedirectUrl = function (url) { const href = window.location.href; if (href.indexOf('wmsapp') >= 0 || href.indexOf('static-eam-app') >= 0) { return this.getRootPath() + '/wmsapp/' + url; } else { return this.getRootPath() + url; } } var IS_TESTING_CONNECTION = false; // 测试网络连接 function testConnection() { if (IS_TESTING_CONNECTION == true) { return; } IS_TESTING_CONNECTION = true; $.ajax({ type: "get", url: Common.getApi("/authApi/LoginResource/heartBeat"), data: { currentTime: new Date().getTime() }, success: function (data) { IS_TESTING_CONNECTION = false; var delay = new Date().getTime() - data; $("[name=testConnection]").remove(); $("body").append("
" ) }, error: function () { IS_TESTING_CONNECTION = false; $("[name=testConnection]").remove(); $("body").append("
" ) } }); } // 测试网络是否连接正常 // (function () { // testConnection(); // setInterval(testConnection, 5000); // })(); function showDelay(delay) { if (delay < 0) { mui.toast("网络连接失败"); } else { mui.toast("当前延时: " + delay + "ms"); } } // ajax的cache默认值为true:使用缓存,这个时候读取的数据是缓存中的数据而不是最新的数据。 // eg:界面返回的时候,ajax使用了缓存中的数据,并没有向服务器发起请求。造成界面数据不刷新。 // 设置Ajax请求的默认值,不允许缓存 $.ajaxSetup({ cache: false });