微信公众号爬坑(react)

  1. wx is not undefined javascript

解决方案: 在引入微信 JS-SDK 前,设置 define 和 require 值为 null。代码以下:java

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 重定向api

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 后添加 # 。app

相关文章
相关标签/搜索