216 lines
4.9 KiB
JavaScript
216 lines
4.9 KiB
JavaScript
/*
|
|
* @Descripttion:
|
|
* @Author: zenghua.wang
|
|
* @Date: 2022-02-23 21:12:37
|
|
* @LastEditors: zenghua.wang
|
|
* @LastEditTime: 2025-02-13 11:28:46
|
|
*/
|
|
import lodash from 'lodash';
|
|
import { Base64 } from 'js-base64';
|
|
import JSEncrypt from 'jsencrypt/bin/jsencrypt.min';
|
|
import { CONSTANTS } from '@/config';
|
|
|
|
/**
|
|
* @Title 判断是否 empty,返回ture
|
|
* @param {*} val:null 'null' undefined 'undefined' 0 '0' "" 返回true
|
|
* @returns
|
|
*/
|
|
export const isEmpty = (val) => {
|
|
if (val && parseInt(val) === 0) return false;
|
|
if (typeof val === 'undefined' || val === 'null' || val == null || val === 'undefined' || val === undefined || val === '') {
|
|
return true;
|
|
} else if (typeof val === 'object' && Object.keys(val).length === 0) {
|
|
return true;
|
|
} else if (val instanceof Array && val.length === 0) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
};
|
|
/**
|
|
* @Title 深度拷贝对象
|
|
* @param {*} obj
|
|
* @returns
|
|
*/
|
|
export const deepClone = (obj = {}) => {
|
|
return lodash.cloneDeep(obj);
|
|
};
|
|
/**
|
|
* @Title 将number转换为px
|
|
* @param {*} val
|
|
* @returns
|
|
*/
|
|
export const setPx = (val) => {
|
|
if (isEmpty(val)) return '';
|
|
val = val + '';
|
|
if (val.indexOf('%') === -1) {
|
|
val = val + 'px';
|
|
}
|
|
return val;
|
|
};
|
|
/**
|
|
* @Title 加密
|
|
* @param {*} n
|
|
* @returns
|
|
*/
|
|
export const encode = (n, flag = false) => {
|
|
if (flag) {
|
|
return (
|
|
((e) => {
|
|
let t = e.length.toString();
|
|
for (let n = 10 - t.length; n > 0; n--) t = '0' + t;
|
|
return t;
|
|
})(n) +
|
|
((e) => {
|
|
const t = Base64.encode(e).split('');
|
|
for (let n = 0; n < Math.floor(e.length / 100 + 1); n++) t.splice(100 * n + 1, 0, 3);
|
|
return t.join('');
|
|
})(n)
|
|
);
|
|
}
|
|
return n;
|
|
};
|
|
/**
|
|
* @Title 解密
|
|
* @param {*} e
|
|
* @returns
|
|
*/
|
|
export const decode = (e, flag = false) => {
|
|
if (flag) {
|
|
try {
|
|
const t = Number(e.substr(0, 10));
|
|
const n = e.substr(10).split('');
|
|
for (let i = 0, s = 0; s < Math.floor(t / 100) + 1; s++) {
|
|
n.splice(100 * s + 1 - i, 1);
|
|
i++;
|
|
}
|
|
const o = Base64.decode(n.join(''));
|
|
return o;
|
|
} catch (error) {
|
|
return e;
|
|
}
|
|
}
|
|
return e;
|
|
};
|
|
|
|
/**
|
|
* @Title 加密
|
|
* @param {*} txt
|
|
* @returns
|
|
*/
|
|
export const encrypt = (txt) => {
|
|
const encryptor = new JSEncrypt();
|
|
encryptor.setPublicKey(CONSTANTS.JS_PUBLICKEY); // 设置公钥
|
|
return encryptor.encrypt(txt); // 对数据进行加密
|
|
};
|
|
|
|
/**
|
|
* @Title 解密
|
|
* @param {*} txt
|
|
* @returns
|
|
*/
|
|
export const decrypt = (txt) => {
|
|
const encryptor = new JSEncrypt();
|
|
encryptor.setPrivateKey(CONSTANTS.JS_PRIVATEKEY); // 设置私钥
|
|
return encryptor.decrypt(txt); // 对数据进行解密
|
|
};
|
|
|
|
/**
|
|
* @Title 图片转base64
|
|
* @param {*} file
|
|
* @returns
|
|
*/
|
|
export const imageToBase64 = (file) => {
|
|
return new Promise((resolve, reject) => {
|
|
const reader = new FileReader();
|
|
reader.readAsDataURL(file);
|
|
reader.onload = () => {
|
|
resolve(reader.result);
|
|
};
|
|
reader.onerror = reject;
|
|
});
|
|
};
|
|
/**
|
|
* @Title bufferToBase64
|
|
* @param {*} buffer
|
|
* @returns
|
|
*/
|
|
export const bufferToBase64 = (buffer) => {
|
|
return 'data:image/jpeg;base64,' + window.btoa(new Uint8Array(buffer).reduce((data, byte) => data + String.fromCharCode(byte), ''));
|
|
};
|
|
/**
|
|
* @Title blob转json
|
|
* @param {*} file
|
|
* @returns
|
|
*/
|
|
export const blobToJSON = (blob) => {
|
|
return new Promise((resolve, reject) => {
|
|
const reader = new FileReader();
|
|
reader.readAsText(blob, 'utf-8');
|
|
reader.onload = () => {
|
|
const res = !isEmpty(reader.result) ? JSON.parse(reader.result) : reader.result;
|
|
resolve(res);
|
|
};
|
|
reader.onerror = reject;
|
|
});
|
|
};
|
|
/**
|
|
* @Title 将array转化为树
|
|
* @param tree
|
|
* @returns
|
|
*/
|
|
export const getTree = (tree = []) => {
|
|
tree.forEach((item) => {
|
|
delete item.children;
|
|
});
|
|
const map = {};
|
|
tree.forEach((item) => {
|
|
map[item.id] = item;
|
|
});
|
|
const arr = [];
|
|
tree.forEach((item) => {
|
|
const parent = map[item.parentId];
|
|
if (parent) {
|
|
(parent.children || (parent.children = [])).push(item);
|
|
} else {
|
|
arr.push(item);
|
|
}
|
|
});
|
|
return arr;
|
|
};
|
|
/**
|
|
* @Title 获取路由中的参数
|
|
* @param name
|
|
* @returns
|
|
*/
|
|
export const getUrlQuery = (name) => {
|
|
const url = window.location.href;
|
|
const hash = url.substring(url.indexOf('#') + 1);
|
|
const searchIndex = hash.indexOf('?');
|
|
const search = searchIndex !== -1 ? hash.substring(searchIndex + 1) : '';
|
|
const usp = new URLSearchParams(search);
|
|
return usp.get(name);
|
|
};
|
|
/**
|
|
* @Title 将Object参数转换为字符串
|
|
* @param {*} json
|
|
* @returns
|
|
*/
|
|
export const obj2Param = (json) => {
|
|
if (!json) return '';
|
|
return Object.keys(json)
|
|
.map((key) => {
|
|
if (isEmpty(json[key])) return '';
|
|
return encodeURIComponent(key) + '=' + encodeURIComponent(json[key]);
|
|
})
|
|
.join('&');
|
|
};
|
|
/**
|
|
* @Title 获取静态资源文件
|
|
* @param {*} url
|
|
* @returns
|
|
*/
|
|
export const getAssetsFile = (url) => {
|
|
return new URL(`../assets/${url}`, import.meta.url);
|
|
};
|