Skip to content

Instantly share code, notes, and snippets.

@mirinzhang
Last active March 5, 2018 09:57
Show Gist options
  • Select an option

  • Save mirinzhang/a9aad356fc91fa9d169428bcc2b93497 to your computer and use it in GitHub Desktop.

Select an option

Save mirinzhang/a9aad356fc91fa9d169428bcc2b93497 to your computer and use it in GitHub Desktop.

Revisions

  1. 泡面君 revised this gist Mar 5, 2018. No changes.
  2. 泡面君 revised this gist Mar 5, 2018. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions openOrDownloadApp.js
    Original file line number Diff line number Diff line change
    @@ -7,10 +7,10 @@ function openOrDownloadApp(microLink, schemeUrl, downloadUrl) {
    // 收集设备信息
    var deviceInfo = {
    userAgent: navigator.userAgent.toLowerCase(),
    isAndroid: !!navigator.userAgent.match(/android/ig),
    isIphone: !!navigator.userAgent.match(/iphone|ipod/ig),
    isIpad: !!navigator.userAgent.match(/ipad/ig),
    isWeixin: !!navigator.userAgent.match(/MicroMessenger/ig),
    isAndroid: !!navigator.userAgent.match(/android/ig),
    isIphone: !!navigator.userAgent.match(/iphone|ipod/ig),
    isIpad: !!navigator.userAgent.match(/ipad/ig),
    isWeixin: !!navigator.userAgent.match(/MicroMessenger/ig),
    };

    // 如果是在微信中打开,则使用微下载进行打开或下载APP
  3. 泡面君 created this gist Mar 5, 2018.
    34 changes: 34 additions & 0 deletions openOrDownloadApp.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    /**
    * @param microLink 应用宝微下载链接
    * @param schemeUrl 需要打开的schemeUrl
    * @param downloadUrl App通用下载地址
    */
    function openOrDownloadApp(microLink, schemeUrl, downloadUrl) {
    // 收集设备信息
    var deviceInfo = {
    userAgent: navigator.userAgent.toLowerCase(),
    isAndroid: !!navigator.userAgent.match(/android/ig),
    isIphone: !!navigator.userAgent.match(/iphone|ipod/ig),
    isIpad: !!navigator.userAgent.match(/ipad/ig),
    isWeixin: !!navigator.userAgent.match(/MicroMessenger/ig),
    };

    // 如果是在微信中打开,则使用微下载进行打开或下载APP
    if(deviceInfo.isWeixin) {
    window.location.href = microLink + '&android_schema=' + window.encodeURI(schemeUrl);
    return;
    }

    // 非微信环境,先尝试打开App
    var iframe = document.createElement('iframe');
    iframe.src = schemeUrl;
    iframe.style.display = 'none';
    document.body.appendChild(iframe);

    // 若2秒内未打开App则说明未安装App,跳转到App下载页面
    window.setTimeout(function() {
    document.body.removeChild(iframe);
    window.location.href = downloadUrl;
    }, 2000);

    }