commonFunction.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. return {
  56. percentFormat,
  57. dateFormatYMD,
  58. dateFormatYMDHMS,
  59. dateFormatHMS,
  60. scaleFormat,
  61. scale2Format,
  62. copyText,
  63. };
  64. }