當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
微信公众平台JSSDK开发
生活随笔
收集整理的這篇文章主要介紹了
微信公众平台JSSDK开发
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
根據(jù)微信開發(fā)文檔步驟如下:1.先登錄微信公眾平臺進(jìn)入“公眾號設(shè)置”的“功能設(shè)置”里填寫“JS接口安全域名”。
JS接口安全域名設(shè)置 mi.com(前面不用帶www/http,域名必須備案過)2.引入JS文件
在需要調(diào)用JS接口的頁面引入如下JS文件,(支持https):http://res.wx.qq.com/open/js/jweixin-1.0.0.js3.通過config接口注入權(quán)限驗(yàn)證配置
| 1 2 3 4 5 6 7 8 | wx.config({ ????debug: true,?// 開啟調(diào)試模式,調(diào)用的所有api的返回值會在客戶端alert出來,若要查看傳入的參數(shù),可以在pc端打開,參數(shù)信息會通過log打出,僅在pc端時才會打印。 ????appId:?'',?// 必填,公眾號的唯一標(biāo)識 ????timestamp: ,?// 必填,生成簽名的時間戳 ????nonceStr:?'',?// 必填,生成簽名的隨機(jī)串 ????signature:?'',// 必填,簽名,見附錄1 ????jsApiList: []?// 必填,需要使用的JS接口列表,所有JS接口列表見附錄2 }); |
?4.通過ready接口處理成功驗(yàn)證
| 1 2 3 4 | wx.ready(function(){ ????// config信息驗(yàn)證后會執(zhí)行ready方法,所有接口調(diào)用都必須在config接口獲得結(jié)果之后,config是一個客戶端的異步操作,所以如果需要在頁面加載時就調(diào)用相關(guān)接口,則須把相關(guān)接口放在ready函數(shù)中調(diào)用來確保正確執(zhí)行。對于用戶觸發(fā)時才調(diào)用的接口,則可以直接調(diào)用,不需要放在ready函數(shù)中。 }); |
?5.通過error接口處理失敗驗(yàn)證,測試時可以先不用這個
| 1 2 3 4 5 | wx.error(function(res){ ????// config信息驗(yàn)證失敗會執(zhí)行error函數(shù),如簽名過期導(dǎo)致驗(yàn)證失敗,具體錯誤信息可以打開config的debug模式查看,也可以在返回的res參數(shù)中查看,對于SPA可以在這里更新簽名。 }); |
JS-SDK網(wǎng)頁所有接口代碼如下:
| 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 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 | <?php require_once?"jssdk.php"; $jssdk?=?new?JSSDK("wx46c6ccb293dfsda5fd",?"e5e7ad8f4b068f118939f2cec0bc98394d");???//APPID和SECRET $signPackage?=?$jssdk->GetSignPackage(); ?> <!DOCTYPE html> <html> <head> ??<meta charset="utf-8"> ??<title>微信JS-SDK Demo</title> ??<meta name="viewport"?content="width=device-width, initial-scale=1, user-scalable=0"> ???<link rel="stylesheet"?href="http://demo.open.weixin.qq.com/jssdk/css/style.css?ts=1420774989">??? </head> <body> <div?class="wxapi_container"> ????<div?class="wxapi_index_container"> ??????<ul?class="label_box lbox_close wxapi_index_list"> ????????<li?class="label_item wxapi_index_item"><a?class="label_inner"?href="#menu-basic">基礎(chǔ)接口</a></li> ????????<li?class="label_item wxapi_index_item"><a?class="label_inner"?href="#menu-share">分享接口</a></li> ????????<li?class="label_item wxapi_index_item"><a?class="label_inner"?href="#menu-image">圖像接口</a></li> ????????<li?class="label_item wxapi_index_item"><a?class="label_inner"?href="#menu-voice">音頻接口</a></li> ????????<li?class="label_item wxapi_index_item"><a?class="label_inner"?href="#menu-smart">智能接口</a></li> ????????<li?class="label_item wxapi_index_item"><a?class="label_inner"?href="#menu-device">設(shè)備信息接口</a></li> ????????<li?class="label_item wxapi_index_item"><a?class="label_inner"?href="#menu-location">地理位置接口</a></li> ????????<li?class="label_item wxapi_index_item"><a?class="label_inner"?href="#menu-webview">界面操作接口</a></li> ????????<li?class="label_item wxapi_index_item"><a?class="label_inner"?href="#menu-scan">微信掃一掃接口</a></li> ????????<li?class="label_item wxapi_index_item"><a?class="label_inner"?href="#menu-shopping">微信小店接口</a></li> ????????<li?class="label_item wxapi_index_item"><a?class="label_inner"?href="#menu-card">微信卡券接口</a></li> ????????<li?class="label_item wxapi_index_item"><a?class="label_inner"?href="#menu-pay">微信支付接口</a></li> ??????</ul> ????</div> ????<div?class="lbox_close wxapi_form"> ??????<h3 id="menu-basic">基礎(chǔ)接口</h3> ??????判斷當(dāng)前客戶端是否支持指定JS接口 ??????<button?class="btn btn_primary"?id="checkJsApi">checkJsApi</button> ??????<h3 id="menu-share">分享接口</h3> ??????獲取“分享到朋友圈”按鈕點(diǎn)擊狀態(tài)及自定義分享內(nèi)容接口 ??????<button?class="btn btn_primary"?id="onMenuShareTimeline">onMenuShareTimeline</button> ??????獲取“分享給朋友”按鈕點(diǎn)擊狀態(tài)及自定義分享內(nèi)容接口 ??????<button?class="btn btn_primary"?id="onMenuShareAppMessage">onMenuShareAppMessage</button> ??????獲取“分享到QQ”按鈕點(diǎn)擊狀態(tài)及自定義分享內(nèi)容接口 ??????<button?class="btn btn_primary"?id="onMenuShareQQ">onMenuShareQQ</button> ??????獲取“分享到騰訊微博”按鈕點(diǎn)擊狀態(tài)及自定義分享內(nèi)容接口 ??????<button?class="btn btn_primary"?id="onMenuShareWeibo">onMenuShareWeibo</button> ??????<h3 id="menu-image">圖像接口</h3> ??????拍照或從手機(jī)相冊中選圖接口 ??????<button?class="btn btn_primary"?id="chooseImage">chooseImage</button> ??????預(yù)覽圖片接口 ??????<button?class="btn btn_primary"?id="previewImage">previewImage</button> ??????上傳圖片接口 ??????<button?class="btn btn_primary"?id="uploadImage">uploadImage</button> ??????下載圖片接口 ??????<button?class="btn btn_primary"?id="downloadImage">downloadImage</button> ??????<h3 id="menu-voice">音頻接口</h3> ??????開始錄音接口 ??????<button?class="btn btn_primary"?id="startRecord">startRecord</button> ??????停止錄音接口 ??????<button?class="btn btn_primary"?id="stopRecord">stopRecord</button> ??????播放語音接口 ??????<button?class="btn btn_primary"?id="playVoice">playVoice</button> ??????暫停播放接口 ??????<button?class="btn btn_primary"?id="pauseVoice">pauseVoice</button> ??????停止播放接口 ??????<button?class="btn btn_primary"?id="stopVoice">stopVoice</button> ??????上傳語音接口 ??????<button?class="btn btn_primary"?id="uploadVoice">uploadVoice</button> ??????下載語音接口 ??????<button?class="btn btn_primary"?id="downloadVoice">downloadVoice</button> ??????<h3 id="menu-smart">智能接口</h3> ??????識別音頻并返回識別結(jié)果接口 ??????<button?class="btn btn_primary"?id="translateVoice">translateVoice</button> ??????<h3 id="menu-device">設(shè)備信息接口</h3> ??????獲取網(wǎng)絡(luò)狀態(tài)接口 ??????<button?class="btn btn_primary"?id="getNetworkType">getNetworkType</button> ??????<h3 id="menu-location">地理位置接口</h3> ??????使用微信內(nèi)置地圖查看位置接口 ??????<button?class="btn btn_primary"?id="openLocation">openLocation</button> ??????獲取地理位置接口 ??????<button?class="btn btn_primary"?id="getLocation">getLocation</button> ??????<h3 id="menu-webview">界面操作接口</h3> ??????隱藏右上角菜單接口 ??????<button?class="btn btn_primary"?id="hideOptionMenu">hideOptionMenu</button> ??????顯示右上角菜單接口 ??????<button?class="btn btn_primary"?id="showOptionMenu">showOptionMenu</button> ??????關(guān)閉當(dāng)前網(wǎng)頁窗口接口 ??????<button?class="btn btn_primary"?id="closeWindow">closeWindow</button> ??????批量隱藏功能按鈕接口 ??????<button?class="btn btn_primary"?id="hideMenuItems">hideMenuItems</button> ??????批量顯示功能按鈕接口 ??????<button?class="btn btn_primary"?id="showMenuItems">showMenuItems</button> ??????隱藏所有非基礎(chǔ)按鈕接口 ??????<button?class="btn btn_primary"?id="hideAllNonBaseMenuItem">hideAllNonBaseMenuItem</button> ??????顯示所有功能按鈕接口 ??????<button?class="btn btn_primary"?id="showAllNonBaseMenuItem">showAllNonBaseMenuItem</button> ??????<h3 id="menu-scan">微信掃一掃</h3> ??????調(diào)起微信掃一掃接口 ??????<button?class="btn btn_primary"?id="scanQRCode0">scanQRCode(微信處理結(jié)果)</button> ??????<button?class="btn btn_primary"?id="scanQRCode1">scanQRCode(直接返回結(jié)果)</button> ??????<h3 id="menu-shopping">微信小店接口</h3> ??????跳轉(zhuǎn)微信商品頁接口 ??????<button?class="btn btn_primary"?id="openProductSpecificView">openProductSpecificView</button> ??????<h3 id="menu-card">微信卡券接口</h3> ??????批量添加卡券接口 ??????<button?class="btn btn_primary"?id="addCard">addCard</button> ??????調(diào)起適用于門店的卡券列表并獲取用戶選擇列表 ??????<button?class="btn btn_primary"?id="chooseCard">chooseCard</button> ??????查看微信卡包中的卡券接口 ??????<button?class="btn btn_primary"?id="openCard">openCard</button> ??????<h3 id="menu-pay">微信支付接口</h3> ??????發(fā)起一個微信支付請求 ??????<button?class="btn btn_primary"?id="chooseWXPay">chooseWXPay</button> ????</div> ??</div> </body> <!--步驟二:引入JS文件? --> <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script> <script> ????//步驟三:通過config接口注入權(quán)限驗(yàn)證配置 ??wx.config({ ??????debug: false, ??????appId:?'<?php echo $signPackage["appId"];?>', ??????timestamp: <?php?echo?$signPackage["timestamp"];?>, ??????nonceStr:?'<?php echo $signPackage["nonceStr"];?>', ??????signature:?'<?php echo $signPackage["signature"];?>', ??????jsApiList: [ ????????'checkJsApi', ????????'onMenuShareTimeline', ????????'onMenuShareAppMessage', ????????'onMenuShareQQ', ????????'onMenuShareWeibo', ????????'hideMenuItems', ????????'showMenuItems', ????????'hideAllNonBaseMenuItem', ????????'showAllNonBaseMenuItem', ????????'translateVoice', ????????'startRecord', ????????'stopRecord', ????????'onRecordEnd', ????????'playVoice', ????????'pauseVoice', ????????'stopVoice', ????????'uploadVoice', ????????'downloadVoice', ????????'chooseImage', ????????'previewImage', ????????'uploadImage', ????????'downloadImage', ????????'getNetworkType', ????????'openLocation', ????????'getLocation', ????????'hideOptionMenu', ????????'showOptionMenu', ????????'closeWindow', ????????'scanQRCode', ????????'chooseWXPay', ????????'openProductSpecificView', ????????'addCard', ????????'chooseCard', ????????'openCard' ??????] ??}); </script> <script> //步驟四:通過ready接口處理成功驗(yàn)證 wx.ready(function?() { ??// 1 判斷當(dāng)前版本是否支持指定 JS 接口,支持批量判斷 ??document.querySelector('#checkJsApi').onclick =?function?() { ????wx.checkJsApi({ ??????jsApiList: [ ????????'getNetworkType', ????????'previewImage' ??????], ??????success:?function?(res) { ????????alert(JSON.stringify(res)); ??????} ????}); ??}; ??// 2. 分享接口 ??// 2.1 監(jiān)聽“分享給朋友”,按鈕點(diǎn)擊、自定義分享內(nèi)容及分享結(jié)果接口 ??document.querySelector('#onMenuShareAppMessage').onclick =?function?() { ????wx.onMenuShareAppMessage({ ??????title:?'<a mcolored="true" href="http://www.it165.net/news/nhlw/" target="_blank" class="keylink">互聯(lián)網(wǎng)</a>之子', ??????desc:?'在長大的過程中,我才慢慢發(fā)現(xiàn),我身邊的所有事,別人跟我說的所有事,那些所謂本來如此,注定如此的事,它們其實(shí)沒有非得如此,事情是可以改變的。更重要的是,有些事既然錯了,那就該做出改變。', ??????link:?'http://movie.douban.com/subject/25785114/', ??????imgUrl:?'http://demo.open.weixin.qq.com/jssdk/images/p2166127561.jpg', ??????trigger:?function?(res) { ????????// 不要嘗試在trigger中使用ajax異步請求修改本次分享的內(nèi)容,因?yàn)榭蛻舳朔窒聿僮魇且粋€同步操作,這時候使用ajax的回包會還沒有返回 ????????alert('用戶點(diǎn)擊發(fā)送給朋友'); ??????}, ??????success:?function?(res) { ????????alert('已分享'); ??????}, ??????cancel:?function?(res) { ????????alert('已取消'); ??????}, ??????fail:?function?(res) { ????????alert(JSON.stringify(res)); ??????} ????}); ????alert('已注冊獲取“發(fā)送給朋友”狀態(tài)事件'); ??}; ??// 2.2 監(jiān)聽“分享到朋友圈”按鈕點(diǎn)擊、自定義分享內(nèi)容及分享結(jié)果接口 ??document.querySelector('#onMenuShareTimeline').onclick =?function?() { ????wx.onMenuShareTimeline({ ??????title:?'<a mcolored="true" href="http://www.it165.net/news/nhlw/" target="_blank" class="keylink">互聯(lián)網(wǎng)</a>之子', ??????link:?'http://movie.douban.com/subject/25785114/', ??????imgUrl:?'http://demo.open.weixin.<a mcolored="true" href="http://www.it165.net/qq/" target="_blank" class="keylink">qq</a>.com/jssdk/images/p2166127561.jpg', ??????trigger:?function?(res) { ????????// 不要嘗試在trigger中使用ajax異步請求修改本次分享的內(nèi)容,因?yàn)榭蛻舳朔窒聿僮魇且粋€同步操作,這時候使用ajax的回包會還沒有返回 ????????alert('用戶點(diǎn)擊分享到朋友圈'); ??????}, ??????success:?function?(res) { ????????alert('已分享'); ??????}, ??????cancel:?function?(res) { ????????alert('已取消'); ??????}, ??????fail:?function?(res) { ????????alert(JSON.stringify(res)); ??????} ????}); ????alert('已注冊獲取“分享到朋友圈”狀態(tài)事件'); ??}; ??// 2.3 監(jiān)聽“分享到QQ”按鈕點(diǎn)擊、自定義分享內(nèi)容及分享結(jié)果接口 ??document.querySelector('#onMenuShareQQ').onclick =?function?() { ????wx.onMenuShareQQ({ ??????title:?'互聯(lián)網(wǎng)之子', ??????desc:?'在長大的過程中,我才慢慢發(fā)現(xiàn),我身邊的所有事,別人跟我說的所有事,那些所謂本來如此,注定如此的事,它們其實(shí)沒有非得如此,事情是可以改變的。更重要的是,有些事既然錯了,那就該做出改變。', ??????link:?'http://movie.douban.com/subject/25785114/', ??????imgUrl:?'http://img3.douban.com/view/movie_poster_cover/spst/public/p2166127561.jpg', ??????trigger:?function?(res) { ????????alert('用戶點(diǎn)擊分享到QQ'); ??????}, ??????complete:?function?(res) { ????????alert(JSON.stringify(res)); ??????}, ??????success:?function?(res) { ????????alert('已分享'); ??????}, ??????cancel:?function?(res) { ????????alert('已取消'); ??????}, ??????fail:?function?(res) { ????????alert(JSON.stringify(res)); ??????} ????}); ????alert('已注冊獲取“分享到 QQ”狀態(tài)事件'); ??}; ??// 2.4 監(jiān)聽“分享到微博”按鈕點(diǎn)擊、自定義分享內(nèi)容及分享結(jié)果接口 ??document.querySelector('#onMenuShareWeibo').onclick =?function?() { ????wx.onMenuShareWeibo({ ??????title:?'互聯(lián)網(wǎng)之子', ??????desc:?'在長大的過程中,我才慢慢發(fā)現(xiàn),我身邊的所有事,別人跟我說的所有事,那些所謂本來如此,注定如此的事,它們其實(shí)沒有非得如此,事情是可以改變的。更重要的是,有些事既然錯了,那就該做出改變。', ??????link:?'http://movie.douban.com/subject/25785114/', ??????imgUrl:?'http://img3.douban.com/view/movie_poster_cover/spst/public/p2166127561.jpg', ??????trigger:?function?(res) { ????????alert('用戶點(diǎn)擊分享到微博'); ??????}, ??????complete:?function?(res) { ????????alert(JSON.stringify(res)); ??????}, ??????success:?function?(res) { ????????alert('已分享'); ??????}, ??????cancel:?function?(res) { ????????alert('已取消'); ??????}, ??????fail:?function?(res) { ????????alert(JSON.stringify(res)); ??????} ????}); ????alert('已注冊獲取“分享到微博”狀態(tài)事件'); ??}; ??// 3 智能接口 ??var?voice = { ????localId:?'', ????serverId:?'' ??}; ??// 3.1 識別音頻并返回識別結(jié)果 ??document.querySelector('#translateVoice').onclick =?function?() { ????if?(voice.localId ==?'') { ??????alert('請先使用 startRecord 接口錄制一段聲音'); ??????return; ????} ????wx.translateVoice({ ??????localId: voice.localId, ??????complete:?function?(res) { ????????if?(res.hasOwnProperty('translateResult')) { ??????????alert('識別結(jié)果:'?+ res.translateResult); ????????}?else?{ ??????????alert('無法識別'); ????????} ??????} ????}); ??}; ??// 4 音頻接口 ??// 4.2 開始錄音 ??document.querySelector('#startRecord').onclick =?function?() { ????wx.startRecord({ ??????cancel:?function?() { ????????alert('用戶拒絕授權(quán)錄音'); ??????} ????}); ??}; ??// 4.3 停止錄音 ??document.querySelector('#stopRecord').onclick =?function?() { ????wx.stopRecord({ ??????success:?function?(res) { ????????voice.localId = res.localId; ??????}, ??????fail:?function?(res) { ????????alert(JSON.stringify(res)); ??????} ????}); ??}; ??// 4.4 監(jiān)聽錄音自動停止 ??wx.onVoiceRecordEnd({ ????complete:?function?(res) { ??????voice.localId = res.localId; ??????alert('錄音時間已超過一分鐘'); ????} ??}); ??// 4.5 播放音頻 ??document.querySelector('#playVoice').onclick =?function?() { ????if?(voice.localId ==?'') { ??????alert('請先使用 startRecord 接口錄制一段聲音'); ??????return; ????} ????wx.playVoice({ ??????localId: voice.localId ????}); ??}; ??// 4.6 暫停播放音頻 ??document.querySelector('#pauseVoice').onclick =?function?() { ????wx.pauseVoice({ ??????localId: voice.localId ????}); ??}; ??// 4.7 停止播放音頻 ??document.querySelector('#stopVoice').onclick =?function?() { ????wx.stopVoice({ ??????localId: voice.localId ????}); ??}; ??// 4.8 監(jiān)聽錄音播放停止 ??wx.onVoicePlayEnd({ ????complete:?function?(res) { ??????alert('錄音('?+ res.localId +?')播放結(jié)束'); ????} ??}); ??// 4.8 上傳語音 ??document.querySelector('#uploadVoice').onclick =?function?() { ????if?(voice.localId ==?'') { ??????alert('請先使用 startRecord 接口錄制一段聲音'); ??????return; ????} ????wx.uploadVoice({ ??????localId: voice.localId, ??????success:?function?(res) { ????????alert('上傳語音成功,serverId 為'?+ res.serverId); ????????voice.serverId = res.serverId; ??????} ????}); ??}; ??// 4.9 下載語音 ??document.querySelector('#downloadVoice').onclick =?function?() { ????if?(voice.serverId ==?'') { ??????alert('請先使用 uploadVoice 上傳聲音'); ??????return; ????} ????wx.downloadVoice({ ??????serverId: voice.serverId, ??????success:?function?(res) { ????????alert('下載語音成功,localId 為'?+ res.localId); ????????voice.localId = res.localId; ??????} ????}); ??}; ??// 5 圖片接口 ??// 5.1 拍照、本地選圖 ??var?images = { ????localId: [], ????serverId: [] ??}; ??document.querySelector('#chooseImage').onclick =?function?() { ????wx.chooseImage({ ??????success:?function?(res) { ????????images.localId = res.localIds; ????????alert('已選擇 '?+ res.localIds.length +?' 張圖片'); ??????} ????}); ??}; ??// 5.2 圖片預(yù)覽 ??document.querySelector('#previewImage').onclick =?function?() { ????wx.previewImage({ ??????current:?'http://img5.douban.com/view/photo/photo/public/p1353993776.jpg', ??????urls: [ ????????'http://img3.douban.com/view/photo/photo/public/p2152117150.jpg', ????????'http://img5.douban.com/view/photo/photo/public/p1353993776.jpg', ????????'http://img3.douban.com/view/photo/photo/public/p2152134700.jpg' ??????] ????}); ??}; ??// 5.3 上傳圖片 ??document.querySelector('#uploadImage').onclick =?function?() { ????if?(images.localId.length == 0) { ??????alert('請先使用 chooseImage 接口選擇圖片'); ??????return; ????} ????var?i = 0, length = images.localId.length; ????images.serverId = []; ????function?upload() { ??????wx.uploadImage({ ????????localId: images.localId[i], ????????success:?function?(res) { ??????????i++; ??????????alert('已上傳:'?+ i +?'/'?+ length); ??????????images.serverId.push(res.serverId); ??????????if?(i < length) { ????????????upload(); ??????????} ????????}, ????????fail:?function?(res) { ??????????alert(JSON.stringify(res)); ????????} ??????}); ????} ????upload(); ??}; ??// 5.4 下載圖片 ??document.querySelector('#downloadImage').onclick =?function?() { ????if?(images.serverId.length === 0) { ??????alert('請先使用 uploadImage 上傳圖片'); ??????return; ????} ????var?i = 0, length = images.serverId.length; ????images.localId = []; ????function?download() { ??????wx.downloadImage({ ????????serverId: images.serverId[i], ????????success:?function?(res) { ??????????i++; ??????????alert('已下載:'?+ i +?'/'?+ length); ??????????images.localId.push(res.localId); ??????????if?(i < length) { ????????????download(); ??????????} ????????} ??????}); ????} ????download(); ??}; ??// 6 設(shè)備信息接口 ??// 6.1 獲取當(dāng)前網(wǎng)絡(luò)狀態(tài) ??document.querySelector('#getNetworkType').onclick =?function?() { ????wx.getNetworkType({ ??????success:?function?(res) { ????????alert(res.networkType); ??????}, ??????fail:?function?(res) { ????????alert(JSON.stringify(res)); ??????} ????}); ??}; ??// 7 地理位置接口 ??// 7.1 查看地理位置 ??document.querySelector('#openLocation').onclick =?function?() { ????wx.openLocation({ ??????latitude: 23.099994, ??????longitude: 113.324520, ??????name:?'TIT 創(chuàng)意園', ??????address:?'廣州市海珠區(qū)新港中路 397 號', ??????scale: 14, ??????infoUrl:?'http://weixin.<a mcolored="true" href="http://www.it165.net/qq/" target="_blank" class="keylink">qq</a>.com' ????}); ??}; ??// 7.2 獲取當(dāng)前地理位置 ??document.querySelector('#getLocation').onclick =?function?() { ????wx.getLocation({ ??????success:?function?(res) { ????????alert(JSON.stringify(res)); ??????}, ??????cancel:?function?(res) { ????????alert('用戶拒絕授權(quán)獲取地理位置'); ??????}, ??????fail:?function?(res) { ??????????alert(JSON.stringify(res)); ????????} ????}); ??}; ??// 8 界面操作接口 ??// 8.1 隱藏右上角菜單 ??document.querySelector('#hideOptionMenu').onclick =?function?() { ????wx.hideOptionMenu(); ??}; ??// 8.2 顯示右上角菜單 ??document.querySelector('#showOptionMenu').onclick =?function?() { ????wx.showOptionMenu(); ??}; ??// 8.3 批量隱藏菜單項(xiàng) ??document.querySelector('#hideMenuItems').onclick =?function?() { ????wx.hideMenuItems({ ??????menuList: [ ????????'menuItem:readMode',?// 閱讀模式 ????????'menuItem:share:timeline',?// 分享到朋友圈 ????????'menuItem:copyUrl'?// 復(fù)制鏈接 ??????], ??????success:?function?(res) { ????????alert('已隱藏“閱讀模式”,“分享到朋友圈”,“復(fù)制鏈接”等按鈕'); ??????}, ??????fail:?function?(res) { ????????alert(JSON.stringify(res)); ??????} ????}); ??}; ??// 8.4 批量顯示菜單項(xiàng) ??document.querySelector('#showMenuItems').onclick =?function?() { ????wx.showMenuItems({ ??????menuList: [ ????????'menuItem:readMode',?// 閱讀模式 ????????'menuItem:share:timeline',?// 分享到朋友圈 ????????'menuItem:copyUrl'?// 復(fù)制鏈接 ??????], ??????success:?function?(res) { ????????alert('已顯示“閱讀模式”,“分享到朋友圈”,“復(fù)制鏈接”等按鈕'); ??????}, ??????fail:?function?(res) { ????????alert(JSON.stringify(res)); ??????} ????}); ??}; ??// 8.5 隱藏所有非基本菜單項(xiàng) ??document.querySelector('#hideAllNonBaseMenuItem').onclick =?function?() { ????wx.hideAllNonBaseMenuItem({ ??????success:?function?() { ????????alert('已隱藏所有非基本菜單項(xiàng)'); ??????} ????}); ??}; ??// 8.6 顯示所有被隱藏的非基本菜單項(xiàng) ??document.querySelector('#showAllNonBaseMenuItem').onclick =?function?() { ????wx.showAllNonBaseMenuItem({ ??????success:?function?() { ????????alert('已顯示所有非基本菜單項(xiàng)'); ??????} ????}); ??}; ??// 8.7 關(guān)閉當(dāng)前窗口 ??document.querySelector('#closeWindow').onclick =?function?() { ????wx.closeWindow(); ??}; ??// 9 微信原生接口 ??// 9.1.1 掃描二維碼并返回結(jié)果 ??document.querySelector('#scanQRCode0').onclick =?function?() { ????wx.scanQRCode(); ??}; ??// 9.1.2 掃描二維碼并返回結(jié)果 ??document.querySelector('#scanQRCode1').onclick =?function?() { ????wx.scanQRCode({ ??????needResult: 1, ??????desc:?'scanQRCode desc', ??????success:?function?(res) { ????????alert(JSON.stringify(res)); ??????} ????}); ??}; ??// 10 微信支付接口 ??// 10.1 發(fā)起一個支付請求 ??document.querySelector('#chooseWXPay').onclick =?function?() { ????// 注意:此 Demo 使用 2.7 版本支付接口實(shí)現(xiàn),建議使用此接口時參考微信支付相關(guān)最新文檔。 ????wx.chooseWXPay({ ??????timestamp: 1414723227, ??????nonceStr:?'noncestr', ??????package:?'addition=action_id%3dgaby1234%26limit_pay%3d&bank_type=WX&body=innertest&fee_type=1&input_charset=GBK?ify_url=http%3A%2F%2F120.204.206.246%2Fcgi-bin%2Fmmsupport-bin%2Fnotifypay&out_trade_no=1414723227818375338&partner=1900000109&spbill_create_ip=127.0.0.1&total_fee=1&sign=432B647FE95C7BF73BCD177CEECBEF8D', ??????signType:?'SHA1',?// 注意:新版支付接口使用 MD5 加密 ??????paySign:?'bd5b1933cda6e9548862944836a9b52e8c9a2b69' ????}); ??}; ??// 11.3? 跳轉(zhuǎn)微信商品頁 ??document.querySelector('#openProductSpecificView').onclick =?function?() { ????wx.openProductSpecificView({ ??????productId:?'pDF3iY_m2M7EQ5EKKKWd95kAxfNw' ????}); ??}; ??// 12 微信卡券接口 ??// 12.1 添加卡券 ??document.querySelector('#addCard').onclick =?function?() { ????wx.addCard({ ??????cardList: [ ????????{ ??????????cardId:?'pDF3iY9tv9zCGCj4jTXFOo1DxHdo', ??????????cardExt:?'{"code": "", "openid": "", "timestamp": "1418301401", "signature":"64e6a7cc85c6e84b726f2d1cbef1b36e9b0f9750"}' ????????}, ????????{ ??????????cardId:?'pDF3iY9tv9zCGCj4jTXFOo1DxHdo', ??????????cardExt:?'{"code": "", "openid": "", "timestamp": "1418301401", "signature":"64e6a7cc85c6e84b726f2d1cbef1b36e9b0f9750"}' ????????} ??????], ??????success:?function?(res) { ????????alert('已添加卡券:'?+ JSON.stringify(res.cardList)); ??????} ????}); ??}; ??// 12.2 選擇卡券 ??document.querySelector('#chooseCard').onclick =?function?() { ????wx.chooseCard({ ??????cardSign:?'97e9c5e58aab3bdf6fd6150e599d7e5806e5cb91', ??????timestamp: 1417504553, ??????nonceStr:?'k0hGdSXKZEj3Min5', ??????success:?function?(res) { ????????alert('已選擇卡券:'?+ JSON.stringify(res.cardList)); ??????} ????}); ??}; ??// 12.3 查看卡券 ??document.querySelector('#openCard').onclick =?function?() { ????alert('您沒有該公眾號的卡券無法打開卡券。'); ????wx.openCard({ ??????cardList: [ ??????] ????}); ??}; ??var?shareData = { ????title:?'微信JS-SDK Demo', ????desc:?'微信JS-SDK,幫助第三方為用戶提供更優(yōu)質(zhì)的移動web服務(wù)', ????link:?'http://demo.open.weixin.qq.com/jssdk/', ????imgUrl:?'http://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRt8Qia4lv7k3M9J1SKqKCImxJCt7j9rHYicKDI45jRPBxdzdyREWnk0ia0N5TMnMfth7SdxtzMvVgXg/0' ??}; ??wx.onMenuShareAppMessage(shareData); ??wx.onMenuShareTimeline(shareData); }); wx.error(function?(res) { ??alert(res.errMsg); }); </script> </html> |
?注:上面的url變量一定得在前面微信公眾平臺設(shè)置的mi.com下的網(wǎng)址,即這個有JS-SDK功能的網(wǎng)頁要放在mi.com下,要不然會出錯。
總結(jié)
以上是生活随笔為你收集整理的微信公众平台JSSDK开发的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 中国银行的卡在其他银行能取钱不
- 下一篇: 广发特定商户限额后多久可以解除