天天看點

vue 開發微信公衆号H5面面--擷取微信授權

近期有項目的需求 是用到微信授權的,在網上找了很多,

但是可能是微信sdk比較新的原因,網上很多都不适用

是以,自己取巧的用了一個方法解決了

//login.vue
  let url = window.location.origin + "/static/getCode.html";

    if (
      wxCode == "" ||
      typeof wxCode == "undefined" ||
      typeof wxCode == "null"
    ) {
      this.url = `code: ${wxCode}`;
      console.log(111, wxCode);
      window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${
        configs.appID
      }&redirect_uri=${encodeURIComponent(
        url
      )}&response_type=code&scope=snsapi_userinfo&state=succ`;
      return;
    }
           
//getCode.html
<!DOCTYPE html>
<html >

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width,height=device-height,inital-scale=1.0,maximum-scale=1.0,user-scalable=no;" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>h5_1xy</title>
</head>

<body>

</body>
<script>
  function getUrlParams(name) {
    let reg = new RegExp("(\\?|&)" + name + "=([^&]*)(&|$)", "i");
    let r = window.location.search.match(reg);
    if (r != null) return unescape(r[2]);
    return null;
  }
  localStorage.wxCode = getUrlParams("code")
  window.location.href = window.location.origin

</script>

</html>
           

用一個放置在static的靜态頁面做中轉頁面,擷取code,這樣,在vue中就不會出現跳轉兩次的情況了

繼續閱讀