/** 通用的工具类 @author weidy */ /** * 去除前后空格 */ String.prototype.trim = function(){ try { return this.replace(/(^\s*)|(\s*$)/g, ''); }catch (e){ return this; } }; /** * 数组元素索引,没有该值时返回-1 */ Array.prototype.indexOf = function(val) { for (var i = 0; i < this.length; i++) { if (this[i] == val) return i; } return -1; }; Array.prototype.remove = function(val) { var index = this.indexOf(val); if (index > -1) { this.splice(index, 1); } }; window.innerWidth =window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth; window.innerHeight =window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight; (function($){ vciWebUtil=function() { this.sessionInfoKey = { orgsOid: 'orgsOid', //用户所属组织 orgsName: 'orgsName',//用户所属组织名称 deptOid: 'deptOid',//所属部门 deptName: 'deptName',//所属部门名称 userOid: 'userOid',//用户主键 userId: 'userId',//用户名 userName: 'userName',//用户姓名 personOid: 'personOid',//用户身份主键 personName: 'personName',//真实身份名称 workgroupOid: 'workgroupOid',//所属班组--这个是MES208独有的 workgroupName: 'workgroupName',//所属班组名称--这个是MES208独有的 usertype: 'usertype',//用户的类型 usertypeText: 'usertypeText',//用户类型文本 sex: 'sex',//性别 sextext: 'sextext',//性别名称 photoUrl: 'photoUrl',//用户头像 isMustChangePassword: 'isMustChangePassword', //是否马上修改密码 passwordInfo: 'passwordInfo', token: 'token', userSecret: 'userSecret', userSecretText: 'userSecretText' }; this.systemValueKey = { currentDateTime: 'curentDateTime', currentDateTimeE: 'curentDateTimeE', currentDateTimeSSS: 'curentDateTimeSSS', currentDate: 'currentDate', currentTime: 'currentTime', uuid: 'uuid', orgsOid: 'orgsOid', orgsName: 'orgsName', deptOid: 'deptOid', deptName: 'deptName', userOid: 'userOid', userId: 'userId', userName: 'userName', personOid: 'personOid', personName: 'personName', workgroupOid: 'workgroupOid', workgroupName: 'workgroupName', usertype: 'usertype', usertypeText: 'usertypeText', sex: 'sex', sextext: 'sextext', photoUrl: 'photoUrl', isMustChangePassword: 'isMustChangePassword', passwordInfo: 'passwordInfo', ip: 'ip', language: 'language', userSecret: 'userSecret', userSecretText: 'userSecretText' }; this.deptOid = ''; // 登录时保存用户部门 this.uuid = function () { var s = []; var hexDigits = "0123456789ABCDEF"; for (var i = 0; i < 36; i++) { s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1); } s[14] = "4"; s[19] = hexDigits.substr(s[19] & 0x3 | 0x8, 1); s[8] = s[13] = s[18] = s[23] = "-"; return s.join(''); }; this.isNotNull = function (v) {//不为空 if (v != undefined && v != null && v.toString().length > 0) return true; else return false; }; this.objIsNull = function (v) { var result = true; for (var key in v) { if (v[key]) { result = false; break; } } return result; }; this.isNull = function (v) {//为空 var me = this; return !me.isNotNull(v); }; this.endWith = function (string, subStr) {//以什么结尾 string=string.toString(); var me = this; if (me.isNull(string) || me.isNull(subStr)) return false; if (string.length < subStr.length) return false; if (string.substring(string.length - subStr.length) == subStr) return true; else return false; }; this.startWith = function (string, subStr) {//以什么开头 string=string.toString(); var me = this; if (me.isNull(string) || me.isNull(subStr)) return false; if (string.length < subStr.length) return false; if (string.substring(0, subStr.length) == subStr) return true; else return false; }; this.isArray = function (array) { if (!array) { return false; } else { if (Object.prototype.toString.call(array) == '[object Array]') { return true; } else { return false; } } }; this.inArray = function (array, v) {//是否在数组中 try { for (var i = 0; i < array.length; i++) { if (v == array[i]) return true; } } catch (e) { } return false; }; this.removeFormArray = function (array, v, attr) { try { for (var i = 0; i < array.length; i++) { if (v == array[i] || (attr && array[i][attr] == v[attr])) { array.splice(i, 1); return true; } } } catch (e) { } }; this.showMsgFromResult = function (result, defaultMsg, callback) { if (!result) { result = { msg: defaultMsg }; } if (result && this.isNull(result.msg)) { result.msg = defaultMsg; } this.showMsg(result.msg, callback); }; this.showMsg = function (msg, callBack) {//显示信息 if (!msg) { msg = ""; } if (callBack) { top.layer.msg(msg, callBack); } else { top.layer.msg(msg); } }; this.showDebugMsg = function (msg) { if (!msg) { msg = ""; } if (top.isDebug) { try { console.log(msg); } catch (e) { } } }; this.showConfirmMsg = function (title, callback, cancleCallback) { var lastCallback = function (index) { if (callback) { callback(index); } top.layer.close(index); }; top.layer.confirm(title, {icon: 3, title: '需要您确认'}, lastCallback, cancleCallback); }; this.showErrorMsg = function (msg, callback) { if (!msg) { msg = ""; } var lastCallback = function (index) { if (callback) { callback(index); } top.layer.close(index); }; top.layer.alert(msg, lastCallback); if (top.isDebug && console) {//调试的时候把引用方法的信息打印出来 try { throw msg; } catch (e) { var caller = arguments.callee; //console.log(caller.caller); } } }; this.showAutoMsg = function (msg, timeout, callback) { if (!msg) { msg = ""; } top.layer.msg(msg, {icon: 1, time: timeout}, callback); }; this.showProgress = function (text) { if (top.progressIndex) { }else { //this.closeProgress(); try { if (this.isNotNull(text)) { top.progressIndex = top.layer.load(2, {content: text, area: ['350px', 'auto']}); $(".layui-layer-loading2").css("width", "100%"); $(".layui-layer-loading2").css("background-positionX", "30px"); $(".layui-layer-loading2").css("background-positionY", "center"); $(".layui-layer-loading2").css("background-color", "#fff"); $(".layui-layer-loading2").css("line-height", "30px"); $(".layui-layer-loading2").css("font-size", "20px"); $(".layui-layer-loading2").css("font-weight", "bold") $(".layui-layer-loading2").css("display", "table") $(".layui-layer-loading2 .layui-layer-content-pane").css("display", "table-cell") $(".layui-layer-loading2 .layui-layer-content-pane").css("vertical-align", "middle") $(".layui-layer-loading2 .layui-layer-content-pane").css("padding", "30px 30px 30px 85px"); } } catch (e) { } } }; this.closeProgress = function () { try { if (top.progressIndex) { top.layer.close(top.progressIndex); top.progressIndex = null; } } catch (e) { } }; this.checkResult = function (result) {//检查结果,异步请求的时候使用这个 var me = this; try { if (!result) { me.showMsg("没有返回结果"); return false; } if (result && result.isNoLogin) { me.showLoginDialog(); return false; } if (result && result.isNoRigh) { me.showMsg("你没有权限做此操作"); return false; } if (!result.success) { me.showMsg(result.msg); return false; } return true; } catch (e) { } return false; }; this.processNullForObject = function (obj) {//处理空值 var me = this; if (obj && typeof obj == "object") { for (var key in obj) { var value = obj[key]; if (me.isNull(value)) value = ""; } } }; this.getIEVersion = function () { var IEVersion = {}; if (navigator.userAgent.toLowerCase().indexOf("msie") > -1) { IEVersion['isIE'] = true; var version = $.browser.version; version = parseInt(version.substring(0, version.indexOf('.'))); if (version <= 8) { IEVersion['isIE8'] = true; } } else { IEVersion['isIE'] = false; } return IEVersion; }; /** * 支持系统参数包括: */ this.getSystemVar = function (code) { var me = this; if (me.isNull(code)) return ""; if (me.systemValueKey.currentDateTime == code) { return me.getSystemVar(me.systemValueKey.currentDate) + " " + me.getSystemVar(me.systemValueKey.currentTime); } if (me.systemValueKey.currentDateTimeSSS == code) { var date = new Date(); return me.getSystemVar(me.systemValueKey.currentDateTime) + "." + date.getMilliseconds(); } if (me.systemValueKey.currentDate == code) { var date = new Date(); var month = date.getMonth() + 1; if (month >= 1 && month <= 9) month = "0" + month; var day = date.getDate(); if (day >= 0 && day <= 9) day = "0" + day; return date.getFullYear() + "-" + month + "-" + day; } if (me.systemValueKey.currentDateTimeE == code) {//含有星期几 var date = new Date(); var week = date.getDay(); var weekName = ['日', '一', '二', '三', '四', '五', '六']; dateStr += " 星期" + weekName[week]; return me.getSystemVar(me.systemValueKey.currentDateTime) + dateStr; } if (me.systemValueKey.currentTime == code) { var date = new Date(); var hours = date.getHours(); if (hours >= 0 && hours <= 9) hours = "0" + hours; var minutes = date.getMinutes(); if (minutes >= 0 && minutes <= 9) minutes = "0" + minutes; var secends = date.getSeconds(); if (secends >= 0 && secends <= 9) secends = "0" + secends; return hours + ":" + minutes + ":" + secends; } if (me.systemValueKey.uuid == code) { return me.uuid(); } var value = top.mySessionInfo[code]; if (value == undefined || value == null) return ""; else return value; }; this.isSso = function () { var me = this; return me.getSystemVar('sso'); }; this.getSsoinfo = function () { var me = this; return me.getSystemVar('ssoInfo'); }; this.getToken = function () { var me = this; var token = me.getSystemVar(me.sessionInfoKey.token); if (!token) { token = $.cookie(TOKEN_KEY); } return token; }; this.getWorkGroupName = function () { var me = this; return me.getSystemVar(me.sessionInfoKey.workgroupName); }; this.getRolesName = function () { var me = this; return me.getSystemVar("rolesname") }; this.isNumber = function (value) { var reg = new RegExp("^[0-9]+$"); if (reg.test(value)) { return true; } else { return false; } }; this.isValidCharactor = function (value) { var reg = new RegExp("^[0-9A-Za-z_]*$"); if (reg.test(value)) { return true; } else { return false; } }; this.dateFormate = { dateTime: 'yyyy-MM-dd HH:mm:ss', date: 'yyyy-MM-dd', dateNoSecond: 'yyyy-MM-dd HH:mm' }; this.formateDateTime = function (value) { return this.formateDateByFormate(value, 'yyyy-MM-dd HH:mm:ss'); }; this.formateDateTimeNoSecond = function (value) { var that = this; return this.formateDateByFormate(value, 'yyyy-MM-dd HH:mm'); }; this.formateDate = function (value) { return this.formateDateByFormate(value, 'yyyy-MM-dd'); }; this.formateDateByFormate = function (value, formate) { if (value != null && formate != null) { //验证value var valueType = typeof value; if (valueType == 'number') { value = value.toString(); } if (value.substring(0, 4).indexOf("-") != -1 || value.substring(0, 4).indexOf("/") != -1) { value = "20" + value } return layui.util.toDateString(value, formate); } else { return ""; } }; this.formateUserSecret = function (value) { if (value * 1 == 30) { return "重要"; } else if (value * 1 == 20) { return "一般"; } else if (value * 1 == 10) { return "内部"; } else { return "默认[内部]" } }; this.formateDataSecret = function (value) { if (value * 1 == 30) { return "机密"; } else if (value * 1 == 20) { return "秘密"; } else if (value * 1 == 10) { return "非密"; } else { return "默认[非密]" } }; this.formateBoolean = function (value) { if (!value) { return '否'; } if (value == true || (value != null && 'true' == value.toLowerCase())) { return "是"; } return "否"; }; this.displayButton = function (buttonInfo) {//显示/隐藏按钮 for (var buttonId in buttonInfo) { if (buttonInfo[buttonId]) { $(buttonId).show(); } else { $(buttonId).hide(); } } }; this.getObjectLength = function (obj) {//获取对象里的属性个数 if (!obj) { return 0; } else { var l = 0; for (var key in obj) { l++; } return l; } }; this.getOidFromGrid = function (gridId, checkEmpty, checkOne, pk) { if (!pk) { pk = "oid"; } var checkData = layui.table.checkStatus(gridId); var oids = ""; if (checkData && checkData.data && checkData.data.length > 0) { var rows = checkData.data; for (var i = 0; i < rows.length; i++) { var value = rows[i][pk]; if (!value || value == "undefined") { value = ""; } if (i == rows.length - 1) { oids += value; } else { oids += value + ","; } } } var me = this; if (checkEmpty && me.isNull(oids)) { me.showErrorMsg("没有选择数据"); return false; } if (checkOne && oids.indexOf(",") > -1) { me.showErrorMsg("每次只能选择一条数据"); return false; } return oids; }; //格式如jsPath#methodName?param=aaa¶m1=bbb this.getUrlParams = function (url, urlobj, splitMethod, splitParams) { if (!urlobj) { urlobj = { jsPath: '', methodName: '', params: {} } } if (!urlobj['params']) { urlobj['params'] = {} } if (!splitMethod) { splitMethod = '#' } if (!splitParams) { splitParams = '&' } if (url && url != 'null') { urlobj['jsPath'] = url.split('?')[0].split(splitMethod)[0]; if (url.indexOf(splitMethod) > -1) { urlobj['methodName'] = url.split('?')[0].split(splitMethod)[1]; } else { urlobj['methodName'] = 'doAction' } if (url.indexOf('?') > -1) { var params = url.split('?')[1].split(splitParams); layui.each(params, function (i, item) { urlobj['params'][item.split('=')[0]] = item.split('=')[1] }) } } return urlobj } this.postJsonP = function (ip, funcName, data, callback, failcallback) { if (!ip || this.isNull(ip)) { alert("这个页面没有传递ip地址"); return; } var url = "http://" + ip + funcName; $.ajax({ type: 'post', url: url, dataType: 'jsonp', data: data, cache: false, async: false, success: function (json) { if (json && callback) { callback(json); } }, error: function (result) { if (failcallback) { failcallback(result); } } }); }; this.post = function (url, data, callback, errorCallback, newPath, noprogress, isasync) { var that = this; if (isasync) { that.async("post", url, data, callback, errorCallback, newPath, noprogress); } else { that.ajax("post", url, data, callback, errorCallback, newPath, noprogress); } }; this.ajax = function (requestType, url, data, callback, errorCallback, newPath, noprogres) { var that = this; url = newPath ? newPath + url : (that.startWith(url, "http:") ? url : (path + url)); if (!noprogres) { that.showProgress("执行中,请稍候..."); } var header = {}; header[TOKEN_KEY] = that.getToken(); $.ajax({ type: requestType, headers: header, url: url, data: data, cache: false, contentType: "application/x-www-form-urlencoded", success: function (data) { that.closeProgress(); var result = {}; if (typeof (data) == 'string') { if (data && data.length > 0) result = eval("(" + data + ")"); } else { result = data; } if (callback) { callback(result); } }, error: function (xhr, error) { that.closeProgress(); if (errorCallback) { errorCallback(xhr, error); } } }); }; this.manualAjax = function (requestType, url, data, callback, errorCallback, newPath, noprogres) { var that = this; jQuery.support.cors = true; url = newPath ? newPath + url : (that.startWith(url, "http:") ? url : (path + url)); if (!noprogres) { that.showProgress("执行中,请稍候..."); } var header = {}; header[TOKEN_KEY] = that.getToken(); $.ajax({ type: requestType, headers: header, url: url, data: data, cache: false, contentType: "application/json", success: function (data) { that.closeProgress(); var result = {}; if (typeof (data) == 'string') { if (data && data.length > 0) result = eval("(" + data + ")"); } else { result = data; } if (callback) { callback(result); } }, error: function (xhr, error) { that.closeProgress(); if (errorCallback) { errorCallback(xhr, error); } } }); }; this.async = function (requestType, url, data, callback, errorCallback, newPath, noprogres) { var that = this; jQuery.support.cors = true; url = newPath ? newPath + url : (that.startWith(url, "http:") ? url : (path + url)); if (!noprogres) { that.showProgress("执行中,请稍候..."); } var header = {}; header[TOKEN_KEY] = that.getToken(); $.ajax({ type: requestType, async: true, headers: header, //crossDomain: true == !(document.all), url: url, data: data, success: function (data) { that.closeProgress(); var result = {}; if (typeof (data) == 'string') { if (data && data.length > 0) result = eval("(" + data + ")"); } else { result = data; } if (callback) { callback(result); } }, error: function (xhr, error) { that.closeProgress(); if (errorCallback) { errorCallback(xhr, error); } } }); }; // 同步 this.synchro = function (url, data, callback, errorCallback, newPath, noprogres) { var that = this; jQuery.support.cors = true; url = newPath ? newPath + url : (that.startWith(url, "http:") ? url : (path + url)); if (!noprogres) { that.showProgress("执行中,请稍候..."); } var header = {}; header[TOKEN_KEY] = that.getToken(); $.ajax({ type: 'get', async: false, headers: header, url: url, data: data, success: function (data) { that.closeProgress(); var result = {}; if (typeof (data) == 'string') { if (data && data.length > 0) result = eval("(" + data + ")"); } else { result = data; } if (callback) { callback(result); } }, error: function (xhr, error) { that.closeProgress(); if (errorCallback) { errorCallback(xhr, error); } } }); }; // 同步 this.syncpost = function (url, data, callback, errorCallback, newPath, noprogres) { var that = this; jQuery.support.cors = true; url = newPath ? newPath + url : (that.startWith(url, "http:") ? url : (path + url)); if (!noprogres) { that.showProgress("执行中,请稍候..."); } var header = {}; header[TOKEN_KEY] = that.getToken(); $.ajax({ type: 'post', async: false, headers: header, url: url, data: data, success: function (data) { that.closeProgress(); var result = {}; if (typeof (data) == 'string') { if (data && data.length > 0) result = eval("(" + data + ")"); } else { result = data; } if (callback) { callback(result); } }, error: function (xhr, error) { that.closeProgress(); if (errorCallback) { errorCallback(xhr, error); } } }); }; this.get = function (url, data, callback, errorCallback, newPath, noProgress, isAsync) { var that = this; if (isAsync) { that.async("get", url, data, callback, errorCallback, newPath, noProgress); } else { that.ajax("get", url, data, callback, errorCallback, newPath, noProgress); } }; this.put = function (url, data, callback, errorCallback, newPath, noProgress, isAsync) { var that = this; if (isAsync) { that.async("put", url, data, callback, errorCallback, newPath, noProgress); } else { that.ajax("put", url, data, callback, errorCallback, newPath, noProgress); } }; this.deleteRequest = function (url, data, callback, errorCallback, newPath, noProgress, isAsync) { var that = this; if (isAsync) { that.async("delete", url, data, callback, errorCallback, newPath, noProgress); } else { that.ajax("delete", url, data, callback, errorCallback, newPath, noProgress); } }; this.postForCheckId = function (url, data, callback, errorCallback, newPath, showProgress, isasync, noprogress) { var that = this; jQuery.support.cors = true; url = newPath ? newPath + url : (that.startWith(url, "http:") ? url : (path + url)); var header = {}; header[TOKEN_KEY] = that.getToken(); $.ajax({ type: 'post', async: !isasync, headers: header, url: url, data: data, success: function (data) { var result = {}; if (typeof (data) == 'string') { if (data && data.length > 0) result = eval("(" + data + ")"); } else { result = data; } if (callback) { callback(result); } }, error: function (xhr, error) { //that.closeProgress(); if (errorCallback) { errorCallback(xhr, error); } } }); }; this.postSetHeader = function (url, data, callback, errorCallback, newPath) { var that = this; jQuery.support.cors = true; url = newPath ? newPath + url : (that.startWith(url, "http:") ? url : (path + url)); $.ajax({ type: 'post', async: true, url: url, data: data, headers: {TOKEN_KEY: that.getToken(), 'appCode': '123', 'userId': that.getCurrentUserId()}, success: function (data) { var result = {}; if (typeof (data) == 'string') { if (data && data.length > 0) result = eval("(" + data + ")"); } else { result = data; } if (callback) { callback(result); } }, error: function (xhr, error) { //that.closeProgress(); if (errorCallback) { errorCallback(xhr, error); } } }); }; /** * 根据源数据来获取按钮 */ this.getButtionsBySourceData = function (sourceData, callback, backPath) { var me = this; if (sourceData) { me.getButtionsByFunctionId(sourceData['functionId'], callback, backPath); } else { me.showDebugMsg("来源数据中没有functionId参数的值"); if (callback) { callback([]); } } }; /** * 根据菜单的编号获取按钮 */ this.getButtionsByFunctionId = function (functionId, callback, backPath) { var me = this; if (me.isNull(functionId)) { if (callback) { callback([]); } } else { me.post("/webMenuController/getButtonsByFunctionId", {functionId: functionId}, function (result) { if (callback) { callback(result); } }, function (xhr, error) { if (callback) { callback([]); } }, backPath); } }; /** * 创建按钮的html * @param sourceData 来源数据 * @param buttonIconMap 按钮的图标映射, * @param buttonDivHtml 按钮所在div的元素对象 * @param appendButtons 不受权限控制的按钮 * @param callback 回调事件 * @param backPath 后台路径,常空 */ this.createButtonHtml = function (sourceData, buttonIconMap, buttonDivHtml, buttonIdPrefix, appendButtons, callback, backPath) { var me = this; me.createButtonHtmlEx(sourceData, buttonIconMap, buttonDivHtml, buttonIdPrefix, appendButtons, null, callback, backPath); }; this.createButtonHtmlEx = function (sourceData, buttonIconMap, buttonDivHtml, buttonIdPrefix, appendButtons, unDisplayButtons, callback, backPath) { var me = this; if (me.isNull(buttonIdPrefix)) { buttonIdPrefix = ""; } me.getButtionsBySourceData(sourceData, function (result) { /** * 功能的主键 functionOid * 功能的编号 moduleNo * 功能和按钮关联的主键 oid * 唯一标识符 uniqueFlag; * 描述 description; * 在功能中的排序号 orderNo; * 按钮名称 name * 按钮昵称,是在功能和按钮的关联中的昵称,前端使用这个来显示按钮 alias; * */ var buttons = result.obj; if (!buttons) { buttons = []; } if (appendButtons && appendButtons.length > 0) {//把不受权限控制的按钮也添加进去 layui.each(appendButtons, function (_index, item) { buttons.push(item); }); } var buttonHtml = []; var hasViewRight = false; var hasQueryRight = false; layui.each(buttons, function (_index, _item) { if (_item.uniqueFlag != 'VIEW' && _item.uniqueFlag != 'SEARCH' //TODO 暂时不显示所有的高级查询按钮 && (!unDisplayButtons || unDisplayButtons == null || !me.inArray(unDisplayButtons, _item.uniqueFlag))) {//VIEW是显示这个页面 buttonHtml.push(me.getButtonHtmlFromBtnObject(_item, buttonIdPrefix, buttonIconMap)); } else { if (_item.uniqueFlag == 'VIEW') { hasViewRight = true; } if (_item.uniqueFlag == 'SEARCH') { hasQueryRight = true; } } }); buttonDivHtml.prepend(buttonHtml.join(''));//添加到前面 if (callback) { callback(buttons, hasViewRight, hasQueryRight); } }, backPath); }; this.getButtonHtmlFromBtnObject = function (button, buttonIdPrefix, buttonIconMap) { var me = this; var buttonHtml = []; var executeJs = ""; if(me.isNotNull(button.executeJs)){ executeJs = " execute-js='" + button.executeJs + "' "; } buttonHtml.push(''); return buttonHtml.join(""); }; /** * 创建默认的查询区域,如果传递了tableId,则会自动自动进行查询 */ this.createSearchHtml = function (searchNameMap, searchDiv, tableId) { var me = this; var ieVersion = me.getIEVersion(); var isShow = "none"; if (ieVersion.isIE) { isShow = "block"; } var html = ['