vue项目获取微信权限前端。

微信公众号的教程确实有些简陋了
需要现在微信公众号里配置域名然后在需要权限的页面 跳转到 微信指定页面 后面会更新微信支付的内容
App.Vue

export default {
  name: "App",
  created() {
    var code = this.GetUrlParame("code"); // 截取code
    console.log(code);
    console.log(this.$route);
    if (!code) {
      window.location.href =
        "https://open.weixin.qq.com/connect/oauth2/authorize?appid=appid&redirect_uri=配置的url&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
    } else {
      this.$http
        .get(`http://www.ooctmedia.com:8898/lecture/user/openid?code=${code}`)//此处是后端给我返回的接口,就是如果有code 做的操作
        .then(res => {
          console.log(res);
          localStorage.setItem("openid", JSON.stringify(res.data.openid));
        });
    }
  },
  methods: {
    GetUrlParame(parameName) {
      var parames = window.location.search;
      // 检测参数是否存在
      if (parames.indexOf(parameName) > -1) {
        var parameValue = "";
        parameValue = parames.substring(
          parames.indexOf(parameName),
          parames.length
        );
        // 检测后面是否还有参数
        if (parameValue.indexOf("&") > -1) {
          // 去除后面多余的参数, 得到最终 parameName=parameValue 形式的值
          parameValue = parameValue.substring(0, parameValue.indexOf("&"));
          // 去掉参数名, 得到最终纯值字符串
          parameValue = parameValue.replace(parameName + "=", "");
          return parameValue;
        }
        return "";
      }
    }
  }
};

你可能感兴趣的:(vue项目获取微信权限前端。)