DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> JavaScript綜合知識 >> 微信WeixinJSBridge API使用實例
微信WeixinJSBridge API使用實例
編輯:JavaScript綜合知識     

   這篇文章主要介紹了微信WeixinJSBridge API使用實例,本文直接給出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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 <!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=273B7EEEE642A8E41F27213 D8517E0E4", //擴展字段,由商戶傳入 "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>
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved