CHAT with me on luogo!

钉钉登录二维码嵌套在vue页面中

转自 https://www.csdn.net/tags/OtDacg3sMjQ2NTgtYmxvZwO0O0OO0O0O.html

  • 钉钉登录二维码嵌套在vue页面中
    2021-09-04 14:42:45
    <template>
      <div id="app">
        <div id="login_container"></div>
      </div>
    </template>
    <script>
    import axios from "axios";
    
    
    export default {
    
    name: "App",
    
    components: {},
    
    data() {
    
    return {
    
    appid: "dingb3x07ieb8wrwmw81",
    
    redirectUrl: "https://console.huishouzx.com/login",
    
    apiUrl:
    
    "<用户信息接口,把redirectUrl回调获取的CODE给后端用于获取扫码用户信息>",
    
    dingCodeConfig: {
    
    id: "login_container",
    
    style: "border:none;background-color:#FFFFFF;",
    
    width: "365",
    
    height: "400",
    
    },
    
    };
    
    },
    
    computed: {
    
    getRedirectUrl() {
    
    return encodeURIComponent(this.redirectUrl);
    
    },
    
    getAuthUrl() {
    
    return https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=${this.appid}&amp;response_type=code&amp;scope=snsapi_login&amp;state=STATE&amp;redirect_uri=${this.getRedirectUrl};
    
    },
    
    getGoto() {
    
    return encodeURIComponent(this.getAuthUrl);
    
    },
    
    getDingCodeConfig() {
    
    return { ...this.dingCodeConfig, goto: this.getGoto };
    
    },
    
    },
    
    created() {
    
    //调用
    
    this.initDingJs();
    
    },
    
    mounted() {
    
    this.addDingListener();
    
    this.initDingLogin();
    
    this.getUser();
    
    },
    
    methods: {
    
    initDingJs() {
    
    !(function (window, document) {
    
    function d(a) {
    
    var e,
    
    c = document.createElement("iframe"),
    
    d = "https://login.dingtalk.com/login/qrcode.htm?goto=" + a.goto;
    
    (d += a.style ? "&style=" + encodeURIComponent(a.style) : ""),
    
    (d += a.href ? "&href=" + a.href : ""),
    
    (c.src = d),
    
    (c.frameBorder = "0"),
    
    (c.allowTransparency = "true"),
    
    (c.scrolling = "no"),
    
    (c.width = a.width ? a.width + "px" : "365px"),
    
    (c.height = a.height ? a.height + "px" : "400px"),
    
    (e = document.getElementById(a.id)),
    
    (e.innerHTML = ""),
    
    e.appendChild(c);
    
    }
    
    
    
        window.DDLogin = d;
      })(window, document);
    },
    addDingListener() {
      let self = this;
    
      let handleLoginTmpCode = function (loginTmpCode) {
        window.location.href =
          self.getAuthUrl + `&amp;loginTmpCode=${loginTmpCode}`;
      };
    
      let handleMessage = function (event) {
        if (event.origin == "https://login.dingtalk.com") {
          handleLoginTmpCode(event.data);
        }
      };
    
      if (typeof window.addEventListener != "undefined") {
        window.addEventListener("message", handleMessage, false);
      } else if (typeof window.attachEvent != "undefined") {
        window.attachEvent("onmessage", handleMessage);
      }
    },
    initDingLogin() {
      window.DDLogin(this.getDingCodeConfig);
    },
    getUser() {
      let getQueryString = function (name) {
        var reg = new RegExp("(^|&amp;)" + name + "=([^&amp;]*)(&amp;|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) {
          return unescape(r[2]);
        }
        return null;
      };
    
      let code = getQueryString("code");
    
      if (code !== null) {
        axios
          .get(`${this.apiUrl}?code=${code}`)
          .then((response) =&gt; {
            console.log(response);
          })
          .catch((error) =&gt; {
            console.log(error);
          });
      }
    },
    
    
    
    
    },
    
    };
    
    </script>
    
    
    
    
    <style>
    
    
    
    app {
    
    https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=${this.appid}&amp;response_type=code&amp;scope=snsapi_login&amp;state=STATE&amp;redirect_uri=${this.getRedirectUrl}
        window.DDLogin = d;
      })(window, document);
    },
    addDingListener() {
      let self = this;
    
      let handleLoginTmpCode = function (loginTmpCode) {
        window.location.href =
          self.getAuthUrl + `&amp;loginTmpCode=${loginTmpCode}`;
      };
    
      let handleMessage = function (event) {
        if (event.origin == "https://login.dingtalk.com") {
          handleLoginTmpCode(event.data);
        }
      };
    
      if (typeof window.addEventListener != "undefined") {
        window.addEventListener("message", handleMessage, false);
      } else if (typeof window.attachEvent != "undefined") {
        window.attachEvent("onmessage", handleMessage);
      }
    },
    initDingLogin() {
      window.DDLogin(this.getDingCodeConfig);
    },
    getUser() {
      let getQueryString = function (name) {
        var reg = new RegExp("(^|&amp;)" + name + "=([^&amp;]*)(&amp;|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) {
          return unescape(r[2]);
        }
        return null;
      };
    
      let code = getQueryString("code");
    
      if (code !== null) {
        axios
          .get(`${this.apiUrl}?code=${code}`)
          .then((response) =&gt; {
            console.log(response);
          })
          .catch((error) =&gt; {
            console.log(error);
          });
      }
    },
    font-family: Avenir, Helvetica, Arial, sans-serif;
    
    -webkit-font-smoothing: antialiased;
    
    -moz-osx-font-smoothing: grayscale;
    
    text-align: center;
    
    color: #2c3e50;
    
    margin-top: 60px;
    
    }
    
    </style>
    
    

    在这里插入图片描述
    在这里插入图片描述

    在这里插入图片描述

    更多相关内容
  • posted @   qsad阿斯顿  阅读(368)  评论(0编辑  收藏  举报
    相关博文:
    阅读排行:
    · 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
    · Manus爆火,是硬核还是营销?
    · 终于写完轮子一部分:tcp代理 了,记录一下
    · 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
    · 单元测试从入门到精通
    点击右上角即可分享
    微信分享提示
    西雅图
    01:03发布
    西雅图
    01:03发布
    6°
    中雨
    西北风
    3级
    空气质量
    相对湿度
    90%
    今天
    中雨
    3°/12°
    周二
    小雨
    4°/11°
    周三
    小雨
    3°/9°