• 在手机浏览器中打开指定的应用商店


    前言

    大牛:你们这应用下载太慢了,为何不跳转到手机自带的应用商店下载,还不占用你们服务器的资源!
    程序猿:哇!!!是的啊!

    一、如何区分现在运行在哪一个厂商的手机里面?

    下面仅仅列出了:华为、小米、OPPO、VIVO这4个厂商,因为我们之上架了这4个商店。

        function verifyManufacturer() {
            const userAgent = navigator.userAgent.toLowerCase()
            const isIphone = userAgent.match(/(iphone|ipad|ipod)/i);
            const isHuawei = userAgent.match(/huawei/i);
            const isHonor = userAgent.match(/honor/i);
            const isOppo = userAgent.match(/oppo/i);
            const isOppoR15 = userAgent.match(/PACM00/i);
            const isVivo = userAgent.match(/vivo/i);
            const isXiaomi = userAgent.match(/mi\s/i);
            const isXIAOMI = userAgent.match(/xiaomi/i);
            const isXiaomi2s = userAgent.match(/mix\s/i);
            const isRedmi = userAgent.match(/redmi/i);
    
            if (isIphone) {
                return 'iphone'
            } else if (isHuawei || isHonor) {
                return 'huawei';
            } else if (isOppo || isOppoR15) {
                return 'oppo';
            } else if (isVivo) {
                return 'vivo';
            } else if (isXiaomi || isRedmi || isXiaomi2s || isXIAOMI) {
                return 'xiaomi';
            } else {
                return 'other'
            }
        }
    
    • 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

    二、如果是通过QQ、微信、支付宝打开的网址怎么办?

        function channelSource() {
            var ua = navigator.userAgent.toLowerCase();
            if (ua.match(/MicroMessenger/i) == "micromessenger") {
                return "weixin";
            } else if (ua.match(/QQ/i) == "qq") {
                return "QQ";
            } else if (ua.match(/Alipay/i) == "alipay" && payway == 2) {
                return "alipay";
            }
            return false;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    三、打开指定应用商店

    如果是通过这几个厂商的浏览器中打开的则直接跳转到对应的应用商店,如果不是通过这几个应用商店打开则只能通过自己的服务器上下载了。

     function goDownload() {
    
            const iosLinkUrl = 'IOS的appstore应用地址';
            const androidLinkurl = '自己服务器的下载地址';
            if (channelSource()) {
                //内置浏览器  可加提示使其打开手机自带浏览器
                window.location.href = androidLinkurl;
                return;
            }
            const huaweiUrl = 'appmarket://details?id=Android包名';
            const oppoUrl = "oppomarket://details?packagename=Android包名";
            const vivoUrl = "vivomarket://details?id=Android包名";
            const xiaomiUrl = 'mimarket://details?id=Android包名';
           
            switch (this.verifyManufacturer()) {
                case 'iphone':
                    window.location.href = iosLinkUrl;
                    break;
                case 'xiaomi':
                    window.location.href = xiaomiUrl;
                    break;
                case 'huawei':
                    window.location.href = huaweiUrl;
                    break;
                case 'vivo':
                    window.location.href = vivoUrl;
                    break;
                case 'oppo':
                    window.location.href = oppoUrl;
                    break;
                default:
                    window.location.href = androidLinkurl;
                    break;
            }
        }
    
    • 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

    四、完整代码

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
        <meta charset="UTF-8">
        <title>下载中心</title>
        <meta name="description" content="">
        <meta name="keywords" content="">
        <link href="__CDN__/assets/css/bootstrap.css" rel="stylesheet">
        <script src="__CDN__/assets/js/jquery.min.js"></script>
        <script type="text/javascript" src="__CDN__/assets/js/bootstrap.min.js"></script>
        <style type="text/css" media="all">
            .weixin-tip {
                display: none;
                position: fixed;
                left: 0;
                top: 0;
                background: rgba(0, 0, 0, 0.8);
                filter: alpha(opacity=80);
                height: 100%;
                width: 100%;
                z-index: 100;
            }
    
            .weixin-tip p {
                text-align: center;
                margin-top: 10%;
                padding: 0 5%;
            }
    
            .img1 {
                width: 100%;
                height: 100%;
                filter: alpha(opacity=100);
                -moz-opacity: 0.4;
                -khtml-opacity: 0.5;
                opacity: 0.5;
            }
        </style>
    </head>
    <body>
    <div class="weixin-tip" id="weixin_tip">
        <img src="http://qny.ruixue.xingnuo.vip/uploads/20231026/FqotZR59umvmB5JtGVWyTHIFqyOa.jpg" class="img1"/>
    </div>
    <div id="down" >
        <div class="col-md-12 text-center" style="margin-top:200px;display:flex;flex-direction: column;">
            <span ><img src="{$logo|htmlentities}" style="width: 60px;height: 60px;"></span>
            <span style="font-size: 24px;" >小伙伴儿</span>
            <div><span style="color:#a1a1a1;">用一次&nbsp&nbsp牛一次</span></div>
        </div>
        <div class="col-md-12 text-center" style="margin-top:50px;"><a id="url" href="{$a_url}" download></a>
            <button onclick="downLoadAppOwen();" style="width: 85%;height: 60px;border-radius: 25px;background-color: #22d49a;outline:none;border: none"><span style="color:#fff;font-size: 20px;margin-top:20px;margin-left:10px;">下载APP</span></button>
        </div>
    </div>  
    <script type="text/javascript" charSet="utf-8">
        var ua = navigator.userAgent.toLowerCase();
        if (ua.match(/MicroMessenger/i) == "micromessenger") {
            var winHeight = window.innerHeight;
            var weixin_top = document.getElementById('weixin_tip');
            weixin_top.style.display = 'block';
            weixin_top.style.height = winHeight;
    
            var down = document.getElementById('down');
            down.style.display = 'none';
        } else { 
            var weixin_top = document.getElementById('weixin_tip');
            weixin_top.style.display = 'none'; 
            var down = document.getElementById('down');
            down.style.display = 'block';
            goDownload(); 
        }
    
        function downLoadAppOwen() {
            var url = '自己服务器的下载地址';
            var isIOS = /(iPhone|iPad|iPod)/i.test(navigator.userAgent); // 通过User-Agent判断是否为iOS设备
            var isAndroid = /android/i.test(navigator.userAgent) && !isIOS; // 通过User-Agent判断是否为Android设备
            if(isIOS){
                url = 'IOS的appstore应用地址'
            }
            window.location.href = url;
        }
     
    </script>
    </body>
    </html>
    
    
    • 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
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87

    五、总结

    上面包含了打开ios的应用商店和Android的应用商店。
    ios的打开应用商店很简单,因为ios只用一个厂商。
    Android手机厂商就多了有很多。华为、oppo、vivo、小米等等

  • 相关阅读:
    【LeetCode】647. 回文子串
    Anaconda3安装pyLDAvis以及找不到funcy库的解决方法
    逻辑漏洞(越权)
    CMMI之项目管理类核心框架
    华为数通方向HCIP-DataCom H12-821题库(单选题:281-300)
    Kubernetes实战(四)-部署docker harbor私有仓库
    请给出python程序运行结果
    纯技术程序员的悲哀和出路
    搞懂TypeScript的类型声明
    使用redis+lua通过原子减解决超卖问题【示例】
  • 原文地址:https://blog.csdn.net/weixin_41620505/article/details/136347477