cosmix.js
// 本文件为工具类
'use strict';
var Base64 = {
// 转码表
table: [
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/'
],
UTF16ToUTF8: function (str) {
var res = [],
len = str.length;
for (var i = 0; i < len; i++) {
var code = str.charCodeAt(i);
var byte1, byte2;
if (code > 0x0000 && code <= 0x007F) {
// 单字节,这里并不考虑0x0000,因为它是空字节
// U+00000000 – U+0000007F 0xxxxxxx
res.push(str.charAt(i));
} else if (code >= 0x0080 && code <= 0x07FF) {
// 双字节
// U+00000080 – U+000007FF 110xxxxx 10xxxxxx
// 110xxxxx
byte1 = 0xC0 | ((code >> 6) & 0x1F);
// 10xxxxxx
byte2 = 0x80 | (code & 0x3F);
res.push(
String.fromCharCode(byte1),
String.fromCharCode(byte2)
);
} else if (code >= 0x0800 && code <= 0xFFFF) {
// 三字节
// U+00000800 – U+0000FFFF 1110xxxx 10xxxxxx 10xxxxxx
// 1110xxxx
byte1 = 0xE0 | ((code >> 12) & 0x0F);
// 10xxxxxx
byte2 = 0x80 | ((code >> 6) & 0x3F);
// 10xxxxxx
var byte3 = 0x80 | (code & 0x3F);
res.push(
String.fromCharCode(byte1),
String.fromCharCode(byte2),
String.fromCharCode(byte3)
);
} else if (code >= 0x00010000 && code <= 0x001FFFFF) {
// 四字节
// U+00010000 – U+001FFFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
} else if (code >= 0x00200000 && code <= 0x03FFFFFF) {
// 五字节
// U+00200000 – U+03FFFFFF 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
} else /** if (code >= 0x04000000 && code <= 0x7FFFFFFF)*/ {
// 六字节
// U+04000000 – U+7FFFFFFF 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
}
}
return res.join('');
},
UTF8ToUTF16: function (str) {
var res = [],
len = str.length;
for (var i = 0; i < len; i++) {
var code = str.charCodeAt(i);
// 对第一个字节进行判断
var code2, code3, byte1, byte2, utf16;
if (((code >> 7) & 0xFF) == 0x0) {
// 单字节
// 0xxxxxxx
res.push(str.charAt(i));
} else if (((code >> 5) & 0xFF) == 0x6) {
// 双字节
// 110xxxxx 10xxxxxx
code2 = str.charCodeAt(++i);
byte1 = (code & 0x1F) << 6;
byte2 = code2 & 0x3F;
utf16 = byte1 | byte2;
res.push(String.fromCharCode(utf16));
} else if (((code >> 4) & 0xFF) == 0xE) {
// 三字节
// 1110xxxx 10xxxxxx 10xxxxxx
code2 = str.charCodeAt(++i);
code3 = str.charCodeAt(++i);
byte1 = (code << 4) | ((code2 >> 2) & 0x0F);
byte2 = ((code2 & 0x03) << 6) | (code3 & 0x3F);
utf16 = ((byte1 & 0x00FF) << 8) | byte2;
res.push(String.fromCharCode(utf16));
} else if (((code >> 3) & 0xFF) == 0x1E) {
// 四字节
// 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
} else if (((code >> 2) & 0xFF) == 0x3E) {
// 五字节
// 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
} else /** if (((code >> 1) & 0xFF) == 0x7E)*/ {
// 六字节
// 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
}
}
return res.join('');
},
encode: function (str) {
if (!str) {
return '';
}
var utf8 = this.UTF16ToUTF8(str); // 转成UTF8
var i = 0; // 遍历索引
var len = utf8.length;
var res = [];
while (i < len) {
var c1 = utf8.charCodeAt(i++) & 0xFF;
res.push(this.table[c1 >> 2]);
// 需要补2个=
if (i == len) {
res.push(this.table[(c1 & 0x3) << 4]);
res.push('==');
break;
}
var c2 = utf8.charCodeAt(i++);
// 需要补1个=
if (i == len) {
res.push(this.table[((c1 & 0x3) << 4) | ((c2 >> 4) & 0x0F)]);
res.push(this.table[(c2 & 0x0F) << 2]);
res.push('=');
break;
}
var c3 = utf8.charCodeAt(i++);
res.push(this.table[((c1 & 0x3) << 4) | ((c2 >> 4) & 0x0F)]);
res.push(this.table[((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6)]);
res.push(this.table[c3 & 0x3F]);
}
return res.join('');
},
decode: function (str) {
if (!str) {
return '';
}
var len = str.length;
var i = 0;
var res = [];
while (i < len) {
var code1 = this.table.indexOf(str.charAt(i++));
var code2 = this.table.indexOf(str.charAt(i++));
var code3 = this.table.indexOf(str.charAt(i++));
var code4 = this.table.indexOf(str.charAt(i++));
var c1 = (code1 << 2) | (code2 >> 4);
var c2 = ((code2 & 0xF) << 4) | (code3 >> 2);
var c3 = ((code3 & 0x3) << 6) | code4;
res.push(String.fromCharCode(c1));
if (code3 != 64) {
res.push(String.fromCharCode(c2));
}
if (code4 != 64) {
res.push(String.fromCharCode(c3));
}
}
return this.UTF8ToUTF16(res.join(''));
}
}
var library = {
/**
* 使用循环的方式判断一个元素是否存在于一个数组中
* @param {Object} arr 数组
* @param {Object} value 元素值
*/
isInArray(array, value) {
return this.posInArray(array, value) >= 0;
},
posInArray(array, value) {
var hit = -1;
for (var i = 0; i < array.length; i++) {
if (typeof (array[i]) == 'string' || typeof (array[i]) == 'number') {
if (array[i] == value) {
hit = i;
break;
}
} else if (value == array[i].key) {
hit = i;
break;
}
}
return hit;
},
//判断字符串是否为JSON格式
isJSON(str) {
if (typeof str == 'string') {
try {
var obj=JSON.parse(str);
if(typeof obj == 'object' && obj ){
return true;
}else{
return false;
}
} catch(e) {
return false;
}
}
console.log('It is not a string!')
},
//将字符串转为Date格式
stringToDate(dateString) {
if (dateString) {
var arr1 = dateString.split(" ");
var sdate = arr1[0].split('-');
var date = new Date(sdate[0], sdate[1]-1, sdate[2]);
return date;
}
},
//判断字符串是否为时间格式
isDate(str){
// 是时间类型
if(str instanceof Date){
return true;
}else if(isNaN(str)&&!isNaN(Date.parse(str))){
return true;
}else{
return false;
}
},
//获取字符串长度
stringLength(strings) {
if (!this.isNull(strings)) {// 先判断是否为null,
return (strings + '').replace(/(^\s*)|(\s*$)/g, '').length
} else {
return 0;
}
},
//判断是否为数字
isNum(s) {
if (s != null && s != "" && typeof (s) != "undefined") {
return !isNaN(s);
}
return false;
},
//是否为NULL
isNull(value) {
if (value == null || typeof (value) == 'undefined' || value === undefined|| value === "undefined") {
return true;
}
return false;
},
//是否为空(包括空字符串、空格、null,{})
isEmpty(strings){
if (!this.isNull(strings)) { // 先判断是否为null,返回true ,判断是否为空字符串,返回true
// 空对象
if(strings instanceof Object){
if(JSON.stringify(strings)== "{}"){
return true
}
}
if ((strings + '').replace(/(^\s*)|(\s*$)/g, '').length === 0) { //已修正bug[当strings为数字时,会报strings.replace is not a function]
return true;
}
} else {
return true;
}
// 不为空返回false
return false;
},
/**
* 对象扁平化:只保留最里层key
* @obj obj
* @prekey ''
* @resobj {}
*/
plat(obj,prekey,resobj){
var comType=['object','array'];
prekey=prekey?prekey+'.':'';
var keys=Object.keys(obj);
keys.forEach(item=>{
var temp=obj[item];
var type=typeof temp;
if(temp && comType.indexOf(type) != -1){
this.plat(temp,prekey+item,resobj);
}else{
resobj[item]=temp;
}
})
return resobj
},
// 过滤删除json对象中为null/undefined/''/[]/{}的属性值
clearDeep(obj) {
if (!obj || !typeof obj === 'object') return
const keys = Object.keys(obj)
for (var key of keys) {
const val = obj[key]
if (typeof val === 'undefined' ||((typeof val === 'object' || typeof val === 'string') && !val)) {
delete obj[key]// 如属性值为null或undefined或'',则将该属性删除
} else if (typeof val === 'object') {
this.clearDeep(obj[key])// 属性值为对象,递归调用
if (Object.keys(obj[key]).length === 0) {
delete obj[key]// 如某属性的值为不包含任何属性的独享,则将该属性删除
}
}
}
},
// 动态修改导航栏的class样式
changeClass(obj,className) {
Object.getOwnPropertyNames(obj).forEach(function(key){
key==className?obj[key]=true:obj[key]=false
})
return obj
},
// 本地缓存
putData(key, value) { // 键 值
try {
if (typeof (Storage) != "undefined") { // 判断浏览器是否支持 localStorage ,不支持用cookie
localStorage.setItem(key, value);
} else {
throw "NoStorage";
}
} catch (oException) { }
},
// 获取缓存内容
getData(key, defaultvalue) { // defaultvalue 为数据获取不到或为空时默认的数据
let temp;
if (typeof (Storage) != "undefined") { // 判断浏览器是否支持 localStorage ,不支持用cookie
temp = localStorage.getItem(key);
}
return (temp != undefined && temp !== '') ? temp : defaultvalue;
},
// 清除缓存
clearData(key) {
if (typeof (Storage) != "undefined") {
localStorage.removeItem(key);
}
},
Base64,
encodeBase64(str) {
if (typeof (str) != 'string')
str = JSON.stringify(str);
str = this.Base64.encode(str);
return str;
},
decodeBase64(str) {
if (typeof (str) != 'string')
str = JSON.stringify(str);
if (this.isEmpty(str)) {
return '';
}
return this.Base64.decode(str);
},
//字符串编码(加密)
safeEncodeBase64(str) {
if (typeof (str) != 'string')
str = JSON.stringify(str);
str = this.Base64.encode(str);
var string = str.replace(/\+/g, "^").replace(/\//g, "_").replace(/=/g, "*");
return string;
},
//字符串解码(解密)
safeDecodeBase64(str) {
if (typeof (str) != 'string')
str = JSON.stringify(str);
var string = str.replace(/\^/g, "+").replace(/\_/g, "/").replace(/\*/g, "=");
return this.Base64.decode(string);
},
//获取url参数
getUrlParam(name) {
var reg = new RegExp(name + "=([^&]*)(&|$)");
var r = window.location.href.match(reg);
if (r != null) {
return r[1];
}
return '';
},
};
var cmx = {
fileOption: {
//限制文件格式为dll .DLL .xml .XML
beforeAvatarUpload(file) {
let fileName = file.name.substring(file.name.lastIndexOf(".") + 1);//获取文件后缀
const isDLL = fileName === "dll";
const isDLL1 = fileName === "DLL";
const isXML = fileName === "xml";
const isXML1 = fileName === "XML";
if (!isDLL &&!isDLL1 &&!isXML &&!isXML1 ) {
$message.error("上传文件只支持.dll、.DLL、.xml、.XML格式!");
}
return (
isDLL ||isDLL1 ||isXML ||isXML1
);
},
//限制文件格式为svg
svgFile(file) {
let fileName = file.name.substring(file.name.lastIndexOf(".") + 1);//获取文件后缀
const isSVG = fileName === "svg";
const isSVG1 = fileName === "SVG";
if (!isSVG &&!isSVG1) {
$message.error("上传文件只支持.svg、.SVG格式!");
}
return (
isSVG ||isSVG1
);
},
//限制文件格式为xml
xmlFile(file) {
let fileName = file.name.substring(file.name.lastIndexOf(".") + 1);//获取文件后缀
const isSVG = fileName === "xml";
const isSVG1 = fileName === "XML";
if (!isSVG &&!isSVG1) {
$message.error("上传文件只支持.xml.XML格式!");
}
return (
isSVG ||isSVG1
);
},
// 限制文件个数
handleExceed() {
$message.warning(`请最多上传 ${this.limit} 个文件。`);
},
// 限制上传两个同名文件且后缀不同的文件
onChangeUpload(file, fileList) {
var temp = 0;
fileList.forEach((item,idx) => {
let fileName=file.name.substring(0,file.name.indexOf("."));//获取文件名
let itemName=item.name.substring(0,item.name.indexOf("."));
if (file.name === item.name) {
temp++
if (temp === 2) {
$message.warning('请上传不同后缀的同名文件');
fileList.splice(idx, 1)
}
}
if (fileName!== itemName) {
$message.warning('请上传两个同名文件');
fileList.splice(1, 1)
}
})
},
// 移除文件
beforeRemove(file, fileList) {
return this.$confirm(`确定移除 ${file.name}?`);
},
},
dateTime:{
// 毫秒整数转化为时分秒
MillisecondToTime (msd) {
var time = parseFloat(msd) / 1000;
if (null != time && "" != time) {
if (time > 60 && time < 60 * 60) {
var a = parseInt(time / 60.0) < 10 ? ('0' + parseInt(time / 60.0)) : parseInt(time / 60.0);
var ms = parseInt((parseFloat(time / 60.0) - parseInt(time / 60.0)) * 60);
var b = (ms < 10) ? ('0' + ms) : ms;
time = '00:' + a + ':' + b;
}
else if (time >= 60 * 60 && time < 60 * 60 * 24) {
var H = parseInt(time / 3600.0) < 10 ? ('0' + parseInt(time / 3600.0)) : parseInt(time / 3600.0);
var ms = parseInt((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60);
var M = (ms < 10) ? ('0' + ms) : ms;
var ss = parseInt((parseFloat((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60) -
parseInt((parseFloat(time / 3600.0) - parseInt(time / 3600.0)) * 60)) * 60);
var S = (ss < 10) ? ('0' + ss) : ss;
time = H + ':' + M + ':' + S;
}
else {
var d = parseInt(time) < 10 ? ('0' + parseInt(time)) : parseInt(time);
time = '00:00:' + d;
}
}
return time;
},
},
scroll:{
// 滚动条定位到底部
scrollToBottom (that,id) {
that.$nextTick(() => {
let ele = document.getElementById(id);
if(!library.isNull(ele)){
ele.scrollTop = ele.scrollHeight;
}
})
},
}
}
var calculation={
/**
* 加法
* @param arg1
* @param arg2
*/
plus(arg1,arg2){
var r1,r2,m;
try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0};
try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0};
m=Math.pow(10,Math.max(r1,r2));
return (arg1*m+arg2*m)/m;
},
/**
* 减法
* @param arg1
* @param arg2
*/
subtract(arg1,arg2){
var r1,r2,m,n;
try{r1=arg1.toString().split(".")[1].length;}catch(e){r1=0;}
try{r2=arg2.toString().split(".")[1].length;}catch(e){r2=0;}
m=Math.pow(10,Math.max(r1,r2));
//动态控制精度长度
n=(r1>=r2)?r1:r2;
return ((arg1*m-arg2*m)/m).toFixed(n);
},
/***
* 乘法,获取精确乘法的结果值
* @param arg1
* @param arg2
*/
multiply(arg1,arg2){
var m=0,s1=arg1.toString(),s2=arg2.toString();
try{m+=s1.split(".")[1].length}catch(e){};
try{m+=s2.split(".")[1].length}catch(e){};
return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m);
},
/***
* 除法,获取精确乘法的结果值
* @param arg1
* @param arg2
*/
divide(arg1,arg2){
var t1=0,t2=0,r1,r2;
try{t1=arg1.toString().split(".")[1].length}catch(e){}
try{t2=arg2.toString().split(".")[1].length}catch(e){}
r1=Number(arg1.toString().replace(".",""));
r2=Number(arg2.toString().replace(".",""));
return (r1/r2)*Math.pow(10,t2-t1);
}
}
export {
cmx,
calculation,
library,
}
vue中 使用
import {library} from "@/Utils/cosmix";
if(library.isNum(ag)){
//.....
}
if(library.isNull(ag)){
//.....
}
//还有许多其他方法 , 请参考js文件中的注释