// 公共方法 let _utils = { /** * 对一段指定的字符串应用 HTML 编码。 * @param html * @returns {*} */ encodeHtml : html = { if ( typeof ( html ) == 'string' ) { return html . replace ( // g , 'amp;' ). replace ( // g , 'lt;'
// 创建容器嵌套子系统iframe let subBox = document.createElement('div'); subBox.setAttribute('id', id); subBox.style.display = 'none'; document.body.appendChild(subBox);
// 子系统 systemList.forEach(item => { // 初始请求参数 let initParams = { njptAuthToken: encodeURIComponent(SZH.path.NjptAuthToken), loginFlag: encodeURIComponent(SZH.path.loginFlag), callbackName: '' };
// 合并后请求参数 let mergeParams = { ...initParams, ...item.params };
// 子系统iframe let subIframe = document.createElement('iframe'); subIframe.setAttribute('id', item.id); subIframe.style.display = 'none'; subIframe.src = item.url + '?njptAuthToken=' + mergeParams.njptAuthToken + '&loginFlag=' + mergeParams.loginFlag + '&callbackName=' + mergeParams.callbackName;
// 将子系统iframe追加到容器中 subBox.appendChild(subIframe); }); }, /** * 跳转子系统-退出验证 * @paramid * @paramsystemList * @returns{boolean} */ logoutSubSystem: (id, systemList) => { // 参数判空 if (id == null || id.length == 0 || systemList == null || systemList.length == 0) return false;
// 创建容器嵌套子系统iframe let subBox = document.createElement('div'); subBox.setAttribute('id', id); subBox.style.display = 'none'; document.body.appendChild(subBox);
// 子系统 systemList.forEach(item => { // 初始请求参数 let initParams = { njptAuthToken: encodeURIComponent(SZH.path.NjptAuthToken), loginFlag: encodeURIComponent(SZH.path.loginFlag), callbackName: '', authType: '' };
// 合并后请求参数 let mergeParams = { ...initParams, ...item.params };
// 子系统iframe let subIframe = document.createElement('iframe'); subIframe.setAttribute('id', item.id); subIframe.style.display = 'none'; subIframe.src = item.url + '?njptAuthToken=' + mergeParams.njptAuthToken + '&loginFlag=' + mergeParams.loginFlag + '&callbackName=' + mergeParams.callbackName + '&authType=' + mergeParams.authType;
// 将子系统iframe追加到容器中 subBox.appendChild(subIframe); }); }, /** * 移除Dom元素 * @paramid */ onRemoveElement: id => { if (id == null || id.length == 0) return false; let element = document.getElementById(id); if (element) { document.body.removeChild(element); } }, /** * 设置cookie * @paramthat * @paramloginFlag * @paramen_userName * @paramen_password * @paramremeber * @paramexpires */ setCookie: (that, loginFlag, en_userName, en_password, remeber, expires) => { if (expires == null) expires = 15;
// 过期时间(默认15分钟) let extime = new Date(new Date().getTime() + expires * 60 * 1000);
// 设置cookie that.$Cookies.set('userName_' + loginFlag, en_userName, { path: '/', expires: extime }); that.$Cookies.set('password_' + loginFlag, en_password, { path: '/', expires: extime }); that.$Cookies.set('remeber_' + loginFlag, remeber, { path: '/', expires: extime }); }, /** * 清除cookie * @paramthat * @paramloginFlag */ clearCookie: (that, loginFlag) => { // 清除cookie that.$Cookies.remove('userName_' + loginFlag); that.$Cookies.remove('password_' + loginFlag); that.$Cookies.remove('remeber_' + loginFlag); }, /** * 读取cookie * @paramthat * @paramloginFlag */ getCookie: (that, loginFlag) => { // 登录用户信息 let loginObj = {}; // 用户名 let userName = that.$Cookies.get('userName_' + loginFlag); // 密码 let password = that.$Cookies.get('password_' + loginFlag); // 记住密码 let remeber = that.$Cookies.get('remeber_' + loginFlag); // 登录用户信息 if (userName && password && remeber) { loginObj = { userName: userName, password: password, remeber: remeber } } // 返回登录用户信息 return loginObj; }, judgeEmpty: function(obj) { return typeof obj === "undefined" || obj === null || obj === ""; } };
export default _utils; // 公共方法 let _utils = { /** * 对一段指定的字符串应用 HTML 编码。 * @paramhtml * @returns{*} */ encodeHtml: html => { if (typeof (html) == 'string') { return html.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/'/g, ''').replace(/"/g, '"'); } else { return html; } }, /** * 对一段指定的字符串应用 HTML 解码。 * @paramhtml * @returns{*} */ decodeHtml: html => { if (typeof (html) == 'string') { let arrEntities = { 'lt': '<', 'gt': '>', 'nbsp': ' ', 'amp': '&', 'quot': '"' }; return html.replace(/&(lt|gt|nbsp|amp|quot);/ig, (all, t) => { return arrEntities[t]; }); } else { return html; } }, /** * tree 转成 list * @paramtree * @returns{Array} */ treeToList: (tree, childrenId) => { if (childrenId == null) childrenId = 'children'; let list = []; tree.forEach(item => { list.push(item); if (item[childrenId] && item[childrenId].length > 0) { let children = _utils.treeToList(item[childrenId]); children.forEach(child => { list.push(child); }); } }); return list; }, /** * list 转成 tree * @paramlist * @paramparent * @paramid * @paramparentId * @returns{Array} */ listToTree: (list, parent, id, parentId, childrenId) => { if (parent == null) parent = ''; if (id == null) id = 'id'; if (parentId == null) parentId = 'parentId'; if (childrenId == null) childrenId = 'children'; let tree = []; list.forEach(item => { if (item[parentId] == null || item[parentId] == '#') item[parentId] = ''; if (item[parentId] == parent) { tree.push(item); item[childrenId] = _utils.listToTree(list, item[id], id, parentId); item[childrenId] = item[childrenId].length == 0 ? null : item[childrenId]; } }); return tree; }, /** * list 转成 tree(全部) * @paramlist * @paramparent * @paramid * @paramparentId * @returns{Array} */ listToTreeAll: (list, parent, id, parentId, childrenId) => { if (parent == null) parent = ''; if (id == null) id = 'id'; if (parentId == null) parentId = 'parentId'; if (childrenId == null) childrenId = 'children'; let tree = []; list.forEach(item => { if (item[parentId] == parent) { tree.push(item); item[childrenId] = _utils.listToTreeAll(list, item[id], id, parentId); item[childrenId] = item[childrenId].length == 0 ? null : item[childrenId]; } }); return tree; }, /** * 生成token * @returns{string} */ generateToken: () => { let number = parseInt(Math.random() * 100000000); let now = new Date(); return now.format('yyyyMMddhhmmss') + number; }, /** * 格式化文件大小(最大支持单位 PB) * @paramsize * @returns{string} */ formatFileSize: size => { size *= 1.0; let units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']; let unit = 0; for (unit = 0; unit < 6 && size >= 1024; unit++) { size /= 1024; } return size.toFixed(2) + ' ' + units[unit]; }, /** * 保留几位小数 * @paramnumber * @paramdigit * @returns{string} */ fixedDecimal: (number, digit) => { if (digit == null) digit = 2; let newNumber = number == null ? null : number == 0 ? 0 : new Number(number).toFixed(digit); return newNumber; }, /** * 从开始时间、结束时间得到时间差(毫秒数) * @paramtime * @returns{number} */ totalMilliseconds: time => { let oldTime = (new Date('1970/1/1 0:00:00')).getTime(); let newTime = (new Date(time)).getTime(); return newTime - oldTime; }, /** * 根据某个属性比较对象数组 * @paramproperty * @returns{function(*, *): number} */ onCompareArray: property => { return (a, b) => { let val1 = a[property]; let val2 = b[property]; return val1 - val2; } }, /** * 删除字符串中特定的字符 * @paramsource * @paramchars */ removeChars: (source, chars) => { let reg = new RegExp(chars, 'gi'); let result = source.replace(reg, ''); return result; }, /** * 在线预览文件 * @paramfileRoot * @paramviewRoot * @paramdocType * @paramdocPath * @returns{string} */ viewDoc: (fileRoot, viewRoot, docType, docPath) => { let officeUrl = ''; switch (docType.toLowerCase()) { case '.doc': case '.docx': case '.txt': case '.csv': case '.cs': case '.wps': case '.js': case '.xml': case '.config':// 普通文档 case '.xls': case '.xlsx': case '.et':// 电子表格 case '.ppt': case '.pptx': case '.wpp': case '.dps':// 幻灯片 case '.zip': case '.rar':// 压缩包 officeUrl = viewRoot + '/Defaultforh5.aspx?url=' + docPath; break; case '.flv': case '.f4v': case '.m3u8':// flash officeUrl = fileRoot + '/MediaPlay/Play/Index?url=DocView/' + docPath; break; case '.jpg': case '.png': case '.ico': case '.gif': case '.bmp':// 图片 case '.mp3': case '.swf': case '.xaml': case '.pdf': case '.html': case '.mp4': case '.svg': officeUrl = fileRoot + '/' + docPath; break; case '.avi': case '.wma': case '.asx': case '.asf': case '.wmv': case '.mpeg': case '.3g2': case '.3pg': case '.mov': case '.au': case '.mpg': case '.ra': case '.ram': case '.rm': case '.smi': case '.smil': default: officeUrl = ''; break; } if (officeUrl != undefined) { return officeUrl; } }, /** * 往下拉框数据添加全部选项 * @paramselectData * @paramprop * @paramid * @paramtext */ addSelectDataAll: (selectData, prop, id, text) => { if (prop == null) prop = { label: 'text', value: 'id' }; if (id == null) id = ''; if (text == null) text = '全部'; selectData.unshift({ [prop.label]: text, [prop.value]: id }); }, /** * 往字典表添加全部选项 * @paramselectData * @paramprop * @paramdicValue * @paramdicName */ addDicDataAll: (selectData, prop, id, text) => { if (prop == null) prop = { label: 'dicName', value: 'dicValue' }; if (id == null) id = ''; if (text == null) text = '全部'; selectData.unshift({ [prop.label]: text, [prop.value]: id }); }, /** * 获取筛选时间 * @paramday * @returns{{startDate: number, endDate: Date}} */ getViewDate: day => { if (day == null) day = 7; let now = new Date(); let startDate = now.getTime() - day * 24 * 3600 * 1000; let endDate = now; return { startDate: startDate, endDate: endDate }; }, /** * 导出Excel文件 * @paramresult * @paramfileName */ exportExcel: (result, fileName) => { // IE浏览器以本地方式保存文件(window.navigator.msSaveBlob + window.navigator.msSaveOrOpenBlob) // window.navigator.msSaveBlob:仅为用户提供‘保存’按钮 // window.navigator.msSaveOrOpenBlob:不但提供‘保存’按钮,还提供‘打开’按钮 let blob = new Blob([result]); if (window.navigator && window.navigator.msSaveOrOpenBlob) { window.navigator.msSaveOrOpenBlob(blob, fileName); } else { let elink = document.createElement('a'); elink.style.display = 'none'; elink.href = window.URL.createObjectURL(blob);// 创建URL对象 elink.download = fileName;// 文件名 document.body.appendChild(elink); elink.click(); window.URL.revokeObjectURL(elink.href);// 释放URL对象 document.body.removeChild(elink);// 移除a链接 } }, /** * 获取合并单元格的数据 * @paramlist * @paramprop * @returns{Array} */ onGetMergeRowData: (list, prop) => { let mergeRowArr = []; let arrIndex = 0; list.forEach((item, index) => { item.index = index; if (index == 0) { mergeRowArr.push(1); } else { if (item[prop] == list[index - 1][prop]) { mergeRowArr[arrIndex] += 1; mergeRowArr.push(0); } else { mergeRowArr.push(1); arrIndex = index; } } }); return mergeRowArr; }, /** * 获取当前年份与默认年份之间的年份列表 * @paramdefaultDate * @returns{Array} */ onGetYearsData: defaultDate => { // 默认时间 if (defaultDate == null) defaultDate = '2020/01/01 00:00:00'; // 默认年份 let defaultYear = new Date(defaultDate).getFullYear(); // 当前年份 let nowYear = new Date().getFullYear(); // 年份列表 let yearList = []; // 当前年份与默认年份的差值 let difference = nowYear - defaultYear; if (difference >= 0) { for (let i = 0; i <= difference; i++) { yearList.push({ id: nowYear - i, sortId: i + 1, text: nowYear - i + '年' }); } } return yearList; }, /** * CryptoJS:JavaScript加密、解密库 * @paramthat * @paramtype * @parampass * @paramstring * @returns{string} */ onCryptoJS: (that, pass, type, string) => { let self = that; // 密钥 let key = self.$CryptoJS.enc.Utf8.parse(pass); // 初始向量,不设置会随机生成(无法解密) let iv = self.$CryptoJS.enc.Utf8.parse(pass); // 加密/解密 switch (type) { case 'encrypt':// 加密 // 字符串 let parseStr = self.$CryptoJS.enc.Utf8.parse(string); // 返回加密后的字符串 return self.$CryptoJS.AES.encrypt(parseStr, key, { iv: iv, mode: self.$CryptoJS.mode.CBC, padding: self.$CryptoJS.pad.Pkcs7 }).toString(); break; case 'decrypt':// 解密 // 返回解密后的字符串 return self.$CryptoJS.AES.decrypt(string, key, { iv: iv, mode: self.$CryptoJS.mode.CBC, padding: self.$CryptoJS.pad.Pkcs7 }).toString(self.$CryptoJS.enc.Utf8); break; } }, /** * 跳转子系统-登录验证 * @paramid * @paramsystemList * @returns{boolean} */ loginSubSystem: (id, systemList) => { // 参数判空 if (id == null || id.length == 0 || systemList == null || systemList.length == 0) return false;
// 创建容器嵌套子系统iframe let subBox = document.createElement('div'); subBox.setAttribute('id', id); subBox.style.display = 'none'; document.body.appendChild(subBox);
// 子系统 systemList.forEach(item => { // 初始请求参数 let initParams = { njptAuthToken: encodeURIComponent(SZH.path.NjptAuthToken), loginFlag: encodeURIComponent(SZH.path.loginFlag), callbackName: '' };
// 合并后请求参数 let mergeParams = { ...initParams, ...item.params };
// 子系统iframe let subIframe = document.createElement('iframe'); subIframe.setAttribute('id', item.id); subIframe.style.display = 'none'; subIframe.src = item.url + '?njptAuthToken=' + mergeParams.njptAuthToken + '&loginFlag=' + mergeParams.loginFlag + '&callbackName=' + mergeParams.callbackName;
// 将子系统iframe追加到容器中 subBox.appendChild(subIframe); }); }, /** * 跳转子系统-退出验证 * @paramid * @paramsystemList * @returns{boolean} */ logoutSubSystem: (id, systemList) => { // 参数判空 if (id == null || id.length == 0 || systemList == null || systemList.length == 0) return false;
// 创建容器嵌套子系统iframe let subBox = document.createElement('div'); subBox.setAttribute('id', id); subBox.style.display = 'none'; document.body.appendChild(subBox);
// 子系统 systemList.forEach(item => { // 初始请求参数 let initParams = { njptAuthToken: encodeURIComponent(SZH.path.NjptAuthToken), loginFlag: encodeURIComponent(SZH.path.loginFlag), callbackName: '', authType: '' };
// 合并后请求参数 let mergeParams = { ...initParams, ...item.params };
// 子系统iframe let subIframe = document.createElement('iframe'); subIframe.setAttribute('id', item.id); subIframe.style.display = 'none'; subIframe.src = item.url + '?njptAuthToken=' + mergeParams.njptAuthToken + '&loginFlag=' + mergeParams.loginFlag + '&callbackName=' + mergeParams.callbackName + '&authType=' + mergeParams.authType;
// 将子系统iframe追加到容器中 subBox.appendChild(subIframe); }); }, /** * 移除Dom元素 * @paramid */ onRemoveElement: id => { if (id == null || id.length == 0) return false; let element = document.getElementById(id); if (element) { document.body.removeChild(element); } }, /** * 设置cookie * @paramthat * @paramloginFlag * @paramen_userName * @paramen_password * @paramremeber * @paramexpires */ setCookie: (that, loginFlag, en_userName, en_password, remeber, expires) => { if (expires == null) expires = 15;
// 过期时间(默认15分钟) let extime = new Date(new Date().getTime() + expires * 60 * 1000);
// 设置cookie that.$Cookies.set('userName_' + loginFlag, en_userName, { path: '/', expires: extime }); that.$Cookies.set('password_' + loginFlag, en_password, { path: '/', expires: extime }); that.$Cookies.set('remeber_' + loginFlag, remeber, { path: '/', expires: extime }); }, /** * 清除cookie * @paramthat * @paramloginFlag */ clearCookie: (that, loginFlag) => { // 清除cookie that.$Cookies.remove('userName_' + loginFlag); that.$Cookies.remove('password_' + loginFlag); that.$Cookies.remove('remeber_' + loginFlag); }, /** * 读取cookie * @paramthat * @paramloginFlag */ getCookie: (that, loginFlag) => { // 登录用户信息 let loginObj = {}; // 用户名 let userName = that.$Cookies.get('userName_' + loginFlag); // 密码 let password = that.$Cookies.get('password_' + loginFlag); // 记住密码 let remeber = that.$Cookies.get('remeber_' + loginFlag); // 登录用户信息 if (userName && password && remeber) { loginObj = { userName: userName, password: password, remeber: remeber } } // 返回登录用户信息 return loginObj; }, judgeEmpty: function(obj) { return typeof obj === "undefined" || obj === null || obj === ""; } };
export default _utils;