钉钉开放文档地址:https://open.dingtalk.com/document/isvapp-server/scan-qr-code-to-log-on-to-third-party-websites
<div v-if="curItem.type === 'DING_TALK'"
class="px-2 py-2 text-caption d-flex justify-center align-center"
style="width:270px;height:270px;margin: 0 auto;overflow:hidden;"
id="ding_code"
ref="ding_code">
</div>
data(){
return {
curItem:{}
}
},
mounted(){
if(this.curItem.type === 'Ding_TALK'){
this.fetchStart()
}
},
methods:{
fetchStart(){
//调接口获取startRes数据
this.loadJs(startRes)
},
async loadJs(startRes) {
const dingJs = "//g.alicdn.com/dingding/dinglogin/0.0.5/ddLogin.js";
await new Promise((resolve, reject) => {
// @see: https://stackoverflow.com/questions/8578617
let scriptEle = document.createElement("script");
scriptEle.type = "text/javascript";
scriptEle.async = false;
scriptEle.src = dingJs;
scriptEle.onload = resolve;
scriptEle.addEventListener("load", resolve);
scriptEle.addEventListener("error", e => reject(e.error));
document.head.appendChild(scriptEle);
});
this.dingLoginFn(
startRes.content.appid,
startRes.content.redirect_uri
);
}
},
dingLoginFn(appid, redirect_uri) {
this.ding_appid = appid;
this.ding_redirecturi = redirect_uri;
return new window.DDLogin({
id: "ding_code",
goto: encodeURIComponent(
`https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=${appid}&response_type=code&scope=snsapi_login&state=state&redirect_uri=${redirect_uri}`
),
style: "border:none;background: none;",
height: "330"
});
},
// 钉钉事件
dingEvent() {
let handleMessage = function(event) {
var origin = event.origin;
if (origin == "https://login.dingtalk.com") {
let loginTmpCode = event.data;
if (loginTmpCode) {
window.location.href = `https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=${this.ding_appid}&response_type=code&scope=snsapi_login&state=state&redirect_uri=${this.ding_redirecturi}&loginTmpCode=${loginTmpCode}`;
}
}
};
if (typeof window.addEventListener != "undefined") {
window.addEventListener("message", handleMessage, false);
} else if (typeof window.attachEvent != "undefined") {
window.attachEvent("onmessage", handleMessage);
}
},
},
watch:{
curItem() {
if (this.curItem.type === "DING_TALK") {
this.dingEvent();
}
}
}