钉钉扫码登陆-免登
1.获取三个重要参数
1.1APP_KEY 1.2 APP_SECRET 1.3AGENT_ID
钉钉登录
2.下载java版本的sdk引入maven
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibaba-dingtalk-service-sdk</artifactId>
<version>2.0.0</version>
</dependency>
3.编写代码
@Autowired
private RedisCache redisCache;
private String APP_KEY = "xxxxxxxxxxx";
private String APP_SECRET = "xxxxxxxxxxxxxxxxxxxxxx";
private Long AGENT_ID = xxxxxxxx;
private String MESSAGE_URL = "https://www.baidu.com";
private String PC_MESSAGE_URL = "https://www.baidu.com";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:dd");
/**
* 获取AccessToken
* @return AccessToken
* @throws ApiException
*/
private String getAccessToken() throws ApiException {
DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
OapiGettokenRequest request = new OapiGettokenRequest();
//Appkey
request.setAppkey(APP_KEY);
//Appsecret
request.setAppsecret(APP_SECRET);
/*请求方式*/
request.setHttpMethod("GET");
OapiGettokenResponse response = client.execute(request);
return response.getAccessToken();
}
/**
* 发送OA消息
* @param mobile 发送消息人的电话,多个英文逗号拼接
* @throws ApiException
*/
public void sendOA(String mobile) throws ApiException {
System.out.println("发送钉钉通知");
String accessToken = getAccessToken();
if(mobile == null || mobile.isEmpty()){
return;
}
//电话号码数组
String[] split = mobile.split(",");
for (String s : split) {
DingTalkClient client2 = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/get_by_mobile");
OapiUserGetByMobileRequest req = new OapiUserGetByMobileRequest();
req.setMobile(s);
req.setHttpMethod("GET");
OapiUserGetByMobileResponse rsp = client2.execute(req, accessToken);
//获取到Urid就是在公司里要发送到那个人的id
String urid = rsp.getUserid();
//根据用户id获取用户详情
DingTalkClient userDetail = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/get");
OapiUserGetRequest userReq = new OapiUserGetRequest();
userReq.setUserid(urid);
userReq.setHttpMethod("GET");
OapiUserGetResponse userRsp = userDetail.execute(userReq, accessToken);
String userName = userRsp.getName();
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
request.setUseridList(urid);
request.setAgentId(AGENT_ID);
request.setToAllUser(false);
OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
msg.setOa(new OapiMessageCorpconversationAsyncsendV2Request.OA());
//跳转链接
msg.getOa().setMessageUrl(MESSAGE_URL);
msg.getOa().setPcMessageUrl(PC_MESSAGE_URL);
//设置head
msg.getOa().setHead(new OapiMessageCorpconversationAsyncsendV2Request.Head());
msg.getOa().getHead().setText("待办事宜");
msg.getOa().getHead().setBgcolor("00409eff");
//设置body
msg.getOa().setBody(new OapiMessageCorpconversationAsyncsendV2Request.Body());
msg.getOa().getBody().setTitle("邮件现有功能已完成开发,抄送、密送以及发送异常处理暂未开始~!");
msg.getOa().getBody().setContent("创建人:" + userName + "\n创建时间:" + sdf.format(new Date()));
//消息类型
msg.setMsgtype("oa");
request.setMsg(msg);
System.out.println("获取发送通知消息体和获取发送通知人完成");
OapiMessageCorpconversationAsyncsendV2Response response = client.execute(request,accessToken);
System.out.println("发送消息是否成功"+response.isSuccess());
System.out.println(response.isSuccess());
System.out.println("消息任务ID"+response.getTaskId());
System.out.println(response.getTaskId());
}
}
/***
* 根据免密登录code获取用户信息
* @param requestAuthCode 免密登录code
* @throws ApiException
*/
@GetMapping("/getdingdingUserInfo/{requestAuthCode}")
public AjaxResult getUserInfo(@PathVariable("requestAuthCode")String requestAuthCode) throws ApiException {
if(StringUtils.isBlank(requestAuthCode)){
return AjaxResult.error("免登录code不能为空");
}
String accessToken = getAccessToken();
/*获取用户ID*/
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/getuserinfo");
OapiUserGetuserinfoRequest request = new OapiUserGetuserinfoRequest();
request.setCode(requestAuthCode);
request.setHttpMethod("GET");
OapiUserGetuserinfoResponse response = client.execute(request, accessToken);
String userId = response.getUserid();
/*获取用户详细信息*/
DingTalkClient getUserInfoClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/get");
OapiUserGetRequest getUserInfoRequest = new OapiUserGetRequest();
getUserInfoRequest.setUserid(userId);
getUserInfoRequest.setHttpMethod("GET");
OapiUserGetResponse execute = getUserInfoClient.execute(getUserInfoRequest, accessToken);
/*手机号*/
String mobile = execute.getMobile();
/*用户名*/
String name = execute.getName();
/*员工工号*/
String jobNumber = execute.getJobnumber();
String redisValues = mobile+":"+name+":"+jobNumber;
String md5Value = MD5.create().digestHex(redisValues);
/*存放Redis*/
try {
redisCache.setCacheObject(requestAuthCode, md5Value, 2, TimeUnit.HOURS);
return AjaxResult.success(execute);
}catch (Exception e){
return AjaxResult.error("token生产存储失败!");
}
}
@RequestMapping(value = "/getUserInfoSm/getUserInfo",method = RequestMethod.GET)
public AjaxResult getUserInfoSm(@RequestParam("code")String code) throws ApiException {
// 获取access_token,注意正式代码要有异常流处理
String access_token= getAccessToken();
// 通过临时授权码获取授权用户的个人信息
DefaultDingTalkClient client2 = new DefaultDingTalkClient("https://oapi.dingtalk.com/sns/getuserinfo_bycode");
OapiSnsGetuserinfoBycodeRequest reqBycodeRequest = new OapiSnsGetuserinfoBycodeRequest();
// 通过扫描二维码,跳转指定的redirect_uri后,向url中追加的code临时授权码
reqBycodeRequest.setTmpAuthCode(code);
OapiSnsGetuserinfoBycodeResponse bycodeResponse = client2.execute(reqBycodeRequest, APP_KEY,APP_SECRET);
// 根据unionid获取userid
String unionid = bycodeResponse.getUserInfo().getUnionid();
DingTalkClient clientDingTalkClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/getbyunionid");
OapiUserGetbyunionidRequest reqGetbyunionidRequest = new OapiUserGetbyunionidRequest();
reqGetbyunionidRequest.setUnionid(unionid);
OapiUserGetbyunionidResponse oapiUserGetbyunionidResponse = clientDingTalkClient.execute(reqGetbyunionidRequest,access_token);
// 根据userId获取用户信息
String userid = oapiUserGetbyunionidResponse.getResult().getUserid();
DingTalkClient clientDingTalkClient2 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
OapiV2UserGetRequest reqGetRequest = new OapiV2UserGetRequest();
reqGetRequest.setUserid(userid);
reqGetRequest.setLanguage("zh_CN");
OapiV2UserGetResponse rspGetResponse = clientDingTalkClient2.execute(reqGetRequest, access_token);
System.out.println(rspGetResponse.getBody());
Map<String, Object> map = new HashMap<String,Object>();
map.put("userInfo", rspGetResponse.getBody());
return AjaxResult.success(map);
}