Commit 8aeb1a30 by weiss

Merge branch 'master' into massage_msgcard

# Conflicts:
#	src/main/java/com/pipihelper/project/feishu/controller/FeiShuEventController.java
2 parents 5f769c8a 73d82a40
package com.pipihelper.project.configurer;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import javax.sql.DataSource;
import java.util.Properties;
/**
* Mybatis & Mapper & PageHelper 配置
......@@ -8,27 +19,27 @@ import org.springframework.context.annotation.Configuration;
@Configuration
public class MybatisConfigurer {
// @Bean
// public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exception {
// SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
// factory.setDataSource(dataSource);
// factory.setTypeAliasesPackage(MODEL_PACKAGE);
//
//// //配置分页插件,详情请查阅官方文档
//// PageHelper pageHelper = new PageHelper();
//// Properties properties = new Properties();
//// properties.setProperty("pageSizeZero", "true");//分页尺寸为0时查询所有纪录不再执行分页
//// properties.setProperty("reasonable", "true");//页码<=0 查询第一页,页码>=总页数查询最后一页
//// properties.setProperty("supportMethodsArguments", "true");//支持通过 Mapper 接口参数来传递分页参数
//// pageHelper.setProperties(properties);
////
//// //添加插件
//// factory.setPlugins(new Interceptor[]{pageHelper});
//
// //添加XML目录
// ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
// factory.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
// return factory.getObject();
// }
@Bean
public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exception {
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
factory.setDataSource(dataSource);
//factory.setTypeAliasesPackage("com.pipitest.project");
//配置分页插件,详情请查阅官方文档
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
properties.setProperty("pageSizeZero", "true");//分页尺寸为0时查询所有纪录不再执行分页
properties.setProperty("reasonable", "true");//页码<=0 查询第一页,页码>=总页数查询最后一页
properties.setProperty("supportMethodsArguments", "true");//支持通过 Mapper 接口参数来传递分页参数
pageHelper.setProperties(properties);
/* //添加插件
factory.setPlugins(new Interceptor[]{pageHelper});*/
//添加XML目录
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
factory.setMapperLocations(resolver.getResources("classpath:mybatis/*.xml"));
return factory.getObject();
}
}
package com.pipihelper.project.feishu.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pipihelper.project.feishu.dto.FeiShuConfig;
import com.pipihelper.project.feishu.dto.FeiShuEventDTO;
import com.pipihelper.project.feishu.dto.FeiShuMsgCardEventDTO;
import com.pipihelper.project.feishu.dto.employee.FeiShuEmployeeDTO;
import com.pipihelper.project.feishu.dto.employee.SystemFieldsDTO;
import com.pipihelper.project.feishu.entity.Employee;
import com.pipihelper.project.feishu.service.EmployeeService;
import com.pipihelper.project.feishu.service.FeiShuApiService;
import com.pipihelper.project.feishu.service.FeiShuEventService;
import com.pipihelper.project.feishu.utils.FeiShuEventDataDecrypter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* @Description: TODO
* @author: charles
* @date: 2022年03月30日 17:39
*/
@Slf4j
@RestController
@RequestMapping("/pipitest/feishu")
public class FeiShuEventController {
......@@ -28,16 +38,22 @@ public class FeiShuEventController {
@Resource
private FeiShuEventService feiShuEventService;
@Autowired
private FeiShuApiService feiShuApiService;
@Autowired
private EmployeeService employeeService;
@PostMapping("/event")
public JSONObject event(@RequestBody String s) throws Exception {
log.info("接收到飞书事件,密文:{}", s);
JSONObject reqJsonObject = JSON.parseObject(s);
System.out.println(s);
FeiShuEventDataDecrypter d = new FeiShuEventDataDecrypter(feiShuConfig.getEncryptKey());
JSONObject encryptJsonObject = JSON.parseObject(d.decrypt(reqJsonObject.getString("encrypt")));
log.info("接收到飞书事件,明文:{}", encryptJsonObject);
if (encryptJsonObject.containsKey("challenge")) {
System.out.println(encryptJsonObject);
if (encryptJsonObject.containsKey("challenge")) {
return encryptJsonObject;
}
FeiShuEventDTO feiShuEventDTO = encryptJsonObject.toJavaObject(FeiShuEventDTO.class);
......@@ -52,22 +68,15 @@ public class FeiShuEventController {
return null;
}
@PostMapping("/msg_card")
public JSONObject msgCardEvent(@RequestBody String s) {
log.info("接收到飞书消息卡片,密文:{}", s);
JSONObject reqJsonObject = JSON.parseObject(s);
if (reqJsonObject.containsKey("challenge")) {
JSONObject verifyRepJson = new JSONObject();
verifyRepJson.put("challenge", reqJsonObject.getString("challenge"));
log.info("接收到飞书机器人接口验证事件,返回值:{}", verifyRepJson);
return verifyRepJson;
}
FeiShuMsgCardEventDTO feiShuMsgCardEventDTO = reqJsonObject.toJavaObject(FeiShuMsgCardEventDTO.class);
String actionType = feiShuMsgCardEventDTO.getAction().getValue().getKey().split("\\.")[0];
System.out.println(s);
return null;
}
// @PostMapping("msg_card")
// public JSONObject msgCardEvent(@RequestBody String s) throws Exception {
// JSONObject reqJsonObject = JSON.parseObject(s);
// System.out.println(s);
// if (reqJsonObject.containsKey("challenge")) {
// return reqJsonObject;
// }
// FeiShuMsgCardEventDTO feiShuMsgCardEventDTO = reqJsonObject.toJavaObject(FeiShuMsgCardEventDTO.class);
// String actionType = feiShuMsgCardEventDTO.getAction().getValue().getKey().split("\\.")[0];
// switch (actionType) {
// case "TEST_DATA":
//
......@@ -76,4 +85,46 @@ public class FeiShuEventController {
// }
//
// }
@PostMapping("/employee-list")
public Object event() {
List<FeiShuEmployeeDTO> dtos = feiShuApiService.getEmployeeList();
List<Employee> employeeList = employeeService.findAll();
Map<String, Employee> employeeMap = employeeList.stream().collect(Collectors.toMap(Employee::getOpenId, Function.identity()));
List<String> openIdList = dtos.stream().map(FeiShuEmployeeDTO::getUserId).collect(Collectors.toList());
employeeList.forEach(it -> {
if (!openIdList.contains(it.getOpenId())) {
employeeService.deleteById(it.getId());
}
});
dtos.forEach(it -> {
Employee currentEmployee = employeeMap.get(it.getUserId());
if (currentEmployee != null) {
return;
}
Employee employee = new Employee();
SystemFieldsDTO systemFieldsDTO = it.getSystemFields();
employee.setOpenId(it.getUserId());
if (systemFieldsDTO != null) {
employee.setGender(systemFieldsDTO.getGender());
employee.setStatus(systemFieldsDTO.getStatus());
employee.setDepartmentId(systemFieldsDTO.getDepartmentId());
employee.setName(systemFieldsDTO.getName());
employee.setMobile(systemFieldsDTO.getMobile());
}
employeeService.create(employee);
});
return dtos;
}
private static class A {
Integer index;
String openId;
String name;
String departMentName;
String timeRange;
}
}
package com.pipihelper.project.feishu.dao;
import com.pipihelper.project.feishu.entity.Employee;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface EmployeeDao {
Employee findById(Integer id);
Employee findByOpenId(String openId);
void deleteById(Integer id);
void create(Employee employee);
void update(Employee employee);
List<Employee> findAll();
}
......@@ -18,4 +18,7 @@ public class FeiShuDataDTO<T> {
private Boolean hasMore;
@JsonProperty("department")
private FeiShuDepartmentDTO department;
@JsonProperty("chat_id")
private String chatId;
}
......@@ -23,4 +23,11 @@ public class FeiShuChatDTO {
private Boolean external;
@JsonProperty("tenant_key")
private String tenantKey;
@JsonProperty("user_id_type")
private String userIdType;
@JsonProperty("user_id_list")
private String[] userIdList;
}
......@@ -6,7 +6,6 @@ import lombok.NoArgsConstructor;
import java.util.List;
@NoArgsConstructor
@Data
public class SystemFieldsDTO {
@JsonProperty("name")
......
package com.pipihelper.project.feishu.entity;
import lombok.Data;
@Data
public class Employee {
private Integer id;
private String openId;
private String mobile;
private String name;
private Integer gender;
private Integer status;
private String departmentId;
}
package com.pipihelper.project.feishu.service;
import com.pipihelper.project.feishu.dao.EmployeeDao;
import com.pipihelper.project.feishu.entity.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class EmployeeService {
@Autowired
private EmployeeDao employeeDao;
public Employee findById(Integer id) {
return employeeDao.findById(id);
}
public Employee findByOpenId(String openId) {
return employeeDao.findByOpenId(openId);
}
public void deleteById(Integer id) {
employeeDao.deleteById(id);
}
public void create(Employee employee) {
employeeDao.create(employee);
}
public void update(Employee employee) {
employeeDao.update(employee);
}
public List<Employee> findAll() {
return employeeDao.findAll();
}
}
......@@ -17,9 +17,12 @@ import com.pipihelper.project.feishu.dto.doc.FieldsDTO;
import com.pipihelper.project.feishu.dto.employee.FeiShuEmployeeDTO;
import com.pipihelper.project.feishu.dto.msg.FeiShuBatchMsgDTO;
import com.pipihelper.project.feishu.dto.msg.FeiShuMsgDTO;
//import org.springframework.http.MediaType;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
......@@ -59,10 +62,10 @@ public class FeiShuApiService {
.expireAfterWrite(Duration.ofHours(2))
.build();
private final Cache<String, List<FeiShuEmployeeDTO>> EMPLOYEE_LIST_CACHE = Caffeine.newBuilder()
/* private final Cache<String, List<FeiShuEmployeeDTO>> EMPLOYEE_LIST_CACHE = Caffeine.newBuilder()
.maximumSize(100)
.expireAfterWrite(Duration.ofHours(2))
.build();
.build();*/
public String getTenantToken() {
return FEISHU_CACHE.get("tenantAccessToken", key -> setTenantAccessToken());
......@@ -84,7 +87,7 @@ public class FeiShuApiService {
* @param sendMsg
* @return
*/
public JSONObject sendMsg( String receive_id_type, JSONObject sendMsg) {
public JSONObject sendMsg(String receive_id_type, JSONObject sendMsg) {
System.out.println(sendMsg.toString());
//发送消息
......@@ -109,7 +112,6 @@ public class FeiShuApiService {
}
@Async
public JSONObject replyMsg(String message_id, JSONObject replyMsg) {
System.out.println(replyMsg.toString());
......@@ -153,7 +155,7 @@ public class FeiShuApiService {
@Async
public JSONObject patchUrgentApp(String message_id, JSONObject content) {
String api = "/im/v1/messages/" + message_id + "/urgent_sms?user_id_type=open_id";
String api = "/im/v1/messages/" + message_id + "/urgent_sms?user_id_type=user_id";
OkHttpClient client = new OkHttpClient();
String url = feiShuConfig.getFeiShuOpenApiHost() + api;
RequestBody requestBody = RequestBody.create(content.toString(), MediaType.parse("application/json"));
......@@ -332,6 +334,7 @@ public class FeiShuApiService {
}
}
/**
* 获取飞书人事花名单信息
* 文档:https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/ehr/ehr-v1/employee/list
......@@ -339,11 +342,7 @@ public class FeiShuApiService {
* @return
*/
public List<FeiShuEmployeeDTO> getEmployeeList() {
return EMPLOYEE_LIST_CACHE.get("employeeList", s -> setEmployeeList());
}
private List<FeiShuEmployeeDTO> setEmployeeList() {
String api = "/ehr/v1/employees?view=full&status=2&status=4&page_size=100";
String api = "/ehr/v1/employees?view=full&status=2&status=4&page_size=100&user_id_type=open_id";
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Bearer " + getTenantToken());
......@@ -375,21 +374,21 @@ public class FeiShuApiService {
*
* @return
*/
public List<FeiShuChatDTO> getChatList() {
public String createChatList(FeiShuChatDTO feiShuChatDTO) {
String api = "/im/v1/chats";
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Bearer " + getTenantToken());
headers.set("Content-Type", "application/json; charset=utf-8");
HttpEntity<String> requestEntity = new HttpEntity<>(headers);
String url = feiShuConfig.getFeiShuOpenApiHost() + api;
try {
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class);
Type type = new TypeReference<FeiShuResultDTO<FeiShuChatDTO>>() {
}.getType();
HttpEntity<String> requestEntity = new HttpEntity<>(objectMapper.writeValueAsString(feiShuChatDTO), headers);
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
System.out.println(responseEntity.getBody());
Type type = new TypeReference<FeiShuResultDTO>() {
}.getType();
FeiShuResultDTO feiShuResultDTO = JSONObject.parseObject(responseEntity.getBody(), type);
return feiShuResultDTO.getData().getItems();
return feiShuResultDTO.getData().getChatId();
} catch (Exception e) {
throw new ServiceException("飞书:" + api + "接口调用失败" + "\n" + e);
}
......@@ -471,7 +470,6 @@ public class FeiShuApiService {
*
* @param appToken
* @param tableId
* @return
*/
public List getTableRecords(String appToken, String tableId) {
......
......@@ -50,6 +50,6 @@ public class FeiShuEventDataDecrypter {
public static void main(String[] args) throws Exception {
FeiShuEventDataDecrypter d = new FeiShuEventDataDecrypter("kudryavka");
System.out.println(d.decrypt("FIAfJPGRmFZWkaxPQ1XrJZVbv2JwdjfLk4jx0k/U1deAqYK3AXOZ5zcHt/cC4ZNTqYwWUW/EoL+b2hW/C4zoAQQ5CeMtbxX2zHjm+E4nX/Aww+FHUL6iuIMaeL2KLxqdtbHRC50vgC2YI7xohnb3KuCNBMUzLiPeNIpVdnYaeteCmSaESb+AZpJB9PExzTpRDzCRv+T6o5vlzaE8UgIneC1sYu85BnPBEMTSuj1ZZzfdQi7ZW992Z4dmJxn9e8FL2VArNm99f5Io3c2O4AcNsQENNKtfAAxVjCqc3mg5jF0nKabA+u/5vrUD76flX1UOF5fzJ0sApG2OEn9wfyPDRBsApn9o+fceF9hNrYBGsdtZrZYyGG387CGOtKsuj8e2E8SNp+Pn4E9oYejOTR+ZNLNi+twxaXVlJhr6l+RXYwEiMGQE9zGFBD6h2dOhKh3W84p1GEYnSRIz1+9/Hp66arjC7RCrhuW5OjCj4QFEQJiwgL45XryxHtiZ7JdAlPmjVsL03CxxFZarzxzffryrWUG3VkRdHRHbTsC34+ScoL5MTDU1QAWdqUC1T7xT0lCvQELaIhBTXAYrznJl6PlA83oqlMxpHh0gZBB1jFbfoUr7OQbBs1xqzpYK6Yjux6diwpQB1zlZErYJUfCqK7G/zI9yK/60b4HW0k3M+AvzMcw=")); //hello world
System.out.println(d.decrypt("5QyoWWZ3QI5xbQ6Q/niQRVUS+nxnOLk+EEoWJbwPoi1+tQvl92BzKwfS8vFc/ubRHJ4VmOROpjA8TZieBLBWBmh5IcphCVIh5ciPSgjMY5wKRA26G+LN2VvldyO8hUwm7z9XGCNpM7Cbdi/qWmQEC9AmFxdVlSaR0H8m15TVVLVKOHZt9yFCa8s4hy7Q2kG/")); //hello world
}
}
......@@ -13,8 +13,8 @@ feishu:
feiShuOpenApiHost: https://open.feishu.cn/open-apis
encryptKey: aGTqmJcfXluKWfFWHGw5SdzIg6QIxPsp
verificationToken: ChUEDdWQbyHpHUV6H5fVeL5fOP3HfBE6
appId: cli_a2cff17bd3f8d013
appSecret: E5vXXmRipOD6vyolib186b25XXLbdYfE
appId: cli_a3c2be2801f8500d
appSecret: bw3ZXzSj47DgHT19YT268bcwYVVnRTZD
fkChatId: oc_5124ee21dbdecf5d802f9e9e33dab722
# 腾讯云配置参数
......
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.pipihelper.project.feishu.dao.EmployeeDao">
<resultMap id="EmployeeResultMap" type="com.pipihelper.project.feishu.entity.Employee">
<id column="id" property="id"/>
<result column="open_id" property="openId"/>
<result column="mobile" property="mobile"/>
<result column="name" property="name"/>
<result column="gender" property="gender"/>
<result column="department_id" property="departmentId"/>
</resultMap>
<!-- <resultMap id="BuffConfigResultBOMap" type="com.pipihelper.project.feishu.entity.Deployee"
extends="EmployeeResultMap">
</resultMap>-->
<sql id="Base_Column_List">
id,open_id,mobile,name,gender,department_id
</sql>
<select id="findById" parameterType="java.lang.Integer" resultMap="EmployeeResultMap">
select
<include refid="Base_Column_List"/>
from t_employee
<where>
id = #{id}
</where>
</select>
<select id="findByOpenId" parameterType="java.lang.String" resultMap="EmployeeResultMap">
select
<include refid="Base_Column_List"/>
from t_employee
<where>
open_id = #{openId}
</where>
</select>
<select id="findAll" resultMap="EmployeeResultMap">
select
<include refid="Base_Column_List"/>
from t_employee
</select>
<delete id="deleteById" parameterType="java.lang.Integer">
delete from t_employee
<where>
id = #{id}
</where>
</delete>
<insert id="create" useGeneratedKeys="true" keyProperty="id"
parameterType="com.pipihelper.project.feishu.entity.Employee">
insert into t_employee (open_id, mobile, name, gender, status, department_id)
values (#{openId}, #{mobile}, #{name}, #{gender}, #{status}, #{departmentId})
</insert>
<update id="update" parameterType="com.pipihelper.project.feishu.entity.Employee">
update t_employee
<set>
<if test="openId != null">
open_id = #{openId},
</if>
<if test="mobile != null">
mobile = #{mobile},
</if>
<if test="gender != null">
gender = #{gender},
</if>
<if test="status != null">
status = #{status},
</if>
<if test="departmentId != null">
department_id = #{departmentId},
</if>
</set>
<where>
id = #{id}
</where>
</update>
</mapper>
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!