import { getGdShopInfo } from "@/api/study.js";

//防抖debounce代码：
export function debounce(fn, delay = 500) {
  var timeout = null; // 创建一个标记用来存放定时器的返回值
  return function (e) {
    // 每当用户输入的时候把前一个 setTimeout clear 掉
    clearTimeout(timeout);
    // 然后又创建一个新的 setTimeout, 这样就能保证interval 间隔内如果时间持续触发，就不会执行 fn 函数
    timeout = setTimeout(() => {
      fn.apply(this, arguments);
    }, delay);
  };
}

// 返回两个经纬度之间的距离 单位米
const earthDistance = function (location1, location2) {
  const lat1 = parseFloat(location1.lat);
  const lng1 = parseFloat(location1.lng);
  const lat2 = parseFloat(location2.lat);
  const lng2 = parseFloat(location2.lng);

  const EARTH_RADIUS = 6378137.0; //单位M
  const PI = Math.PI;

  function getRad(d) {
    return (d * PI) / 180.0;
  }

  let f = getRad((lat1 + lat2) / 2);
  let g = getRad((lat1 - lat2) / 2);
  let l = getRad((lng1 - lng2) / 2);

  let sg = Math.sin(g);
  let sl = Math.sin(l);
  let sf = Math.sin(f);

  let s, c, w, r, d, h1, h2;
  let a = EARTH_RADIUS;
  let fl = 1 / 298.257;

  sg = sg * sg;
  sl = sl * sl;
  sf = sf * sf;

  s = sg * (1 - sl) + (1 - sf) * sl;
  c = (1 - sg) * (1 - sl) + sf * sl;

  w = Math.atan(Math.sqrt(s / c));
  r = Math.sqrt(s * c) / w;
  d = 2 * w * a;
  h1 = (3 * r - 1) / 2 / c;
  h2 = (3 * r + 1) / 2 / s;

  return d * (1 + fl * (h1 * sf * (1 - sg) - h2 * (1 - sf) * sg));
};

//rpx转换px  px=rpx * (屏幕宽度 / 750)
export const rpxTopx = function (rpx) {
  let width = uni.getWindowInfo().screenWidth;
  let px = rpx * (width / 750);
  return px;
};

export const getNavHight = function (f) {
  //#ifndef H5 || MP-ALIPAY ||APP-PLUS
  //获取小程序胶囊的高度
  let { bottom, height } = uni.getMenuButtonBoundingClientRect();
  // this.height = height;
  this.navBareight = bottom;
  if (f) {
    return bottom;
  }
  return bottom + "px";
  //#endif
};

export const GDZShopiID = async function () {
  const res = await getGdShopInfo();
  if (res.state == "Success") {
    uni.setStorageSync("gdShopId", res.content.shopId);
		return res.content.shopId;
  }
};
