YZProjectCloud/yanzhu-ui-vue3/src/utils/index.js

443 lines
10 KiB
JavaScript
Raw Normal View History

2025-04-22 00:07:38 +08:00
import { parseTime } from "./ruoyi";
2024-08-17 12:11:19 +08:00
/**
* 表格时间格式化
*/
export function formatDate(cellValue) {
if (cellValue == null || cellValue == "") return "";
2025-04-22 00:07:38 +08:00
var date = new Date(cellValue);
var year = date.getFullYear();
var month =
date.getMonth() + 1 < 10
? "0" + (date.getMonth() + 1)
: date.getMonth() + 1;
var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
var minutes =
date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
var seconds =
date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
return (
year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds
);
2024-08-17 12:11:19 +08:00
}
/**
* @param {number} time
* @param {string} option
* @returns {string}
*/
export function formatTime(time, option) {
2025-04-22 00:07:38 +08:00
if (("" + time).length === 10) {
time = parseInt(time) * 1000;
2024-08-17 12:11:19 +08:00
} else {
2025-04-22 00:07:38 +08:00
time = +time;
2024-08-17 12:11:19 +08:00
}
2025-04-22 00:07:38 +08:00
const d = new Date(time);
const now = Date.now();
2024-08-17 12:11:19 +08:00
2025-04-22 00:07:38 +08:00
const diff = (now - d) / 1000;
2024-08-17 12:11:19 +08:00
if (diff < 30) {
2025-04-22 00:07:38 +08:00
return "刚刚";
2024-08-17 12:11:19 +08:00
} else if (diff < 3600) {
// less 1 hour
2025-04-22 00:07:38 +08:00
return Math.ceil(diff / 60) + "分钟前";
2024-08-17 12:11:19 +08:00
} else if (diff < 3600 * 24) {
2025-04-22 00:07:38 +08:00
return Math.ceil(diff / 3600) + "小时前";
2024-08-17 12:11:19 +08:00
} else if (diff < 3600 * 24 * 2) {
2025-04-22 00:07:38 +08:00
return "1天前";
2024-08-17 12:11:19 +08:00
}
if (option) {
2025-04-22 00:07:38 +08:00
return parseTime(time, option);
2024-08-17 12:11:19 +08:00
} else {
return (
d.getMonth() +
1 +
2025-04-22 00:07:38 +08:00
"月" +
2024-08-17 12:11:19 +08:00
d.getDate() +
2025-04-22 00:07:38 +08:00
"日" +
2024-08-17 12:11:19 +08:00
d.getHours() +
2025-04-22 00:07:38 +08:00
"时" +
2024-08-17 12:11:19 +08:00
d.getMinutes() +
2025-04-22 00:07:38 +08:00
"分"
);
2024-08-17 12:11:19 +08:00
}
}
/**
* @param {string} url
* @returns {Object}
*/
export function getQueryObject(url) {
2025-04-22 00:07:38 +08:00
url = url == null ? window.location.href : url;
const search = url.substring(url.lastIndexOf("?") + 1);
const obj = {};
const reg = /([^?&=]+)=([^?&=]*)/g;
2024-08-17 12:11:19 +08:00
search.replace(reg, (rs, $1, $2) => {
2025-04-22 00:07:38 +08:00
const name = decodeURIComponent($1);
let val = decodeURIComponent($2);
val = String(val);
obj[name] = val;
return rs;
});
return obj;
2024-08-17 12:11:19 +08:00
}
/**
* @param {string} input value
* @returns {number} output value
*/
export function byteLength(str) {
// returns the byte length of an utf8 string
2025-04-22 00:07:38 +08:00
let s = str.length;
2024-08-17 12:11:19 +08:00
for (var i = str.length - 1; i >= 0; i--) {
2025-04-22 00:07:38 +08:00
const code = str.charCodeAt(i);
if (code > 0x7f && code <= 0x7ff) s++;
else if (code > 0x7ff && code <= 0xffff) s += 2;
if (code >= 0xdc00 && code <= 0xdfff) i--;
2024-08-17 12:11:19 +08:00
}
2025-04-22 00:07:38 +08:00
return s;
2024-08-17 12:11:19 +08:00
}
/**
* @param {Array} actual
* @returns {Array}
*/
export function cleanArray(actual) {
2025-04-22 00:07:38 +08:00
const newArray = [];
2024-08-17 12:11:19 +08:00
for (let i = 0; i < actual.length; i++) {
if (actual[i]) {
2025-04-22 00:07:38 +08:00
newArray.push(actual[i]);
2024-08-17 12:11:19 +08:00
}
}
2025-04-22 00:07:38 +08:00
return newArray;
2024-08-17 12:11:19 +08:00
}
/**
* @param {Object} json
* @returns {Array}
*/
export function param(json) {
2025-04-22 00:07:38 +08:00
if (!json) return "";
2024-08-17 12:11:19 +08:00
return cleanArray(
2025-04-22 00:07:38 +08:00
Object.keys(json).map((key) => {
if (json[key] === undefined) return "";
return encodeURIComponent(key) + "=" + encodeURIComponent(json[key]);
2024-08-17 12:11:19 +08:00
})
2025-04-22 00:07:38 +08:00
).join("&");
2024-08-17 12:11:19 +08:00
}
/**
* @param {string} url
* @returns {Object}
*/
export function param2Obj(url) {
2025-04-22 00:07:38 +08:00
const search = decodeURIComponent(url.split("?")[1]).replace(/\+/g, " ");
2024-08-17 12:11:19 +08:00
if (!search) {
2025-04-22 00:07:38 +08:00
return {};
2024-08-17 12:11:19 +08:00
}
2025-04-22 00:07:38 +08:00
const obj = {};
const searchArr = search.split("&");
searchArr.forEach((v) => {
const index = v.indexOf("=");
2024-08-17 12:11:19 +08:00
if (index !== -1) {
2025-04-22 00:07:38 +08:00
const name = v.substring(0, index);
const val = v.substring(index + 1, v.length);
obj[name] = val;
2024-08-17 12:11:19 +08:00
}
2025-04-22 00:07:38 +08:00
});
return obj;
2024-08-17 12:11:19 +08:00
}
/**
* @param {string} val
* @returns {string}
*/
export function html2Text(val) {
2025-04-22 00:07:38 +08:00
const div = document.createElement("div");
div.innerHTML = val;
return div.textContent || div.innerText;
2024-08-17 12:11:19 +08:00
}
/**
* Merges two objects, giving the last one precedence
* @param {Object} target
* @param {(Object|Array)} source
* @returns {Object}
*/
export function objectMerge(target, source) {
2025-04-22 00:07:38 +08:00
if (typeof target !== "object") {
target = {};
2024-08-17 12:11:19 +08:00
}
if (Array.isArray(source)) {
2025-04-22 00:07:38 +08:00
return source.slice();
2024-08-17 12:11:19 +08:00
}
2025-04-22 00:07:38 +08:00
Object.keys(source).forEach((property) => {
const sourceProperty = source[property];
if (typeof sourceProperty === "object") {
target[property] = objectMerge(target[property], sourceProperty);
2024-08-17 12:11:19 +08:00
} else {
2025-04-22 00:07:38 +08:00
target[property] = sourceProperty;
2024-08-17 12:11:19 +08:00
}
2025-04-22 00:07:38 +08:00
});
return target;
2024-08-17 12:11:19 +08:00
}
/**
* @param {HTMLElement} element
* @param {string} className
*/
export function toggleClass(element, className) {
if (!element || !className) {
2025-04-22 00:07:38 +08:00
return;
2024-08-17 12:11:19 +08:00
}
2025-04-22 00:07:38 +08:00
let classString = element.className;
const nameIndex = classString.indexOf(className);
2024-08-17 12:11:19 +08:00
if (nameIndex === -1) {
2025-04-22 00:07:38 +08:00
classString += "" + className;
2024-08-17 12:11:19 +08:00
} else {
classString =
classString.substr(0, nameIndex) +
2025-04-22 00:07:38 +08:00
classString.substr(nameIndex + className.length);
2024-08-17 12:11:19 +08:00
}
2025-04-22 00:07:38 +08:00
element.className = classString;
2024-08-17 12:11:19 +08:00
}
/**
* @param {string} type
* @returns {Date}
*/
export function getTime(type) {
2025-04-22 00:07:38 +08:00
if (type === "start") {
return new Date().getTime() - 3600 * 1000 * 24 * 90;
2024-08-17 12:11:19 +08:00
} else {
2025-04-22 00:07:38 +08:00
return new Date(new Date().toDateString());
2024-08-17 12:11:19 +08:00
}
}
/**
* @param {Function} func
* @param {number} wait
* @param {boolean} immediate
* @return {*}
*/
export function debounce(func, wait, immediate) {
2025-04-22 00:07:38 +08:00
let timeout, args, context, timestamp, result;
2024-08-17 12:11:19 +08:00
2025-04-22 00:07:38 +08:00
const later = function () {
2024-08-17 12:11:19 +08:00
// 据上一次触发时间间隔
2025-04-22 00:07:38 +08:00
const last = +new Date() - timestamp;
2024-08-17 12:11:19 +08:00
// 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait
if (last < wait && last > 0) {
2025-04-22 00:07:38 +08:00
timeout = setTimeout(later, wait - last);
2024-08-17 12:11:19 +08:00
} else {
2025-04-22 00:07:38 +08:00
timeout = null;
2024-08-17 12:11:19 +08:00
// 如果设定为immediate===true因为开始边界已经调用过了此处无需调用
if (!immediate) {
2025-04-22 00:07:38 +08:00
result = func.apply(context, args);
if (!timeout) context = args = null;
2024-08-17 12:11:19 +08:00
}
}
2025-04-22 00:07:38 +08:00
};
2024-08-17 12:11:19 +08:00
2025-04-22 00:07:38 +08:00
return function (...args) {
context = this;
timestamp = +new Date();
const callNow = immediate && !timeout;
2024-08-17 12:11:19 +08:00
// 如果延时不存在,重新设定延时
2025-04-22 00:07:38 +08:00
if (!timeout) timeout = setTimeout(later, wait);
2024-08-17 12:11:19 +08:00
if (callNow) {
2025-04-22 00:07:38 +08:00
result = func.apply(context, args);
context = args = null;
2024-08-17 12:11:19 +08:00
}
2025-04-22 00:07:38 +08:00
return result;
};
2024-08-17 12:11:19 +08:00
}
/**
* This is just a simple version of deep copy
* Has a lot of edge cases bug
* If you want to use a perfect deep copy, use lodash's _.cloneDeep
* @param {Object} source
* @returns {Object}
*/
export function deepClone(source) {
2025-04-22 00:07:38 +08:00
if (!source && typeof source !== "object") {
throw new Error("error arguments", "deepClone");
2024-08-17 12:11:19 +08:00
}
2025-04-22 00:07:38 +08:00
const targetObj = source.constructor === Array ? [] : {};
Object.keys(source).forEach((keys) => {
if (source[keys] && typeof source[keys] === "object") {
targetObj[keys] = deepClone(source[keys]);
2024-08-17 12:11:19 +08:00
} else {
2025-04-22 00:07:38 +08:00
targetObj[keys] = source[keys];
2024-08-17 12:11:19 +08:00
}
2025-04-22 00:07:38 +08:00
});
return targetObj;
2024-08-17 12:11:19 +08:00
}
/**
* @param {Array} arr
* @returns {Array}
*/
export function uniqueArr(arr) {
2025-04-22 00:07:38 +08:00
return Array.from(new Set(arr));
2024-08-17 12:11:19 +08:00
}
/**
* @returns {string}
*/
export function createUniqueString() {
2025-04-22 00:07:38 +08:00
const timestamp = +new Date() + "";
const randomNum = parseInt((1 + Math.random()) * 65536) + "";
return (+(randomNum + timestamp)).toString(32);
2024-08-17 12:11:19 +08:00
}
/**
* Check if an element has a class
* @param {HTMLElement} elm
* @param {string} cls
* @returns {boolean}
*/
export function hasClass(ele, cls) {
2025-04-22 00:07:38 +08:00
return !!ele.className.match(new RegExp("(\\s|^)" + cls + "(\\s|$)"));
2024-08-17 12:11:19 +08:00
}
/**
* Add class to element
* @param {HTMLElement} elm
* @param {string} cls
*/
export function addClass(ele, cls) {
2025-04-22 00:07:38 +08:00
if (!hasClass(ele, cls)) ele.className += " " + cls;
2024-08-17 12:11:19 +08:00
}
/**
* Remove class from element
* @param {HTMLElement} elm
* @param {string} cls
*/
export function removeClass(ele, cls) {
if (hasClass(ele, cls)) {
2025-04-22 00:07:38 +08:00
const reg = new RegExp("(\\s|^)" + cls + "(\\s|$)");
ele.className = ele.className.replace(reg, " ");
2024-08-17 12:11:19 +08:00
}
}
export function makeMap(str, expectsLowerCase) {
2025-04-22 00:07:38 +08:00
const map = Object.create(null);
const list = str.split(",");
2024-08-17 12:11:19 +08:00
for (let i = 0; i < list.length; i++) {
2025-04-22 00:07:38 +08:00
map[list[i]] = true;
2024-08-17 12:11:19 +08:00
}
2025-04-22 00:07:38 +08:00
return expectsLowerCase ? (val) => map[val.toLowerCase()] : (val) => map[val];
2024-08-17 12:11:19 +08:00
}
2025-04-22 00:07:38 +08:00
export const exportDefault = "export default ";
2024-08-17 12:11:19 +08:00
export const beautifierConf = {
html: {
2025-04-22 00:07:38 +08:00
indent_size: "2",
indent_char: " ",
max_preserve_newlines: "-1",
2024-08-17 12:11:19 +08:00
preserve_newlines: false,
keep_array_indentation: false,
break_chained_methods: false,
2025-04-22 00:07:38 +08:00
indent_scripts: "separate",
brace_style: "end-expand",
2024-08-17 12:11:19 +08:00
space_before_conditional: true,
unescape_strings: false,
jslint_happy: false,
end_with_newline: true,
2025-04-22 00:07:38 +08:00
wrap_line_length: "110",
2024-08-17 12:11:19 +08:00
indent_inner_html: true,
comma_first: false,
e4x: true,
2025-04-22 00:07:38 +08:00
indent_empty_lines: true,
2024-08-17 12:11:19 +08:00
},
js: {
2025-04-22 00:07:38 +08:00
indent_size: "2",
indent_char: " ",
max_preserve_newlines: "-1",
2024-08-17 12:11:19 +08:00
preserve_newlines: false,
keep_array_indentation: false,
break_chained_methods: false,
2025-04-22 00:07:38 +08:00
indent_scripts: "normal",
brace_style: "end-expand",
2024-08-17 12:11:19 +08:00
space_before_conditional: true,
unescape_strings: false,
jslint_happy: true,
end_with_newline: true,
2025-04-22 00:07:38 +08:00
wrap_line_length: "110",
2024-08-17 12:11:19 +08:00
indent_inner_html: true,
comma_first: false,
e4x: true,
2025-04-22 00:07:38 +08:00
indent_empty_lines: true,
},
};
2024-08-17 12:11:19 +08:00
// 首字母大小
export function titleCase(str) {
2025-04-22 00:07:38 +08:00
return str.replace(/( |^)[a-z]/g, (L) => L.toUpperCase());
2024-08-17 12:11:19 +08:00
}
// 下划转驼峰
export function camelCase(str) {
2025-04-22 00:07:38 +08:00
return str.replace(/_[a-z]/g, (str1) => str1.substr(-1).toUpperCase());
2024-08-17 12:11:19 +08:00
}
export function isNumberStr(str) {
2025-04-22 00:07:38 +08:00
return /^[+-]?(0|([1-9]\d*))(\.\d+)?$/g.test(str);
}
export function fileSize(filesize) {
if (filesize == null || filesize === "") {
return "0 Bytes";
}
var unitArr = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
var index = 0;
var srcsize = parseFloat(filesize);
index = Math.floor(Math.log(srcsize) / Math.log(1024));
var size = srcsize / Math.pow(1024, index);
size = size.toFixed(2); // 保留两位小数
return size + " " + unitArr[index];
2024-08-17 12:11:19 +08:00
}
2025-06-30 15:34:40 +08:00
export function MergeArray(arr1, arr2) {
var _arr = new Array();
for (var i = 0; i < arr1.length; i++) {
_arr.push(arr1[i]);
}
for (var i = 0; i < arr2.length; i++) {
var flag = true;
for (var j = 0; j < arr1.length; j++) {
if (arr2[i] == arr1[j]) {
flag = false;
break;
}
}
if (flag) {
_arr.push(arr2[i]);
}
}
return _arr;
}
export function DelArray(array1, array2) {
var result = [];
for (var i = 0; i < array1.length; i++) {
var k = 0;
for (var j = 0; j < array2.length; j++) {
if (array1[i] != array2[j]) {
k++;
if (k == array2.length) {
result.push(array1[i]);
}
}
}
}
return result;
}