<div id="login-qr-code"></div>
computed: {
getRedirectUrl() {
return encodeURIComponent(window.location.href);
},
getAuthUrl() {
return `https://oapi.dingtalk.com/connect/qrconnect?appid=${this.appid}&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=${this.getRedirectUrl}`;
},
getGoto() {
return encodeURIComponent(this.getAuthUrl);
},
},
data() {
return {
appid: undefined,
dingCodeConfig: {
id: "login-qr-code",
style: "border:none;background-color:#FFFFFF;",
width: "400",
height: "400",
},
};
},
async mounted(){
this.scanQrCodeLogin()
await this.getAppkey()
await this.getQrCode()
},
methods:{
getAppkey() {
api({
tenantId: tenantId
}).then(res => {
if (res.success && res.data) {
const {appKey} =res.data
this.appid=appKey
}
}).finally(() => {
})
},
getQrCode() {
let that = this;
let url = that.getRedirectUrl;
console.log('生成钉钉二维码=== getRedirectUrl',that.getRedirectUrl)
console.log('生成钉钉二维码=== appid',that.appid)
console.log('生成钉钉二维码===getGoto', that.getGoto)
let obj = DDLogin({
id: "login-qr-code",
goto: that.getGoto,
style: "border:none;background-color:#FFFFFF;",
width: "400",
height: "400"
});
console.log('生成钉钉二维码===getGoto', that.getGoto)
let handleMessage = (event) => {
let origin = event.origin;
console.log("origin", event.origin);
if (origin == "https://login.dingtalk.com") {
let loginTmpCode = event.data;
console.log("loginTmpCode", loginTmpCode);
window.location.href = `https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=${that.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);
}
},
扫码成功后会把页面进行重定向,地址会携带code 通过code进行接口登录。
scanQrCodeLogin(){
if(this.$route.query.code && this.$route.query.state){
api({
code: this.$route.query.code,
tenantId: tenantId,
type: 'scan'
}).then(res => {
})
}
},
}