目錄
開發文檔:http://work.weixin.qq.com/api/doc#10093
第四步 : Contacts_DepartmentService
開發文檔: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;//入職時間
企業微信通訊錄同步就做好了,其中一些類和我上一篇發表的掃碼登陸中的類一樣。