var Notify = require("./Notify.js"); module.exports = { pageSize: 20, // 异常处理 processException: function (XMLHttpRequest, textStatus, errorThrown) { var _self = this; console.log(XMLHttpRequest); if (XMLHttpRequest.status == 400) { // 400 Bad Request Notify.error("400", XMLHttpRequest.responseText, true); } else if (XMLHttpRequest.status == 401) { var currentUrl = window.location; var href = window.location.href; // 当前未处于登陆的界面 // 系统未登录 if (href.indexOf('login') < 0 && href.indexOf('redirectUrl=') < 0) { // 处理钉钉免登陆 const clientId = this.getRouteParam("clientId"); const appName = this.getRouteParam("appName"); const corpId = this.getRouteParam("corpId"); let newUrl; if (clientId != null && clientId.length > 0 && appName != null && appName.length > 0 && corpId != null && corpId.length > 0) { newUrl = _self.getRedirectUrl("#/login?clientId=" + clientId + "&appName=" + appName + "&corpId=" + corpId + "&redirectUrl=" + encodeURIComponent(currentUrl)) } else { newUrl = _self.getRedirectUrl("#/login?redirectUrl=" + encodeURIComponent(currentUrl)); } window.location = newUrl; } } else if (XMLHttpRequest.status == 500) { // 500 Internal Server Error Notify.error("500", XMLHttpRequest.responseText, true); if (XMLHttpRequest.responseText.indexOf("登录超时") > 0) { // 如果异常信息包含“登录超时”,则2秒后跳转到登录页面 setTimeout(function () { window.location = _self.getRedirectUrl("#/login"); }, 2 * 1000); } } else { Notify.error("服务器异常", XMLHttpRequest.responseText, true); } }, /** * 获取主机地址 */ getRootPath: function () { var protocol = window.location.protocol; //console.log("protocol:" + protocol); var host = window.location.host; //console.log("host:" + host); var localhostPaht = protocol + "//" + host; //console.log("localhostPaht:" + localhostPaht); return localhostPaht; }, getHostPageBaseURL: function () { return this.getRootPath() + "/" }, // 获取图片路径url getFileServerUrl: function () { return this.getRootPath() + "/" }, // 获取API的地址 getApiURL: function (apiName) { return this.getHostPageBaseURL() + "api/" + apiName; }, /** * 获取API的地址 * @param {*} apiName * @returns */ getApiUrl2: function (apiName) { if (apiName === undefined || apiName === null || apiName.length === 0) { return this.getRootPath(); } if (apiName[0] === '/') { return this.getRootPath() + apiName; } else { return this.getRootPath() + "/" + apiName; } }, // 获取测试API的地址 getTestApiURL: function (apiName) { return "http://xxx/" + "api/" + apiName; }, // 获取图片路径 getImageUrl: function (imageName) { if (imageName == null || imageName == '') { return this.getFileServerUrl() + 'notFound.png'; } else { return this.getFileServerUrl() + imageName; } }, // 获取图片路径 getImageSrc: function (className, imageName) { var accountId = localStorage.getItem("account"); if (imageName == null) { return null; } if (imageName != null && imageName[0] == '/') { return this.getFileServerUrl() + "Files/" + accountId + "/Images/" + className + imageName; } else { return this.getFileServerUrl() + "Files/" + accountId + "/Images/" + className + "/" + imageName; } }, // 获取略缩图图片路径 getThumbnailImageSrc: function (className, imageName) { var accountId = localStorage.getItem("account"); if (imageName == null) { return null; } if (imageName != null && imageName[0] == '/') { return this.getFileServerUrl() + "Files/" + accountId + "/Images/" + className + "/thumbnail" + imageName; } else { return this.getFileServerUrl() + "Files/" + accountId + "/Images/" + className + "/thumbnail/" + imageName; } }, /** * 获取附件的路径 * @param {[type]} className [description] * @param {[type]} imageName [description] * @return {[type]} [description] */ getAttachmentsSrc: function (className, imageName) { var accountId = localStorage.getItem("account"); return this.getFileServerUrl() + "Files/" + accountId + "/Attachments/" + className + "/" + imageName; }, // 获取图片路径 getVideoSrc: function (className, imageName) { var accountId = localStorage.getItem("account"); if (imageName == undefined || imageName == "") { return this.getHostPageBaseURL() + "static/image/noImage.jpg"; } return this.getFileServerUrl() + "Files/" + accountId + "/Video/" + className + "/" + imageName; }, // 给请求头中加上account和token信息 addTokenToRequest: function (request) { var token = $.cookie('token'); var account = $.cookie('account'); if (token == undefined) { var localStorageToken = localStorage.getItem('token'); if (localStorageToken != undefined) { token = localStorageToken; } } if (account == undefined) { var localStorageAccount = localStorage.getItem('account'); if (localStorageAccount != undefined) { account = localStorageAccount; } } request.setRequestHeader("account", account); request.setRequestHeader("token", token); }, /** * 获取Token */ getToken: function () { return $.cookie('token'); }, // 清空 Cookie clearCookie: function () { var keys = document.cookie.match(/[^ =;]+(?=\=)/g); if (keys) { for (var i = keys.length; i--;) { // 清除当前域名路径的有限日期 document.cookie = keys[i] + '=0;path=/;expires=' + new Date(0).toUTCString(); // Domain Name域名 清除当前域名的 document.cookie = keys[i] + '=0;path=/;domain=' + document.domain + ';expires=' + new Date(0).toUTCString(); // 清除一级域名下的或指定的 document.cookie = keys[i] + '=0;path=/;domain=baidu.com;expires=' + new Date(0).toUTCString(); } } }, // 获取路由中的参数 getRouteParam: function (name) { var reg = new RegExp("(^|\\?|&)" + name + "=([^&]*)(\\s|&|$)", "i"); if (reg.test(location.href)) return unescape(RegExp.$2.replace(/\+/g, " ")); return ""; }, /** * 获取跳转的路径 * @param {*} url */ getRedirectUrl: function (url) { var href = window.location.href; if (href.indexOf("pcapp") >= 0) { return this.getRootPath() + "/pcapp/" + url; } else { return this.getRootPath() + "/" + url; } }, clearLocalStorage: function () { // 清理localStorage时需要保留的参数列表 var reserveParams = ["hostPageBaseURL", "workShopId", "resourceInstanceId", "resourceInstanceName", "apsBaseUrl", "cameraBaseURL"]; //存放的信息 var reserveParamValues = []; //获取参数信息 var len = reserveParams.length; for (var i = 0; i < len; i++) { var reserveParam = reserveParams[i]; var reserveParamValue = ""; if (localStorage.getItem(reserveParam) != undefined) { reserveParamValue = localStorage.getItem(reserveParam); } reserveParamValues.push(reserveParamValue); } //清理localStorage window.localStorage.clear(); //还原参数信息 for (var i = 0; i < len; i++) { localStorage.setItem(reserveParams[i], reserveParamValues[i]); } }, }