Java企业微信开发_02_通讯录同步
1 package ;
2
3 import ;
4 import ;
5 import ;
6
7 import ;
8
9 import org.;
10 import org.;
11
12 import ;
13 import ;
14 import ;
15 import ;
16
17 public class Contacts_UserService {
18 private static Logger log = (UserTest.class);
19
20 private static String createUser_url="/cgi-bin/user/create?access_token=ACCESS_TOKEN";
21 private static String getUser_url="/cgi-bin/user/get?access_token=ACCESS_TOKEN&userid=USERID";
22 private static String updateUser_url="/cgi-bin/user/update?access_token=ACCESS_TOKEN";
23 private static String deleteUser_url="/cgi-bin/user/delete?access_token=ACCESS_TOKEN&userid=USERID";
24 private static String batchdeleteUser_url="/cgi-bin/user/batchdelete?access_token=ACCESS_TOKEN";
25 private static String getDepartmentUser_url="/cgi-bin/user/simplelist?access_token=ACCESS_TOKEN&department_id=DEPARTMENT_ID&fetch_child=FETCH_CHILD";
26 private static String getDepartmentUserDetails_url="/cgi-bin/user/list?access_token=ACCESS_TOKEN&department_id=DEPARTMENT_ID&fetch_child=FETCH_CHILD";
27
28
29 //1.创建成员
30 public void createUser(String accessToken,User user) {
31
32 //1.获取json字符串:将user对象转换为json字符串
33 Gson gson = new Gson();
34 String jsonU1 =(user); //使用(user)即可将user对象顺序转成json
35 ("jsonU1:"+jsonU1);
36
37
38 //2.获取请求的url
39 createUser_url=createUser_url.replace("ACCESS_TOKEN", accessToken);
40
41 //3.调用接口,发送请求,创建成员
42 JSONObject jsonObject = (createUser_url, "POST", jsonU1);
43 ("jsonObject:"+());
44
45 //4.错误消息处理
46 if (null != jsonObject) {
47 if (0 != ("errcode")) {
48 ("创建成员失败 errcode:{} errmsg:{}", ("errcode"), ("errmsg"));
49 }
50 }
51 }
52
53 //2.获取成员
54 public void getUser(String accessToken,String userId) {
55
56 //1.获取请求的url
57 getUser_url=getUser_url.replace("ACCESS_TOKEN", accessToken)
58 .replace("USERID", userId);
59
60 //2.调用接口,发送请求,获取成员
61 JSONObject jsonObject = (getUser_url, "GET", null);
62 ("jsonObject:"+());
63
64 //3.错误消息处理
65 if (null != jsonObject) {
66 if (0 != ("errcode")) {
67 ("获取成员失败 errcode:{} errmsg:{}", ("errcode"), ("errmsg"));
68 }
69 }
70 }
71
72 //3.更新成员
73 public void updateUser(String accessToken,User user) {
74
75 //1.获取json字符串:将user对象转换为json字符串
76 Gson gson = new Gson();
77 String jsonU1 =(user); //使用(user)即可将user对象顺序转成json
78 ("jsonU1:"+jsonU1);
79
80
81 //2.获取请求的url
82 updateUser_url=updateUser_url.replace("ACCESS_TOKEN", accessToken);
83
84 //3.调用接口,发送请求,创建成员
85 JSONObject jsonObject = (updateUser_url, "POST", jsonU1);
86 ("jsonObject:"+());
87
88 //4.错误消息处理
89 if (null != jsonObject) {
90 if (0 != ("errcode")) {
91 ("更新成员失败 errcode:{} errmsg:{}", ("errcode"), ("errmsg"));
92 }
93 }
94 }
95
96 //4.删除成员
97 public void deleteUser(String accessToken,String userId) {
98
99 //1.获取请求的url
100 deleteUser_url=deleteUser_url.replace("ACCESS_TOKEN", accessToken)
101 .replace("USERID", userId);
102
103 //2.调用接口,发送请求,删除成员
104 JSONObject jsonObject = (deleteUser_url, "GET", null);
105 ("jsonObject:"+());
106
107 //3.错误消息处理
108 if (null != jsonObject) {
109 if (0 != ("errcode")) {
110 ("删除成员失败 errcode:{} errmsg:{}", ("errcode"), ("errmsg"));
111 }
112 }
113 }
114
115 //5.批量删除成员
116 public void batchdeleteUser(String accessToken,List<String> userIdList){
117 //1.获取json字符串:将user对象转换为json字符串
118 Map<String, Object> content = new HashMap<String, Object>();
119 ("useridlist", userIdList);
120
121 Gson gson=new Gson();
122 String useridlist=(content);
123 (useridlist);
124
125 //2.获取请求的url
126 batchdeleteUser_url=batchdeleteUser_url.replace("ACCESS_TOKEN", accessToken);
127
128 //3.调用接口,发送请求,创建成员
129 JSONObject jsonObject = (batchdeleteUser_url, "POST", useridlist);
130 ("jsonObject:"+());
131
132 //4.错误消息处理
133 if (null != jsonObject) {
134 if (0 != ("errcode")) {
135 ("批量删除成员失败 errcode:{} errmsg:{}", ("errcode"), ("errmsg"));
136 }
137 }
138 }
139
140
141 //6.获取部门成员
142 public void getDepartmentUser(String accessToken,String departmentId,String fetchChild) {
143
144 //1.获取请求的url
145 getDepartmentUser_url=getDepartmentUser_url.replace("ACCESS_TOKEN", accessToken)
146 .replace("DEPARTMENT_ID", departmentId)
147 .replace("FETCH_CHILD", fetchChild);
148
149 //2.调用接口,发送请求,获取部门成员
150 JSONObject jsonObject = (getDepartmentUser_url, "GET", null);
151 ("jsonObject:"+());
152
153 //3.错误消息处理
154 if (null != jsonObject) {
155 if (0 != ("errcode")) {
156 ("获取部门成员失败 errcode:{} errmsg:{}", ("errcode"), ("errmsg"));
157 }
158 }
159 }
160
161
162 //7.获取部门成员详情
163 public void getDepartmentUserDetails(String accessToken,String departmentId,String fetchChild) {
164 //1.获取请求的url
165 getDepartmentUserDetails_url=getDepartmentUserDetails_url.replace("ACCESS_TOKEN", accessToken)
166 .replace("DEPARTMENT_ID", departmentId)
167 .replace("FETCH_CHILD", fetchChild);
168
169 //2.调用接口,发送请求,获取部门成员
170 JSONObject jsonObject = (getDepartmentUserDetails_url, "GET", null);
171 ("jsonObject:"+());
172
173 //3.错误消息处理
174 if (null != jsonObject) {
175 if (0 != ("errcode")) {
176 ("获取部门成员详情失败 errcode:{} errmsg:{}", ("errcode"), ("errmsg"));
177 }
178 }
179 }
180
181 }