utils项目规划全局方法

时间:2021-09-01
本文章向大家介绍utils项目规划全局方法,主要包括utils项目规划全局方法使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
/***
 * 验证是否微信客户端
 * @return {boolean} 可能值 true/false
 * */
export const isWeixin = (function () {
  const ua = window.navigator.userAgent.toLowerCase();
  return ua.match(/MicroMessenger/i) &&
    ua.match(/MicroMessenger/i)[0] === "micromessenger"
    ? true
    : false;
})();
/***
 * 时间戳转换普通时间格式
 * @param {number} now 属性名称 时间戳
 * */
export const formatDate = (now) => {
  var year = now.getFullYear();
  var month = now.getMonth() + 1;
  var date = now.getDate();
  var hour = now.getHours();
  var minute = now.getMinutes();
  return (
    year +
    "年" +
    timeFormat(month) +
    "月" +
    timeFormat(date) +
    "日 " +
    timeFormat(hour) +
    ":" +
    timeFormat(minute)
  );
};
/***
 * 验证手机号
 * @param {number} phone 属性名称 手机号
 * */
export const checkPhone = (phone) => {
  var reg_tel =
    /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/; //11位手机号码正则
  if (phone == "") {
    return false;
  } else if (!reg_tel.test(phone)) {
    return false;
  }
};
/***
 * 判断当前运行环境
 * @returns [string]可能值 ios / android / pc / other
 * */
export const os = (function () {
  var ua = navigator.userAgent.toLowerCase();
  return /(?:iphone|ipad|ipod)/.test(ua)
    ? "ios"
    : /(?:android|adr )/.test(ua)
    ? "android"
    : !/mobile/.test(ua)
    ? "pc"
    : "other";
})();
/**
 * 获取url上的指定属性的值
 * @param {String} name 属性名称
 * @param {String} url 指定或者当前页面地址
 */
export const getUrlParam = (name, url = location.href) => {
  let reg = new RegExp("(^|&)" + name + "=([^&#]*)(&|#|$)", "i");
  let r = (url.split("?")[1] || window.location.search.substr(1)).match(reg); // 获取地址"?"符后的字符串并正则匹配
  let context = "";
  if (r != null) {
    context = r[2];
  }
  reg = null;
  r = null;
  return context == null || context === "" || context === "undefined"
    ? ""
    : decodeURIComponent(context);
};
眼睛如果有等级,那么青色一定最高贵,黑色最深邃

原文地址:https://www.cnblogs.com/black-eyes/p/15215665.html