微信小程序工具函数

/**
 * 处理富文本里的图片宽度自适应
 * 1.去掉img标签里的style、width、height属性
 * 2.img标签添加style属性:max-width:100%;height:auto
 * 3.修改所有style里的width属性为max-width:100%
 * 4.去掉<br/>标签
 * 5.处理table固定宽度
 * @param html
 * @returns {void|string|*}
 */
const formatRichText = function (html) {
  let newContent = html;
  //图片处理
  newContent = newContent.replace(/<(img).*?(\/>|<\/img>|>)/g, function (mats) {
    if (mats.indexOf('style') < 0) {
      // mats = mats.replace(/<\s*img/, '<img style="max-width:100%;height:auto;"');
      mats = mats.replace(/<\s*img/, '<img style="max-width:100%;height:auto; display:block;"');//display:block;解决图片底部留白问题,如需添加
    } else {
      mats = mats.replace(/(max-)*width="[^"]*"/mg, '').replace(/(max-)*width='[^']*'/gi, '');
      mats = mats.replace(/(max-)*height="[^"]*"/mg, '').replace(/(max-)*height='[^']*'/gi, '');
      // mats = mats.replace(/style=("|')/mg, 'style=$1max-width:100%;height:auto;');
      mats = mats.replace(/style=("|')/mg, 'style=$1max-width:100%;height:auto; display:block;');//display:block;解决图片底部留白问题,如需添加
    }
    return mats;
  });
  //br处理
  newContent = newContent.replace(/<br[^>]*\/>/mg, '');
  //处理table固定宽度
  newContent = newContent.replace(/<table(.*?)width="\d+"\>/mg, '<table width="100%">');
  return newContent;
}
// 获取当前页面的路由及携带的参数
const getCurrentPageUrlWithArgs = function () {
  const pages = getCurrentPages();
  const currentPage = pages[pages.length - 1];
  const url = currentPage.route;
  const options = currentPage.options;
  let urlWithArgs = `/${url}?`;
  for (let key in options) {
    const value = options[key];
    urlWithArgs += `${key}=${value}&`;
  }
  urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length - 1);
  return urlWithArgs;
}
// 获取变量的实际类型,微信小程序暂不支持bigint
const getDataType = function (data) {
  let type_str = Object.prototype.toString.call(data);
  switch (type_str) {
    case '[object Number]'   : return 'number';
    case '[object String]'   : return 'string';
    case '[object Boolean]'  : return 'boolean';
    case '[object Undefined]': return 'undefined';
    case '[object Symbol]'   : return 'symbol';
    case '[object Object]'   : return 'object';
    case '[object Array]'    : return 'array';
    case '[object Function]' : return 'function';
    case '[object Null]'     : return 'null';
    case '[object BigInt]'   : return 'bigint';
    default                  : return '';
  }
}
// 空对象检查
const isEmpty = function (obj) {
  if (Object.prototype.toString.call(obj) == '[object Object]') {
    //getOwnPropertyNames -> ES5中如果参数不是一个原始对象类型,将抛出一个 TypeError 异常; ES2015中,非对象参数被强制转换为对象 。
    return  !Object.getOwnPropertyNames(obj).length &&  !Object.getOwnPropertySymbols(obj).length;
  } else {
    return false;
  }
}

// 空对象检查
const isEmptyObject = function (obj) {
  if ( JSON.stringify(obj) == '{}' ) return true;
  return false;
}
// 电话号码检查
const isPhone = function (phone) {
  if (!phone) return false;
  return /^1[3-9]\d{9}$/.test(phone);
}
//判断两个数组值是否都相同
const isSameArr = (arr, arr1) => {
  if (arr.length != arr1.length) return false;
  if (arr.length == arr1.length && arr.length == 0) return true;
  const temp_arr = [...arr1];
  return arr.every(val=>{
    const index = temp_arr.findIndex(v => v === val);
    return index >= 0 && temp_arr.splice(index, 1);
  })
}
/**
 * 数字保留指定位数
 * @param {*} number 
 * @param {*} precision 
 */
const round = function (number, precision) {
  return Math.round(+number + 'e' + precision) / Math.pow(10, precision);
  //return Number(Math.round(+number + 'e' + precision) + 'e-' + precision);
}
/**
 * 获取指定区间的随机整数
 * @param {*} min 
 * @param {*} max 
 */
const getRandomInt = function (min, max) {
  min = +min;
  max = +max;
  if (isNaN(min) || isNaN(max)) {
    return false;
  }
  min = Math.ceil(min);
  max = Math.floor(max);
  let diff = Math.abs(max - min);
  let true_min = min < max ? min : max;

  return Math.floor(Math.random() * diff) + true_min; //不含最大值,含最小值;需要含有最大值是 diff+1
}
//简单的统一返回数据结构
const resData = function (return_data) {
  //resData: response_data; 响应数据
  //ret_data: return_data; 返回数据
  return_data = return_data || {};
  let {
    code = 1, msg = 'ok', data = {}, ...extra
  } = return_data;
  let ret_data = {};
  ret_data.code = code;
  ret_data.msg = msg;
  ret_data.data = data;
  ret_data.extra = extra;
  return ret_data;
}
/**
 * 计算给定两经纬度之间的距离
 * @param {Decimal} longitude_start 起点经度
 * @param {Decimal} latitude_start  起点纬度
 * @param {Decimal} longitude_end 终点经度
 * @param {Decimal} latitude_end  终点纬度
 * @param {Number} unit        单位:1-米, 2-公里
 * @param {Number} precision   精度:保留小数位数
 */
const getDistance = function ({longitude_start=0, latitude_start=0, longitude_end=0, latitude_end=0, unit = 1, precision = 2}) {
  let EARTH_RADIUS = 6370.996; // 地球半径系数
  let PI = 3.1415926;

  let lat_start = latitude_start * PI / 180.0;
  let lat_end = latitude_end * PI / 180.0;

  let lng_start = longitude_start * PI / 180.0;
  let lng_end = longitude_end * PI / 180.0;

  let lat_diff = lat_start - lat_end;
  let lng_diff = lng_start - lng_end;

  let distance = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(lat_diff / 2), 2) + Math.cos(lat_start) * Math.cos(lat_end) * Math.pow(Math.sin(lng_diff / 2), 2)));
  distance = distance * EARTH_RADIUS * 1000;

  if (unit == 2) {
    distance = distance / 1000;
  }

  // return this.round(distance, precision);
  return Math.round(+distance + 'e' + precision) / Math.pow(10, precision);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值