commonFunction.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 copyText = (text: string) => {
  41. return new Promise((resolve, reject) => {
  42. try {
  43. //复制
  44. toClipboard(text);
  45. //下面可以设置复制成功的提示框等操作
  46. ElMessage.success(t('message.layout.copyTextSuccess'));
  47. resolve(text);
  48. } catch (e) {
  49. //复制失败
  50. ElMessage.error(t('message.layout.copyTextError'));
  51. reject(e);
  52. }
  53. });
  54. };
  55. // 去掉Html标签(取前面5个字符)
  56. const removeHtmlSub = (value: string) => {
  57. var str = value.replace(/<[^>]+>/g, '');
  58. if (str.length > 50) return str.substring(0, 50) + '......';
  59. else return str;
  60. };
  61. // 去掉Html标签
  62. const removeHtml = (value: string) => {
  63. return value.replace(/<[^>]+>/g, '');
  64. };
  65. // 获取枚举描述
  66. const getEnumDesc = (key: any, lstEnum: any) => {
  67. return lstEnum.find((x: any) => x.value == key)?.describe;
  68. };
  69. return {
  70. percentFormat,
  71. dateFormatYMD,
  72. dateFormatYMDHMS,
  73. dateFormatHMS,
  74. scaleFormat,
  75. scale2Format,
  76. copyText,
  77. removeHtmlSub,
  78. removeHtml,
  79. getEnumDesc,
  80. };
  81. }