private final String SDINGTALKSERVICE="https://oapi.dingtalk.com";private final String METHOD_GET="GET";/*** 先从缓存查,再到钉钉查
*@paramcorpId
*@paramcorpSecret
*@return
*/
publicString getToken(String corpId,String corpSecret){
String access_token=RedisUtils.get("ACCESS_TOKEN");if(StringUtils.isEmpty(access_token)){
access_token=getTokenFromDing(corpId,corpSecret);
}returnaccess_token;
}/*** 企业内部开发获取access_token
*@paramcorpId 企业Id
*@paramcorpSecret 企业应用的凭证密钥
*@return
*/
publicString getTokenFromDing(String corpId,String corpSecret){
OapiGettokenResponse response= null;try{
DingTalkClient client= new DefaultDingTalkClient(SDINGTALKSERVICE+"/gettoken");
OapiGettokenRequest request= newOapiGettokenRequest();
request.setCorpid(corpId);
request.setCorpsecret(corpSecret);
request.setHttpMethod(METHOD_GET);
response=client.execute(request);
}catch(ApiException e) {
e.printStackTrace();
}
String body=response.getBody();
JSONObject jo=JSONObject.parseObject(body);
String errcode=jo.getString("errcode");
String access_token=null;if("0".equals(errcode)){
access_token=(String) jo.get("access_token");
RedisUtils.set("ACCESS_TOKEN", access_token, 3600);
}returnaccess_token;
}publicString getSnsToken(String appId, String appSecret){
String access_token=RedisUtils.get("SNS_ACCESS_TOKEN");if(StringUtils.isEmpty(access_token)){
access_token=getOpenToken(appId,appSecret);}returnaccess_token;
}/*** 获取钉钉开放应用的ACCESS_TOKEN
*@paramappId
*@paramappSecret
*@return
*/
publicString getOpenToken(String appId, String appSecret) {
OapiSnsGettokenResponse response= null;try{
DingTalkClient client= new DefaultDingTalkClient(SDINGTALKSERVICE+"/sns/gettoken");
OapiSnsGettokenRequest request= newOapiSnsGettokenRequest();
request.setAppid(appId);
request.setAppsecret(appSecret);
request.setHttpMethod(METHOD_GET);
response=client.execute(request);
}catch(ApiException e) {
e.printStackTrace();
}
String body=response.getBody();
JSONObject jo=JSONObject.parseObject(body);
String errcode=jo.getString("errcode");
String access_token=null;if("0".equals(errcode)){
access_token=(String) jo.get("access_token");
RedisUtils.set("SNS_ACCESS_TOKEN", access_token, 3600);
}returnaccess_token;
}/*** 获取用户授权的持久授权码
*@paramaccessToken
*@return
*/
publicString get_persistent_code(String accessToken,String code){
OapiSnsGetPersistentCodeResponse response= null;try{
DingTalkClient client= new DefaultDingTalkClient(SDINGTALKSERVICE+"/sns/get_persistent_code");
OapiSnsGetPersistentCodeRequest request= newOapiSnsGetPersistentCodeRequest();
request.setTmpAuthCode(code);
response=client.execute(request,accessToken);
}catch(ApiException e) {
e.printStackTrace();
}returnresponse.getBody();
}/*** 获取用户授权的SNS_TOKEN
*@paramopenId
*@parampersistentCode
*@paramaccessToken 开放应用的token
*@return
*/
publicString get_sns_token(String openId, String persistentCode,String accessToken) {
OapiSnsGetSnsTokenResponse response= null;try{
DingTalkClient client= new DefaultDingTalkClient(SDINGTALKSERVICE + "/sns/get_sns_token");
OapiSnsGetSnsTokenRequest request= newOapiSnsGetSnsTokenRequest();
request.setOpenid(openId);
request.setPersistentCode(persistentCode);
response=client.execute(request, accessToken);
}catch(ApiException e) {
e.printStackTrace();
}returnresponse.getSnsToken();
}/*** 获取用户授权的个人信息
*@paramsnsToken
*@return
*/
publicString get_sns_userinfo_unionid(String snsToken) {
OapiSnsGetuserinfoResponse response= null;try{
DingTalkClient client= new DefaultDingTalkClient(SDINGTALKSERVICE + "/sns/getuserinfo");
OapiSnsGetuserinfoRequest request= newOapiSnsGetuserinfoRequest();
request.setSnsToken(snsToken);
request.setHttpMethod(METHOD_GET);
response=client.execute(request);
}catch(ApiException e) {
e.printStackTrace();
}returnresponse.getBody();
}/*** 根据unionid获取userid
*@paramaccessToken
*@paramunionid
*@return
*/
publicString getUseridByUnionid(String accessToken,String unionid) {
OapiUserGetUseridByUnionidResponse response= null;try{
DingTalkClient client= new DefaultDingTalkClient(SDINGTALKSERVICE + "/user/getUseridByUnionid");
OapiUserGetUseridByUnionidRequest request= newOapiUserGetUseridByUnionidRequest();
request.setUnionid(unionid);
request.setHttpMethod(METHOD_GET);
response=client.execute(request, accessToken);
}catch(ApiException e) {
e.printStackTrace();
}returnresponse.getBody();
}/*** 获取用户授权的个人信息
*@paramaccessToken
*@paramuid
*@return
*/
publicString getUserGetOne(String accessToken,String uid) {
OapiUserGetResponse response= null;try{
DingTalkClient client= new DefaultDingTalkClient(SDINGTALKSERVICE + "/user/get");
OapiUserGetRequest request= newOapiUserGetRequest();
request.setUserid(uid);
request.setHttpMethod(METHOD_GET);
response=client.execute(request, accessToken);
}catch(ApiException e) {
e.printStackTrace();
}returnresponse.getBody();
}/*** 获取部门详情(根据部门id查询)
*@paramaccessToken
*@paramdeptid
*@return
*/
publicString getDepartmentInfo(String accessToken, String deptid) {
OapiDepartmentGetResponse response= null;try{
DingTalkClient client= new DefaultDingTalkClient(SDINGTALKSERVICE + "/department/get");
OapiDepartmentGetRequest request= newOapiDepartmentGetRequest();
request.setId(deptid);
request.setHttpMethod(METHOD_GET);
response=client.execute(request, accessToken);
}catch(ApiException e) {
e.printStackTrace();
}returnresponse.getBody();
}