PC网页端第三方微信登录

1、window.open 弹出框

 loginClick(tab, event) {
      if (this.activeName == "third") {
        var top = (window.screen.availHeight - 450) / 2; //获得窗口的垂直位置;
        var left = (window.screen.availWidth - 400) / 2; //获得窗口的水平位置;
        window.open(
          `yourUrl/qrCode.html`,
          "_blank",
          `top=${top},left=${left},width=400,height=450,menubar=no,toolbar=no,status=no,scrollbars=yes`
        );
      }
    },

2、qrcode.html,获取二维码

<html>
    <head>

    </head>
    <body>
        <div style="height:400px">
            <iframe
                frameborder="no"
                border="0"
                marginwidth="0"
                marginheight="0"
                scrolling="no"
                id="qrcode"
                title="Inline Frame Example"
                width="400"
                height="400"
                src=""
            ></iframe>
        </div>
        
        <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
        <script>
             function getQueryVariable(variable) {
                var query = window.location.search.substring(1);
                var vars = query.split("&");
                for (var i = 0; i < vars.length; i++) {
                var pair = vars[i].split("=");
                if (pair[0] == variable) {
                    return pair[1];
                }
                }
                return false;
            }
            var userId = getQueryVariable("userId");
            console.log('userId: ', userId);
            var params = {
                source: "wechat_open",
            }
            if(userId){
                params.userId = userId
            }
            //生成二维码
            $.ajax({
                    type: "get",
                    url:"yourUrlblade-auth/oauth/render",
                    data:params,
                    success:function(res){
                       $("#qrcode").attr("src",res.data)
                    }
                })
        </script>
    </body>
</html>

3、微信扫码回调页面

<html>
  <head> </head>
  <body>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script>
      function getQueryVariable(variable) {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
          var pair = vars[i].split("=");
          if (pair[0] == variable) {
            return pair[1];
          }
        }
        return false;
      }
      var code = getQueryVariable("code");
      var state = getQueryVariable("state");
      $.ajax({
        type: "get",
        url: "yourUrl/blade-auth/oauth/check/wechat_open",
        data: {
          code: code,
          state: state,
        },
        success: function (res) {
          var bindState = 1;
          var message = "绑定成功";
          if (res.data.binding == "0") {
            bindState = 0;
            tip = res.msg;
          } 
          window.opener.postMessage(
            {
              status: bindState,
              message:message,
              code:code,
              state:state,
              oauthId:res.data.oauthId
            }, '*'
          );
          window.close();
        },
      });
    </script>
  </body>
</html>

4、回调处理页面,根据返回结果,处理各自业务逻辑

created() {
    this.addEventListenerMessage();
  },
destroyed() {
    this.removeEventListener();
  },
  methods: {
    removeEventListener(){
        window.removeEventListener("message",this.eventHandle);
    },
    eventHandle(e){
        let that = this;
        let data = e.data;
         console.log('data.status: ', data.status);
        if (data.status == 1) {
          //获取用户信息
          that.$store.dispatch("LoginBySocial", {
              oauthId:data.oauthId
          }).then(() => {
            //获取用户信息
            getUserInfo().then((res) => {
              let user = res.data.data;
              window.localStorage.setItem("roleId", user.roleId);
              window.localStorage.setItem("deptName", user.deptName);
              window.localStorage.setItem("tenantId", user.tenantId);
              that.$router.push({ path: "/wel/index" });
            });
          });
        } else if (data.status == 0) {
          that.$router.push({ path: "/accountBind", query: {
              oauthId:data.oauthId
          } });
        }
    }
    }
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值