微信浏览器自动选择默认浏览器

需求分析

不管是app的下载链接还是普通文件的链接在微信内置浏览器包括QQ内置浏览器都会被屏蔽。这是微信对第三方下载域名实施的屏蔽政策。那么这个问题该如何避免乃至解决呢?

首先我们需要对分享域名加JS防封接口,其次我们需要对域名进行处理,以达到ios用户在微信中的打开系统自动提示前往Safari打开下载页,安卓用户自动打开手机默认浏览器前往下载页。但是对于很多用户而言并不知道这样的实现,所以需要在代码中进行相关操作。目前ios只能通过遮罩层来提示用户,安卓则可以直接跳转外面默认浏览器,下面就分别介绍下这2种方式的实现方式。

实现方式

1. 遮罩提示(多用于ios系统)

遮罩引导法,这种是最简单的方法,一般会用在ios上较多。屏蔽图如下:

1.1  实现代码

​
function isWx(){//判断是否为微信
    var ua = window.navigator.userAgent.toLowerCase();
    if(ua.match(/MicroMessenger/i) == 'micromessenger'){
        return true;
    }
    return false;
};
if(isWx()){//判断浏览器是否微信
    var html='<div class="box"><img src="images/head.png"></box>'
    layer.open({//这里使用了layer的弹窗组件,你也可以自己写
        type: 1,content: html,anim: 'up',
           style: 'position:fixed; bottom:0; left:0; width: 100%; height: 100%; padding:0; border:none;'
      });
      return;
}<--有不懂的咨询我的q:3358246772-->

1.2  实现效果

2.  链接跳转

2.1  安卓系统处理方法

对于安卓系统而言,可实现微信内置浏览器直接跳转手机默认浏览器打开指定页面。实现代码如下:

<?php
function get_ticket($code){
    //初始化
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
    $headers = array();
    $headers[] = 'User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 11_1_2 like Mac OS X; zh-CN) AppleWebKit/537.51.1 (KHTML, like Gecko) Mobile/15B202 UCBrowser/11.7.7.1031 Mobile  AliApp(TUnionSDK/0.1.20)';
    $headers[] = 'Referer: https://m.mall.qq.com/release/?busid=mxd2&ADTAG=jcp.h5.index.dis';
    $headers[] = 'Content-Type:application/x-www-form-urlencoded; charset=UTF-8';
 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $content = curl_exec($ch);
    curl_close($ch);
    //$arr = json_decode($content,1);
    //if($arr['success'] == '1'){
    //    $shotCode = $arr['shotCode'];
    //}else{
    //    $shotCode = '';
    //}
    //preg_match('/openlink\":\"(.*?)\"}/',$content,$result);
    //$url = $result[1];
     
    preg_match('/href=\"(.*?)#wechat/',$content,$result);
    $url = $result[1];
    return $url;
}
    $time = time()-$info['ticket_time'];
    $minute=floor($time/60);
    query_update ( "jump_logs", "count=count+1". " where code='" . $code . "'" );
    if($minute >= 59){
        //如果超过1小时,更新ticket
        $url = get_ticket($w_url_code);
        if($url){
        query_update ( "jump_logs", "ticket_time='".time()."', ticket='" . $url . "' where code='" . $code . "'" );
        $ticket_url = $url.'#';
        if(strpos($_SERVER['HTTP_USER_AGENT'], 'baiduboxapp')||strpos($_SERVER['HTTP_USER_AGENT'], 'baiduboxapp')){//安卓百度手机APP
            echo '<script>window.location.href = "bdbox://utils?action=sendIntent&minver=7.4&params=%7b%22intent%22%3a%22'.$url.'%23wechat_redirect%23wechat_redirect%23Intent%3bend%22%7d";</script>';
            }else{
                echo '<script>window.location.href = "'.$ticket_url.'";</script>';
            }
        }
    }else{
        $ticket_url = $info['ticket'].'#';
        if(strpos($_SERVER['HTTP_USER_AGENT'], 'baiduboxapp')||strpos($_SERVER['HTTP_USER_AGENT'], 'baiduboxapp')){//安卓百度手机APP
            echo '<script>window.location.href = "bdbox://utils?action=sendIntent&minver=7.4&params=%7b%22intent%22%3a%22'.$info['ticket'].'%23wechat_redirect%23wechat_redirect%23Intent%3bend%22%7d";</script>';
            }else{
                echo '<script>window.location.href = "'.$ticket_url.'";</script>';
            }
    }
}
?>
<?php
function get_ticket($code){
    //初始化
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
    $headers = array();
    $headers[] = 'User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 11_1_2 like Mac OS X; zh-CN) AppleWebKit/537.51.1 (KHTML, like Gecko) Mobile/15B202 UCBrowser/11.7.7.1031 Mobile  AliApp(TUnionSDK/0.1.20)';
    $headers[] = 'Referer: https://m.mall.qq.com/release/?busid=mxd2&ADTAG=jcp.h5.index.dis';
    $headers[] = 'Content-Type:application/x-www-form-urlencoded; charset=UTF-8';
 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $content = curl_exec($ch);
    curl_close($ch);
    //$arr = json_decode($content,1);
    //if($arr['success'] == '1'){
    //    $shotCode = $arr['shotCode'];
    //}else{
    //    $shotCode = '';
    //}
    //preg_match('/openlink\":\"(.*?)\"}/',$content,$result);
    //$url = $result[1];
     
    preg_match('/href=\"(.*?)#wechat/',$content,$result);
    $url = $result[1];
    return $url;
}
    $time = time()-$info['ticket_time'];
    $minute=floor($time/60);
    query_update ( "jump_logs", "count=count+1". " where code='" . $code . "'" );
    if($minute >= 59){
        //如果超过1小时,更新ticket
        $url = get_ticket($w_url_code);
        if($url){
        query_update ( "jump_logs", "ticket_time='".time()."', ticket='" . $url . "' where code='" . $code . "'" );
        $ticket_url = $url.'#';
        if(strpos($_SERVER['HTTP_USER_AGENT'], 'baiduboxapp')||strpos($_SERVER['HTTP_USER_AGENT'], 'baiduboxapp')){//安卓百度手机APP
            echo '<script>window.location.href = "bdbox://utils?action=sendIntent&minver=7.4&params=%7b%22intent%22%3a%22'.$url.'%23wechat_redirect%23wechat_redirect%23Intent%3bend%22%7d";</script>';
            }else{
                echo '<script>window.location.href = "'.$ticket_url.'";</script>';
            }
        }
    }else{
        $ticket_url = $info['ticket'].'#';
        if(strpos($_SERVER['HTTP_USER_AGENT'], 'baiduboxapp')||strpos($_SERVER['HTTP_USER_AGENT'], 'baiduboxapp')){//安卓百度手机APP
            echo '<script>window.location.href = "bdbox://utils?action=sendIntent&minver=7.4&params=%7b%22intent%22%3a%22'.$info['ticket'].'%23wechat_redirect%23wechat_redirect%23Intent%3bend%22%7d";</script>';
            }else{
                echo '<script>window.location.href = "'.$ticket_url.'";</script>';
            }
    }
}
?>
<!--有不懂的地方联系我的Q:3358246772--> 

2.2  ios系统  

对于ios系统则无法直接实现,需要添加判断,判断代码如下

​
function isDevice(){//判断是android还是ios还是web
    var ua = navigator.userAgent.toLowerCase();
    if(ua.match(/iPhone\sOS/i) == "iphone os" || ua.match(/iPad/i)=="ipad"){//ios
        return "iOS";
    }
    if(ua.match(/Android/i) == "android") {
        return "Android";    
    }
    return "Web";

​
​
header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename='downloaded.pdf'");

此时微信会因为头部是下载处理,自动跳转到浏览器中打开这个链接,主要就是让服务端返回一个PDF下载就行了,这样微信就会直接跳出到浏览器打开。

至此,我们在微信中分享下载链接或二维码就不会出现无法自动跳转的问题了,本代码另外还附带了防封接口,可最大程度防止微信封域名的情况。这样我们就可以直接用微信扫描二维码在微信中分享和宣传引流了。如此我们即能够极大的提高自己的APP在微信中的推广转化率。也解决掉了微信中下载链接被屏蔽等问题。充分利用微信的用户群体来宣传引流。

猜你喜欢

转载自www.cnblogs.com/xxnhh/p/10327520.html