filters.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * 全局过滤器
  3. */
  4. // 格式化时间
  5. export function formatTime(date, format = 'YYYY-MM-DD HH:mm:ss') {
  6. if (!date) return '';
  7. date = new Date(date);
  8. const year = date.getFullYear();
  9. const month = date.getMonth() + 1;
  10. const day = date.getDate();
  11. const hour = date.getHours();
  12. const minute = date.getMinutes();
  13. const second = date.getSeconds();
  14. const formatNumber = n => {
  15. n = n.toString();
  16. return n[1] ? n : '0' + n;
  17. };
  18. return format
  19. .replace('YYYY', year)
  20. .replace('MM', formatNumber(month))
  21. .replace('DD', formatNumber(day))
  22. .replace('HH', formatNumber(hour))
  23. .replace('mm', formatNumber(minute))
  24. .replace('ss', formatNumber(second));
  25. }
  26. // 格式化数字,添加千分位
  27. export function formatNumber(num) {
  28. if (!num) return '0';
  29. return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  30. }
  31. // 格式化文件大小
  32. export function formatFileSize(bytes) {
  33. if (!bytes) return '0 B';
  34. const k = 1024;
  35. const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
  36. const i = Math.floor(Math.log(bytes) / Math.log(k));
  37. return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
  38. }
  39. // 格式化手机号码
  40. export function formatPhone(phone) {
  41. if (!phone) return '';
  42. return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
  43. }
  44. // 格式化金额,保留两位小数
  45. export function formatMoney(money) {
  46. if (!money) return '0.00';
  47. return parseFloat(money).toFixed(2);
  48. }
  49. // 格式化百分比
  50. export function formatPercent(num) {
  51. if (!num) return '0%';
  52. return (num * 100).toFixed(2) + '%';
  53. }
  54. // 格式化时长(秒转时分秒)
  55. export function formatDuration(seconds) {
  56. if (!seconds) return '00:00';
  57. const hour = Math.floor(seconds / 3600);
  58. const minute = Math.floor((seconds % 3600) / 60);
  59. const second = seconds % 60;
  60. const formatNumber = n => {
  61. n = n.toString();
  62. return n[1] ? n : '0' + n;
  63. };
  64. if (hour > 0) {
  65. return `${formatNumber(hour)}:${formatNumber(minute)}:${formatNumber(second)}`;
  66. } else {
  67. return `${formatNumber(minute)}:${formatNumber(second)}`;
  68. }
  69. }
  70. // 过滤HTML标签
  71. export function filterHtml(html) {
  72. if (!html) return '';
  73. return html.replace(/<[^>]+>/g, '');
  74. }
  75. // 截取字符串
  76. export function cutString(str, length) {
  77. if (!str) return '';
  78. if (str.length <= length) return str;
  79. return str.substring(0, length) + '...';
  80. }
  81. // 格式化性别
  82. export function formatGender(gender) {
  83. if (gender === 1) return '男';
  84. if (gender === 2) return '女';
  85. return '未知';
  86. }
  87. // 格式化数字为k/w单位
  88. export function formatNumberToK(num) {
  89. if (!num) return '0';
  90. if (num < 1000) return num.toString();
  91. if (num < 10000) return (num / 1000).toFixed(1) + 'k';
  92. if (num < 100000) return (num / 10000).toFixed(1) + 'w';
  93. if (num < 1000000) return (num / 10000).toFixed(1) + 'w';
  94. if (num < 10000000) return (num / 1000000).toFixed(1) + 'kw';
  95. if (num < 100000000) return (num / 1000000).toFixed(1) + 'kw';
  96. return (num / 100000000).toFixed(1) + '亿';
  97. }
  98. // 导出所有过滤器
  99. export default {
  100. formatTime,
  101. formatNumber,
  102. formatFileSize,
  103. formatPhone,
  104. formatMoney,
  105. formatPercent,
  106. formatDuration,
  107. filterHtml,
  108. cutString,
  109. formatGender,
  110. formatNumberToK
  111. };