open-app.js 3.61 KB
function queryString() {
  var vars = {},
    hash,
    i;
  var hashes = window.location.search.slice(1).split('&');

  for (i = 0; i < hashes.length; i++) {
    hash = hashes[i].split('=');
    vars[decodeURIComponent(hash[0])] = decodeURIComponent(hash[1]);
  }
  return vars;
}

const getAppPath = () => {
  let params = queryString();

  let openbyYohobuy = params['openby:yohobuy'] || '';

  let appPath = '';

  if (openbyYohobuy) {
    appPath = 'yohobuy://yohobuy.com/goapp?openby:yohobuy=' + openbyYohobuy;
  } else {
    appPath =
      document.getElementById('main-wrap').dataset.apppath ||
      'yohobuy://yohobuy.com/goapp?openby:yohobuy={"action":"go.home","params":{"gender":"1","channel":"2"}}';
  }

  return appPath;
};

const u = navigator.userAgent;
// eslint-disable-next-line no-unused-vars
const isFromYOHO = /m\.yohobuy\.com/i.test(document.referrer);
const isApp =
  /yoho/i.test(u) ||
  !!window.yohoInterface ||
  /app_version=/i.test(location.search) ||
  /openrefer=/i.test(location.search);

const isiOS = /(iPhone|iPad|iPod|iOS)/i.test(u); // ios终端
const isAndroid = /Android/i.test(u); // android终端
const isWechat = /MicroMessenger/i.test(navigator.userAgent);
const isMobileQQ = /MQQBrowser.+QQ/i.test(navigator.userAgent);
const isWechatDevtool = /wechatdevtools/i.test(u); // 微信开发者工具
// eslint-disable-next-line no-unused-vars
const nodownload = document.getElementById('no-download'); // 页面不需要下载

const getIOSVersion = () => {
  const verion = navigator.appVersion.match(/OS (\d+)_(\d+)_?(\d+)?/);

  return verion ? parseInt(verion[1], 10) : 0;
};
const ua = navigator.userAgent;
const isOriginalChrome =
  /chrome\/[\d.]+ Mobile Safari\/[\d.]+/i.test(ua) &&
  isAndroid &&
  ua.indexOf('Version') < 0;

const getFallUrl = schemeUrl => {
  const appPath =
    (schemeUrl || '').replace('yohobuy://yohobuy.com/goapp?', '') ||
    'openby:yohobuy={"action":"go.home","params":{"gender":"1","channel":"2"}}';

  return `https://union.yoho.cn/union/app-downloads.html?${appPath}`;
};

const checkOpenFall = (url, callFunc, noFall) => {
  let time = Date.now();

  callFunc();

  const fallUrl = getFallUrl(url);

  if (noFall) {
    return;
  }

  window.setTimeout(function() {
    if (Date.now() - time < 1200) {
      window.location.href = fallUrl;
    }
  });
};

const callIframe = (url, noFall) => {
  checkOpenFall(
    url,
    () => {
      const ifr = document.createElement('iframe');

      ifr.src = url;
      ifr.style.display = 'none';
      document.body.appendChild(ifr);
    },
    noFall,
  );
};

const callUrl = (url, noFall) => {
  checkOpenFall(
    url,
    () => {
      window.location.href = url;
    },
    noFall,
  );
};

const callA = (url, noFall) => {
  checkOpenFall(
    url,
    () => {
      const ca = document.createElement('a');

      ca.setAttribute('href', url);
      ca.style.display = 'none';
      document.body.appendChild(ca);

      ca.click();
    },
    noFall,
  );
};

const toAppPage = (appUrl, noFall) => {
  if (isOriginalChrome) {
    callA(appUrl, noFall);
  } else if (isiOS) {
    if (getIOSVersion() < 9) {
      callIframe(appUrl, noFall);
    } else {
      callUrl(appUrl, noFall);
    }
  } else {
    callIframe(appUrl, noFall);
  }
};

const canOpenApp = () => {
  if (isWechatDevtool || isApp) {
    return false;
  }
  return isAndroid || isiOS;
};

export default function openApp() {
  if (canOpenApp()) {
    setTimeout(function() {
      let appPath = getAppPath();

      toAppPage(appPath, false);
    });
  }
}

// 在 android 中,不在微信和QQ和App中,打开页面
if (isAndroid && !(isMobileQQ || isWechat) && !isApp) {
  openApp();
}