1、进入企业后台管理,创建一个企业自建应用 ,获取App ID,App Secret
2、 对应用设置api权限及数据权限
3、Java客户端获取用户,部门信息
有两种方式可以获取:1、api 2、sdk的方式
我这里采用sdk的方式,需要引入maven依赖
<dependency>
<groupId>com.larksuite.oapi</groupId>
<artifactId>oapi-sdk</artifactId>
<version>2.3.2</version>
</dependency>
Java代码为:
package com.hniclear.system.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hniclear.common.mapper.UserInfoMapper;
import com.hniclear.common.model.po.UserInfo;
import com.hniclear.system.mapper.DepartmentMapper;
import com.hniclear.system.model.po.DepartmentInfo;
import com.hniclear.system.model.vo.rsp.devopsLog.DepartmentTreeRsp;
import com.hniclear.system.service.UserService;
import com.lark.oapi.Client;
import com.lark.oapi.service.contact.v3.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.time.LocalDateTime;
import java.util.*;
@Service
public class UserServiceImpl implements UserService {
private static final Logger log = LoggerFactory.getLogger(UserServiceImpl.class);
@Value("${feishu.appId}")
private String appId;
@Value("${feishu.appSecret}")
private String appSecret;
@Autowired
private UserInfoMapper userInfoMapper;
@Autowired
private DepartmentMapper departmentMapper;
@Override
public List<DepartmentTreeRsp> getDepartmentTree(String parentDepartmentId) {
List<DepartmentTreeRsp> departmentTreeRsps = new ArrayList<>();
LambdaQueryWrapper<DepartmentInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(DepartmentInfo::getParentDepartmentId, parentDepartmentId)
.eq(DepartmentInfo::getIsDeleted, 0);
List<DepartmentInfo> departmentInfos = departmentMapper.selectList(lambdaQueryWrapper);
if(CollectionUtils.isEmpty(departmentInfos)){
return null;
}
for(DepartmentInfo departmentInfo : departmentInfos){
DepartmentTreeRsp departmentTreeRsp = new DepartmentTreeRsp();
departmentTreeRsp.setId(departmentInfo.getId());
departmentTreeRsp.setName(departmentInfo.getName());
LambdaQueryWrapper<UserInfo> userInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
userInfoLambdaQueryWrapper.eq(UserInfo::getDepartmentId, departmentInfo.getDepartmentId())
.eq(UserInfo::getIsDelete, 0);
List<UserInfo> userInfos = userInfoMapper.selectList(userInfoLambdaQueryWrapper);
List<DepartmentTreeRsp.UserInfoRsp> userInfoRsps = new ArrayList<>();
userInfos.forEach(userInfo -> {
DepartmentTreeRsp.UserInfoRsp userInfoRsp = new DepartmentTreeRsp.UserInfoRsp();
BeanUtils.copyProperties(userInfo, userInfoRsp);
userInfoRsps.add(userInfoRsp);
});
departmentTreeRsp.setUsers(userInfoRsps);
departmentTreeRsp.setChildren(getDepartmentTree(departmentInfo.getDepartmentId()));
departmentTreeRsps.add(departmentTreeRsp);
}
return departmentTreeRsps;
}
@Override
public void sync() {
Client client = Client.newBuilder(appId, appSecret).build();
List<Department> departments = syncDepartmentFromFeishu(client);
if (!CollectionUtils.isEmpty(departments)) {
insertDepartment(departments);
List<User> users = syncUsersFromFeishu(departments, client);
if (!CollectionUtils.isEmpty(users)) {
insertUser(users);
}
}
}
@Override
public List<User> syncUsersFromFeishu(List<Department> departmentList, Client client) {
try {
if (CollectionUtils.isEmpty(departmentList)) {
return null;
}
insertDepartment(departmentList);
List<User> userList = new ArrayList<>();
for (Department department : departmentList) {
ListUserResp listUserResp = client.contact().v3().user().list(new ListUserReq(ListUserReq.newBuilder().departmentId(department.getOpenDepartmentId())));
User[] items = listUserResp.getData().getItems();
if (items != null) {
userList.addAll(Arrays.asList(items));
}
}
return userList;
} catch (Exception e) {
log.error("sync users from feishu error", e);
}
return null;
}
@Override
public List<Department> syncDepartmentFromFeishu(Client client) {
try {
ListDepartmentReq req = new ListDepartmentReq();
req.setPageSize(50);
ListDepartmentResp listUserResp = client.contact().v3().department().list(req);
List<Department> departmentList = new ArrayList<>();
Department[] items = listUserResp.getData().getItems();
if (items == null) {
return null;
}
for (Department department : items) {
getDepartmentList(department.getDepartmentId(), departmentList, client);
}
return departmentList;
} catch (Exception e) {
log.error("sync users from feishu error", e);
}
return null;
}
void getDepartmentList(String departmentId, List<Department> departmentList, Client client) throws Exception {
ChildrenDepartmentReq childrenDepartmentReq = new ChildrenDepartmentReq();
childrenDepartmentReq.setDepartmentId(departmentId);
ChildrenDepartmentResp childrenDepartmentResp = client.contact().v3().department().children(childrenDepartmentReq);
Department[] items = childrenDepartmentResp.getData().getItems();
if (items == null) {
return;
}
departmentList.addAll(Arrays.asList(items));
for (Department item : items) {
getDepartmentList(item.getOpenDepartmentId(), departmentList, client);
}
}
private void insertDepartment(List<Department> departmentList) {
List<DepartmentInfo> addDepartmentInfos = new ArrayList<>();
for (Department department : departmentList) {
DepartmentInfo departmentInfo = new DepartmentInfo();
LambdaQueryWrapper<DepartmentInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(DepartmentInfo::getDepartmentId, department.getOpenDepartmentId())
.eq(DepartmentInfo::getIsDeleted, 0);
DepartmentInfo existDepartmentInfo = departmentMapper.selectOne(lambdaQueryWrapper);
if (existDepartmentInfo != null) {
departmentInfo.setId(existDepartmentInfo.getId());
}
departmentInfo.setName(department.getName())
.setZhCn(department.getI18nName().getZhCn())
.setParentDepartmentId(department.getParentDepartmentId())
.setDepartmentId(department.getOpenDepartmentId())
.setOrders(department.getOrder())
.setCreateTime(new Date())
.setIsDeleted(department.getStatus().getIsDeleted())
.setMemberCount(department.getMemberCount());
addDepartmentInfos.add(departmentInfo);
}
departmentMapper.insertOrUpdate(addDepartmentInfos);
}
private void insertUser(List<User> userList) {
List<UserInfo> userInfos = new ArrayList<>();
for (User item : userList) {
if (item.getMobile() == null) {
continue;
}
String telephone = item.getMobile().contains("+86") ? item.getMobile().replace("+86", "") : item.getMobile();
UserInfo userInfo = new UserInfo();
LambdaQueryWrapper<UserInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(UserInfo::getTelephone, telephone).eq(UserInfo::getIsDelete, 0);
UserInfo existUserInfo = userInfoMapper.selectOne(lambdaQueryWrapper);
if (existUserInfo != null) {
userInfo.setId(existUserInfo.getId());
}
String[] departmentIds = item.getDepartmentIds();
userInfo.setUserName(item.getName())
.setDepartmentId(departmentIds[departmentIds.length - 1])
.setTelephone(telephone)
.setOpenId(item.getOpenId())
.setIsDelete(0)
.setStatus(1)
.setCreateTime(LocalDateTime.now())
.setPassword("hni2Meta.")
.setType(2);
userInfos.add(userInfo);
}
userInfoMapper.insertOrUpdate(userInfos);
}
}