开发文档:http://work.weixin.qq.com/api/doc#10093
第一步: 后台管理界面开启通讯录同步
第二步. 后面要设置接收事件服务器,目前我还没做,可以参考:
https://www.cnblogs.com/shirui/p/7365538.html#commentform
第三步. AccessTokenController中,直接在网页上访问这个接口
// 2.企业微信通讯录同步
@RequestMapping(value = "getUser")
public String getUser(Model model) {
// 声明一个存放多个user对象的集合
List<User> userss = new ArrayList<User>();
List<HashMap<String, Object>> userList = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> user = null;
// 1查询出access_Token的值
// List<AccessToken> acce = accService.findAccessToken();
// for(AccessToken a:acce){
String access_token = WeiXinUtil.getAccessToken(accService, "contacts",
WeiXinParamesUtil.corpId, WeiXinParamesUtil.contactsSecret);
// 获取部门列表信息(不填则是查询出所有的部门列表)
Contacts_DepartmentService departService = new Contacts_DepartmentService();
List<String> depts = departService.getDepartmentList(access_token, "");
// 根据部门信息去获取成员的详细信息(查询到的数据)
Contacts_UserService userService = new Contacts_UserService();
List<User> users = userService.getDepartmentUserDetails(depts,
access_token, "1");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
String entryTime = sdf.format(date);
for (User d : users) {
user = new HashMap<String, Object>();
user.put("userId", d.getUserId());
user.put("name", d.getName());
user.put("password", "123456");
user.put("mobile", d.getMobile());
user.put("power", 1);
user.put("department", d.getDepartment());
user.put("email", d.getEmail());
user.put("position", d.getPosition());
user.put("gender", d.getGender());
user.put("status", "0");
user.put("entryTime", entryTime);
userList.add(user);
}
// 将其保存到数据库中
int row = accService.insertUsers(userList);
// 重定向到首页查询出所有数据
return "redirect:showAll";
}
第四步 Contacts_DepartmentService
private static String getDepartmentList_url="https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=ACCESS_TOKEN&id=ID";
public List<String> getDepartmentList(String accessToken, String departmentId) {
List<String> departments = new ArrayList<String>();
// 1.获取请求的url
getDepartmentList_url = getDepartmentList_url.replace("ACCESS_TOKEN",
accessToken).replace("ID", departmentId);
// 2.调用接口,发送请求,获取成员
JSONObject jsonObject = WeiXinUtil.httpRequest(getDepartmentList_url,
"GET", null);
System.out.println("jsonObject:" + jsonObject.toString());
// 3.错误消息处理
if (null != jsonObject) {
if (0 != jsonObject.getInt("errcode")) {
log.error("获取部门列表 errcode:{} errmsg:{}",
jsonObject.getInt("errcode"),
jsonObject.getString("errmsg"));
} else {// 查询成功
JSONArray array = jsonObject.getJSONArray("department");
if (null != array) {
for (int i = 0; i < array.size(); i++) {
JSONObject dept = JSONObject.fromObject(array.get(i));
if (null != dept.get("id")) {
departments.add(dept.get("id").toString());
}
}
}
}
}
return departments;
}
第五步:我的用户表(用来保存用户信息到数据库中)
private int id;
private String userId;
private String name;//成员名字
private String password;//成员密码
private String zhanghao;
private String mobile;//成员手机号码
private int power;//0代表系统管理员 2代表普通成员1。代表管理员
private String department;//部门
private String email;//邮件地址
private String position;//职位
private String gender;//性别
private String status;//是否在职
private String entryTime;//入职时间
企业微信通讯录同步就做好了,其中一些类和我上一篇发表的扫码登陆中的类一样。