var
wujingjing
2025-02-18 6789050c488dc2c41fa02f7b664df9f08dac8f7a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// 通用函数
import { ElMessage } from 'element-plus';
import useClipboard from 'vue-clipboard3';
import { useI18n } from 'vue-i18n';
import { formatDate } from '/@/utils/formatTime';
 
export default function () {
    const { t } = useI18n();
    const { toClipboard } = useClipboard();
 
    // 百分比格式化
    const percentFormat = (row: EmptyArrayType, column: number, cellValue: string) => {
        return cellValue ? `${cellValue}%` : '-';
    };
    // 列表日期时间格式化
    const dateFormatYMD = (row: EmptyArrayType, column: number, cellValue: string) => {
        if (!cellValue) return '-';
        return formatDate(new Date(cellValue), 'YYYY-mm-dd');
    };
    // 列表日期时间格式化
    const dateFormatYMDHMS = (row?: EmptyArrayType, column?: number, cellValue?: string) => {
        if (!cellValue) return '-';
        return formatDate(new Date(cellValue), 'YYYY-mm-dd HH:MM:SS');
    };
    // 列表日期时间格式化
    const dateFormatHMS = (row: EmptyArrayType, column: number, cellValue: string) => {
        if (!cellValue) return '-';
        let time = 0;
        if (typeof row === 'number') time = row;
        if (typeof cellValue === 'number') time = cellValue;
        return formatDate(new Date(time * 1000), 'HH:MM:SS');
    };
    // 小数格式化
    const scaleFormat = (value: string = '0', scale: number = 4) => {
        return Number.parseFloat(value).toFixed(scale);
    };
    // 小数格式化
    const scale2Format = (value: string = '0') => {
        return Number.parseFloat(value).toFixed(2);
    };
    //数据多大格式化(万)
    const formatValueToW = (value) => {
        return (value / 10000).toFixed(2) + 'w';
    };
    // 点击复制文本
    const copyText = (text: string) => {
        return new Promise((resolve, reject) => {
            try {
                //复制
                toClipboard(text);
                //下面可以设置复制成功的提示框等操作
                ElMessage.success('复制成功');
                resolve(text);
            } catch (e) {
                //复制失败
                ElMessage.error('复制失败');
                reject(e);
            }
        });
    };
    return {
        percentFormat,
        dateFormatYMD,
        dateFormatYMDHMS,
        dateFormatHMS,
        scaleFormat,
        scale2Format,
        copyText,
        formatValueToW,
    };
}