1. wx is not undefined
解決方案: 在引入微信 JS-SDK 前,設置 define 和 require 值為 null。代碼如下:
1 <script type="text/javascript"> 2 define = null; 3 require = null; 4 </script> 5 <script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
2. 微信授權,獲取 code。使用 window.location.href 重定向
1 // 此地址需要公眾號后台管理中,接口權限==>網頁授權==>網頁授權域名 設置; 使用 encodeURIComponent 編碼回調地址 2 let backUrl = '微信授權后的回調地址'; // 例如 test.com/authorize, 3 let WXUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + APPID 4 +"&redirect_uri = " + encodeURIComponent(backUrl) + 5 "&response_type = code" + 6 "&scope = snsapi_userinfo" + 7 "&state = STATE#wechat_redirect"; 8 9 window.location.href = WXUrl;
3. 通過 config 接口注入權限驗證配置 iOS 平台必須保證 timestamp 參數是字符串
1 wx.config({ 2 debug: true, 3 appId: '', 4 timestamp: timestamp + ‘’, // 必填,生成簽名的時間戳。保證是字符串類型 5 nonceStr: '', 6 signature: '', 7 jsApiList: [] 8 });
4. iPhone 平台使用 H5 的 history 路由模式,頁面跳轉之后,window.location.href 方法獲取的地址始終是第一次進入頁面的 url ,導致調用 JS-SDK API 時失敗。可以使用 hash 路由模式解決這個問題,但是 hash 路由會在 url 后添加 # 。