add: 增加新功能

This commit is contained in:
gyq
2025-10-17 11:28:41 +08:00
parent 0405a0fb99
commit 44482d9dc9
30 changed files with 4695 additions and 507 deletions

View File

@@ -1,10 +1,11 @@
import { BigNumber } from "bignumber.js";
/**
* Check if an element has a class
* @param {HTMLElement} ele
* @param {string} cls
* @returns {boolean}
*/
export function hasClass(ele : HTMLElement, cls : string) {
export function hasClass(ele: HTMLElement, cls: string) {
return !!ele.className.match(new RegExp("(\\s|^)" + cls + "(\\s|$)"));
}
@@ -13,7 +14,7 @@ export function hasClass(ele : HTMLElement, cls : string) {
* @param {HTMLElement} ele
* @param {string} cls
*/
export function addClass(ele : HTMLElement, cls : string) {
export function addClass(ele: HTMLElement, cls: string) {
if (!hasClass(ele, cls)) ele.className += " " + cls;
}
@@ -22,7 +23,7 @@ export function addClass(ele : HTMLElement, cls : string) {
* @param {HTMLElement} ele
* @param {string} cls
*/
export function removeClass(ele : HTMLElement, cls : string) {
export function removeClass(ele: HTMLElement, cls: string) {
if (hasClass(ele, cls)) {
const reg = new RegExp("(\\s|^)" + cls + "(\\s|$)");
ele.className = ele.className.replace(reg, " ");
@@ -35,7 +36,7 @@ export function removeClass(ele : HTMLElement, cls : string) {
* @param {string} path
* @returns {Boolean}
*/
export function isExternal(path : string) {
export function isExternal(path: string) {
const isExternal = /^(https?:|http?:|mailto:|tel:)/.test(path);
return isExternal;
}
@@ -46,7 +47,7 @@ export function isExternal(path : string) {
* @param growthRate
* @returns
*/
export function formatGrowthRate(growthRate : number) {
export function formatGrowthRate(growthRate: number) {
if (growthRate === 0) {
return "-";
}
@@ -62,7 +63,7 @@ export function formatGrowthRate(growthRate : number) {
* @param {string} cFormat
* @returns {string}
*/
export function parseTime(time : string | number | Date | null, cFormat : string | undefined) {
export function parseTime(time: string | number | Date | null, cFormat: string | undefined) {
if (arguments.length === 0) {
return null;
}
@@ -90,7 +91,7 @@ export function parseTime(time : string | number | Date | null, cFormat : string
s: date.getSeconds(),
a: date.getDay()
};
const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key : keyof typeof formatObj) : string => {
const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key: keyof typeof formatObj): string => {
let value = formatObj[key];
// Note: getDay() returns 0 on Sunday
if (key === "a") {
@@ -105,7 +106,7 @@ export function parseTime(time : string | number | Date | null, cFormat : string
}
// 下载文件
export function downloadFile(obj : BlobPart, name : string, suffix : string, useUnix = true) {
export function downloadFile(obj: BlobPart, name: string, suffix: string, useUnix = true) {
const url = window.URL.createObjectURL(new Blob([obj]));
const link = document.createElement("a");
link.style.display = "none";
@@ -133,9 +134,9 @@ export function isSyncStatus() {
/**
* 判断是否有某权限
*/
export function hasPermission(params : any) {
export function hasPermission(params: any) {
let $PermissionObj = JSON.parse(localStorage.getItem("permission") || '[]')
const obj = $PermissionObj.find((v : any) => v == params || v == params)
const obj = $PermissionObj.find((v: any) => v == params || v == params)
if (obj) {
return obj
}
@@ -232,4 +233,30 @@ export function convertTimeToDate(timeStr, options = {}) {
// 5. 按指定格式返回日期字符串
return dateObj.format(format);
}
}
/**
* 乘法计算并格式化结果
* @param {string|number} num1 - 第一个乘数
* @param {string|number} num2 - 第二个乘数
* @returns {string} 保留两位小数的结果(不四舍五入,补零)
*/
export const multiplyAndFormat = (num1: any, num2: any = 1): string => {
try {
// 转换为BigNumber使用字符串构造避免精度问题
const bigNum1 = new BigNumber(num1.toString());
const bigNum2 = new BigNumber(num2.toString());
// 1. 乘法计算
const product = bigNum1.multipliedBy(bigNum2);
// 2. 截断到两位小数(不四舍五入)
const truncated = product.decimalPlaces(2, BigNumber.ROUND_DOWN);
// 3. 格式化保留两位小数(补零)
return truncated.toFixed(2);
} catch (error) {
console.error('计算错误:', error);
return '0.00'; // 出错时返回默认值
}
};