123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- /**
- * 全局过滤器
- */
- // 格式化时间
- export function formatTime(date, format = 'YYYY-MM-DD HH:mm:ss') {
- if (!date) return '';
- date = new Date(date);
- const year = date.getFullYear();
- const month = date.getMonth() + 1;
- const day = date.getDate();
- const hour = date.getHours();
- const minute = date.getMinutes();
- const second = date.getSeconds();
- const formatNumber = n => {
- n = n.toString();
- return n[1] ? n : '0' + n;
- };
- return format
- .replace('YYYY', year)
- .replace('MM', formatNumber(month))
- .replace('DD', formatNumber(day))
- .replace('HH', formatNumber(hour))
- .replace('mm', formatNumber(minute))
- .replace('ss', formatNumber(second));
- }
- // 格式化数字,添加千分位
- export function formatNumber(num) {
- if (!num) return '0';
- return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
- }
- // 格式化文件大小
- export function formatFileSize(bytes) {
- if (!bytes) return '0 B';
- const k = 1024;
- const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
- const i = Math.floor(Math.log(bytes) / Math.log(k));
- return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
- }
- // 格式化手机号码
- export function formatPhone(phone) {
- if (!phone) return '';
- return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
- }
- // 格式化金额,保留两位小数
- export function formatMoney(money) {
- if (!money) return '0.00';
- return parseFloat(money).toFixed(2);
- }
- // 格式化百分比
- export function formatPercent(num) {
- if (!num) return '0%';
- return (num * 100).toFixed(2) + '%';
- }
- // 格式化时长(秒转时分秒)
- export function formatDuration(seconds) {
- if (!seconds) return '00:00';
- const hour = Math.floor(seconds / 3600);
- const minute = Math.floor((seconds % 3600) / 60);
- const second = seconds % 60;
-
- const formatNumber = n => {
- n = n.toString();
- return n[1] ? n : '0' + n;
- };
-
- if (hour > 0) {
- return `${formatNumber(hour)}:${formatNumber(minute)}:${formatNumber(second)}`;
- } else {
- return `${formatNumber(minute)}:${formatNumber(second)}`;
- }
- }
- // 过滤HTML标签
- export function filterHtml(html) {
- if (!html) return '';
- return html.replace(/<[^>]+>/g, '');
- }
- // 截取字符串
- export function cutString(str, length) {
- if (!str) return '';
- if (str.length <= length) return str;
- return str.substring(0, length) + '...';
- }
- // 格式化性别
- export function formatGender(gender) {
- if (gender === 1) return '男';
- if (gender === 2) return '女';
- return '未知';
- }
- // 格式化数字为k/w单位
- export function formatNumberToK(num) {
- if (!num) return '0';
- if (num < 1000) return num.toString();
- if (num < 10000) return (num / 1000).toFixed(1) + 'k';
- if (num < 100000) return (num / 10000).toFixed(1) + 'w';
- if (num < 1000000) return (num / 10000).toFixed(1) + 'w';
- if (num < 10000000) return (num / 1000000).toFixed(1) + 'kw';
- if (num < 100000000) return (num / 1000000).toFixed(1) + 'kw';
- return (num / 100000000).toFixed(1) + '亿';
- }
- // 导出所有过滤器
- export default {
- formatTime,
- formatNumber,
- formatFileSize,
- formatPhone,
- formatMoney,
- formatPercent,
- formatDuration,
- filterHtml,
- cutString,
- formatGender,
- formatNumberToK
- };
|