APP內資訊分享到朋友圈,推薦使用APP打開時先判斷本地是否安裝APP,安裝的話直接打開APP預覽資訊,未安裝跳轉到下載APP頁面

<!DOCTYPE html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>資訊詳情</title>
    <link rel="shortcut icon" href="/static/favicon.ico" type="image/x-icon" />
    <script src="/static/jquery-3.1.1.js" type="text/javascript"></script>
    <style>
        body{
            max-width: 100%!important;
            line-height: 200%!important;
            font-family: "SimHei" !important;
            letter-spacing:2px !important;
            text-align: start!important;
            word-wrap:break-word !important;
        }
        .header{
            font-size: 16px!important;
        }
        .title{
            font-size: 20px!important;
            font-weight: bold!important;
        }
        .content p{
            font-size: 16px!important;
            padding: 0 !important;
            text-indent: 2em!important;
        }

        img{
            max-width: 100% !important;
            height: auto!important;
            border-radius: 4px!important;
        }

        p img{
            display: block!important;
        }
        a{
            text-decoration:none;
            color: #4498EE;
        }
    </style>
</head>
<body>
    <a id="linkToCart" href="#">APP內閱讀,獲得更多精彩內容</a>
    <div class="title"></div>
    <div class="header"></div>
    <div class="content"></div>
<script>
    //採用正則表達式獲取地址欄參數
    function getQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return decodeURIComponent(r[2]);
        return null;
    }

    function getDateYmdhm (timestamp) {
        if(timestamp){
            var date = new Date(timestamp * 1000); // 時間戳爲10位需*1000,時間戳爲13位的話不需乘1000
            var Y = date.getFullYear();
            var M = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1);
            var D = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
            var h = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
            var m = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
            return Y + "-" + M + "-" + D + " " + h + ":" + m;
        }else{
            return "";
        }
    }

    var nid = getQueryString("nid");
    //點擊鏈接的時候調用
    $("#linkToCart").click(function(){
        var curPageUrl = window.document.location.href;
        curPageUrl = curPageUrl.replace("https://","").replace("http://","");
        var toUrl = "XXXnews://"+curPageUrl;//打開app的協議
        var downloadUrl = "https://aicptest.xhkjedu.com/app.html";//測試環境下載app的地址
        if (curPageUrl.indexOf("test" == -1)){
            downloadUrl = "https://aicp.xhkjedu.com/app.html";//正式環境下載app的地址
        }
        var u = navigator.userAgent;
        if(u.indexOf('MicroMessenger') > -1){//微信中打開
            alert('點擊右上角選擇在瀏覽器中打開')
        } else {
            var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //android終端或者uc瀏覽器
            var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios終端
            if(isiOS){
                var nowTime = new Date().valueOf();
                var ifr = document.createElement("iframe");
                ifr.src = toUrl;
                ifr.style.display = "none";
                document.body.appendChild(ifr);
                window.setTimeout(function(){
                    document.body.removeChild(ifr);
                    var launchTime = new Date().valueOf() - nowTime;
                    if(launchTime < 2003) {//值要比設置的超時時間長不然總是會執行下載
                        window.location.href = downloadUrl;
                    } else {
                        window.close();
                    }
                },2000)
            } else {
                var nowTime = new Date().valueOf();
                window.location.href = toUrl;
                window.setTimeout(function () {
                    var launchTime = new Date().valueOf() - nowTime;
                    if(launchTime < 2003) {//值要比設置的超時時間長不然總是會執行下載
                        window.location.href = downloadUrl;
                    } else {
                        window.close();
                    }
                }, 2000);
            }
        }
    });
    
    $.ajax({
        type: "post",
        url: "/news/detail?nid=" + nid,
        dataType: "json",
        success: function(data) {
            if (data.code == 0) {
                var news = data.obj;
                $(".title").html(news.ntitle);
                $(".header").html(news.ntypename+"&nbsp;&nbsp;發佈於"+getDateYmdhm(news.createtime)+"&nbsp;&nbsp;已閱讀"+news.ncount+"次");
                $(".content").html(news.ncontent);

                var news_imgs = document.getElementsByTagName('img');
                for(i = 0; i < news_imgs.length; i++){
                    news_imgs[i].src = news_imgs[i].src+"!newinfo";
                }
            } else {
                alert(data.msg);
            }
        }
    })
</script>
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章