H5 在微信加载?获取微信网页授权,页面重新加载一次?

一进入页面,跳转到微信网页授权,拿code,此时页面重新加载了一次,而且还能回退,怎么不让它重新加载且禁止回退呢?

      window.location.replace(
        `https://open.weixin.qq.com/connect/oauth2/authorize?appid=xxxx&redirect_uri=${encodeURIComponent(
          location.href
        )}&response_type=code&scope=snsapi_base&state=wechat_redirect#wechat_redirect`
      );
阅读 2.5k
1 个回答
function getQueryString(name) {
  const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
  const r = window.location.search.substr(1).match(reg);
  if (r != null) return unescape(r[2]);
  return null;
}

if (!getQueryString('code')) {
  const newUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=xxxx&redirect_uri=${encodeURIComponent(
    location.href
  )}&response_type=code&scope=snsapi_base&state=wechat_redirect#wechat_redirect`;

  history.replaceState(null, null, newUrl);

  window.addEventListener('popstate', function (e) {
    e.preventDefault();
  });

  window.location.href = newUrl;
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题