Vue3项目filter.js组件封装

1、element-plus(el-table)修改table的行样式

export function elTableRowClassName({
     row, rowIndex }) {
   
  if (rowIndex % 2 != 0) {
   
    return 'default-row'
  }
}

2、时间戳转换格式

export function parseTimeFilter(dateTime, dateType) {
   
  if (dateTime == '' || dateTime == undefined || dateTime == 0) {
   
    return '';
  }
  let date = new Date(parseInt(dateTime) * 1000);
  let Year = date.getFullYear();
  let Moth = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
  let Day = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate());
  let Hour = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours());
  let Minute = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes());
  let Sechond = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
  if (dateType === 'YYYY/MM/DD') {
   
    return Year + '/' + Moth + '/' + Day
  } else if (dateType === 'YYYY-MM-DD') {
   
    return Year + '-' + Moth + '-' + Day
  } else if (dateType === 'YYYY/MM/DD HH:MM:SS') {
   
    return Year + '/' + Moth + '/' + Day + '   ' + Hour + ':' + Minute + ':' + Sechond;
  } else if (dateType === 'YYYYMMDDHHMMSS') {
   
    return Year + '' + Moth + '' + Day + '' + Hour + '' + Minute + '' + Sechond;
  }
  return Year + '-' + Moth + '-' + Day + '   ' + Hour + ':' + Minute + ':' + Sechond;
}

3、对象中过滤掉空的对象或没用到的对象

export function filtrationObject(options, optionsArr) {
   
  for (let key in options) {
   
    if (optionsArr.indexOf(key) === -1 || options[key] === '') {
   
      delete options[key];
    }
  }
  return options;
}

export function filtrationObjectNull(options) {
   
  for (let key in options) {
   
    if (options[key] === '' || options[key] === null) {
   
      delete options[key];
    }
  }
  return options;
}

4、金额正则校验

export function amountRegularCheck(val) {
   
  let money = val.toString(); // 数值转成字符串
  money = money.replace(/[^\d.]+/, ''); // 禁止非数字和点
  money = money.replace(/^\./g, ''); // 禁止以点开头
  money = money.replace(/\.{2,}/g, '.'); // 禁止连两次输入点
  money = money.replace(/(\.)(\d*)(\1*)/g, "$1$2"); // 一个点后面禁止输入点
  money = money.replace(/^(-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); // 禁止小数超过两位
  money = money.replace(/^0+/, '0'); // 禁止开头连续输入两个0
  if (Number(money) >= 1) {
   
    money = money.replace(/^0+/, ''); // 输入字符大于等于 1 时剔除开头的 0 
  }
  return money;
}

5、随机生成字符串

const NUMS = '0123456789';
const NUMSANDLETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
export function randomStr(n, isPureNum = false) {
   
  let possible = isPureNum ? NUMS : NUMSANDLETTERS;
  let ret = '';
  for (let i = 0; i < n; i++) {
   
    ret += possible.charAt(Math.floor(Math.random() * possible.length));
  }
  return ret;
}

6、获取路由中的path

export function getUrlPath() {
   
  let href = location.href;
  let start = href.indexOf('/#') + 2;
  let end = href.indexOf('?');
  if (end === -1) {
   
    end = href.length;
  }
  return href.slice(start, end);
}

7、下载文件通用函数

// 示例:then(dowloadFile(filename))
export function downloadFile(filename, content) {
   
  const blob = new Blob([content]);
  const file = filename;
  if ('download' in document.createElement('a')) {
    // 非IE下载
    const elink = document.createElement('a');
    elink.download = filename;
    elink.style.display = 'none';
    elink.href = URL.createObjectURL(blob);
    document.body.appendChild(elink);
    elink.click();
    URL.revokeObjectURL(elink.href); // 释放URL 对象
    document.body.removeChild(elink);
  } else {
    // IE10+下载
    navigator.msSaveBlob(blob, file);
  }
}

8、判断小数是几位

// 获取类型
export function getType(obj) {
   
  return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase();
}

// 判断类型
export function isType(obj, type) {
   
  if (this.getType(type) !== 'string') {
   
    throw new Error('type param must be string in util.js');
  }
  return this.getType(obj) === type;
}

// 是否是number
export function isNumber(val) {
   
  return this.isType(val, 'number');
}

export function countDecimals(n) {
   
  if (!this.isNumber(n)) {
   
    n = parseFloat(n);
  }
  if (Math.floor(n) === n.valueOf()) {
   
    return 0;
  }
  return n.toString().split('.')[1].length;
}

相关推荐

  1. Vue3项目filter.js封装

    2023-12-15 21:48:03       38 阅读
  2. vue3.0-monaco封装

    2023-12-15 21:48:03       15 阅读
  3. Vue3封装svg

    2023-12-15 21:48:03       16 阅读
  4. vue3 封装一个通用echarts

    2023-12-15 21:48:03       40 阅读
  5. Vue3 封装Tree树形,且只支持单选

    2023-12-15 21:48:03       29 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-15 21:48:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-15 21:48:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-15 21:48:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-15 21:48:03       20 阅读

热门阅读

  1. 矩阵求逆(C语言)

    2023-12-15 21:48:03       36 阅读
  2. C语言:判断大端小端

    2023-12-15 21:48:03       41 阅读
  3. 微信小程序生成二维码海报并分享

    2023-12-15 21:48:03       40 阅读
  4. Unity3D 如何读取策划给定的Excel表格详解

    2023-12-15 21:48:03       42 阅读
  5. Next.js:前端开发的新篇章

    2023-12-15 21:48:03       33 阅读
  6. Linux vmstat命令:监控系统资源

    2023-12-15 21:48:03       35 阅读
  7. bug 记录

    2023-12-15 21:48:03       38 阅读
  8. C# Channel实现线程间通信

    2023-12-15 21:48:03       31 阅读
  9. MFC 调用.NET类库Com Dll,Activex Dll

    2023-12-15 21:48:03       41 阅读