UNPKG

efview

Version:

A high quality Service UI components Library with Vue.js

677 lines (640 loc) 22.4 kB
import {deepCopy} from '../../utils/assist'; import storage from 'good-storage'; import {exportExcel, exportMultiSheetExcel, exportExcelStyle} from '../../static/excel/exportexcel'; let commom = {}; commom.padLeftZero = function (res, str) { return (res + str).substr(str.length); }; commom.formatDate = function (date, fmt) { if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length)); } let o = { 'M+': date.getMonth() + 1, 'd+': date.getDate(), 'H+': date.getHours(), 'h+': date.getHours(), 'm+': date.getMinutes(), 's+': date.getSeconds() }; for (let k in o) { if (new RegExp(`(${k})`).test(fmt)) { let str = o[k] + ''; fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : commom.padLeftZero('00', str)); } } return fmt; }; commom.getToday = function () { let to=new Date(); let M=Number(to.getMonth())+1; return to.getFullYear()+ '-'+commom.padLeftZero('00', M+'') + '-'+ commom.padLeftZero('00', to.getDate()+''); }; commom.getTomorrow = function (str) { let tom; if (str) { tom = new Date(str); } else { tom = new Date(); } tom.setDate(tom.getDate()+1); let M=Number(tom.getMonth())+1; return tom.getFullYear()+'-'+commom.padLeftZero('00', M+'')+'-'+commom.padLeftZero('00', tom.getDate()+''); }; commom.getWeekFirstDay = function () { let Nowdate=new Date(); let WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*86400000); let M=Number(WeekFirstDay.getMonth())+1; return WeekFirstDay.getFullYear()+'-'+commom.padLeftZero('00', M+'')+'-'+commom.padLeftZero('00', WeekFirstDay.getDate()+''); }; commom.getWeekLastDay = function () { let Nowdate=new Date(); let WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*86400000); let WeekLastDay=new Date((WeekFirstDay/1000+6*86400)*1000); let M=Number(WeekLastDay.getMonth())+1; return WeekLastDay.getFullYear()+'-'+commom.padLeftZero('00', M+'')+'-'+commom.padLeftZero('00', WeekLastDay.getDate()+''); }; commom.getMonthFirstDay = function () { let Nowdate=new Date(); let MonthFirstDay=new Date(Nowdate.getFullYear(),Nowdate.getMonth(),1); let M=Number(MonthFirstDay.getMonth())+1; return MonthFirstDay.getFullYear()+'-'+commom.padLeftZero('00', M+'')+'-'+commom.padLeftZero('00', MonthFirstDay.getDate()+''); }; commom.getMonthLastDay = function (str) { let Nowdate; if (str) { Nowdate = new Date(str); } else { Nowdate = new Date(); } let MonthNextFirstDay=new Date(Nowdate.getFullYear(),Nowdate.getMonth()+1,1); let MonthLastDay=new Date(MonthNextFirstDay-86400000); let M=Number(MonthLastDay.getMonth())+1; return MonthLastDay.getFullYear()+'-'+commom.padLeftZero('00', M+'')+'-'+commom.padLeftZero('00', MonthLastDay.getDate()+''); }; commom.getNextMonthFirstDay = function (str) { let Nowdate; if (str) { Nowdate = new Date(str); } else { Nowdate = new Date(); } let MonthNextFirstDay=new Date(Nowdate.getFullYear(),Nowdate.getMonth()+1,1); let M=Number(MonthNextFirstDay.getMonth())+1; return MonthNextFirstDay.getFullYear()+'-'+commom.padLeftZero('00', M+'')+'-'+commom.padLeftZero('00', MonthNextFirstDay.getDate()+''); }; commom.getPreMonthFirstDay = function (str) { let Nowdate; if (str) { Nowdate = new Date(str); } else { Nowdate = new Date(); } let MonthPreFirstDay=new Date(Nowdate.getFullYear(),Nowdate.getMonth()-1,1); let M=Number(MonthPreFirstDay.getMonth())+1; return MonthPreFirstDay.getFullYear()+'-'+commom.padLeftZero('00', M+'')+'-'+commom.padLeftZero('00', MonthPreFirstDay.getDate()+''); }; commom.getYearFirstDay = function () { let Nowdate=new Date(); return Nowdate.getFullYear()+'-01-01'; }; commom.getYearLastDay = function () { let Nowdate=new Date(); return Nowdate.getFullYear()+'-12-31'; }; commom.getWeek = function (str) { let Nowdate=new Date(str); return Nowdate.getDay(); }; /* commom.copy = function (data) { let obj = {}; obj = JSON.parse(JSON.stringify(data)); return obj; };*/ commom.copy = deepCopy; commom.getUrlParam = function (name) { let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)'); // 构造一个含有目标参数的正则表达式对象 let list = window.location.href.split('?'); let r = ''; if (list.length > 1) { r = list[1].match(reg); // 匹配目标参数 } if (!r) { if (list.length > 2) { r = list[2].match(reg); // 匹配目标参数 } } if (r) return decodeURI(r[2]); return ''; // 返回参数值 }; commom.formatNumber= function (value,style) { let precision = 0; if (!value) { value = '0'; } else { if (!isNaN(value)) value = value +''; } if (!style) { style = 'n'; } else { if (style.length === 2) { if (Number(style[1])) { precision = Number(style[1]); } } } let reData = Number(value).toFixed(precision); if (style.startsWith('c')) { let value2Array = reData.split('.'); let intPartFormat = ''; let floatPart = ''; if (value2Array.length > 0) { intPartFormat = value2Array[0].toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,'); } if (value2Array.length > 1) { floatPart = value2Array[1]; } if (floatPart) { reData = intPartFormat + '.' + floatPart; } else { reData = intPartFormat; } } if (style.startsWith('p')) { if (precision === 0) { precision = 2; } reData = (Number(value)*100).toFixed(precision); reData = reData + '%'; } return reData; }; commom.precisionNumber= function (value,style) { let precision = 0; if (!value) { value = '0'; } else { if (!isNaN(value)) value = value +''; } if (!style) { style = 'n'; } else { if (style.length === 2) { if (Number(style[1])) { precision = Number(style[1]); } if (style.startsWith('p')) { precision = precision + 2; } } } return Number(value).toFixed(precision); }; function randomHsl () { let H = Math.random(); let S = Math.random(); let L = Math.random(); return [H, S, L]; } function hslToRgb (hsl) { for (let i = 0; i < hsl.length; i++) { let H = hsl[i][0]; let S = hsl[i][1]; let L = hsl[i][2]; let R, G, B; if (+S === 0) { R = G = B = L; // 饱和度为0 为灰色 } else { let hue2Rgb = function (p, q, t) { if (t < 0) t += 1; if (t > 1) t -= 1; if (t < 1 / 6) return p + (q - p) * 6 * t; if (t < 1 / 2) return q; if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6; return p; }; let Q = L < 0.5 ? L * (1 + S) : L + S - L * S; let P = 2 * L - Q; R = hue2Rgb(P, Q, H + 1 / 3); G = hue2Rgb(P, Q, H); B = hue2Rgb(P, Q, H - 1 / 3); } hsl[i][0] = Math.round(R * 255); hsl[i][1] = Math.round(G * 255); hsl[i][2] = Math.round(B * 255); } } function colorHex (hsl) { let colorHex = []; let strHex = ''; for (let i = 0; i < hsl.length; i++) { let aColor = hsl[i]; strHex = '#'; for (let j = 0; j < aColor.length; j++) { let hex = Number(aColor[j]).toString(16); if (hex === '0') { hex += hex; } strHex += hex; } colorHex.push(strHex); } return colorHex; } function getHslArray (hslLength) { let HSL = []; for (let i = 0; i < hslLength; i++) { let ret = randomHsl(); // 颜色相邻颜色差异须大于 0.25 if (i > 0 && Math.abs(ret[0] - HSL[i - 1][0]) < 0.25) { i--; continue; // 重新获取随机色 } ret[1] = 0.7 + (ret[1] * 0.2); // [0.7 - 0.9] 排除过灰颜色 ret[2] = 0.4 + (ret[2] * 0.4); // [0.4 - 0.8] 排除过亮过暗色 // 数据转化到小数点后两位 ret = ret.map(function (item) { return parseFloat(item.toFixed(2)); }); HSL.push(ret); } hslToRgb(HSL); return colorHex(HSL); } commom.getColorList = function (old, size) { let num = Number(size) - old.length; if (num === 0) { return old; } let newcolor = getHslArray(num); let flag = false; for (;;) { flag = false; for (let i = 0; i < newcolor.length; i++) { for (let j = 0; j < old.length; j++) { if (newcolor[i] === old[j]) { flag = true; break; } } } if (!flag) { newcolor = old.concat(newcolor); break; } else { newcolor = getHslArray(num); } } return newcolor; }; commom.getColor = function () { let newcolor = getHslArray(1); return newcolor[0]; }; commom.getGlobalColor = function (n,isRandom,pro,that) { let newColor = []; if (n > 0) { let color = that.$Store.state.app.mychartColor[pro]; if (n > color.length) { if (isRandom) { newColor = that.$Method.getColorList(color,n); that.$nextTick(()=>{ that.$Store.commit('setMychartColor',pro,newColor); }); } else { newColor = color.concat(color.slice(0, n - color.length)); } } else { newColor = color.slice(0,n); } } return newColor; }; commom.getHeight = function(that) { // let height = window.innerHeight? window.innerHeight: document.documentElement.clientHeight? document.documentElement.clientHeight: screen.height; // return height; return that.$Store.state.app.clientHeight; }; commom.getWidth = function(that) { // let width = window.innerWidth? window.innerWidth: document.documentElement.clientWidth? document.documentElement.clientWidth: screen.width; // return width; return that.$Store.state.app.clientWidth; }; commom.getXDpi = function() { let xDpi = 0; if (window.screen.deviceXDPI) { xDpi = window.screen.deviceXDPI; } else { let tmpNode = document.createElement('DIV'); tmpNode.style.cssText = 'width:1in;height:1in;position:absolute;left:0px;top:0px;z-index:99;visibility:hidden'; document.body.appendChild(tmpNode); xDpi = parseInt(tmpNode.offsetWidth); document.body.removeChild(tmpNode); } return xDpi; } commom.getYDpi = function() { let yDpi = 0; if (window.screen.deviceXDPI) { yDpi = window.screen.deviceYDPI; } else { let tmpNode = document.createElement('DIV'); tmpNode.style.cssText = 'width:1in;height:1in;position:absolute;left:0px;top:0px;z-index:99;visibility:hidden'; document.body.appendChild(tmpNode); yDpi = parseInt(tmpNode.offsetHeight); document.body.removeChild(tmpNode); } return yDpi; } commom.mmToYpixe = function(value,ydpi) { let tmp = ydpi; if (!tmp) { tmp = commom.getYDpi();; } let y = (value / 25.4) * tmp; return Number(y.toFixed(2)); }, commom.mmToXpixe = function(value,xdpi) { let tmp = xdpi; if (!tmp) { tmp = commom.getXDpi();; } let x = (value / 25.4) * tmp; return Number(x.toFixed(2)); }, commom.xPixelToMm = function(value,xdpi) { let tmp = xdpi; if (!tmp) { tmp = commom.getXDpi();; } let xmm = (value / tmp) * 25.4; return Number(xmm.toFixed(2)); }, commom.yPixelToMm = function(value,ydpi) { let tmp = ydpi; if (!tmp) { tmp = commom.getYDpi();; } let ymm = (value / tmp) * 25.4; return Number(ymm.toFixed(2)); }, commom.unique = function (arr) { //定义常量 res,值为一个Map对象实例 const res = new Map(); //返回arr数组过滤后的结果,结果为一个数组 //过滤条件是,如果res中没有某个键,就设置这个键的值为1 return arr.filter((a) => !res.has(a) && res.set(a, 1)); }; commom.formatComboBox= function (value, config) { if (!value && value !== 0) { return value; } if (config.renderValue == undefined) { config.renderValue = {}; } if (config.renderValue[value]) { return config.renderValue[value]; } let result = value; if (config.data && config.data.length > 0) { let valueField = 'code'; let textField = 'name'; if (config.valueField) { valueField = config.valueField; } if (config.textField) { textField = config.textField; } const index = config.data.findIndex(item => item[valueField] === value); if (index > -1) { if (config.data[index][textField]) { if (config.codeWithName !== false) { result = '['+ value +']' + config.data[index][textField]; } else { result = config.data[index][textField]; } } } } config.renderValue[value] = result; return result; }; commom.formatDataBox = function (value,config) { if (!value) { return ''; } if (config.renderFormat == undefined) { config.renderFormat = 'yyyy-MM-dd'; if (config && config.format === undefined) { if (config.vtype === 'date') { config.renderFormat = 'yyyy-MM-dd'; } else if (config.vtype === 'datetime') { config.renderFormat = 'yyyy-MM-dd hh:mm:ss'; } else if (config.vtype ==='month') { config.renderFormat = 'yyyy-MM'; } } else { if (config.format) { config.renderFormat = config.format; } } } if (typeof value === 'string' || typeof value === 'number') { return commom.formatDate(new Date(value),config.renderFormat); } else if (value.constructor === Date){ return commom.formatDate(value,config.renderFormat); } else { return value; } }; commom.formatValue= function (value,row,config) { if (config) { if (config.textName) { if (row[config.textName]) { return row[config.textName]; } else { return value?value:''; } } if (config.type) { let type = config.type.toLowerCase(); if (type === 'textbox') { return value; } else if (type === 'numberbox') { return commom.formatNumber(value,config.format); } else if (type === 'combobox') { return commom.formatComboBox(value,config); } else if (type === 'datebox') { return commom.formatDataBox(value,config); } else { return value; } } else { return value; } } else { return value; } }; commom.getDefaultFieldStyle = function (column) { let tempStyle = ''; if (column.type === 'DateBox') { let format =''; if (column.format !== undefined) { format = column.format; } else { if (column.vtype === 'date') { format = 'yyyy-MM-dd'; } else if (column.vtype === 'datetime') { format = 'yyyy-MM-dd hh:mm:ss'; } else if (column.vtype ==='month') { format = 'yyyy-MM'; } else { format = 'yyyy-MM-dd'; } } tempStyle = tempStyle +'d' + '=' + format + '=center'; } if (column.type === 'NumberBox') { let format =''; if (column.format !== undefined) { format = column.format; } else { format = 'n2'; } tempStyle = tempStyle + format +'==right'; } return tempStyle; }; commom.getGridField = function(columns, separator ) { let fields = ''; for (let i = 0; i < columns.length; i++) { let column = columns[i]; if (column.visible !== false && column.isExport !== false) { if (column.name) { fields += column.name + separator + column.label; let style = ''; if (column.excel && column.excel.type) { style = column.excel.type; if (column.excel.format) { style += '=' + column.excel.format; } else { style += '=' ; } if (column.excel.align) { style += '=' + column.excel.align; } else { style += '=' ; } } else { style = commom.getDefaultFieldStyle(column); } if (style) { fields += separator + style + ','; } else { fields += ','; } } if (column.children) { for (let j = 0; j < column.children.length; j++) { let tempcolumn = column.children[j]; if (tempcolumn.name) { let header = ''; if (column.label) { header = column.label + '(' + tempcolumn.label + ')'; } else { header = tempcolumn.label; } fields += tempcolumn.name + separator + header; let style = ''; if (tempcolumn.excel && tempcolumn.excel.type) { style = tempcolumn.excel.type; if (tempcolumn.excel.format) { style += '=' + tempcolumn.excel.format; } else { style += '='; } if (tempcolumn.excel.align) { style += '=' + tempcolumn.excel.align; } else { style += '='; } } else { style = commom.getDefaultFieldStyle(tempcolumn); } if (style) { fields += separator + style + ','; } else { fields += ','; } } } } } } if (fields.length > 0) { fields = fields.substring(0, fields.length - 1); } return fields; }; commom.print = function(object,printCss) { let printStr = '<html><head>' + document.head.innerHTML +'</head>'; if (object) { printStr = printStr + '<body>' + object.$el.innerHTML +'</body></html>'; } else { printStr = printStr + '<body>' + document.body.innerHTML +'</body></html>'; } // 如果是本地测试,需要先新建Print.htm 如果是在域中使用,则不需要 let pwin = window.open('Print.htm', 'print'); pwin.document.write(printStr); if (printCss) { let styles=document.createElement('style'); styles.setAttribute('type','text/css');//media="print" styles.innerHTML = printCss; pwin.document.getElementsByTagName('head')[0].appendChild(styles); } pwin.document.close(); // 这句很重要,没有就无法实现 pwin.print(); pwin.close(); }; commom.openPage = function(modulecode,params) { let result = {}; result.modulecode = modulecode, result.params = params; window.parent.postMessage({data:result, type:'pageJump'}, '*'); }; commom.setLocalStorage = function(key,value, expire) { let obj = {}; obj.data = value; obj.time = Date.now(); obj.expire = expire; storage.set(key,JSON.stringify(obj)); }; commom.getLocalStorage = function(key) { let val = storage.get(key); if(!val){ return null; } val =JSON.parse(val); let expire = Number(val.expire) * 60 * 60 * 1000; if(Date.now()- val.time> expire){ storage.remove(key); return null; } return val.data; }; commom.getTextWidth = function(text) { const dom = document.createElement('span'); dom.style.display='inline-block;'; dom.textContent = text; document.body.appendChild(dom); const width = dom.offsetWidth; document.body.removeChild(dom); let resultWidth = Math.ceil(width/100) *100 + 40; return resultWidth; }; commom.exportExcel = exportExcel; commom.exportMultiSheetExcel = exportMultiSheetExcel; commom.exportExcelStyle = exportExcelStyle; export default commom;