public OrgDingtalkInfoVo dingLogin(String code){
String accessToken = AccessTokenUtil.getAccessToken(appKey, appSecret);
String userId = getUserId(code, accessToken);
//String userId = getUser(code, accessToken);
OapiV2UserGetResponse.UserGetResponse oapiV2UserGetResponseByUserId = getOapiV2UserGetResponseByUserId(userId, accessToken);
String phone = oapiV2UserGetResponseByUserId.getMobile();
if (StringUtils.isBlank(phone)){
throw new IgnoredException(ErrorCode.SYS_ERROR,"手机号为空");
}
List<OrgUserDingtalkDto> orgUserDingtalkDtos = orgUserDingtalkDtoRpcAdapter.getByPhone(phone);
AdminUserDto adminUserDto = adminUserDtoRpcAdapter.getByPhone(phone);
if (CollectionUtils.isEmpty(orgUserDingtalkDtos) && Objects.isNull(adminUserDto)){
throw new IgnoredException(ErrorCode.SYS_ERROR,"用户不存在机构");
}
List<OrgInfoVo> orgInfoVos = new ArrayList<>();
for (OrgUserDingtalkDto orgUserDingtalkDto : orgUserDingtalkDtos) {
OrgInfoVo orgInfoVo = new OrgInfoVo();
OrgDto orgDto = orgDtoRpcAdapter.getById(orgUserDingtalkDto.getOrgId());
BeanUtils.copyProperties(orgUserDingtalkDto,orgInfoVo);
orgInfoVo.setOrgName(orgDto.getOrgName());
orgInfoVos.add(orgInfoVo);
}
OrgDingtalkInfoVo orgDingtalkInfoVo = new OrgDingtalkInfoVo();
orgDingtalkInfoVo.setOrgUserInfo(orgInfoVos);
//判断是否是管理员
if (adminUserDto!=null){
orgDingtalkInfoVo.setIsAdmin(true);
orgDingtalkInfoVo.setUserName(adminUserDto.getUserName());
}else {
orgDingtalkInfoVo.setIsAdmin(false);
List<OrgUserDto> orgUserDtos = orgUserDtoRpcAdapter.getByPhone(phone);
orgDingtalkInfoVo.setUserName(orgUserDtos.get(0).getName());
}
return orgDingtalkInfoVo;
}
private String getUserId(String authCode, String accessToken) {
DingTalkClient client = new DefaultDingTalkClient(UrlConstant.GET_USER_INFO_URL);
OapiV2UserGetuserinfoRequest req = new OapiV2UserGetuserinfoRequest();
req.setCode(authCode);
OapiV2UserGetuserinfoResponse oapiV2UserGetuserinfoResponse;
try {
oapiV2UserGetuserinfoResponse = client.execute(req, accessToken);
if (oapiV2UserGetuserinfoResponse.isSuccess()) {
OapiV2UserGetuserinfoResponse.UserGetByCodeResponse userGetByCodeResponse = oapiV2UserGetuserinfoResponse.getResult();
return userGetByCodeResponse.getUserid();
} else {
throw new IgnoredException(ErrorCode.SYS_ERROR, oapiV2UserGetuserinfoResponse.getErrmsg());
}
} catch (ApiException e) {
e.printStackTrace();
throw new IgnoredException(ErrorCode.SYS_ERROR, e.getErrMsg());
}
}
java实现钉钉免登对接
最新推荐文章于 2024-09-08 10:40:56 发布