项目背景:
一个H5活动页,获取企业微信的用户openId,然后通过openId调用接口判断用户是否是第一次登陆,第一登陆跳转index,不是第一次调转about。
开发:
参考很多博客,用了中转页面的方法,新建了一个welcome的白屏菜单,用来中转。
先上代码

<template>
  <div>
    <div>
      <p></p>
    </div>
  </div>
</template>

<script>
import { api } from "@/api/api";

export default {
  name: "welcome",
  data() {
    return {};
  },
  created() {
    this.getCode();
  },
  mounted() {},
  methods: {
    getCode() {
      // 非静默授权,第一次有弹框
      const code = this.getUrlParam("code"); // 截取路径中的code,如果没有就去微信授权,如果已经获取到了就直接传code给后台获取openId
      const local = window.location.href;
      const APPID = "wwcxxxxxxxxxx"; // 企业微信
      if (code == null || code == "") {
        window.location.href =
          "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
          APPID +
          "&redirect_uri=" +
          encodeURIComponent(local) +
          "&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
      } else {
        this.getOpenId(code); //把code传给后台获取用户信息
        console.log(code);
      }
    },
    getUrlParam(name) {
      var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");

      var r = window.location.search.substr(1).match(reg);
      console.log(r);

      if (r != null) return unescape(r[2]);

      return null;
    },

    getOpenId(code) {
      // 通过code获取 openId等用户信息,/api/user/wechat/login 为后台接口
      let _this = this;
      console.log(code);
      let param = {
        code: code
      };
      api("/app/weChat/getUserId", "get", param).then(res => {
        console.log(res);
          let userId ;
        if (res.data.code == 2000) {
              userId = res.data.data;
        }else{
            console.log(res);
            return;
        }
        localStorage.setItem("userId", userId);
        _this.getindexOne(userId);
      });
    },
    getindexOne(userId) {
      let params = {
        'channel': "qyvx",
        'openID': userId
      };
      api("/app/arrange/judgeOrder", "get", params).then(res => {
        if (res.data.data == 0) {
          this.$router.push({ name: "index" });
        //   window.location.replace("/#/index");
        } else {
          this.$router.push({ name: "about" });
        //   window.location.replace("/#/about");
        }
      });
    }
  }
};
</script>

Logo

前往低代码交流专区

更多推荐