Created
May 29, 2020 10:20
-
-
Save akkuman/4bef607211309991084f3293db350538 to your computer and use it in GitHub Desktop.
钉钉扫码前端demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="zh"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<script src="http://g.alicdn.com/dingding/dinglogin/0.0.5/ddLogin.js"></script> | |
<!--[if IE]> | |
<script src="http://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script> | |
<![endif]--> | |
</head> | |
<body> | |
<!-- Page Content --> | |
<div id="login_container">test</div> | |
<!-- /#wrapper --> | |
<script src="http://cdn.bootcss.com/jquery/1.11.0/jquery.min.js" type="text/javascript"></script> | |
<script>window.jQuery || document.write('<script src="js/jquery-1.11.0.min.js"><\/script>')</script> | |
<script type="text/javascript"> | |
$(document).ready(function () { | |
// 钉钉扫码 | |
let appId = 'dingoaki41bp1dxpifejrd'; // appId 点击创建扫码登录应用授权生成 | |
let baseUrl = encodeURIComponent('http://127.0.0.1:8000/dingcallback'); // 回调域名:扫码成功跳转的url,需要进行编码 | |
dingdata = { | |
id:"login_container",//这里需要你在自己的页面定义一个HTML标签并设置id,例如<div id="login_container"></div>或<span id="login_container"></span> | |
goto: encodeURIComponent("https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid="+ appId + "&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=" + baseUrl), | |
style: "", | |
href: "", | |
width : "300px", | |
height: "300px" | |
} | |
var obj = DDLogin(dingdata); | |
// 获取钉钉postMessage返回的code | |
var loginTmpCode = '' | |
var hanndleMessage = function (event) { | |
var loginTmpCode = event.data; //拿到loginTmpCode后就可以在这里构造跳转链接进行跳转了 | |
var origin = event.origin; | |
console.log(loginTmpCode) | |
console.log(origin) | |
if (origin === 'https://login.dingtalk.com') { | |
redirect_url = 'https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=APPID&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=' + baseUrl+ '&loginTmpCode=' + loginTmpCode | |
window.location.href = redirect_url; | |
console.log(redirect_url) | |
} | |
}; | |
if (typeof window.addEventListener != 'undefined') { | |
window.addEventListener('message', hanndleMessage, false); | |
} else if (typeof window.attachEvent != 'undefined') { | |
window.attachEvent('onmessage', hanndleMessage); | |
} | |
var trigger = $('.hamburger'), | |
overlay = $('.overlay'), | |
isClosed = false; | |
trigger.click(function () { | |
hamburger_cross(); | |
}); | |
function hamburger_cross() { | |
if (isClosed == true) { | |
overlay.hide(); | |
trigger.removeClass('is-open'); | |
trigger.addClass('is-closed'); | |
isClosed = false; | |
} else { | |
overlay.show(); | |
trigger.removeClass('is-closed'); | |
trigger.addClass('is-open'); | |
isClosed = true; | |
} | |
} | |
$('[data-toggle="offcanvas"]').click(function () { | |
$('#wrapper').toggleClass('toggled'); | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment