2025-06-27 18:37:42 +08:00
|
|
|
/*
|
|
|
|
|
* @Author: RenXiaoDong
|
|
|
|
|
* @Date: 2025-06-27 17:36:31
|
|
|
|
|
*/
|
2025-06-28 15:28:54 +08:00
|
|
|
import dayjs from 'dayjs';
|
2025-07-16 10:14:04 +08:00
|
|
|
|
2025-06-27 18:37:42 +08:00
|
|
|
export function toFixed(num: number | string, n: number): number {
|
|
|
|
|
return parseFloat(parseFloat(num.toString()).toFixed(n));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function isNotData(n: number): boolean {
|
|
|
|
|
if (n === undefined) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return n === -2147483648;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function splitNumber(num: number): string | number {
|
|
|
|
|
if (!num) {
|
|
|
|
|
return num;
|
|
|
|
|
}
|
|
|
|
|
const parts = num.toString().split('.');
|
|
|
|
|
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
|
|
|
|
return parts.join('.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function formatNumberShow(...args: any[]): string | number {
|
|
|
|
|
const [_args] = args;
|
|
|
|
|
const { value, len = 2, split = true, showExactValue = false } = typeof _args === 'object' ? _args : { value: _args };
|
|
|
|
|
const getNumber = (value: number) => {
|
|
|
|
|
return split ? splitNumber(value) : value;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (isNotData(value)) {
|
|
|
|
|
return '-';
|
|
|
|
|
}
|
|
|
|
|
if (value < 0) {
|
|
|
|
|
return `-${formatNumberShow({
|
|
|
|
|
value: -value,
|
|
|
|
|
len,
|
|
|
|
|
split,
|
|
|
|
|
showExactValue,
|
|
|
|
|
})}`;
|
|
|
|
|
}
|
|
|
|
|
if (showExactValue) {
|
|
|
|
|
return getNumber(toFixed(value, len));
|
|
|
|
|
}
|
|
|
|
|
if (value < 10000) {
|
|
|
|
|
return getNumber(toFixed(value, len));
|
|
|
|
|
} else if (value < 100000000) {
|
|
|
|
|
const _n = Math.round((value / 10000) * 100) / 100;
|
|
|
|
|
return split ? `${splitNumber(_n)}w` : `${toFixed(_n, len)}w`;
|
|
|
|
|
} else {
|
|
|
|
|
const _n = Math.round((value / 100000000) * 100) / 100;
|
|
|
|
|
|
|
|
|
|
return split ? `${splitNumber(_n)}亿` : `${toFixed(_n, len)}亿`;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-06-28 11:31:49 +08:00
|
|
|
|
|
|
|
|
export function formatTableField(fieldItem: any, rowValue: any, showExactValue = false) {
|
|
|
|
|
// 获取嵌套属性值的函数
|
|
|
|
|
const getNestedValue = (obj: any, path: string) => {
|
|
|
|
|
if (!obj || !path) return undefined;
|
|
|
|
|
|
|
|
|
|
// 如果路径包含点号,说明是链式取值
|
|
|
|
|
if (path.includes('.')) {
|
|
|
|
|
return path.split('.').reduce((current, key) => {
|
|
|
|
|
return current && current[key] !== undefined ? current[key] : undefined;
|
|
|
|
|
}, obj);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 普通属性取值
|
|
|
|
|
return obj[path];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const _getValue = (value: any) => {
|
2025-07-02 17:55:20 +08:00
|
|
|
if (!isNumber(value)) return value || '-';
|
2025-06-28 11:31:49 +08:00
|
|
|
return formatNumberShow({ value, showExactValue });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 使用链式取值获取数据
|
|
|
|
|
const rawValue = getNestedValue(rowValue, fieldItem.dataIndex);
|
|
|
|
|
const value = _getValue(rawValue ?? '-');
|
|
|
|
|
|
|
|
|
|
return `${fieldItem.prefix || ''}${value}${fieldItem.suffix || ''}`;
|
|
|
|
|
}
|
2025-06-28 15:28:54 +08:00
|
|
|
|
|
|
|
|
export function exactFormatTime(val: number, curYearFmt = 'MM-DD HH:mm', otherYearFmt = 'YYYY-MM-DD HH:mm') {
|
|
|
|
|
if (!val) return '-';
|
|
|
|
|
const year = dayjs(val * 1000).year();
|
|
|
|
|
const currYear = dayjs().year();
|
|
|
|
|
const diff = year - currYear;
|
|
|
|
|
const fmt = diff === 0 ? curYearFmt : otherYearFmt;
|
|
|
|
|
return dayjs(val * 1000).format(fmt);
|
|
|
|
|
}
|
2025-07-04 16:05:45 +08:00
|
|
|
|
|
|
|
|
// 导出文件
|
|
|
|
|
export function downloadByUrl(url: string, filename?: string) {
|
|
|
|
|
const a = document.createElement('a');
|
|
|
|
|
a.href = url;
|
|
|
|
|
if (filename) {
|
|
|
|
|
a.download = filename;
|
|
|
|
|
}
|
|
|
|
|
a.style.display = 'none';
|
|
|
|
|
document.body.appendChild(a);
|
|
|
|
|
a.click();
|
|
|
|
|
document.body.removeChild(a);
|
|
|
|
|
}
|
2025-07-17 17:23:40 +08:00
|
|
|
|