util.js 3.28 KB
Newer Older
朱国瑞's avatar
朱国瑞 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
export function calcAdapt() {
  const maxScale = 2.16
  const minScale = 1.78
  const dot = 2
  const { scale } = getSystemInfo()
  return {
    calcCoord: function ({ minTop, maxTop }, currentScale = scale) {
      if (!currentScale) return {
        top: maxTop
      }
      const s = +((currentScale - minScale) / (maxScale - minScale)).toFixed(dot)
      const top = (maxTop - minTop) * s + minTop
      return {
        top
      }
    },
    calc: function ({
      minWidth, minHeight, maxWidth, maxHeight
    }, currentScale = scale) {
      if (!currentScale) return
      // 1.78 
      // 2.16
      let width, height;
      // (minWidth / maxWidth)
      // 比例因子
      const s = +((currentScale - minScale) / (maxScale - minScale)).toFixed(dot)
      width = (maxWidth - minWidth) * s + minWidth
      height = (maxHeight - minHeight) * s + minHeight
      return {
        width,
        height,
      }
    }
  }
}

export const isBigScreen = () => {
朱国瑞's avatar
朱国瑞 committed
38 39 40 41 42 43 44
  let { scale } = getSystemInfo();
  if (scale > 2) return true
  return false
}
export const isSmallScreen = () => {
  let { scale } = getSystemInfo();
  if (scale < 1) return true
朱国瑞's avatar
朱国瑞 committed
45 46 47 48 49 50
  return false
}

export const getSystemInfo = () => {
  let width = 0;
  let height = 0;
朱国瑞's avatar
朱国瑞 committed
51
  if (!isMobile()) {
朱国瑞's avatar
朱国瑞 committed
52 53
    width = 375;
    height = document.body.clientHeight > 667 ? document.body.clientHeight : 667;
朱国瑞's avatar
朱国瑞 committed
54 55
  } else {
    width = document.body.clientWidth;
朱国瑞's avatar
朱国瑞 committed
56
    height = document.body.clientHeight;
朱国瑞's avatar
朱国瑞 committed
57
  }
朱国瑞's avatar
朱国瑞 committed
58

朱国瑞's avatar
朱国瑞 committed
59 60
  if (height === 0) {
    height = window.innerHeight;
朱国瑞's avatar
朱国瑞 committed
61
    if (!isMobile()) {
朱国瑞's avatar
朱国瑞 committed
62
      height = window.innerHeight > 667 ? window.innerHeight : 667;
朱国瑞's avatar
朱国瑞 committed
63
    }
朱国瑞's avatar
朱国瑞 committed
64 65
  }
  let scale = height / width;
朱国瑞's avatar
朱国瑞 committed
66
  scale = parseFloat(scale.toFixed(2))
朱国瑞's avatar
朱国瑞 committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
  return {
    windowWidth: width,
    windowHeight: height,
    screenWidth: width,
    screenHeight: height,
    scale: scale
  }
}
export const authCamera = (lying) => {
  const that = this;
  return new Promise((resolve, reject) => {
    if (navigator.mediaDevices === undefined) {
      navigator.mediaDevices = {};
    }

    if (navigator.mediaDevices.getUserMedia === undefined) {
      navigator.mediaDevices.getUserMedia = function (constraints) {
        // 首先获取现存的getUserMedia(如果存在)
        let getUserMedia =
          navigator.webkitGetUserMedia ||
          navigator.mozGetUserMedia ||
          navigator.getUserMedia;
        if (!getUserMedia) {
          return Promise.reject(
            new Error("getUserMedia is not implemented in this browser")
          );
        }
        return new Promise(function (resolve, reject) {
          getUserMedia.call(navigator, constraints, resolve, reject);
        });
      };
    }

    const constraints = {
      audio: false,
      video: {
        transform: "scaleX(-1)"
      }
    };

    navigator.mediaDevices
      .getUserMedia(constraints)
      .then(function (stream) {
        resolve({ camera: 1, lying })
      })
      .catch(err => {
        console.log("没有开启摄像头权限或浏览器版本不兼容");
        console.log('获取用户授权信息失败')
朱国瑞's avatar
朱国瑞 committed
115
        resolve({ camera: 0, lying })
朱国瑞's avatar
朱国瑞 committed
116 117
      });
  })
朱国瑞's avatar
朱国瑞 committed
118 119 120 121 122 123 124
}

export const isMobile = () => {
  let flag = navigator.userAgent.match(
    /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
  );
  return flag;
朱国瑞's avatar
朱国瑞 committed
125
}