1、在微信開放平台配置關聯相關APP、微信小程序、微信公眾號;
2、在微信公眾號中設置安全域名;
3、在開放者平台中的APP配置關聯JS網頁安全域名;
4、把相關Html頁面代碼放到對應域名下;
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no"> <title>Demo</title> </head> <body> <div id="app"> <wx-open-launch-weapp path="pages/index/index" id="launch-wxapp" username="gh_12"> <template> <style> .btn { padding: 12px } </style> <button class="btn-download">點擊打開小程序</button> </template> </wx-open-launch-weapp> <wx-open-launch-app id="launch-btn" appid="wx12" extinfo='{"PageType":1001}'> <script type="text/wxtag-template"> <style> .btn { padding: 12px } </style> <button class="btn">點擊打開APP</button> </script> </wx-open-launch-app> </div> </body> <script type="text/javascript" src="js/jquery-1.11.1.min.js"></script> <script type="text/javascript" src="https://res2.wx.qq.com/open/js/jweixin-1.6.0.js "></script> <script> getAppList() function getAppList() { var link = location.href; let _params = { url: link }; $.ajax({ url: "https://demo.com/WeiXinService.svc/GetWxInfo", type: "POST", contentType: "application/json", data: JSON.stringify(_params), success: function (data) { const result = data; wx.config({ debug: false, // 開啟調試模式,調用的所有api的返回值會在客戶端alert出來,若要查看傳入的參數,可以在pc端打開,參數信息會通過log打出,僅在pc端時才會打印。 appId: result.appid, // 必填,公眾號的唯一標識 timestamp: result.timestamp, // 必填,生成簽名的時間戳 nonceStr: result.noncestr, // 必填,生成簽名的隨機串 signature: result.signature, // 必填,簽名 jsApiList: ['onMenuShareTimeline', 'chooseImage'], // 必填,需要使用的JS接口列表 openTagList: ['wx-open-launch-weapp', 'wx-open-launch-app'] // 可選,需要使用的開放標簽列表,例如['wx-open-launch-app'] }) wx.ready(function (res) { // config信息驗證后會執行ready方法,所有接口調用都必須在config接口獲得結果之后,config是一個客戶端的異步操作,所以如果需要在頁面加載時就調用相關接口,則須把相關接口放在ready函數中調用來確保正確執行。對於用戶觸發時才調用的接口,則可以直接調用,不需要放在ready函數中 var btn = document.getElementById('launch-btn'); btn.addEventListener('launch', function (e) { console.log('success', e); }); btn.addEventListener('error', function (e) { console.log('fail', e.detail); //打開失敗則跳轉到下載頁 if (e.detail.errMsg == 'launch:fail') { if (navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)) { window.location = "https://itunes.apple.com/"; //ios app下載地址 } else if (navigator.userAgent.match(/android/i)) { window.location = "https://demo.com/download.html"; //android app下載地址 } } }); }); wx.error(function (res) { // config信息驗證失敗會執行error函數,如簽名過期導致驗證失敗,具體錯誤信息可以打開config的debug模式查看,也可以在返回的res參數中查看,對於SPA可以在這里更新簽名 }); } }); } </script> </html>
