钉钉授权扫码登录

回调域名,需要在后台配置
AppKey,在钉钉后台获取
等在手机确定授权登录,会调用重定向到钉钉那,然后钉钉在跳转回来,
会带上一个临时的code ,

http://172.16.150.72:8080/#/login?code=1f0e565439bf37419036f38f08ad10e3&state=STATE

然后我们就可以在login这个页面,加入判断,带上code请求后台获取用户信息了

<template>
	<div id="login_container"></div>
</template>
<script>
export default {
	data() {
		return {
			AppKey: '你的AppKey',
		};
	},
	methods: {
		DDLogin(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);
		}
	},
	mounted() {
        let baseUrl = encodeURIComponent('http://172.16.150.72:8080/#/login') // 回调域名:扫码成功跳转的url,需要进行编码
		let url = encodeURIComponent(
            `https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=${this.AppKey}&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=${baseUrl}`
            ) // goto参数,需要进行编码
        var obj = this.DDLogin({
            id: "login_container", //这里需要你在自己的页面定义一个HTML标签并设置id,例如<div id="login_container"></div>或<span id="login_container"></span>
            goto: url, // 转码后的url
            style: "border:none;background-color:#FFFFFF;", // 二维码的样式
            width: "400", // 二维码的宽度
            height: "400" // 二维码的高度
        });
        // **width和height不代表二维码的大小,二维码大小是固定的210px210px。
        var hanndleMessage = function (event) {	
            let {origin} = event;
            if (origin == "https://login.dingtalk.com") { //判断是否来自ddLogin扫码事件。
                let {data:loginTmpCode} = event; //拿到loginTmpCode后就可以在这里构造跳转链接进行跳转了
                // 通过JS获取到loginTmpCode后,需要由你构造并跳转到如下链接:
                // `https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=APPID&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=REDIRECT_URI&loginTmpCode=loginTmpCode` 
                // 此链接处理成功后,会302跳转到你goto参数指定的redirect_uri,并向url参数中追加临时授权码code及state两个参数。
                window.location.href=`https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=${this.AppKey}&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=${baseUrl}&loginTmpCode=${loginTmpCode}`
			}
        };
        if (typeof window.addEventListener != 'undefined') {
            window.addEventListener('message', hanndleMessage, false);
        } else if (typeof window.attachEvent != 'undefined') {
            window.attachEvent('onmessage', hanndleMessage);
        }

	}
};
</script>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值