This commit is contained in:
Billy 2022-05-06 22:29:53 +08:00
parent f9609c5543
commit 3f66ed73f1
8 changed files with 236 additions and 16869 deletions

16919
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,45 @@
/*
* @Author: Billy
* @Date: 2022-03-15 15:18:55
* @LastEditors: Billy
* @LastEditTime: 2022-03-24 14:37:08
* @Description: 剪切板工具
*/
/**
* @description 把字符串粘贴到剪切板
* @param {string} text 要粘贴到剪切板的文字内容
*/
function copyStr(text) {
var aux = document.createElement("input");
aux.setAttribute("value", text);
document.body.appendChild(aux);
aux.select();
document.execCommand("copy");
document.body.removeChild(aux);
}
/**
* @description 把base64格式的图片粘贴到剪切板
* @param {string} base64 base64格式的图片
*/
async function copyImgBase64(base64) {
if (navigator.clipboard) {
const imgURL = base64;
const data = await fetch(imgURL);
const blob = await data.blob();
await navigator.clipboard.write([
new window.ClipboardItem({
[blob.type]: blob,
}),
]);
} else {
// 如果navigator.clipboard属性返回undefined就说明当前浏览器不支持这个 API
console.log('当前浏览器不支持 navigator.clipboard');
}
}
export default {
copyStr,
copyImgBase64
}

View File

@ -2,7 +2,7 @@
* @Author: Billy
* @Date: 2020-09-16 08:55:25
* @LastEditors: Billy
* @LastEditTime: 2021-06-06 01:59:30
* @LastEditTime: 2022-01-18 16:13:51
* @Description: 下载工具函数
*/
@ -12,7 +12,7 @@
* @param {string} fileName 下载的文件保存本地时的文件名(含后缀)
*/
function downloadByAnchorTag(filePath, fileName = '') {
let anchorTag = document.createElement('a');
const anchorTag = document.createElement('a');
anchorTag.setAttribute('href', filePath);
anchorTag.setAttribute('download', fileName);
anchorTag.click();

View File

@ -0,0 +1,59 @@
/*
* @Author: Billy
* @Date: 2020-09-20 01:12:40
* @LastEditors: Billy
* @LastEditTime: 2022-04-16 16:27:29
* @Description: 工具函数库
*/
const crypto = require("crypto");
// function ramdomStrEng(length = 10) {
// let s = '';
// for (let i = 0; i < length; i++)
// s += (Math.random() * 36 | 0).toString(36);
// return s;
// }
/**
* 生成uuid/guid
* @see https://stackoverflow.com/questions/105034/how-to-create-guid-uuid
* @returns {string} uuid 字符串
*/
function uuidv4() {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0,
v = c == "x" ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
/**
* 把字符串单向加密
* @param {string} str 需要加密的字符串
* @returns {string} 加密后的字符串
*/
function createHmac(str) {
const secret = "qwertyuiop";
return crypto.createHmac("sha256", secret).update(str).digest("hex");
}
/**
* 生成一个随机的n位字符串(位数越长越不可能重复)
* @param {number} n 位数
* @returns {string} 随机字符串
*/
function ramdomStr(n) {
if (n <= 50) {
let full = createHmac(String(Date.now()));
return full.slice(0, n);
} else {
throw new Error("位数n不能大于50");
}
}
module.exports = {
uuidv4,
createHmac,
ramdomStr
};

View File

@ -2,30 +2,28 @@
* @Author: Billy
* @Date: 2020-09-15 10:05:45
* @LastEditors: Billy
* @LastEditTime: 2021-10-18 15:16:42
* @LastEditTime: 2021-08-17 09:27:21
* @Description: 文件相关帮助方法
*/
/**
* 格式化输出文件尺寸
* @param {Number} size 文件尺寸的数字单位为Byte
* @param {Number} pointLength 小数点后的位数
* @param {Array} units 自定义单位数组
* 返回字符串如 1M
* 格式化输出文件大小
* @param {Number} size 文件大小(单位为字节)
* @param {Number} pointLength 小数点后的位数(默认2位)
* @returns {string} 返回 1M
* @todo 当结果大于1024TB时会出现XXX.XXundefined的结果即单位为undefined
*/
function formatFileSize(
size,
pointLength = 2,
units = ["B", "K", "M", "G", "TB"]
pointLength = 2
) {
// 格式化输出文件尺寸
const units = ["B", "KB", "MB", "G", "TB"];
let unit;
while ((unit = units.shift()) && size > 1024) {
while ((unit = units.shift()) && size >= 1024) {
size = size / 1024;
}
return (unit === "B" ? size : size.toFixed(pointLength)) + unit;
}
export default {
formatFileSize
};

27
src/util/InputOptimize.js Normal file
View File

@ -0,0 +1,27 @@
/*
* @Author: Billy
* @Date: 2022-03-17 11:49:58
* @LastEditors: Billy
* @LastEditTime: 2022-03-17 12:01:56
* @Description: 输入框优化函数
*/
/**
* @description 防抖函数
* @param {Function} fn 回调
* @param {number} delay 延迟时间(毫秒)
* @returns
*/
function debounce(fn, delay) {
let timerId;
return function (...args) {
if (timerId) clearTimeout(timerId);
timerId = setTimeout(() => {
fn.apply(this, args) // setTimout会发生this隐式丢失改变this指向为调用debounce所指的对象
}, delay);
}
}
export default {
debounce
}

View File

@ -2,20 +2,20 @@
* @Author: ChileeHong
* @Date: 2021-03-01 17:10:55
* @LastEditors: Billy
* @LastEditTime: 2022-01-07 14:23:39
* @Description: 安全的route.push方法
* @LastEditTime: 2022-02-24 11:02:28
* @Description: 安全的 route.push 方法
*/
import router from '../router/_index'
// 跟this.$router.push() 用法一致
function savePush(location, onComplete, onAbort) {
function safePush(location, onComplete, onAbort) {
const current = router.currentRoute;
const {
resolved
} = router.resolve(location);
// console.log('current.path :>> ', current.path);
// console.log(' resolved.path :>> ', resolved.path);
// console.log('resolved.path :>> ', resolved.path);
// console.log('current.query :>> ', current.query);
// console.log('resolved.query :>> ', resolved.query);
// console.log('current.params :>> ', current.params);
@ -41,4 +41,4 @@ function savePush(location, onComplete, onAbort) {
);
}
export default savePush
export default safePush

View File

@ -2,7 +2,7 @@
* @Author: Billy
* @Date: 2021-07-23 01:56:55
* @LastEditors: Billy
* @LastEditTime: 2022-01-04 09:54:37
* @LastEditTime: 2022-04-27 15:13:26
* @Description: 树状结构帮助方法(v1.1)
*/
@ -23,6 +23,20 @@ function treeNodesToArray(nodes = [], keyName = 'id', childAttrName = 'children'
return arr
}
/**
* @description 递归遍历树状数组
* @param {Array} nodes 树结构数组
* @param {string} childAttrName 子树节点数组的属性名
* @param {Function} func 回调方法(参数1: 节点参数2: 父节点)
*/
function recurveTree(nodes = [], childAttrName = "children", func, parentNode = null) {
for (let node of nodes) {
func(node, parentNode)
if (node[childAttrName] && node[childAttrName].length)
recurveTree(node[childAttrName], childAttrName, func, node)
}
}
/**
* @description 获取树结构数组内的某一层的所有元素的指定属性(N叉树的层序遍历)
* @param {Array} nodes 树结构数组
@ -92,7 +106,7 @@ function arrayToGroup(array, attrName) {
* @param {Function} func 操作函数(两个参数level:层级(从1开始)node:节点)
* @returns
*/
function setEveryLevelOfTree(nodes = [], childAttrName = "children", func) {
function loopEveryLevelOfTree(nodes = [], childAttrName = "children", func) {
if (!nodes) {
return;
}
@ -121,7 +135,8 @@ function setEveryLevelOfTree(nodes = [], childAttrName = "children", func) {
module.exports = {
treeNodesToArray,
recurveTree,
getLevelOfTree,
arrayToGroup,
setEveryLevelOfTree
loopEveryLevelOfTree
}