(钉钉)第三方WEB网站扫码登录

[size=large]年底在做钉钉和公司的知识库产品的对接,怎么使用钉钉api的如下:[/size]
[size=large][b]第一步:[/b][/size]
[size=large]登录:https://oa.dingtalk.com/#/welcome[/size]
[size=large]这点可以自己建立一个企业账号进行测试[/size]

[img]http://dl2.iteye.com/upload/attachment/0129/0208/2d690eac-b6a3-3a94-8ac4-b0690794f2e2.png[/img]

[size=large]点击工作台建立应用[/size]

[img]http://dl2.iteye.com/upload/attachment/0129/0214/d7573e78-e551-3d85-af6d-27159a13587e.png[/img]

[size=large]设置[/size]


[img]http://dl2.iteye.com/upload/attachment/0129/0220/8783681b-96d0-3593-a90e-5075d0db5301.png[/img]


[size=large]参数说明:
1.应用logo:....
2.应用名称:必填
3.AgentID:自动生成,手机app自动登录需要使用
4.功能介绍:....
5.后端地址:访问api的接口地址
6.首页地址:改应用首页地址
7.pc版首页地址:登录页面地址
[/size]

[size=large]第二步[/size]
[size=large]扫码登录的js引入和api调用[/size]

[size=large]钉钉官网api地址:https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7629140.0.0.Dc2Nel&treeId=385&articleId=104882&docType=1[/size]

[size=large]使用[/size]
[size=large]1.引入[/size]


//声明钉钉二维码参数包括【扫描授权id、企业id】
var ding={"appid":"","agentid":"","corpid":"","uri":"","redirect_uri":"","code":"","accesstoken":""};
//1.5.1.设置扫描授权id
ding.appid="";
//1.5.2.设置重定向地址
ding.redirect_uri="";
//1.5.3.扫码触发跳转钉钉接口地址
ding.uri = "https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid="+ding.appid+"&response_type=code&scope=snsapi_login&state=STATE&redirect_uri="+ ding.redirect_uri;




//2.2.扫码触发事件
var obj = DDLogin({
id:"login_container",
goto: encodeURIComponent(ding.uri),
style: "border:none;background-color:#FFFFFF;",
width : "240",
height: "300",
});

//2.3.获取code
var getcode = function (event) {
var origin = event.origin;
console.log("origin", event.origin);
if( origin == "https://login.dingtalk.com" ) { //判断是否来自ddLogin扫码事件。
var loginTmpCode = event.data; //拿到loginTmpCode后就可以在这里构造跳转链接进行跳转了
ding.code=loginTmpCode;
console.log("获取到code", loginTmpCode);
window.parent.postMessage(loginTmpCode,'*');
//1.跳转到主页面,微应用自身截取code比对用户信息
var redirect_uri_check = "https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=dingoazjmclpvolnxq3wba&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=http://&loginTmpCode=";
window.location.href=redirect_uri_check+loginTmpCode;
};
};
//2.4.钉钉扫码监听
if (typeof window.addEventListener != 'undefined') {
window.addEventListener('message', getcode, false);
} else if (typeof window.attachEvent != 'undefined') {
window.attachEvent('onmessage', getcode);
};
//2.5.这儿主要是获取code,返回钉钉id,进行后端的钉钉id比对,api接口见下
var url = window.location.search.split("&");
var code=url[0].substring(url[0].lastIndexOf("=")+1);
console.log(code);
if(code!=""){
var uri="/"
$http({
url:uri,
method:"GET",
headers : {'Content-Type' : 'application/x-www-form-urlencoded'}
}).success(function(data){
console.log(data)
if(data!=""){
$window.localStorage["user"] = angular.toJson(data[0]);
window.location.href="/";
}else{
alert(data[0].msg);
$scope.user.info="";
window.location.href="/";
}
});
};



调用api在附件中

你可能感兴趣的:(钉钉)