微信WeixinJSBridge API使用实例


Posted in Javascript onMay 25, 2015

注意:请在微信中测试

<!DOCTYPE html>
<html>
 <head>
 <title>微信WeixinJSBridge API</title> 
 <meta charset="utf-8" /> 
 <script type="text/javascript">
(function(){
var a=document.getElementsByTagName("html")[0];
window.Session={appDomain:a.getAttribute("data-app-domain")||"",staticDomain:a.getAttribute("data-static-domain")||""}
})();
 
window.registNS=function(fullNS,isIgnorSelf){
var reg=/^[_$a-z]+[_$a-z0-9]*/i;var nsArray=fullNS.split(".");
var sEval="";
var sNS="";
var n=isIgnorSelf?nsArray.length-1:nsArray.length;
for(var i=0;i<n;i++){
  if(!reg.test(nsArray[i])){throw new Error("Invalid namespace:"+nsArray[i]+"");
  return
  }
  if(i!=0){sNS+="."}
  sNS+=nsArray[i];
  sEval+="if(typeof("+sNS+")=='undefined') "+sNS+"=new Object();
  else "+sNS+";"
}
if(sEval!=""){
return eval(sEval)
}
return{}
 
};
 
</script> 
 </head> 
 <body>
 <section class="mod-page-body"> 
  <div class="mod-page-main wordwrap clearfix"> 
  <div class="mod-pageheader"></div> 
  <div class="mod-pagecontent"> 
   <div class="mod-weixinjsapi"> 
   <div class="x-desc">
    微信客户端自带的Js Api:WeixinJSBridge
   </div> 
   <div id="WeixinJsApi"> 
    <input type="button" id="imagePreview" value="图片预览" /> 
    <input type="button" id="profile" value="查看profile" /> 
    <a href="weixin://profile/gh_412d74fbb474">企业微信小助手</a> 
    <input type="button" id="shareWeibo" value="分享微博" /> 
    <input type="button" id="shareFB" value="分享facebook" /> 
    <input type="button" id="addContact" value="添加联系人" /> 
    <input type="button" id="scanQRCode" value="扫描二维码" /> 
    <input type="button" id="jumpToBizProfile" value="跳转到指定公众账号页面" /> 
    <input type="button" id="toggleMenuBtn" value="隐藏右上角按钮" /> 
    <input type="button" id="toggleToolbar" value="隐藏底部导航栏" /> 
    <input type="button" id="getNetType" value="获取网络状态" /> 
    <input type="button" id="closeWindow" value="关闭" /> 
    <input type="button" id="getBrandWCPayRequest" value="发起公众号微信支付" /> 
    <input type="button" id="setPageState" value="设置页面状态" /> 
    <input type="button" id="sendEmail" value="发邮件" /> 
    <input type="button" id="openSpecificView" value="微信团队打开webView,跳到指定页面" /> 
    <input type="button" id="getCanIAPPay" value="getCanIAPPay" /> 
    <input type="button" id="getBrandIAPPayRequest" value="发起公众号IAP支付" /> 
    <input type="button" id="openUrlByExtBrowser" value="用safari打开指定链接" /> 
    <input type="button" id="openProductView" value="跳转微信商品页" /> 
    <input type="button" id="openLocation" value="查看地理位置" /> 
    <input type="button" id="timelineCheckIn" value="朋友圈签到" /> 
    <input type="button" id="getBrandWCPayCreateCreditCardRequest" value="开通微信信用卡" /> 
    <input type="button" id="geoLocation" value="获取地理位置" /> 
    <input type="button" id="getInstallState" value="获取某app是否安装" /> 
    <input type="button" id="editAddress" value="公众号编辑收货地址" /> 
    <input type="button" id="getLatestAddress" value="公众号获取最近的收货地址" /> 
    <input type="button" id="launch3rdApp" value="启动第三方APP" /> 
    <input type="button" id="jumpWCMall" value="跳转微信商品购买界面" /> 
    <input type="button" id="addEmoticon" value="添加表情" /> 
    <input type="button" id="cancelAddEmoticon" value="取消下载某表情" /> 
    <input type="button" id="hasEmoticon" value="查询是否存在某表情" /> 
   </div> 
   </div> 
  </div> 
  </div> 
 </section> 
 <script>
function onBridgeReady() {
  WeixinJSBridge.on('menu:share:appmessage', function(argv) 
  {
    WeixinJSBridge.invoke('sendAppMessage',{
          "link":"http://m.exmail.qq.com/",
          "desc":"desc",
          "title":"title for WeiXinJsBridge"
    }, function(res) {
      WeixinJSBridge.log(res.err_msg);
    });
  });
  WeixinJSBridge.on('menu:share:timeline', function(argv) 
  {
  WeixinJSBridge.invoke("shareTimeline",{
    "link":"http://m.exmail.qq.com",
    "img_url":"http://rescdn.qqmail.com/bizmail/zh_CN/htmledition/images/bizmail/v3/logo1ca3fe.png",
    "img_width":"172",
    "img_height":"40",
    "desc":"i am description",
    "title":"just test from WeixinJsBridge"
    },
    function(e){
    alert(e.err_msg);
    })
  });
}
 
if (typeof WeixinJSBridge === "undefined"){
  if (document.addEventListener){
    document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
  }
}else{
  onBridgeReady();
}
 
  var menuHidden=!1,toolbarHidden=!1,netType={"network_type:wifi":"wifi网络","network_type:edge":"非wifi,包含3G/2G","network_type:fail":"网络断开连接","network_type:wwan":"2g或者3g"};
  document.addEventListener("WeixinJSBridgeReady",function(){
      document.getElementById("imagePreview").addEventListener(
      "click",function(){
                WeixinJSBridge.invoke("imagePreview",{
        "urls":[
        "http://rescdn.qqmail.com/bizmail/zh_CN/htmledition/images/bizmail/v3/logo1ca3fe.png",
        "http://rescdn.qqmail.com/bizmail/zh_CN/htmledition/images/bizmail/v3/icons_features1ca3fe.png",
        "http://rescdn.qqmail.com/bizmail/zh_CN/htmledition/images/bizmail/v3/icons_workStyle1ca3fe.png"
        ],
        "current":"http://rescdn.qqmail.com/bizmail/zh_CN/htmledition/images/bizmail/v3/icons_features1ca3fe.png"
        })
            },!1),
    document.getElementById("profile").addEventListener(
      "click",function(){
        alert("profile clicked");
        WeixinJSBridge.invoke("profile",{
          "username":"gh_412d74fbb474",
          "nickname":"企业微信小助手"  
        })
      },!1),
    document.getElementById("shareWeibo").addEventListener(
      "click",function(){
        WeixinJSBridge.invoke("shareWeibo",{
          "type":"link",
          "link":"http://m.exmail.qq.com"
        },
        function(e){
          alert(e.err_msg);
        })
      },!1),
    document.getElementById("shareFB").addEventListener(
      "click",function(){
        WeixinJSBridge.invoke("shareFB",{
          "link":"http://m.exmail.qq.com"
        })
      },!1),
    document.getElementById("scanQRCode").addEventListener(
      "click",function(){
        WeixinJSBridge.invoke("scanQRCode",{
        })
      },!1),
    document.getElementById("addEmoticon").addEventListener(
      "click",function(){
        WeixinJSBridge.invoke("addEmoticon",{
          "url":"http://rescdn.qqmail.com/bizmail/zh_CN/htmledition/images/bizmail/v3/icons_features1ca3fe.png",
          "thumb_url":"http://rescdn.qqmail.com/bizmail/zh_CN/htmledition/images/bizmail/v3/logo1ca3fe.png"
 
        },
        function(e){
                    alert(e.err_msg);
                })
      },!1),
    document.getElementById("cancelAddEmoticon").addEventListener(
      "click",function(){
        WeixinJSBridge.invoke("cancelAddEmoticon",{
          "url":"http://rescdn.qqmail.com/bizmail/zh_CN/htmledition/images/bizmail/v3/icons_features1ca3fe.png"
 
        },
        function(e){
                    alert(e.err_msg);
                })
      },!1),
    document.getElementById("hasEmoticon").addEventListener(
      "click",function(){
        WeixinJSBridge.invoke("hasEmoticon",{
          "url":"http://rescdn.qqmail.com/bizmail/zh_CN/htmledition/images/bizmail/v3/icons_features1ca3fe.png"
 
        },
        function(e){
                    alert(e.err_msg);
                })
      },!1),
    document.getElementById("addContact").addEventListener(
      "click",function(){
        WeixinJSBridge.invoke("addContact",{
          "webtype":"1",
          "username":"gh_412d74fbb474"
        },
        function(e){
          alert(e.err_msg);
        })
      },!1),
    document.getElementById("jumpToBizProfile").addEventListener(
      "click",function(){
        WeixinJSBridge.invoke("jumpToBizProfile",{
          "tousername":"gh_2248a2ade13e"
        },
        function(e){
          alert(e.err_msg);
        })
      },!1),
    document.getElementById("toggleMenuBtn").addEventListener(
      "click",function(){
        menuHidden?
        (WeixinJSBridge.call("showOptionMenu"),menuHidden=!1,this.value="隐藏右上角按钮")
        :
        (WeixinJSBridge.call("hideOptionMenu"),menuHidden=!0,this.value="显示右上角按钮")
      },!1),
    document.getElementById("toggleToolbar").addEventListener(
      "click",function(){
        toolbarHidden?
        (WeixinJSBridge.call("showToolbar"),toolbarHidden=!1,this.value="隐藏底部导航栏")
        :
        (WeixinJSBridge.call("hideToolbar"),toolbarHidden=!0,this.value="显示底部导航栏")
      },!1),
    document.getElementById("getNetType").addEventListener(
      "click",function(){
        WeixinJSBridge.invoke("getNetworkType",{},
          function(e){
            alert(netType[e.err_msg])
          })
      },!1),
     document.getElementById("closeWindow").addEventListener(
            "click",function(){
                WeixinJSBridge.invoke("closeWindow",{},function(e){})
            },!1),
    document.getElementById("getBrandWCPayRequest").addEventListener(
      "click",function(){
      WeixinJSBridge.invoke("getBrandWCPayRequest",{
        "appId" : "wxf8b4f85f3a794e77", //公众号名称,由商户传入
        "timeStamp" : "189026618", //时间戳 这里随意使用了一个值
        "nonceStr" : "adssdasssd13d", //随机串
        "package" :
        "body=xxx&fee_type=1&input_charset=GBK¬ify_url=http&out_trade_no=16642817866003386000&partner=1900000109&return_url=http&spbill_create_ip=127.0.0.1&total_fee=1&sign=273B7EEEE642A8E41F27213D8517E0E4", //扩展字段,由商户传入
        "signType" : "SHA1", //微信签名方式:sha1
        "paySign" : "b737015b5b1eabe5db580945a07eac08c7bb55f8" //微信签名
        },
        function(e){
          alert(e.err_msg)
        })
      },!1),
    document.getElementById("setPageState").addEventListener(
      "click",function(){
      WeixinJSBridge.invoke("setPageState",{
        "state" : "1"
        })
      },!1),
 
    document.getElementById("sendEmail").addEventListener(
      "click",function(){
      WeixinJSBridge.invoke("sendEmail",{
        "title" : "title!",
        "content" : "i am an Email!", //时间戳 这里随意使用了一个值
        },
        function(e){
    //     alert(e.err_msg)
        })
      },!1),
    document.getElementById("openSpecificView").addEventListener(
      "click",function(){
      WeixinJSBridge.invoke("openSpecificView",{
        "specificview" : "contacts"
        },
        function(e){
          alert(e.err_msg)
        })
      },!1),
    document.getElementById("getCanIAPPay").addEventListener(
      "click",function(){
      WeixinJSBridge.invoke("getCanIAPPay",{ },
        function(e){
          alert(e.err_msg)
        })
      },!1),
    document.getElementById("getBrandIAPPayRequest").addEventListener(
      "click",function(){
      WeixinJSBridge.invoke("getBrandIAPPayRequest",{
        "appId" : "wxf8b4f85f3a794e77", //公众号名称,由商户传入
        "timeStamp" : "189026618", //时间戳 这里随意使用了一个值
        "nonceStr" : "adssdasssd13d", //随机串
        "package" : "bankType=CITIC_CREDIT&bankName=%e4%b8%ad%e4%bf%a1%e9%93%b6%e8%a1%8c&sign=CF8922F49431FFE8A1834D0B32B25CE3",
        //扩展字段,由商户传入
        "signType" : "SHA1", //微信签名方式:sha1
        "paySign" : "1e6f13f78ca0ec43fbb80899087f77568af66987" //微信签名
        },
        function(e){
          alert(e.err_msg)
        })
      },!1),
    document.getElementById("openLocation").addEventListener(
      "click",function(){
      WeixinJSBridge.invoke("openProductView",{  
        "latitude" : 23.113, //纬度
        "longitude" : 113.23, //经度
        "name" : "TIT创意园", //POI名称
        "address" : "广州市海珠区新港中路397号", //地址
        "scale" : 14, //地图缩放级别
        "infoUrl" : "http://weixin.qq.com/", //查看位置界面底部的超链接        
        },
        function(e){
          alert(e.err_msg)
        })
      },!1),
    document.getElementById("timelineCheckIn").addEventListener(
      "click",function(){
      WeixinJSBridge.invoke("timelineCheckIn",{  
        "img_url": "http://mmsns.qpic.cn/mmsns/RLllkTm3DUdV24xbZnKicx9jJWxXI0Bq84zzbtibGuRyk/0", // 分享到朋友圈的缩略图
        "img_width": "640", // 图片的长度
        "img_height": "640", // 图片高度
        "link": "http://news.qq.com/zt2012/cxkyym/index.htm", // 连接地址
        "desc": "这个是描述啊啊", // 描述
        "title": "朝鲜称中国渔船越界捕捞", // 分享标题
        "latitude" : 23.113, //纬度
        "longitude" : 113.23, //经度
        "poiId" : "dianping_2331037", //商户id
        "poiName" : "TIT创意园", //POI名称
        "poiAddress" : "广州市海珠区新港中路397号", //地址
        "poiScale" : 14, //地图缩放级别
        "poiInfoUrl" : "http://weixin.qq.com/" //查看位置界面底部的超链接
        },
        function(e){
          alert(e.err_msg)
        })
      },!1),
    document.getElementById("geoLocation").addEventListener(
      "click",function(){
      WeixinJSBridge.invoke("geoLocation",{  
        },
        function(e){
          alert(e.err_msg)
        })
      },!1),
    document.getElementById("getBrandWCPayCreateCreditCardRequest").addEventListener(
      "click",function(){
      WeixinJSBridge.invoke("getBrandWCPayCreateCreditCardRequest",{ 
        "appId" : "wxf8b4f85f3a794e77", //公众号名称,由商户传入
        "timeStamp" : "189026618", //时间戳 这里随意使用了一个值
        "nonceStr" : "adssdasssd13d", //随机串
        "package" : "bankType=CITIC_CREDIT&bankName=%e4%b8%ad%e4%bf%a1%e9%93%b6%e8%a1%8c&sign= CF8922F49431FFE8A1834D0B32B25CE3",
        //扩展字段,由商户传入
        "signType" : "SHA1", //微信签名方式:sha1
        "paySign" : "1e6f13f78ca0ec43fbb80899087f77568af66987" //微信签名
        },
        function(e){
          alert(e.err_msg)
        })
      },!1),
    document.getElementById("getInstallState").addEventListener(
      "click",function(){
      WeixinJSBridge.invoke("getInstallState",{  
          "packageUrl":"teamcircle://"
        },
        function(e){
          alert(e.err_msg)
        })
      },!1),
    document.getElementById("openProductView").addEventListener(
      "click",function(){
      WeixinJSBridge.invoke("openProductView",{  
          "productInfo":"json"
        },
        function(e){
          alert(e.err_msg)
        })
      },!1),
    document.getElementById("getLatestAddress").addEventListener(
        "click",function(){
        WeixinJSBridge.invoke("getLatestAddress",{ 
          "appId" : "wxf8b4f85f3a794e77", //公众号名称,由商户传入
          "timeStamp" : "189026618", //时间戳 这里随意使用了一个值
          "nonceStr" : "adssdasssd13d", //随机串
          "signType" : "SHA1", //微信签名方式:sha1
          "addrSign" : "b737015b5b1eabe5db580945a07eac08c7bb55f8", //微信签名
          "scope"  : "snsapi"
        },
        function(e){
          alert(e.err_msg)
        })
      },!1),
    document.getElementById("editAddress").addEventListener(
        "click",function(){
        WeixinJSBridge.invoke("editAddress",{  
          "appId" : "wxf8b4f85f3a794e77", //公众号名称,由商户传入
          "timeStamp" : "189026618", //时间戳 这里随意使用了一个值
          "nonceStr" : "adssdasssd13d", //随机串
          "signType" : "SHA1", //微信签名方式:sha1
          "addrSign" : "b737015b5b1eabe5db580945a07eac08c7bb55f8", //微信签名
          "scope"  : "snsapi"
        },
        function(e){
          alert(e.err_msg)
        })
      },!1),
    document.getElementById("launch3rdApp").addEventListener(
        "click",function(){
        WeixinJSBridge.invoke("launch3rdApp",{ 
          "appId" : "wx5823bf96d3bd56c7", //公众号名称,由商户传入
        },
        function(e){
          alert(e.err_msg)
        })
      },!1),
    document.getElementById("jumpWCMall").addEventListener(
        "click",function(){
        WeixinJSBridge.invoke("jumpWCMall",{  
          "appId" : "wx5823bf96d3bd56c7", //公众号名称,由商户传入
          "funcId":"1000"
        },
        function(e){
          alert(e.err_msg)
        })
      },!1),
    document.getElementById("openUrlByExtBrowser").addEventListener(
      "click",function(){
      WeixinJSBridge.invoke("openUrlByExtBrowser",{
        "url" : "http://m.exmail.qq.com"
        },
        function(e){
          alert(e.err_msg)
        })
      },!1)
    }
  );
</script> 
 </body>
</html>
Javascript 相关文章推荐
js验证是否为数字的总结
Apr 14 Javascript
Javascript Ajax异步读取RSS文档具体实现
Dec 12 Javascript
邮箱下拉自动填充选择示例代码附图
Apr 03 Javascript
一个简单的jQuery计算器实现了连续计算功能
Jul 21 Javascript
jQuery搜索同辈元素方法
Feb 10 Javascript
JavaScript实现强制重定向至HTTPS页面
Jun 10 Javascript
js实现密码强度检测【附示例】
Mar 30 Javascript
浅析Node.js实现HTTP文件下载
Aug 05 Javascript
JS实现登录页密码的显示和隐藏功能
Dec 06 Javascript
React和Vue中监听变量变化的方法
Nov 14 Javascript
Node.js原生api搭建web服务器的方法步骤
Feb 15 Javascript
在Express中提供静态文件的实现方法
Oct 17 Javascript
jquery.validate使用时遇到的问题
May 25 #Javascript
微信内置浏览器私有接口WeixinJSBridge介绍
May 25 #Javascript
微信浏览器内置JavaScript对象WeixinJSBridge使用实例
May 25 #Javascript
JS中的THIS和WINDOW.EVENT.SRCELEMENT详解
May 25 #Javascript
jQuery实现表格行上移下移和置顶的方法
May 22 #Javascript
jquery控制表单输入框显示默认值的方法
May 22 #Javascript
jQuery实现自动滚动到页面顶端的方法
May 22 #Javascript
You might like
受疫情影响 动画《Re从零开始的异世界生活》第二季延期至7月
2020/03/10 日漫
php面向对象全攻略 (十五) 多态的应用
2009/09/30 PHP
php CI框架插入一条或多条sql记录示例
2014/07/29 PHP
浅析php原型模式
2014/11/25 PHP
ThinkPHP5实现作业管理系统中处理学生未交作业与已交作业信息的方法
2016/11/12 PHP
静态html文件执行php语句的方法(推荐)
2016/11/21 PHP
PHP实现查询手机归属地的方法详解
2017/04/28 PHP
PHP实现广度优先搜索算法(BFS,Broad First Search)详解
2017/09/16 PHP
Yii2框架实现登录、退出及自动登录功能的方法详解
2017/10/24 PHP
javascript 解析后的xml对象的读取方法细解
2009/07/25 Javascript
基于JQuery实现异步刷新的代码(转载)
2011/03/29 Javascript
浅谈JavaScript中的字符编码转换问题
2015/07/07 Javascript
xtemplate node.js 的使用方法实例解析
2016/08/22 Javascript
通过修改360抢票的刷新频率和突破8车次限制实现方法
2017/01/04 Javascript
VUE JS 使用组件实现双向绑定的示例代码
2017/01/10 Javascript
jqgrid实现简单的单行编辑功能
2017/09/30 Javascript
vue-router路由与页面间导航实例解析
2017/11/07 Javascript
[56:45]DOTA2上海特级锦标赛D组小组赛#1 EG VS COL第一局
2016/02/28 DOTA
[52:06]FNATIC vs NIP 2019国际邀请赛小组赛 BO2 第二场 8.16
2019/08/19 DOTA
python和shell变量互相传递的几种方法
2013/11/20 Python
Python脚本实现DNSPod DNS动态解析域名
2015/02/14 Python
python使用线程封装的一个简单定时器类实例
2015/05/16 Python
Python中条件判断语句的简单使用方法
2015/08/21 Python
简单了解python中对象的取反运算符
2019/07/01 Python
Python使用pyautocad+openpyxl处理cad文件示例
2019/07/11 Python
python 进程的几种创建方式详解
2019/08/29 Python
Python ORM框架Peewee用法详解
2020/04/29 Python
python实现扫雷游戏的示例
2020/10/20 Python
详解修改Anaconda中的Jupyter Notebook默认工作路径的三种方式
2021/01/24 Python
CSS3实现文本垂直排列的方法
2018/07/10 HTML / CSS
HashMap和Hashtable的区别
2013/05/18 面试题
三年级语文教学反思
2014/02/01 职场文书
安全生产中长期规划实施方案
2014/02/21 职场文书
水污染治理工程专业自荐信
2014/06/21 职场文书
中学生运动会广播稿
2015/08/19 职场文书
导游词之山东八大关
2019/12/18 职场文书