Vue3项目filter.js组件封装

2023-12-14 11:36:28

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;
}

文章来源:https://blog.csdn.net/DHLSP15/article/details/134987597
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。