efview-plus
Version:
A high quality Service UI components Library with Vue.js
816 lines (779 loc) • 27.5 kB
JavaScript
import {deepCopy} from '../../utils/assist';
import storage from 'good-storage';
import {exportExcel, exportMultiSheetExcel, exportExcelStyle} from '../../static/excel/exportexcel';
import { client as EfuClient } from '../../utils/EfuMessenger.js'
window.EfuMessenger = new EfuClient()
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, notzero) {
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('p')) {
reData = (Number(value)*100).toFixed(precision);
}
let value2Array = reData.split('.');
let intPartFormat = '';
let floatPart = '';
if (value2Array.length > 0) {
intPartFormat = value2Array[0];
}
if (style.startsWith('c')) {
intPartFormat = intPartFormat.replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
}
if (value2Array.length > 1) {
floatPart = value2Array[1];
}
if (floatPart) {
let valueFloatPart =floatPart;
if (notzero) {
for (let i= floatPart.length - 1; i>=0; i--) {
if (Number(floatPart[i]) === 0) {
valueFloatPart = valueFloatPart.slice(0, -1);
} else {
break;
}
}
}
floatPart = valueFloatPart;
}
let result = '';
if (floatPart) {
result = intPartFormat + '.' + floatPart;
} else {
result = intPartFormat;
}
if (style.startsWith('p')) {
result = result + '%';
}
return result;
};
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 = config.data[index][textField];
} else {
result = '['+ value +']' + 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.type) {
let type = config.type.toLowerCase();
if (type === 'textbox') {
return value;
} else if (type === 'numberbox') {
if (!value) {
if (value === config.defaultValue) {
return value;
}
}
return commom.formatNumber(value,config.format,config.notzero);
} else if (type === 'combobox') {
return commom.formatComboBox(value,config);
} else if (type === 'datebox') {
return commom.formatDataBox(value,config);
} else {
if (config.textName) {
if (row[config.textName]) {
return row[config.textName];
} else {
return value?value:'';
}
}
return value;
}
} else {
return value;
}
} else {
return value;
}
};
commom.getDefaultFieldStyle = function (column,dictData,dictConfig) {
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';
}
if (column.type === 'ComboBox') {
let dictkey = column.dictkey;
if (!dictkey) {
dictkey = column.name;
}
let format ='t' + '=' + dictkey;
if (column.codeWithName === false) {
format = format + '=N';
} else {
format = format + '=Y';
}
tempStyle = tempStyle + format +'=left';
let valueField = 'code';
let textField = 'name'
if (column.valueField) {
valueField = column.valueField;
}
if (column.textField) {
textField = column.textField;
}
if (dictData[dictkey] && !dictConfig[dictkey]) {
dictConfig[dictkey] = [];
dictData[dictkey].forEach(el => {
let tmp = {};
tmp.code = el[valueField];
tmp.name = el[textField];
dictConfig[dictkey].push(tmp);
});
} else {
if (column.data != undefined) {
dictConfig[dictkey] = commom.copy(column.data);
}
}
}
return tempStyle;
};
commom.getGridField = function(columns, separator, dictData, dictConfig) {
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,dictData,dictConfig);
}
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,dictData,dictConfig);
}
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.EfuMessenger.openPortalTabByModuleCode(result)
// 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.changeMoneyToChinese = function(money) {
let cnNums = new Array('零','壹','贰','叁','肆','伍','陆','柒','捌','玖'); //汉字的数字
let cnIntRadice = new Array('','拾','佰','仟'); //基本单位
let cnIntUnits = new Array('','万','亿','兆'); //对应整数部分扩展单位
let cnDecUnits = new Array('角','分','毫','厘'); //对应小数部分单位
//let cnInteger = "整"; //整数金额时后面跟的字符
let cnIntLast = '元'; //整型完以后的单位
let cnIntLast_F = '负';
let maxNum = 999999999999999.9999; //最大处理的数字
let IntegerNum; //金额整数部分
let DecimalNum; //金额小数部分
let ChineseStr=''; //输出的中文金额字符串
let parts; //分离金额后用的数组,预定义
if(money == ''){
return '';
}
money = parseFloat(money);
if(money >= maxNum){
this.alert('超出最大处理数字');
return '';
}
if(money == 0){
//ChineseStr = cnNums[0]+cnIntLast+cnInteger;
ChineseStr = cnNums[0]+cnIntLast;
//document.getElementById("show").value=ChineseStr;
return ChineseStr;
}
//提供对负数的支持
if(money < 0){
ChineseStr = cnIntLast_F;
money = money*-1;
}
money = money.toString(); //转换为字符串
if(money.indexOf('.') === -1){
IntegerNum = money;
DecimalNum = '';
}else{
parts = money.split('.');
IntegerNum = parts[0];
DecimalNum = parts[1].substr(0,4);
}
if(parseInt(IntegerNum,10) > 0){//获取整型部分转换
let zeroCount = 0;
let IntLen = IntegerNum.length;
for(let i=0;i<IntLen;i++ ){
let n = IntegerNum.substr(i,1);
let p = IntLen - i - 1;
let q = p / 4;
let m = p % 4;
if( n == '0' ){
zeroCount++;
}else{
if( zeroCount > 0 ){
ChineseStr += cnNums[0];
}
zeroCount = 0; //归零
ChineseStr += cnNums[parseInt(n)]+cnIntRadice[m];
}
if( m==0 && zeroCount<4 ){
ChineseStr += cnIntUnits[q];
}
}
ChineseStr += cnIntLast;
//整型部分处理完毕
}
if(DecimalNum !== ''){//小数部分
let decLen = DecimalNum.length;
for(let i=0; i<decLen; i++){
let n = DecimalNum.substr(i,1);
if( n != '0' ){
ChineseStr += cnNums[Number(n)]+cnDecUnits[i];
}
}
}
if(ChineseStr === ''){
//ChineseStr += cnNums[0]+cnIntLast+cnInteger;
ChineseStr += cnNums[0]+cnIntLast;
}/* else if( DecimalNum == '' ){
ChineseStr += cnInteger;
ChineseStr += cnInteger;
} */
return ChineseStr;
},
commom.exportExcel = exportExcel;
commom.exportMultiSheetExcel = exportMultiSheetExcel;
commom.exportExcelStyle = exportExcelStyle;
export default commom;