'-'
zhangwei
4 天以前 ddbf9504fc5faf6764fffa4e9263a3ad927331d8
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// 通用函数
// import useClipboard from "vue-clipboard3";
// import { ElMessage } from "element-plus";
import { formatDate } from "@/utils/formatTime";
 
export default function () {
  // 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 groupSeparator = (value: number, minimumFractionDigits: number = 2) => {
    return value.toLocaleString("en-US", {
      minimumFractionDigits: minimumFractionDigits,
      maximumFractionDigits: 2
    });
  };
 
  /**
   * 删除字符串首尾指定字符
   * @param Str 源字符
   * @param char 去除的指定字符
   * @param type 类型,右边或左边,为空是替换首尾
   */
  const trimChar = (Str: string, char: string, type: string) => {
    if (char) {
      if (type == "left") {
        return Str.replace(new RegExp("^\\" + char + "+", "g"), "");
      } else if (type == "right") {
        return Str.replace(new RegExp("\\" + char + "+$", "g"), "");
      }
      return Str.replace(
        new RegExp("^\\" + char + "+|\\" + char + "+$", "g"),
        ""
      );
    }
    return Str.replace(/^\s+|\s+$/g, "");
  };
  // 点击复制文本
  // const copyText = (text: string) => {
  //   return new Promise((resolve, reject) => {
  //     try {
  //       //复制
  //       toClipboard(text);
  //       //下面可以设置复制成功的提示框等操作
  //       ElMessage.success("复制成功");
  //       resolve(text);
  //     } catch (e) {
  //       //复制失败
  //       ElMessage.error("复制失败");
  //       reject(e);
  //     }
  //   });
  // };
  // 去掉Html标签(取前面5个字符)
  const removeHtmlSub = (value: string) => {
    const str = value.replace(/<[^>]+>/g, "");
    if (str.length > 50) return str.substring(0, 50) + "......";
    else return str;
  };
  // 去掉Html标签
  const removeHtml = (value: string) => {
    return value.replace(/<[^>]+>/g, "");
  };
  // 获取枚举描述
  const getEnumDesc = (key: any, lstEnum: any) => {
    return lstEnum.find((x: any) => x.value == key)?.describe;
  };
  // 追加query参数到url
  const appendQueryParams = (url: string, params: { [key: string]: any }) => {
    if (!params || Object.keys(params).length == 0) return url;
    const queryString = Object.keys(params)
      .map(
        key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`
      )
      .join("&");
    return `${url}${url.includes("?") ? "&" : "?"}${queryString}`;
  };
  return {
    percentFormat,
    dateFormatYMD,
    dateFormatYMDHMS,
    dateFormatHMS,
    scaleFormat,
    scale2Format,
    groupSeparator,
    copyText,
    removeHtmlSub,
    removeHtml,
    getEnumDesc,
    appendQueryParams,
    trimChar
  };
}