手机浏览器调用手机qq客户端的js

来源:互联网 发布:淘宝买兔子炒了 编辑:程序博客网 时间:2024/06/05 21:02

在做web开时发,我们经常会遇到在线QQ客户这种梗,如果在pc端上的话直接调用连接即可,像这样:

<img  style="CURSOR: pointer" onclick="javascript:window.open('http://b.qq.com/webc.htm?new=0&sid=1000000&o=ssssssss&q=7','_blank','height=502,toolbar=no,scrollbars=no,menubar=no,status=no');"  border="0" SRC=http://wpa.qq.com/pa?p=1:1000000:1 alt="点击这里给我发消息">

调用上面的代码,你的网页上的QQ在线就长这样:这里写图片描述
我们也可以去网上生成,现在有好多自动生成QQ在线客户的网站
比如这个:http://bizapp.qq.com/webpres.htm

但是如果是手机端的浏览器的话,你这样写是没用的。于是我在网络找了一个,也就是下面的js,
支持各种手机系统,点击可以直接打开QQ客户端(提前是你得安装了手机qq)。

function openqq(){            var type = undefined;            var param = "";            var sid = 2;            var rawuin = <{$qq}>;//这里填写QQ号            var qsig = "undefined";            var QQApi = {                openURL: function(url){                    var i = document.createElement('iframe');                    i.style.display = 'none';                    i.onload = function() { i.parentNode.removeChild(i); };                    i.src = url;                    document.body.appendChild(i);                    var returnValue = QQApi.__RETURN_VALUE;                    QQApi.__RETURN_VALUE = undefined;                    return returnValue;                },                isAppInstalled: function(scheme) {                    var parameters = {'scheme':scheme};                    var r = QQApi.openURL('jsbridge://app/isInstalled_?p=' + encodeURIComponent(JSON.stringify(parameters)));                    return r ? r.result : null;                },                isQQWebView: function(){                    return QQApi.isAppInstalled('mqq') == true;                },                __RETURN_VALUE: undefined            };            var usa=navigator.userAgent;            var p;            var mobile_q_jump = {                android:"https://play.google.com/store/apps/details?id=com.tencent.mobileqq",                ios:"itms-apps://itunes.apple.com/cn/app/qq-2011/id444934666?mt=8",                winphone:"http://www.windowsphone.com/zh-cn/store/app/qq/b45f0a5f-13d8-422b-9be5-c750af531762",                pc:"http://mobile.qq.com/index.html"            };            var isMQ = 0;            if(typeof type == "undefined") type = 1;            if(usa.indexOf("Android")>-1){                p = "android";            }            else if(usa.indexOf("iPhone")>-1 || usa.indexOf("iPad")>-1 || usa.indexOf("iPod")>-1){                p = "ios";            }            else if(usa.indexOf("Windows Phone") > -1 || usa.indexOf("WPDesktop") > -1){                p = "winphone";            }            else {                p = "pc";            }            if(p == "ios"){                //防止循环                if(history.pushState)                    history.pushState({},"t","#");                isMQ = QQApi.isQQWebView();                if (!isMQ){                     var sc = document.createElement("script");                    sc.src = "http://__.qq.com/api/qqapi.js";                    sc.onload = function(){                        if(window['iOSQQApi']){                            isMQ =iOSQQApi.device.isMobileQQ();                         }                    };                    document.body.appendChild(sc);                }            }            else if(p == "pc" && qsig != "undefined"){                window.open(qsig,"_self");            }            if(type == 1){//手Q                var isSuccess = true;                var f = document.createElement("iframe");                f.style.display = "none";                document.body.appendChild(f);                f.onload = function(){                    isSuccess = false;                };                if(p == "ios" && sid == 1){                    f.src = "mqqapi://card/show_pslcard?src_type=internal&version=1&uin="+ rawuin +"&card_type=person&source=qrcode";                }                if(p == "ios" && sid == 2){//ios并且为群名片                    f.src = "mqqapi://card/show_pslcard?src_type=internal&version=1&uin="+ rawuin +"&card_type=person&source=qrcode";                }                else if(p != "pc"){                    var url = window.location.href.split("&");                    f.src = "mqqopensdkapi://bizAgent/qm/qr?url=" + encodeURIComponent(url[0]);                }                if(p == "android" && sid == 1){                    f.src = "mqqapi://card/show_pslcard?src_type=internal&version=1&uin="+ rawuin +"&card_type=person&source=qrcode";                }                if(p == "android" && sid == 2){//ios并且为群名片                    f.src = "mqqapi://card/show_pslcard?src_type=internal&version=1&uin="+ rawuin +"&card_type=person&source=qrcode";                }                var now = Date.now();                setTimeout( function(){                    if((p == "ios" && !isMQ && Date.now() - now < 2000) || (p == "android" && !isSuccess) || ((p == "winphone" && Date.now() - now < 2000))){                        var jumpUrl = mobile_q_jump[p];                         if(jumpUrl) window.open(jumpUrl,"_self");                    }                } , 1500);            }        }

代码使用很简单只要在代码第五行<{$qq}>改为你得qq号码 ,直接调用即可打开手机qq;
例如:

<a href="javascript:;" onclick="openqq()">点击聊天</a>  

当我们点击上面的a标签时就会打开手机QQ并找到对应的QQ。(亲测有效,已经用在项目中了…)

原文地址 :http://www.thinkphp.cn/code/543.html

0 0
原创粉丝点击