tool.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { getGdShopInfo } from "@/api/study.js";
  2. //防抖debounce代码:
  3. export function debounce(fn, delay = 500) {
  4. var timeout = null; // 创建一个标记用来存放定时器的返回值
  5. return function (e) {
  6. // 每当用户输入的时候把前一个 setTimeout clear 掉
  7. clearTimeout(timeout);
  8. // 然后又创建一个新的 setTimeout, 这样就能保证interval 间隔内如果时间持续触发,就不会执行 fn 函数
  9. timeout = setTimeout(() => {
  10. fn.apply(this, arguments);
  11. }, delay);
  12. };
  13. }
  14. // 返回两个经纬度之间的距离 单位米
  15. const earthDistance = function (location1, location2) {
  16. const lat1 = parseFloat(location1.lat);
  17. const lng1 = parseFloat(location1.lng);
  18. const lat2 = parseFloat(location2.lat);
  19. const lng2 = parseFloat(location2.lng);
  20. const EARTH_RADIUS = 6378137.0; //单位M
  21. const PI = Math.PI;
  22. function getRad(d) {
  23. return (d * PI) / 180.0;
  24. }
  25. let f = getRad((lat1 + lat2) / 2);
  26. let g = getRad((lat1 - lat2) / 2);
  27. let l = getRad((lng1 - lng2) / 2);
  28. let sg = Math.sin(g);
  29. let sl = Math.sin(l);
  30. let sf = Math.sin(f);
  31. let s, c, w, r, d, h1, h2;
  32. let a = EARTH_RADIUS;
  33. let fl = 1 / 298.257;
  34. sg = sg * sg;
  35. sl = sl * sl;
  36. sf = sf * sf;
  37. s = sg * (1 - sl) + (1 - sf) * sl;
  38. c = (1 - sg) * (1 - sl) + sf * sl;
  39. w = Math.atan(Math.sqrt(s / c));
  40. r = Math.sqrt(s * c) / w;
  41. d = 2 * w * a;
  42. h1 = (3 * r - 1) / 2 / c;
  43. h2 = (3 * r + 1) / 2 / s;
  44. return d * (1 + fl * (h1 * sf * (1 - sg) - h2 * (1 - sf) * sg));
  45. };
  46. //rpx转换px px=rpx * (屏幕宽度 / 750)
  47. export const rpxTopx = function (rpx) {
  48. let width = uni.getWindowInfo().screenWidth;
  49. let px = rpx * (width / 750);
  50. return px;
  51. };
  52. export const getNavHight = function (f) {
  53. //#ifndef H5 || MP-ALIPAY ||APP-PLUS
  54. //获取小程序胶囊的高度
  55. let { bottom, height } = uni.getMenuButtonBoundingClientRect();
  56. // this.height = height;
  57. this.navBareight = bottom;
  58. if (f) {
  59. return bottom;
  60. }
  61. return bottom + "px";
  62. //#endif
  63. };
  64. export const GDZShopiID = async function () {
  65. const res = await getGdShopInfo();
  66. if (res.state == "Success") {
  67. uni.setStorageSync("gdShopId", res.content.shopId);
  68. return res.content.shopId;
  69. }
  70. };