前提:
钉钉管理后台创建应用,添加个人信息接口权限,填写登录回调域名,即redirect_uri
方式一:钉钉扫码登录
index.html中的head标签中引入
<script src="https://g.alicdn.com/dingding/dinglogin/0.0.5/ddLogin.js"></script>
<template>
<div id="dd-login" />
</template>
<script>
export default {
mounted() {
this.ddInit()
},
methods: {
ddInit() {
const appid = ''
const url = encodeURIComponent('重定向的url')
const goto = encodeURIComponent(`https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=${appid}&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=${url}`)
window.DDLogin({
id: 'dd-login',
goto, //请参考注释里的方式
style: 'border:none;background-color:#FFFFFF;',
width: '100%',
height: '300'
});
let handleMessage = (event) =>{
let origin = event.origin;
console.log("origin", event.origin);
if (origin === "https://login.dingtalk.com") {
//判断是否来自ddLogin扫码事件。
const loginTmpCode = event.data;
//获取到loginTmpCode后就可以在这里构造跳转链接进行跳转了
console.log("loginTmpCode", loginTmpCode);
window.location.href = `https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=${appid}&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=${url}&loginTmpCode=${loginTmpCode}`;
}
};
if (typeof window.addEventListener != 'undefined') {
window.addEventListener('message', handleMessage, false);
} else if (typeof window.attachEvent != 'undefined') {
window.attachEvent('onmessage', handleMessage);
}
},
}
}
</script>
方式二:对接钉钉统一身份认证
redirect_uri:编码后的重定向地址
client_id:
-
企业内部应用:client_id为应用的AppKey。
-
第三方企业应用:client_id为应用的SuiteKey。
其他参数固定默认值
钉钉在url返回authCode,后台根据code获取个人信息