微信公众号爬坑,react

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