commonFunction.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // 通用函数
  2. import useClipboard from 'vue-clipboard3';
  3. import { ElMessage } from 'element-plus';
  4. import { formatDate } from '/@/utils/formatTime';
  5. import { useI18n } from 'vue-i18n';
  6. export default function () {
  7. const { t } = useI18n();
  8. const { toClipboard } = useClipboard();
  9. // 百分比格式化
  10. const percentFormat = (row: EmptyArrayType, column: number, cellValue: string) => {
  11. return cellValue ? `${cellValue}%` : '-';
  12. };
  13. // 列表日期时间格式化
  14. const dateFormatYMD = (row: EmptyArrayType, column: number, cellValue: string) => {
  15. if (!cellValue) return '-';
  16. return formatDate(new Date(cellValue), 'YYYY-mm-dd');
  17. };
  18. // 列表日期时间格式化
  19. const dateFormatYMDHMS = (row: EmptyArrayType, column: number, cellValue: string) => {
  20. if (!cellValue) return '-';
  21. return formatDate(new Date(cellValue), 'YYYY-mm-dd HH:MM:SS');
  22. };
  23. // 列表日期时间格式化
  24. const dateFormatHMS = (row: EmptyArrayType, column: number, cellValue: string) => {
  25. if (!cellValue) return '-';
  26. let time = 0;
  27. if (typeof row === 'number') time = row;
  28. if (typeof cellValue === 'number') time = cellValue;
  29. return formatDate(new Date(time * 1000), 'HH:MM:SS');
  30. };
  31. // 小数格式化
  32. const scaleFormat = (value: string = '0', scale: number = 4) => {
  33. return Number.parseFloat(value).toFixed(scale);
  34. };
  35. // 小数格式化
  36. const scale2Format = (value: string = '0') => {
  37. return Number.parseFloat(value).toFixed(2);
  38. };
  39. // 千分符,默认保留两位小数
  40. const groupSeparator = (value: number, minimumFractionDigits: number = 2) => {
  41. return value.toLocaleString('en-US', {
  42. minimumFractionDigits: minimumFractionDigits,
  43. maximumFractionDigits: 2,
  44. });
  45. };
  46. /**
  47. * 删除字符串首尾指定字符
  48. * @param Str 源字符
  49. * @param char 去除的指定字符
  50. * @param type 类型,右边或左边,为空是替换首尾
  51. */
  52. const trimChar =(Str:string,char:string, type:string) =>{
  53. if (char) {
  54. if (type == 'left') {
  55. return Str.replace(new RegExp('^\\'+char+'+', 'g'), '');
  56. } else if (type == 'right') {
  57. return Str.replace(new RegExp('\\'+char+'+$', 'g'), '');
  58. }
  59. return Str.replace(new RegExp('^\\'+char+'+|\\'+char+'+$', 'g'), '');
  60. }
  61. return Str.replace(/^\s+|\s+$/g, '');
  62. }
  63. // 点击复制文本
  64. const copyText = (text: string) => {
  65. return new Promise((resolve, reject) => {
  66. try {
  67. //复制
  68. toClipboard(text);
  69. //下面可以设置复制成功的提示框等操作
  70. ElMessage.success(t('message.layout.copyTextSuccess'));
  71. resolve(text);
  72. } catch (e) {
  73. //复制失败
  74. ElMessage.error(t('message.layout.copyTextError'));
  75. reject(e);
  76. }
  77. });
  78. };
  79. // 去掉Html标签(取前面5个字符)
  80. const removeHtmlSub = (value: string) => {
  81. var str = value.replace(/<[^>]+>/g, '');
  82. if (str.length > 50) return str.substring(0, 50) + '......';
  83. else return str;
  84. };
  85. // 去掉Html标签
  86. const removeHtml = (value: string) => {
  87. return value.replace(/<[^>]+>/g, '');
  88. };
  89. // 获取枚举描述
  90. const getEnumDesc = (key: any, lstEnum: any) => {
  91. return lstEnum.find((x: any) => x.value == key)?.describe;
  92. };
  93. // 追加query参数到url
  94. const appendQueryParams = (url: string, params: { [key : string]: any }) => {
  95. if (!params || Object.keys(params).length == 0) return url;
  96. const queryString = Object.keys(params).map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`).join('&');
  97. return `${url}${url.includes('?') ? '&' : '?'}${queryString}`;
  98. };
  99. return {
  100. percentFormat,
  101. dateFormatYMD,
  102. dateFormatYMDHMS,
  103. dateFormatHMS,
  104. scaleFormat,
  105. scale2Format,
  106. groupSeparator,
  107. copyText,
  108. removeHtmlSub,
  109. removeHtml,
  110. getEnumDesc,
  111. appendQueryParams,
  112. trimChar,
  113. };
  114. }