utils此文件下存放自己封装的工具类函数(常用的方法)



/**
 * @name: newDate 新建一个日期
 * @param num年龄
 * @return: 2020/01/22
 */
export const newDate = (num) => {
  const newDate = new Date();
  const newYear = parseInt(newDate.getFullYear() - num);
  let month = newDate.getMonth() + 1;
  let day = newDate.getDate();
  if (parseInt(month) < 10) {
    month = "0" + month;
  }
  if (parseInt(day) < 10) {
    day = "0" + day;
  }
  if (month == "02") {
    day = day > 28 ? "28" : day
  }
  return newYear + "/" + month + "/" + day;
}

/**
 * @name: dataFormat 日期格式化
 * @param {date: Tue Mar 10 2020 00:00:00 GMT+0800 (中国标准时间)} 
 * @return: 2020/03/10
 */
export const dateFormat = (date) => {
  const year = date.getFullYear();
  let month = date.getMonth() + 1;
  if (parseInt(month) < 10) {
    month = "0" + month;
  }
  let day = date.getDate();
  if (parseInt(day) < 10) {
    day = "0" + day;
  }
  return year + "/" + month + "/" + day;
}

/**
 * @name: dateInterval 年龄区间
 * @param {max最大年龄,min最小年龄},callback回调函数
 * @return: {max 最年轻的日期,min 最老的日期}
 */
export const dateInterval = ({
  max = 60,
  min = 0
}, callback) => {
  const newDate = new Date();
  const maxYear = newDate.getFullYear() - max - 1;
  const month = newDate.getMonth() + 1;
  const day = newDate.getDate();
  const minDate = new Date(maxYear + "/" + month + "/" + day);
  let maxDate;
  if (min > 0) {
    const minYear = newDate.getFullYear() - min;
    maxdate = new Date(minYear + "/" + month + "/" + day);
    maxDate = new Date(maxdate.getTime() - 24 * 60 * 60 * 1000);
  } else {
    maxDate = new Date(newDate.getTime() - 30 * 24 * 60 * 60 * 1000)
  }

  callback && callback({
    max: maxDate,
    min: new Date(minDate.getTime() + 24 * 60 * 60 * 1000)
  })

  return {
    max: maxDate,
    min: new Date(minDate.getTime() + 24 * 60 * 60 * 1000)
  }
}

/**
 * @name: getAgeByBirth 根据出生日期获取年龄
 * @param birthday "2020/01/22" 
 * @return: age
 */
export const getAgeByBirth = (birthday) => {
  if (!(/^(\d{4})(\/)(\d{2})(\/)(\d{2})$/.test(birthday))) {
    return "";
  }

  let age;
  const year = birthday.substring(0, birthday.indexOf("/")) * 1;
  const month = birthday.substring(birthday.indexOf("/") + 1, birthday.lastIndexOf("/")) * 1;
  const day = birthday.substring(birthday.lastIndexOf("/") + 1) * 1;
  const date = new Date();
  const currentYear = date.getFullYear();
  const currentMonth = date.getMonth() + 1;
  const cuttentDay = date.getDate();

  if (month > currentMonth) {
    age = currentYear - year - 1;
  } else if (month < currentMonth) {
    age = currentYear - year;
  } else {
    if (day > cuttentDay) {
      age = currentYear - year - 1;
    } else {
      age = currentYear - year;
    }
  }

  return age > 0 ? age : 0;

}

/**
 * @name: getBirthByIdNo 根据身份证号码获取出生日期
 * @param iIdNo 身份证号码 
 * @return: 2020/01/22
 */
export const getBirthByIdNo = (iIdNo) => {
  let tmpStr = "";
  if (iIdNo.length == 15) {
    tmpStr = iIdNo.substring(6, 12);
    tmpStr = "19" + tmpStr;
    tmpStr = tmpStr.substring(0, 4) + "/" + tmpStr.substring(4, 6) + "/" + tmpStr.substring(6);
  } else {
    tmpStr = iIdNo.substring(6, 14);
    tmpStr = tmpStr.substring(0, 4) + "/" + tmpStr.substring(4, 6) + "/" + tmpStr.substring(6);
  }
  return tmpStr;
}


/**
 * @name: getQueryUrlParamVal
 * @param  name 地址栏参数key
 * @return: 有返回value,没有返回""
 */
export const getQueryUrlParamVal = (name) => {
  // 如果链接没有参数,或者链接中不存在我们要获取的参数,直接返回空
  if (location.href.indexOf("?") == -1 || location.href.indexOf(name + '=') == -1) {
    return '';
  }
  // 获取链接中参数部分
  var queryString = location.href.substring(location.href.indexOf("?") + 1);
  queryString = decodeURI(queryString);

  // 分离参数对 ?key=value&key2=value2
  var parameters = queryString.split("&");
  var pos, paraName, paraValue;
  for (var i = 0; i < parameters.length; i++) {
    // 获取等号位置
    pos = parameters[i].indexOf('=');
    if (pos == -1) {
      continue;
    }
    // 获取name 和 value
    paraName = parameters[i].substring(0, pos);
    paraValue = parameters[i].substring(pos + 1);
    // 如果查询的name等于当前name,就返回当前值,同时,将链接中的+号还原成空格
    if (paraName == name) {
      return unescape(paraValue.replace(/\+/g, " "));
    }
  }
  return '';
}

/**
 * @name: trimNull 判断传入的值是不是存在且不等于null和undefind
 * @param val
 * @return:  val 或者 ''
 */
export const trimNull = (val) => {
  if (val && val != "null" && val != "undefind") {
    return val;
  }
  return "";
}

/**
 * @name: maleOrFemalByIdCard 根据身份证号码判断男女
 * @param idCard身份证号码
 * @return: 1男,2女
 */
export const maleOrFemalByIdCard = (idCard) => {
  idCard = idCard.replace(/\s/g, ""); // 对身份证号码做处理。包括字符间有空格。
  if (idCard.length == 15) {
    if (idCard.substring(14, 15) % 2 == 0) {
      return '2';
    } else {
      return '1';
    }
  } else if (idCard.length == 18) {
    if (idCard.substring(14, 17) % 2 == 0) {
      return '2';
    } else {
      return '1';
    }
  } else {
    return null;
  }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 TypeScript 中,可以使用 `import()` 方法来实现动态导入图片。下面是一个简单的工具类示例,封装了动态导入图片的方法: ```typescript /** * 动态导入图片 * @param imageUrl 图片的 URL * @returns Promise 返回一个包含图片元素的 Promise 对象 */ export async function loadImage(imageUrl: string): Promise<HTMLImageElement> { const image = new Image(); return new Promise((resolve, reject) => { // 设置图片加载成功的回调函数 image.onload = () => { resolve(image); }; // 设置图片加载失败的回调函数 image.onerror = reject; // 开始加载图片 image.src = imageUrl; }); } ``` 这个工具类中的 `loadImage` 方法接收一个图片 URL 参数,并返回一个包含图片元素的 Promise 对象。在方法内部,使用 `new Image()` 创建一个新的图片元素,然后为 `onload` 和 `onerror` 事件分别设置加载成功和加载失败的回调函数。最后,调用 `image.src` 开始加载图片。 使用该工具类,可以在项目中方便地动态导入图片,例如: ```typescript import { loadImage } from './utils'; async function loadAndShowImage() { const imageUrl = 'https://example.com/image.png'; const image = await loadImage(imageUrl); document.body.appendChild(image); } loadAndShowImage(); ``` 在上述代码中,我们使用 `loadImage` 方法动态导入一张图片,并将其添加到页面中。注意,在使用 `await` 关键字调用 `loadImage` 方法时,需要将其放在 `async` 函数中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值