1. 钉钉部分
链接: https://open-doc.dingtalk.com/microapp/serverapi2/kymkv6
2.login页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="login_container"></div>
<script src="http://g.alicdn.com/dingding/dinglogin/0.0.5/ddLogin.js"></script>
<script>
var url = encodeURIComponent('http://10.0.5.189:8000/#/user/login');
var goto = encodeURIComponent('https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=xxxxxxx&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=' + url)
var obj = DDLogin({
id: "login_container",//这里需要你在自己的页面定义一个HTML标签并设置id,例如<div id="login_container"></div>或<span id="login_container"></span>
goto: goto,
style: "border:none;background-color:#FFFFFF;",
width: "300",
height: "400"
});
var hanndleMessage = function (event) {
var origin = event.origin;
console.log("origin", event.origin);
if (origin == "https://login.dingtalk.com") { //判断是否来自ddLogin扫码事件。
var loginTmpCode = event.data; //拿到loginTmpCode后就可以在这里构造跳转链接进行跳转了
console.log("loginTmpCode", loginTmpCode);
var url2 = "https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=xxxxxxx&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=" + url + "&loginTmpCode=" + loginTmpCode;
window.location.href = url2;
}
};
if (typeof window.addEventListener != 'undefined') {
window.addEventListener('message', hanndleMessage, false);
} else if (typeof window.attachEvent != 'undefined') {
window.attachEvent('onmessage', hanndleMessage);
}
</script>
</body>
</html>
3. django view视图
def login(request):
"""登录验证"""
if request.method == "GET":
##########二维码认证登录#############
code = request.GET.get('code', )
appId = 'xxxxxxx'
appSecret = 'xxxxxxxx'
token = requests.get(
'https://oapi.dingtalk.com/sns/gettoken?appid={appId}&appsecret={appSecret}'.format(appId=appId,
appSecret=appSecret))
access_token = token.json()["access_token"]
tmp_auth_code = requests.post(
"https://oapi.dingtalk.com/sns/get_persistent_code?access_token={access_token}".format(
access_token=access_token),
json={
"tmp_auth_code": code
})
tmp_code = tmp_auth_code.json()
print(tmp_code)
openid = tmp_code['openid']
persistent_code = tmp_code['persistent_code']
sns_token_request = requests.post(
"https://oapi.dingtalk.com/sns/get_sns_token?access_token={access_token}".format(access_token=access_token),
json={
"openid": openid,
"persistent_code": persistent_code
})
sns_token = sns_token_request.json()['sns_token']
user_info_request = requests.get(
'https://oapi.dingtalk.com/sns/getuserinfo?sns_token={sns_token}'.format(sns_token=sns_token))
user_info = user_info_request.json()['user_info']
unionid = user_info.get('unionid')
user_obj = UserInfo.objects.filter(unionid=unionid).first()
request.session['username'] = user_obj.username # 登录成功后,用户登录信息存>放于session
request.session.set_expiry(86400) # 设置登录过期时间
content = {'code': 0,
'msg': 'success',
'user_info': {
'user_id': user_obj.id,
'username': user_obj.username,
'user_iphone': user_obj.phone,
'user_email': user_obj.email,
'user': user_obj.user,
'D_user': user_obj.D_user
}
}
####################################
content = {'code': 0, 'msg':'success',}
return JsonResponse(data=content,status=status.HTTP_200_OK)