第一步:
登录:https://oa.dingtalk.com/#/welcome
这点可以自己建立一个企业账号进行测试
![2d690eac-b6a3-3a94-8ac4-b0690794f2e2.png](https://i-blog.csdnimg.cn/blog_migrate/b72249e81de18eae16cf8bd3862295d1.png)
点击工作台建立应用
![d7573e78-e551-3d85-af6d-27159a13587e.png](https://i-blog.csdnimg.cn/blog_migrate/25fb5302189b9ea5f7abc5db54ca94be.png)
设置
![8783681b-96d0-3593-a90e-5075d0db5301.png](https://i-blog.csdnimg.cn/blog_migrate/e3d2fc617c08af6aae65148d00aeaf79.png)
参数说明:
1.应用logo:....
2.应用名称:必填
3.AgentID:自动生成,手机app自动登录需要使用
4.功能介绍:....
5.后端地址:访问api的接口地址
6.首页地址:改应用首页地址
7.pc版首页地址:登录页面地址
第二步
扫码登录的js引入和api调用
钉钉官网api地址:https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7629140.0.0.Dc2Nel&treeId=385&articleId=104882&docType=1
使用
1.引入
<script src="//g.alicdn.com/dingding/dinglogin/0.0.5/ddLogin.js"></script>
//声明钉钉二维码参数包括【扫描授权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在附件中