@ -1,170 +1,26 @@
package com.dsideal.FengHuang. Util ;
package com.dsideal.FengHuang. DingTalk ;
import cn.binarywang.tools.generator.ChineseMobileNumberGenerator ;
import cn.hutool.core.util.PhoneUtil ;
import com.alibaba.fastjson.JSONArray ;
import com.alibaba.fastjson.JSONObject ;
import com.aliyun.dingtalkoauth2_1_0.models.* ;
import com.aliyun.dingtalkoauth2_1_0.models.GetUserTokenRequest ;
import com.aliyun.dingtalkoauth2_1_0.models.GetUserTokenResponse ;
import com.aliyun.tea.TeaException ;
import com.aspose.slides.Collections.ArrayList ;
import com.dingtalk.api.DefaultDingTalkClient ;
import com.dingtalk.api.DingTalkClient ;
import com.dingtalk.api.request.* ;
import com.dingtalk.api.response.* ;
import com.dsideal.FengHuang.Util.CommonUtil ;
import com.jfinal.kit.Kv ;
import com.jfinal. plugin.activerecord.Db ;
import com.jfinal. kit.StrKit ;
import com.jfinal.plugin.activerecord.Record ;
import com.jfinal.plugin.redis.Redis ;
import com.taobao.api.ApiException ;
import java.util.* ;
public class DingTalkUtil {
/ * *
* 使 用 Token 初 始 化 账 号 Client
*
* @return Client
* @throws Exception
* /
private static com . aliyun . dingtalkoauth2_1_0 . Client createClient ( ) throws Exception {
com . aliyun . teaopenapi . models . Config config = new com . aliyun . teaopenapi . models . Config ( ) ;
config . protocol = "https" ;
config . regionId = "central" ;
return new com . aliyun . dingtalkoauth2_1_0 . Client ( config ) ;
}
/ *
功 能 : 获 取 钉 钉 的 AccessToken
作 者 : 黄 海
时 间 : 2023 - 06 - 06
* /
public static String getAccessToken ( String appKey , String appSecret ) throws Exception {
final String KEY = "DingTalkAccessToken" ;
if ( ! Redis . use ( ) . exists ( KEY ) ) {
String accessToken = null ;
com . aliyun . dingtalkoauth2_1_0 . Client client = createClient ( ) ;
com . aliyun . dingtalkoauth2_1_0 . models . GetAccessTokenRequest getAccessTokenRequest = new com . aliyun . dingtalkoauth2_1_0 . models . GetAccessTokenRequest ( )
. setAppKey ( appKey )
. setAppSecret ( appSecret ) ;
try {
GetAccessTokenResponse res = client . getAccessToken ( getAccessTokenRequest ) ;
accessToken = res . getBody ( ) . getAccessToken ( ) ;
} catch ( TeaException err ) {
if ( ! com . aliyun . teautil . Common . empty ( err . code ) & & ! com . aliyun . teautil . Common . empty ( err . message ) ) {
}
} catch ( Exception _err ) {
TeaException err = new TeaException ( _err . getMessage ( ) , _err ) ;
if ( ! com . aliyun . teautil . Common . empty ( err . code ) & & ! com . aliyun . teautil . Common . empty ( err . message ) ) {
}
}
Redis . use ( ) . setex ( KEY , 5400 , accessToken ) ; //5400:一个半小时过期
return accessToken ;
} else {
return Redis . use ( ) . get ( KEY ) ;
}
}
/ *
功 能 : 获 取 钉 钉 的 JsApiToken - > ticket
作 者 : 黄 海
时 间 : 2023 - 06 - 06
* /
public static String getJsApiToken ( String access_token ) throws Exception {
final String KEY = "DingTalkJsApiToken" ;
if ( ! Redis . use ( ) . exists ( KEY ) ) {
String jsApiToken = null ;
com . aliyun . dingtalkoauth2_1_0 . Client client = createClient ( ) ;
com . aliyun . dingtalkoauth2_1_0 . models . CreateJsapiTicketHeaders createJsapiTicketHeaders = new com . aliyun . dingtalkoauth2_1_0 . models . CreateJsapiTicketHeaders ( ) ;
createJsapiTicketHeaders . xAcsDingtalkAccessToken = access_token ;
try {
CreateJsapiTicketResponse res = client . createJsapiTicketWithOptions ( createJsapiTicketHeaders , new com . aliyun . teautil . models . RuntimeOptions ( ) ) ;
jsApiToken = res . getBody ( ) . getJsapiTicket ( ) ;
} catch ( TeaException err ) {
if ( ! com . aliyun . teautil . Common . empty ( err . code ) & & ! com . aliyun . teautil . Common . empty ( err . message ) ) {
}
} catch ( Exception _err ) {
TeaException err = new TeaException ( _err . getMessage ( ) , _err ) ;
if ( ! com . aliyun . teautil . Common . empty ( err . code ) & & ! com . aliyun . teautil . Common . empty ( err . message ) ) {
}
}
Redis . use ( ) . setex ( KEY , 5400 , jsApiToken ) ; //5400:一个半小时过期
return jsApiToken ;
} else {
return Redis . use ( ) . get ( KEY ) ;
}
}
/ *
功 能 : 获 取 钉 钉 的 SsoToken
作 者 : 黄 海
时 间 : 2023 - 06 - 06
* /
public static String getSsoToken ( String corpId , String ssoSecret ) throws Exception {
final String KEY = "DingTalkSsoToken" ;
if ( ! Redis . use ( ) . exists ( KEY ) ) {
String ssoToken = null ;
com . aliyun . dingtalkoauth2_1_0 . Client client = createClient ( ) ;
GetSsoAccessTokenRequest getSsoAccessTokenRequest = new GetSsoAccessTokenRequest ( )
. setCorpid ( corpId )
. setSsoSecret ( ssoSecret ) ;
try {
GetSsoAccessTokenResponse res = client . getSsoAccessToken ( getSsoAccessTokenRequest ) ;
ssoToken = res . getBody ( ) . getAccessToken ( ) ;
} catch ( TeaException err ) {
if ( ! com . aliyun . teautil . Common . empty ( err . code ) & & ! com . aliyun . teautil . Common . empty ( err . message ) ) {
}
} catch ( Exception _err ) {
TeaException err = new TeaException ( _err . getMessage ( ) , _err ) ;
if ( ! com . aliyun . teautil . Common . empty ( err . code ) & & ! com . aliyun . teautil . Common . empty ( err . message ) ) {
}
}
Redis . use ( ) . setex ( KEY , 5400 , ssoToken ) ; //5400:一个半小时过期
return ssoToken ;
} else {
return Redis . use ( ) . get ( KEY ) ;
}
}
/ * *
* 功 能 : 获 取 单 个 人 员 信 息
*
* @param access_token
* @param userId
* @return
* /
public static String getPerson ( String access_token , String userId ) {
try {
DingTalkClient client = new DefaultDingTalkClient ( "https://oapi.dingtalk.com/topapi/v2/user/get" ) ;
OapiV2UserGetRequest req = new OapiV2UserGetRequest ( ) ;
req . setUserid ( userId ) ;
req . setLanguage ( "zh_CN" ) ;
OapiV2UserGetResponse rsp = client . execute ( req , access_token ) ;
return rsp . getBody ( ) ;
} catch ( ApiException e ) {
e . printStackTrace ( ) ;
}
return null ;
}
//TODO 未完成
public static String getPersonToken ( String appKey , String appSecret ) throws Exception {
com . aliyun . dingtalkoauth2_1_0 . Client client = createClient ( ) ;
GetUserTokenRequest getUserTokenRequest = new GetUserTokenRequest ( )
. setClientId ( appKey )
. setClientSecret ( appSecret )
//.setCode("abcd")
. setRefreshToken ( "abcd" )
. setGrantType ( "authorization_code" ) ;
try {
GetUserTokenResponse res = client . getUserToken ( getUserTokenRequest ) ;
System . out . println ( res . getBody ( ) . getAccessToken ( ) ) ;
System . out . println ( res . getBody ( ) . getRefreshToken ( ) ) ;
} catch ( TeaException err ) {
System . out . println ( err ) ;
} catch ( Exception _err ) {
System . out . println ( _err ) ;
}
return null ;
}
public class OrgPerson {
/ * *
* 功 能 : 创 建 部 门
@ -172,12 +28,16 @@ public class DingTalkUtil {
* @param access_token
* @throws ApiException
* /
public static long createDept ( String access_token , String deptName , long parentId , long orderId ) throws ApiException {
public static long createDept ( String access_token , String deptName , long parentId , long orderId , boolean isRoot ) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient ( "https://oapi.dingtalk.com/topapi/v2/department/create" ) ;
OapiV2DepartmentCreateRequest req = new OapiV2DepartmentCreateRequest ( ) ;
req . setParentId ( parentId ) ;
req . setOrder ( orderId ) ;
req . setName ( deptName ) ;
if ( isRoot ) {
req . setOuterDept ( true ) ;
req . setOuterDeptOnlySelf ( true ) ;
}
OapiV2DepartmentCreateResponse rsp = client . execute ( req , access_token ) ;
JSONObject jo = JSONObject . parseObject ( rsp . getBody ( ) ) ;
if ( jo . getLong ( "errcode" ) > 0 ) {
@ -229,6 +89,7 @@ public class DingTalkUtil {
* /
public static List < Kv > deptList = new ArrayList ( ) ;
public static void getDeptList ( String access_token , long dept_id ) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient ( "https://oapi.dingtalk.com/topapi/v2/department/listsub" ) ;
OapiV2DepartmentListsubRequest req = new OapiV2DepartmentListsubRequest ( ) ;
@ -244,101 +105,22 @@ public class DingTalkUtil {
long childDeptId = j . getLongValue ( "dept_id" ) ;
kv . set ( "dept_id" , childDeptId ) ;
kv . set ( "name" , j . getString ( "name" ) ) ;
//kv.set("parent_id", j.getLongValue("parent_id"));
deptList . add ( kv ) ;
getDeptList ( access_token , childDeptId ) ; //递归
}
}
/ * *
* 功 能 : 创 建 人 员
*
* @param access_token
* @param deptId
* @param userId
* @param personName
* @param tel
* @param zhiWei
* @throws ApiException
* /
public static void createPerson ( String access_token , String deptId , String userId , String personName , String tel , String zhiWei ) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient ( "https://oapi.dingtalk.com/topapi/v2/user/create" ) ;
OapiV2UserCreateRequest req = new OapiV2UserCreateRequest ( ) ;
req . setUserid ( userId ) ;
req . setName ( personName ) ;
req . setMobile ( tel ) ;
req . setTitle ( zhiWei ) ;
req . setDeptIdList ( deptId ) ;
OapiV2UserCreateResponse rsp = client . execute ( req , access_token ) ;
//System.out.println(rsp.getBody());
}
/ * *
* 功 能 : 删 除 人 员
*
* @param access_token
* @param userId
* @throws ApiException
* /
public static void delPerson ( String access_token , String userId ) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient ( "https://oapi.dingtalk.com/topapi/v2/user/delete" ) ;
OapiV2UserDeleteRequest req = new OapiV2UserDeleteRequest ( ) ;
req . setUserid ( userId ) ;
OapiV2UserDeleteResponse rsp = client . execute ( req , access_token ) ;
//System.out.println(rsp.getBody());
}
public static void writeDtDeptId ( int orgId , String value ) {
String sql = "update t_base_organization set dingtalk_dept_id=? where org_id=?" ;
Db . update ( sql , value , orgId ) ;
}
public static long getDtDeptId ( int org_id ) {
String sql = "select dingtalk_dept_id from t_base_organization where org_id=?" ;
Record r = Db . findFirst ( sql , org_id ) ;
if ( r . get ( "dingtalk_dept_id" ) = = null ) return 1 ;
return r . get ( "dingtalk_dept_id" ) ;
}
public static Record getOrgByOrgName ( String org_name ) {
String sql = "select org_id,org_name,sort_id from t_base_organization where org_name = ?" ;
return Db . findFirst ( sql , org_name ) ;
}
public static List < Record > getOrgList ( int orgId ) {
// 学校及学校下的部门
String sql = "select org_id,org_name,parent_id,sort_id,dingtalk_dept_id from t_base_organization where bureau_id=? and org_id<>bureau_id order by org_id" ;
List < Record > list = Db . find ( sql , orgId ) ;
return list ;
}
public static void importYptOrg ( String accessToken , int orgId ) throws ApiException {
//1、读取云平台数据, 创建学校
/ *
需 要 先 手 动 执 行 , 对 表 增 加 一 列 , 记 录 钉 钉 的 部 门 ID 号 :
ALTER TABLE ` dsideal_db ` . ` t_base_organization `
ADD COLUMN ` dingtalk_dept_id ` bigint NULL COMMENT ' 钉 钉 中 的 部 门 ID ' AFTER ` zydz ` ,
ADD INDEX ( ` dingtalk_dept_id ` ) ;
* /
//单位需要清空一下这个属性,有枣没枣都打一下子
writeDtDeptId ( orgId , null ) ;
// 学校及学校下的部门
List < Record > list = getOrgList ( orgId ) ;
public static void delAllDept ( String access_token , String orgName ) throws ApiException {
Record rOrg = Model . getOrgByOrgName ( orgName ) ;
int bureauId = rOrg . getInt ( "org_id" ) ;
//获取数据库中记录的钉钉端的dept_id,但不一定准确存在,需要继续判断一下
long DT_BureauId = Model . getDtDeptId ( bureauId ) ;
delDept ( access_token , DT_BureauId ) ;
for ( int i = 0 ; i < list . size ( ) ; i + + ) {
orgId = list . get ( i ) . getInt ( "org_id" ) ;
int parentId = list . get ( i ) . getInt ( "parent_id" ) ;
if ( parentId = = - 1 ) parentId = orgId ;
String dept_name = list . get ( i ) . getStr ( "org_name" ) ;
long dingtalk_dept_id = getDtDeptId ( parentId ) ;
long sortId = list . get ( i ) . getLong ( "sort_id" ) ;
//创建
long dt_dept_id = DingTalkUtil . createDept ( accessToken , dept_name , dingtalk_dept_id , sortId ) ;
//回写
writeDtDeptId ( orgId , String . valueOf ( dt_dept_id ) ) ;
deptList . clear ( ) ;
getDeptList ( access_token , DT_BureauId ) ;
for ( int i = 0 ; i < deptList . size ( ) ; i + + ) {
delDept ( access_token , deptList . get ( i ) . getLong ( "dept_id" ) ) ;
}
}
@ -358,18 +140,16 @@ public class DingTalkUtil {
}
public static void syncOrg ( String accessToken , String orgName ) throws ApiException {
Record rOrg = getOrgByOrgName( orgName ) ;
Record rOrg = Model. getOrgByOrgName( orgName ) ;
int bureauId = rOrg . getInt ( "org_id" ) ;
int sortId = rOrg . getInt ( "sort_id" ) ;
//获取部门信息
deptList . clear ( ) ;
//获取数据库中记录的钉钉端的dept_id,但不一定准确存在,需要继续判断一下
long DT_BureauId = getDtDeptId( bureauId ) ;
long DT_BureauId = Model . getDtDeptId ( bureauId ) ;
JSONObject jRes = getDeptInfo ( accessToken , DT_BureauId ) ;
if ( jRes . getLong ( "errcode" ) > 0 ) {
DT_BureauId = createDept ( accessToken , orgName , 1 , sortId );
writeDtDeptId( bureauId , String . valueOf ( DT_BureauId ) ) ; //记得要回写
DT_BureauId = createDept ( accessToken , orgName , 1 , sortId , true );
Model. writeDtDeptId( bureauId , String . valueOf ( DT_BureauId ) ) ; //记得要回写
CommonUtil . Print ( "单位名称:" + orgName + "不存在!已创建!" ) ;
} else {
CommonUtil . Print ( "单位名称:" + orgName + "已存在,保留!" ) ;
@ -397,7 +177,7 @@ public class DingTalkUtil {
mapOfA . put ( deptId , jo ) ;
}
//B:云平台
List < Record > list = getOrgList( bureauId ) ;
List < Record > list = Model. getOrgList( bureauId ) ;
for ( int i = 0 ; i < list . size ( ) ; i + + ) {
if ( list . get ( i ) . get ( "dingtalk_dept_id" ) ! = null ) {
long deptId = list . get ( i ) . getLong ( "dingtalk_dept_id" ) ;
@ -447,20 +227,245 @@ public class DingTalkUtil {
int parentId = mapOfB . get ( key ) . getInteger ( "parent_id" ) ;
int org_id = mapOfB . get ( key ) . getInteger ( "org_id" ) ;
//这个部门是在哪个部门下的
long dingTalkParentDeptId = getDtDeptId( parentId ) ;
long dt_dept_id = createDept ( accessToken , org_name , dingTalkParentDeptId , sId );
writeDtDeptId( org_id , String . valueOf ( dt_dept_id ) ) ;
long dingTalkParentDeptId = Model. getDtDeptId( parentId ) ;
long dt_dept_id = createDept ( accessToken , org_name , dingTalkParentDeptId , sId , false );
Model. writeDtDeptId( org_id , String . valueOf ( dt_dept_id ) ) ;
CommonUtil . Print ( "成功创建部门:" + org_name ) ;
}
}
}
/ * *
* 功 能 : 创 建 人 员
*
* @param access_token
* @param deptId
* @param userId
* @param personName
* @param tel
* @param zhiWei
* @throws ApiException
* /
public static void createPerson ( String access_token , String deptId , String userId , String personName , String tel , String zhiWei ) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient ( "https://oapi.dingtalk.com/topapi/v2/user/create" ) ;
OapiV2UserCreateRequest req = new OapiV2UserCreateRequest ( ) ;
req . setUserid ( userId ) ;
req . setName ( personName ) ;
req . setMobile ( tel ) ;
req . setTitle ( zhiWei ) ;
req . setDeptIdList ( deptId ) ;
OapiV2UserCreateResponse rsp = client . execute ( req , access_token ) ;
//System.out.println(rsp.getBody());
}
/ * *
* 功 能 : 删 除 人 员
*
* @param access_token
* @param userId
* @throws ApiException
* /
public static void delPerson ( String access_token , String userId ) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient ( "https://oapi.dingtalk.com/topapi/v2/user/delete" ) ;
OapiV2UserDeleteRequest req = new OapiV2UserDeleteRequest ( ) ;
req . setUserid ( userId ) ;
OapiV2UserDeleteResponse rsp = client . execute ( req , access_token ) ;
//System.out.println(rsp.getBody());
}
public static void updatePerson ( String access_token , String person_id , String person_name ) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient ( "https://oapi.dingtalk.com/topapi/v2/user/update" ) ;
OapiV2UserUpdateRequest req = new OapiV2UserUpdateRequest ( ) ;
req . setUserid ( person_id ) ;
req . setName ( person_name ) ;
OapiV2UserUpdateResponse rsp = client . execute ( req , access_token ) ;
System . out . println ( rsp . getBody ( ) ) ;
}
public static List < Kv > personList = new ArrayList ( ) ;
public static void getDeptPerson ( String access_token , long dept_id ) throws ApiException {
DingTalkClient client = new DefaultDingTalkClient ( "https://oapi.dingtalk.com/topapi/user/listsimple" ) ;
long next_cursor = 0 ;
while ( true ) {
OapiUserListsimpleRequest req = new OapiUserListsimpleRequest ( ) ;
req . setDeptId ( dept_id ) ;
req . setCursor ( next_cursor ) ;
req . setSize ( 10L ) ;
req . setOrderField ( "modify_desc" ) ;
req . setContainAccessLimit ( false ) ;
req . setLanguage ( "zh_CN" ) ;
OapiUserListsimpleResponse rsp = client . execute ( req , access_token ) ;
JSONObject jo = JSONObject . parseObject ( rsp . getBody ( ) ) ;
JSONArray ja = jo . getJSONObject ( "result" ) . getJSONArray ( "list" ) ;
if ( ja = = null ) return ;
for ( int i = 0 ; i < ja . size ( ) ; i + + ) {
JSONObject j = ja . getJSONObject ( i ) ;
Kv kv = Kv . create ( ) ;
String userid = j . getString ( "userid" ) ;
String name = j . getString ( "name" ) ;
kv . set ( "userid" , userid ) ;
kv . set ( "name" , name ) ;
personList . add ( kv ) ;
}
if ( jo . getJSONObject ( "result" ) . getBoolean ( "has_more" ) )
next_cursor = jo . getJSONObject ( "result" ) . getLong ( "next_cursor" ) ;
else break ;
}
}
public static void getBureauPerson ( String access_token , long dept_id ) throws ApiException {
//获取有哪些部门
deptList . clear ( ) ;
personList . clear ( ) ;
getDeptPerson ( access_token , dept_id ) ;
getDeptList ( access_token , dept_id ) ;
for ( int i = 0 ; i < deptList . size ( ) ; i + + ) {
getDeptPerson ( access_token , deptList . get ( i ) . getInt ( "dept_id" ) ) ;
}
}
public static void delBureauPerson ( String accessToken , String orgName ) throws ApiException {
Record record = Model . getOrgByOrgName ( orgName ) ;
int dingtalk_dept_id = record . getInt ( "dingtalk_dept_id" ) ;
public static List < Record > getPersonList ( int bureau_id ) {
String sql = "select t1.person_id,t1.person_name,t2.dingtalk_dept_id from t_base_person as t1 " +
"inner join t_base_organization as t2 on t1.org_id=t2.org_id " +
" where t1.bureau_id=? and t1.b_use=1" ;
return Db . find ( sql , bureau_id ) ;
getBureauPerson ( accessToken , dingtalk_dept_id ) ;
for ( int i = 0 ; i < personList . size ( ) ; i + + ) {
String userid = personList . get ( i ) . getStr ( "userid" ) ;
String person_name = personList . get ( i ) . getStr ( "name" ) ;
delPerson ( accessToken , userid ) ;
CommonUtil . Print ( "成功删除:" + person_name ) ;
}
}
/ * *
* 功 能 : 同 步 人 员 信 息
*
* @param accessToken
* @param orgName
* @throws ApiException
* /
public static void syncPerson ( String accessToken , String orgName ) throws ApiException {
Record rOrg = Model . getOrgByOrgName ( orgName ) ;
int bureauId = rOrg . getInt ( "org_id" ) ;
//获取数据库中记录的钉钉端的dept_id,但不一定准确存在,需要继续判断一下
long DT_BureauId = Model . getDtDeptId ( bureauId ) ;
getBureauPerson ( accessToken , DT_BureauId ) ;
Set < String > keysOfA = new HashSet < > ( ) ;
Map < String , JSONObject > mapOfA = new HashMap < > ( ) ;
Set < String > keysOfB = new HashSet < > ( ) ;
Map < String , JSONObject > mapOfB = new HashMap < > ( ) ;
//A:钉钉
for ( int i = 0 ; i < personList . size ( ) ; i + + ) {
String userid = personList . get ( i ) . getStr ( "userid" ) ;
String name = personList . get ( i ) . getStr ( "name" ) ;
keysOfA . add ( userid ) ;
JSONObject jo = new JSONObject ( ) ;
jo . put ( "person_id" , userid ) ;
jo . put ( "person_name" , name ) ;
mapOfA . put ( userid , jo ) ;
}
//B:云平台
List < Record > list = Model . getDeptPerson ( bureauId ) ;
for ( int i = 0 ; i < list . size ( ) ; i + + ) {
Record r = list . get ( i ) ;
int person_id = r . getInt ( "person_id" ) ;
String person_name = r . getStr ( "person_name" ) ;
long dingtalk_dept_id = r . getLong ( "dingtalk_dept_id" ) ;
String tel = r . getStr ( "tel" ) ;
if ( StrKit . isBlank ( tel ) | | ! PhoneUtil . isMobile ( tel ) ) {
tel = ChineseMobileNumberGenerator . getInstance ( ) . generate ( ) ; //生成一个随机临时测试用的手机号
tel = "14" + tel . substring ( 2 ) ; //以14段开头, 避开已存在的号码
}
keysOfB . add ( String . valueOf ( person_id ) ) ;
JSONObject jo = new JSONObject ( ) ;
jo . put ( "person_id" , person_id ) ;
jo . put ( "person_name" , person_name ) ;
jo . put ( "dingtalk_dept_id" , dingtalk_dept_id ) ;
jo . put ( "tel" , tel ) ;
mapOfB . put ( String . valueOf ( person_id ) , jo ) ;
}
//在A不在B
for ( String key : keysOfA ) {
if ( ! keysOfB . contains ( key ) ) {
// key只存在于A中
// 删除
CommonUtil . Print ( "发现钉钉中多出的user_id:" + key + ",将删除掉..." ) ;
delPerson ( accessToken , key ) ;
} else {
// key在A和B中都存在
String aName = mapOfA . get ( key ) . getString ( "person_name" ) ;
String bName = mapOfB . get ( key ) . getString ( "person_name" ) ;
if ( ! aName . equals ( bName ) ) {
updatePerson ( accessToken , key , mapOfB . get ( key ) . getString ( "person_name" ) ) ;
CommonUtil . Print ( "发现钉钉与云平台间部门人员姓名不一致,将修改,person_name=" + mapOfB . get ( key ) . getString ( "person_name" ) ) ;
} else {
CommonUtil . Print ( "钉钉与云平台间部门人员姓名一致,无需修改,person_name=" + mapOfB . get ( key ) . getString ( "person_name" ) ) ;
}
}
}
//在B不在A
for ( String key : keysOfB ) {
if ( ! keysOfA . contains ( key ) ) {
// key只存在于B中
// 增加
String person_id = mapOfB . get ( key ) . getString ( "person_id" ) ;
String person_name = mapOfB . get ( key ) . getString ( "person_name" ) ;
String tel = mapOfB . get ( key ) . getString ( "tel" ) ;
long deptId = mapOfB . get ( key ) . getLong ( "dingtalk_dept_id" ) ;
createPerson ( accessToken , String . valueOf ( deptId ) , person_id , person_name , tel , "教师" ) ;
CommonUtil . Print ( "成功加入人员:" + person_name ) ;
}
}
}
/ * *
* 功 能 : 获 取 单 个 人 员 信 息
*
* @param access_token
* @param userId
* @return
* /
public static String getPerson ( String access_token , String userId ) {
try {
DingTalkClient client = new DefaultDingTalkClient ( "https://oapi.dingtalk.com/topapi/v2/user/get" ) ;
OapiV2UserGetRequest req = new OapiV2UserGetRequest ( ) ;
req . setUserid ( userId ) ;
req . setLanguage ( "zh_CN" ) ;
OapiV2UserGetResponse rsp = client . execute ( req , access_token ) ;
return rsp . getBody ( ) ;
} catch ( ApiException e ) {
e . printStackTrace ( ) ;
}
return null ;
}
//TODO 未完成
public static String getPersonToken ( String appKey , String appSecret ) throws Exception {
com . aliyun . dingtalkoauth2_1_0 . Client client = Common . createClient ( ) ;
GetUserTokenRequest getUserTokenRequest = new GetUserTokenRequest ( )
. setClientId ( appKey )
. setClientSecret ( appSecret )
//.setCode("abcd")
. setRefreshToken ( "abcd" )
. setGrantType ( "authorization_code" ) ;
try {
GetUserTokenResponse res = client . getUserToken ( getUserTokenRequest ) ;
System . out . println ( res . getBody ( ) . getAccessToken ( ) ) ;
System . out . println ( res . getBody ( ) . getRefreshToken ( ) ) ;
} catch ( TeaException err ) {
System . out . println ( err ) ;
} catch ( Exception _err ) {
System . out . println ( _err ) ;
}
return null ;
}
}